Skip to content

Commit

Permalink
add mapValues, which mirrors mapHeaders (#66)
Browse files Browse the repository at this point in the history
* add mapValues, which mirrors mapHeaders

* remove stray log
  • Loading branch information
contra authored and mafintosh committed Nov 6, 2017
1 parent 234c57e commit 3525c89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var Parser = function (opts) {

this.headers = opts.headers || null
this.strict = opts.strict || null
this.mapHeaders = opts.mapHeaders || defaultMapHeaders
this.mapHeaders = opts.mapHeaders || identity
this.mapValues = opts.mapValues || identity

this._raw = !!opts.raw
this._prev = null
Expand Down Expand Up @@ -207,15 +208,16 @@ Parser.prototype._oncell = function (buf, start, end) {
y++
}

return this._onvalue(buf, start, y)
var value = this._onvalue(buf, start, y)
return this._first ? value : this.mapValues(value)
}

Parser.prototype._onvalue = function (buf, start, end) {
if (this._raw) return buf.slice(start, end)
return buf.toString('utf-8', start, end)
}

function defaultMapHeaders (id) {
function identity (id) {
return id
}

Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,19 @@ test('rename columns', function (t) {
}
})

test('format values', function (t) {
collect('dummy.csv', {mapValues: mapValues}, verify)
function mapValues (v) {
return parseInt(v, 10)
}
function verify (err, lines) {
t.false(err, 'no err')
t.same(lines[0], {a: 1, b: 2, c: 3}, 'first row')
t.equal(lines.length, 1, '1 row')
t.end()
}
})

// helpers

function fixture (name) {
Expand Down

0 comments on commit 3525c89

Please sign in to comment.