forked from nicolashery/example-stream-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.js
More file actions
42 lines (35 loc) · 1.07 KB
/
transform.js
File metadata and controls
42 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Example: transform
//
// Add some more transform logic after parsing the data file
var fs = require('fs')
, Transform = require('stream').Transform
, JSONStream = require('JSONStream')
, parser = require('../index');
var dataFile = fs.createReadStream('data/demo.csv');
var transform = new Transform({objectMode: true});
transform.header = null;
transform._transform = function(data, encoding, done) {
// First data object should be header
if (data.header) {
this.header = data.header;
return done();
}
// Add some game info to all records using the header
data['Game'] = {
'id': this.header['GameId'],
'players': this.header['Players'],
'map': this.header['Map']
};
this.push(data);
done();
};
// We could do something with our transformed data records,
// like save them to a database by piping them to a `Writable` stream
// that will handle that.
// Here we'll just stringify them to stdout
dataFile
.pipe(parser())
.pipe(transform)
.pipe(JSONStream.stringify(false))
.pipe(process.stdout);
process.stdout.on('error', process.exit);