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
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var CsvWriteStream = function(opts) {
if (!opts) opts = {}
stream.Transform.call(this, {objectMode:true, highWaterMark:16})

this.mapHeaders = opts.mapHeaders || function(header) { return header }
this.sendHeaders = opts.sendHeaders !== false
this.headers = opts.headers || null
this.separator = opts.separator || opts.seperator || ','
Expand Down Expand Up @@ -51,21 +52,34 @@ CsvWriteStream.prototype._transform = function(row, enc, cb) {
if (!isArray && !this.headers) this.headers = Object.keys(row)

if (this._first && this.headers) {

var printedHeaders = [];

for (var i = 0; i < this.headers.length; i++) {
var key = this.headers[i]
var printedHeader = this.mapHeaders(key, i)
printedHeaders.push(printedHeader)
if (typeof printedHeader !== 'string') {
this.headers[i] = null
}
}

this._first = false

var objProps = []
var arrProps = []
var heads = []

for (var i = 0; i < this.headers.length; i++) {
arrProps.push('obj['+i+']')
objProps.push(gen('obj', this.headers[i]))
if (this.headers[i] !== null) {
arrProps.push('obj['+i+']')
objProps.push(gen('obj', this.headers[i]))
}
}

this._objRow = this._compile(objProps)
this._arrRow = this._compile(arrProps)

if (this.sendHeaders) this.push(this._arrRow(this.headers))
if (this.sendHeaders) this.push(this._arrRow(printedHeaders || this.headers))
}

if (isArray) {
Expand Down
50 changes: 50 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,56 @@ test('encode from object w/ auto headers', function(t) {
writer.end()
})

test('encode from object w/ auto headers and one header skipped', function(t) {
var writer = csv({
mapHeaders: function (header) {
return header !== 'foo' ? header : null
}
})

writer.pipe(concat(function(data) {
t.equal(data.toString(), 'hello,baz\nworld,taco\n')
t.end()
}))

writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})

test('encode from object w/ auto headers and change the header', function(t) {
var writer = csv({
mapHeaders: function (header) {
return header !== 'foo' ? header : 'Foo'
}
})

writer.pipe(concat(function(data) {
t.equal(data.toString(), 'hello,Foo,baz\nworld,bar,taco\n')
t.end()
}))

writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})

test('encode from object edit header and filter one column', function(t) {
var writer = csv({
headers: ['hello', 'foo', 'baz'],
mapHeaders: function (header) {
return header === 'foo' ? null
: header.substring(0,1).toUpperCase() + header.substring(1)
}
})

writer.pipe(concat(function(data) {
t.equal(data.toString(), 'Hello,Baz\nworld,taco\n')
t.end()
}))

writer.write(["world", "bar", "taco"])
writer.end()
})

test('no headers specified', function(t) {
var writer = csv()

Expand Down