Skip to content
Closed
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
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ The options are also passed to the underlying transform stream, so you can pass
objectMode: false,

// if set to true, uses first row as keys -> [ { column1: value1, column2: value2 }, ...]
// a function can be passed to columns to map the array of columns
// columns: (cols) => cols.map(col => col.toLowerCase())
columns: false
}
```
Expand Down
8 changes: 6 additions & 2 deletions csv-streamify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var through = require('through2')
* @param {string} [opts.quote='"'] The quote character
* @param {string} [opts.empty=''] Which character to return for empty fields
* @param {boolean} [opts.objectMode=true] Whether to return an object or a buffer per line
* @param {boolean} [opts.columns=false] Whether to parse headers
* @param {boolean|function} [opts.columns=false] Whether to parse headers or a function to parse headers
* @param {function} [cb] Callback function
*/
module.exports = function (opts, cb) {
Expand Down Expand Up @@ -53,7 +53,11 @@ function createParser (opts, state) {

if (opts.hasColumns) {
if (state.lineNo === 0) {
state._columns = state._line
if (typeof opts.hasColumns === 'function') {
state._columns = opts.hasColumns(state._line)
} else {
state._columns = state._line
}
state.lineNo += 1
reset()
return
Expand Down
13 changes: 13 additions & 0 deletions test/csv-streamify.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ describe('object mode', function () {

str('COL0,COL1\ncol0,col1\ncol2,col3').pipe(parser)
})

it('should emit multiple columns parsed by custom function', function (done) {
var parser = csv({
columns: (cols) => cols.map(col => col.toLowerCase())
}, function (err, res) {
if (err) return done(err)
assert.deepEqual(res[0], { col0: 'col0', col1: 'col1' })
assert.deepEqual(res[1], { col0: 'col2', col1: 'col3' })
done()
})

str('COL0,COL1\ncol0,col1\ncol2,col3').pipe(parser)
})
})

describe('edge cases', function () {
Expand Down