Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ fs.createReadStream('scores.csv')

<a name="csv" href="#csv">#</a> etl.<b>csv</b>([<i>options</i>])

Parses incoming csv text into individual records. For parsing options see [csv-parser](https://www.npmjs.com/package/csv-parser). If `options` contains a `transform` object containing functions, those functions will be applied on the values of any matching keys in the data. If a key in the `transform` object is set to `null` then value with that key will not be included in the downstream packets. If option `santitize` is set to true, then headers will be trimmed, converted to lowercase, spaces converted to underscore and any blank values (empty strings) will be set to undefined.
Parses incoming csv text into individual records. For parsing options see [csv-parser](https://www.npmjs.com/package/csv-parser). If `options` contains a `transform` object containing functions, those functions will be applied on the values of any matching keys in the data. If a key in the `transform` object is set to `null` then value with that key will not be included in the downstream packets. If option `santitize` is set to true, then headers will be trimmed, converted to lowercase, spaces converted to underscore and any blank values (empty strings) will be set to undefined. The option `addLineNumbers` is `true` by default, if is set to `false` (boolean) the result won't have the `__line` numbers for each object.

A `header` event will be emitted when headers have been parsed. An event listener can change the headers in-place before the stream starts piping out parsed data.

Example

```js
// Here the test.csv is parsed but field dt is converted to date. Each packet will
// Here the test.csv is parsed but field dt is converted to date. Each packet will
// contain the following properties: __filename, __path, __line and csv fields
etl.file('test.csv')
.pipe(etl.csv({
Expand Down
6 changes: 5 additions & 1 deletion lib/csv_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ Csv_parser.prototype._push = function(d) {
obj[key] = d[key];
}
}
obj.__line = ++this.line;

if (this.options.addLineNumbers !== false) {
obj.__line = ++this.line;
}

this.push(obj);
};

Expand Down
82 changes: 59 additions & 23 deletions test/csv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,70 @@ const t = require('tap');
const data = require('./data');

t.test('csv',async t => {
const csv = etl.csv_parser({
sanitize: true,
transform: {
dt: d => new Date(d)
}
});

etl.file(path.join(__dirname,'test.csv')).pipe(csv);
t.test('parsing',async t => {
const csv = etl.csv_parser({
sanitize: true,
transform: {
dt: d => new Date(d)
}
});

etl.file(path.join(__dirname,'test.csv')).pipe(csv);

// Adjust expected values to the csv
const expected = data.copy().map(function(d) {
d.__line = d.__line +1;
d.__filename = 'test.csv';

// Adjust expected values to the csv
const expected = data.copy().map(function(d) {
d.__line = d.__line +1;
d.__filename = 'test.csv';
// Clear out __path and text as they are volatile
d.__path = undefined;
d.text = undefined;

return d;
});

// Clear out __path and text as they are volatile
d.__path = undefined;
d.text = undefined;

return d;
});
const d = await csv.pipe(etl.expand()).promise();

d.forEach(d => {
d.__path = undefined;
d.text = undefined;
});


const d = await csv.pipe(etl.expand()).promise();

d.forEach(d => {
d.__path = undefined;
d.text = undefined;
t.same(d,expected,'parses data correctly');
});

t.same(d,expected,'parses data correctly');
t.test('when addLineNumbers is set to false',async t => {
const csv = etl.csv_parser({
sanitize: true,
addLineNumbers: false,
transform: {
dt: d => new Date(d)
}
});

etl.file(path.join(__dirname,'test.csv')).pipe(csv);

// Adjust expected values to the csv
const expected = data.copy().map(function(d) {
d.__filename = 'test.csv';

// Clear out __path and text as they are volatile
d.__path = undefined;
d.text = undefined;
delete d.__line;

return d;
});

const d = await csv.pipe(etl.expand()).promise();

d.forEach(d => {
d.__path = undefined;
d.text = undefined;
});

t.same(d,expected,'parses data correctly');
});
});