From 6f82be024643f582c5b4da6f36dcad93e94ed839 Mon Sep 17 00:00:00 2001 From: Gaston Elhordoy Date: Thu, 28 Apr 2016 15:51:52 -0300 Subject: [PATCH 1/5] Make ir possible to add the sep metadata to the CSV --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index e550bd4..2cc3644 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ var CsvWriteStream = function(opts) { this.sendHeaders = opts.sendHeaders !== false this.headers = opts.headers || null this.separator = opts.separator || opts.seperator || ',' + this.sendMetadata = !!opts.sendMetadata this.newline = opts.newline || '\n' this._objRow = null @@ -65,6 +66,7 @@ CsvWriteStream.prototype._transform = function(row, enc, cb) { this._objRow = this._compile(objProps) this._arrRow = this._compile(arrProps) + if (this.sendMetadata) this.push('sep=' + this.separator + this.newline) if (this.sendHeaders) this.push(this._arrRow(this.headers)) } From 264fdb7af218b78de22d0a6dc9deef4695cbaa5b Mon Sep 17 00:00:00 2001 From: Gaston Elhordoy Date: Thu, 9 Jun 2016 17:10:34 -0300 Subject: [PATCH 2/5] Fix typo --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 2cc3644..decbcff 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ CsvWriteStream.prototype._compile = function(headers) { return 'a'+i }) - for (var i = 0; i < headers.length; i += 500) { // do not overflowi the callstack on lots of cols + for (var i = 0; i < headers.length; i += 500) { // do not overflow the callstack on lots of cols var part = headers.length < 500 ? headers : headers.slice(i, i + 500) str += i ? 'result += "'+sep+'" + ' : 'var result = ' part.forEach(function(prop, j) { From 4f562c78665084387831ca635da657f3323450b5 Mon Sep 17 00:00:00 2001 From: Gaston Elhordoy Date: Thu, 9 Jun 2016 17:13:43 -0300 Subject: [PATCH 3/5] Document sendMetadata option in README --- readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a5d0bb5..78762f8 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,8 @@ var writer = csvWriter() separator: ',', newline: '\n', headers: undefined, - sendHeaders: true + sendHeaders: true, + sendMetadata: false } ``` From e3d1ad3fcbd3668bcaafd4456fcda18bae5fe788 Mon Sep 17 00:00:00 2001 From: Gaston Elhordoy Date: Thu, 9 Jun 2016 17:14:56 -0300 Subject: [PATCH 4/5] Allow to write arrays without headers specified --- index.js | 22 +++++++++++++--------- test.js | 8 ++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index decbcff..c0432c3 100644 --- a/index.js +++ b/index.js @@ -51,19 +51,23 @@ CsvWriteStream.prototype._transform = function(row, enc, cb) { if (!isArray && !this.headers) this.headers = Object.keys(row) - if (this._first && this.headers) { + if (this._first) { 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) { + for (var i = 0; i < this.headers.length; i++) { + arrProps.push('obj['+i+']') + objProps.push(gen('obj', this.headers[i])) + } + this._objRow = this._compile(objProps) + } else { + for (var j = 0; j < row.length; j++) { + arrProps.push('obj['+j+']') + } + this.sendHeaders = false; } - - this._objRow = this._compile(objProps) this._arrRow = this._compile(arrProps) if (this.sendMetadata) this.push('sep=' + this.separator + this.newline) @@ -71,9 +75,9 @@ CsvWriteStream.prototype._transform = function(row, enc, cb) { } if (isArray) { - if (!this.headers) return cb(new Error('no headers specified')) this.push(this._arrRow(row)) } else { + if (!this.headers) return cb(new Error('no headers specified for object after the first element')) this.push(this._objRow(row)) } diff --git a/test.js b/test.js index 7fec402..ddbbd07 100644 --- a/test.js +++ b/test.js @@ -86,13 +86,13 @@ test('encode from object w/ auto headers', function(t) { writer.end() }) -test('no headers specified', function(t) { +test('no headers specified for array', function(t) { var writer = csv() - writer.on('error', function(err) { - t.equal(err.message, 'no headers specified') + writer.pipe(concat(function(data) { + t.equal('foo,bar\n', data.toString()) t.end() - }) + })) writer.write(['foo', 'bar']) writer.end() From d4888cf34991781f1c3683786a9022c3c80253e2 Mon Sep 17 00:00:00 2001 From: Gaston Elhordoy Date: Fri, 10 Jun 2016 02:45:08 -0300 Subject: [PATCH 5/5] Allow to specify empty string as a valid separator, useful for fixed width records --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c0432c3..437f0d1 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ var CsvWriteStream = function(opts) { this.sendHeaders = opts.sendHeaders !== false this.headers = opts.headers || null - this.separator = opts.separator || opts.seperator || ',' + this.separator = opts.separator === undefined || opts.separator === null ? ',' : opts.separator this.sendMetadata = !!opts.sendMetadata this.newline = opts.newline || '\n' @@ -43,7 +43,8 @@ CsvWriteStream.prototype._compile = function(headers) { str += 'return result +'+JSON.stringify(newline)+'\n}' - return new Function('esc', 'return '+str)(esc) + var escape = sep === '' ? noEsc : esc; + return new Function('esc', 'return '+str)(escape) } CsvWriteStream.prototype._transform = function(row, enc, cb) { @@ -70,7 +71,7 @@ CsvWriteStream.prototype._transform = function(row, enc, cb) { } this._arrRow = this._compile(arrProps) - if (this.sendMetadata) this.push('sep=' + this.separator + this.newline) + if (this.sendMetadata && this.separator) this.push('sep=' + this.separator + this.newline) if (this.sendHeaders) this.push(this._arrRow(this.headers)) } @@ -103,3 +104,6 @@ module.exports = function(opts) { function esc(cell) { return '"'+cell.replace(/"/g, '""')+'"' } +function noEsc(cell) { + return cell; +} \ No newline at end of file