csv-write-stream
Advanced tools
Comparing version 0.3.0 to 1.0.0
@@ -6,3 +6,4 @@ ## Collaborators | ||
<table><tbody><tr><th align="left">maxogden</th><td><a href="https://github.com/maxogden">GitHub/maxogden</a></td></tr> | ||
<tr><th align="left">mafintosh</th><td><a href="https://github.com/mafintosh">GitHub/mafintosh</a></td></tr> | ||
<tr><th align="left">finnp</th><td><a href="https://github.com/finnp">GitHub/finnp</a></td></tr> | ||
</tbody></table> |
29
index.js
@@ -17,2 +17,3 @@ var stream = require('stream') | ||
this._first = true | ||
this._destroyed = false | ||
} | ||
@@ -23,2 +24,3 @@ | ||
CsvWriteStream.prototype._compile = function(headers) { | ||
var newline = this.newline | ||
var sep = this.separator | ||
@@ -34,10 +36,13 @@ var str = 'function toRow(obj) {\n' | ||
str += 'return ' | ||
for (var i = 0; i < headers.length; i += 500) { // do not overflowi 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) { | ||
str += (j ? '+"'+sep+'"+' : '') + '(/['+sep+'\\r\\n"]/.test('+prop+') ? esc('+prop+'+"") : '+prop+')' | ||
}) | ||
str += '\n' | ||
} | ||
headers.forEach(function(prop, i) { | ||
str += (i ? '+"'+sep+'"+' : '') + '(/['+sep+'\\r\\n"]/.test('+prop+') ? esc('+prop+'+"") : '+prop+')' | ||
}) | ||
str += 'return result +'+JSON.stringify(newline)+'\n}' | ||
str += '+'+JSON.stringify(this.newline)+'\n}' | ||
return new Function('esc', 'return '+str)(esc) | ||
@@ -79,2 +84,14 @@ } | ||
CsvWriteStream.prototype.destroy = function (err) { | ||
if (this._destroyed) return | ||
this._destroyed = true | ||
var self = this | ||
process.nextTick(function () { | ||
if (err) self.emit('error', err) | ||
self.emit('close') | ||
}) | ||
} | ||
module.exports = function(opts) { | ||
@@ -81,0 +98,0 @@ return new CsvWriteStream(opts) |
{ | ||
"name": "csv-write-stream", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"description": "A CSV encoder stream that produces properly escaped CSVs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
65
test.js
@@ -7,3 +7,3 @@ var test = require('tape') | ||
var writer = csv({ headers: ["hello", "foo"]}) | ||
writer.pipe(concat(function(data) { | ||
@@ -13,3 +13,3 @@ t.equal('hello,foo\nworld,bar\n', data.toString()) | ||
})) | ||
writer.write(["world", "bar"]) | ||
@@ -21,3 +21,3 @@ writer.end() | ||
var writer = csv({ headers: ['why "hello" there', "foo"]}) | ||
writer.pipe(concat(function(data) { | ||
@@ -27,3 +27,3 @@ t.equal('"why ""hello"" there",foo\nworld,bar\n', data.toString()) | ||
})) | ||
writer.write(["world", "bar"]) | ||
@@ -35,3 +35,3 @@ writer.end() | ||
var writer = csv({ headers: ["hello", "foo"]}) | ||
writer.pipe(concat(function(data) { | ||
@@ -41,3 +41,3 @@ t.equal('hello,foo\nworld,"this is an ""escaped"" cell"\n', data.toString()) | ||
})) | ||
writer.write(["world", 'this is an "escaped" cell']) | ||
@@ -49,3 +49,3 @@ writer.end() | ||
var writer = csv({ headers: ["hello", "foo"]}) | ||
writer.pipe(concat(function(data) { | ||
@@ -55,3 +55,3 @@ t.equal('hello,foo\nworld,"this is a\nmultiline cell"\n', data.toString()) | ||
})) | ||
writer.write(["world", 'this is a\nmultiline cell']) | ||
@@ -63,3 +63,3 @@ writer.end() | ||
var writer = csv({ headers: ["hello", "foo"]}) | ||
writer.pipe(concat(function(data) { | ||
@@ -69,3 +69,3 @@ t.equal('hello,foo\nworld,"this is a cell with, commas, in it"\n', data.toString()) | ||
})) | ||
writer.write(["world", 'this is a cell with, commas, in it']) | ||
@@ -77,3 +77,3 @@ writer.end() | ||
var writer = csv({ headers: ["hello", "foo"]}) | ||
writer.pipe(concat(function(data) { | ||
@@ -83,3 +83,3 @@ t.equal('hello,foo\nworld,bar\n', data.toString()) | ||
})) | ||
writer.write({hello: "world", foo: "bar", baz: "taco"}) | ||
@@ -91,3 +91,3 @@ writer.end() | ||
var writer = csv() | ||
writer.pipe(concat(function(data) { | ||
@@ -97,3 +97,3 @@ t.equal('hello,foo,baz\nworld,bar,taco\n', data.toString()) | ||
})) | ||
writer.write({hello: "world", foo: "bar", baz: "taco"}) | ||
@@ -105,3 +105,3 @@ writer.end() | ||
var writer = csv() | ||
writer.on('error', function(err) { | ||
@@ -111,3 +111,3 @@ t.equal(err.message, 'no headers specified') | ||
}) | ||
writer.write(['foo', 'bar']) | ||
@@ -156,1 +156,34 @@ writer.end() | ||
}) | ||
test('destroy with error', function (t) { | ||
var writer = csv({sendHeaders: false}) | ||
t.plan(2) | ||
writer.pipe(concat(function (data) { | ||
t.equal(data, '1,2\n', 'date received') | ||
})) | ||
writer.on('error', function (err) { | ||
writer.end() | ||
t.equal(err.message, 'error') | ||
}) | ||
writer.write({a: 1, b : 2}) | ||
writer.destroy(new Error('error')) | ||
}) | ||
test('lots of cols', function (t) { | ||
var writer = csv() | ||
var obj = {} | ||
writer.pipe(concat(function (data) { | ||
t.equal(data, Object.keys(obj).join(',') + '\n' + Object.keys(obj).join(',') + '\n') | ||
t.end() | ||
})) | ||
for (var i = 0; i < 5000; i++) obj[i] = '' + i | ||
writer.write(obj) | ||
writer.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12159
230
0
9