csv-parser
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -13,6 +13,8 @@ #!/usr/bin/env node | ||
o: 'output', | ||
s: 'separator' | ||
s: 'separator', | ||
e: 'escape' | ||
}, | ||
default: { | ||
s: ',' | ||
s: ',', | ||
e: '"' | ||
}, | ||
@@ -36,2 +38,3 @@ boolean: ['version', 'help'] | ||
' --separator,-s Set the separator character ("," by default)\n' + | ||
' --escape,-e Set the escape character (\'"\' by default)\n' + | ||
' --strict Require column length match headers length\n' + | ||
@@ -38,0 +41,0 @@ ' --version,-v Print out the installed version\n' + |
25
index.js
@@ -6,3 +6,3 @@ var stream = require('stream') | ||
var quote = new Buffer('"')[0] | ||
var escape = new Buffer('"')[0] | ||
var comma = new Buffer(',')[0] | ||
@@ -19,2 +19,3 @@ var cr = new Buffer('\r')[0] | ||
this.separator = opts.separator ? new Buffer(opts.separator)[0] : comma | ||
this.escape = opts.escape ? new Buffer(opts.escape)[0] : escape | ||
if (opts.newline) { | ||
@@ -35,3 +36,3 @@ this.newline = new Buffer(opts.newline)[0] | ||
this._first = true | ||
this._quoting = false | ||
this._escaped = false | ||
this._empty = this._raw ? new Buffer(0) : '' | ||
@@ -61,4 +62,4 @@ this._Row = null | ||
for (var i = start; i < buf.length; i++) { | ||
if (buf[i] === quote) this._quoting = !this._quoting | ||
if (!this._quoting) { | ||
if (buf[i] === this.escape) this._escaped = !this._escaped | ||
if (!this._escaped) { | ||
if (this._first && !this.customNewline) { | ||
@@ -97,3 +98,3 @@ if (buf[i] === nl) { | ||
Parser.prototype._flush = function (cb) { | ||
if (this._quoting || !this._prev) return cb() | ||
if (this._escaped || !this._prev) return cb() | ||
this._online(this._prev, this._prevEnd, this._prev.length + 1) // plus since online -1s | ||
@@ -109,15 +110,15 @@ cb() | ||
var cells = [] | ||
var inQuotes = false | ||
var isEscaped = false | ||
var offset = start | ||
for (var i = start; i < end; i++) { | ||
if (buf[i] === quote) { // " | ||
if (i < end - 1 && buf[i + 1] === quote) { // "" | ||
if (buf[i] === this.escape) { // " | ||
if (i < end - 1 && buf[i + 1] === this.escape) { // "" | ||
i++ | ||
} else { | ||
inQuotes = !inQuotes | ||
isEscaped = !isEscaped | ||
} | ||
continue | ||
} | ||
if (buf[i] === comma && !inQuotes) { | ||
if (buf[i] === comma && !isEscaped) { | ||
cells.push(this._oncell(buf, offset, i)) | ||
@@ -174,3 +175,3 @@ offset = i + 1 | ||
Parser.prototype._oncell = function (buf, start, end) { | ||
if (buf[start] === quote && buf[end - 1] === quote) { | ||
if (buf[start] === this.escape && buf[end - 1] === this.escape) { | ||
start++ | ||
@@ -181,3 +182,3 @@ end-- | ||
for (var i = start, y = start; i < end; i++) { | ||
if (buf[i] === quote && buf[i + 1] === quote) i++ | ||
if (buf[i] === this.escape && buf[i + 1] === this.escape) i++ | ||
if (y !== i) buf[y] = buf[i] | ||
@@ -184,0 +185,0 @@ y++ |
{ | ||
"name": "csv-parser", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Streaming CSV parser that aims for maximum speed as well as compatibility with the csv-spectrum test suite", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -38,2 +38,3 @@ # csv-parser | ||
separator: ',', // specify optional cell separator | ||
escape: '"', // specify optional escape character | ||
newline: '\n', // specify a newline character | ||
@@ -59,5 +60,6 @@ strict: true // require column length match headers length | ||
var stream = csv({ | ||
raw: false, // do not decode to utf-8 strings | ||
raw: false, // do not decode to utf-8 strings | ||
separator: ',', // specify optional cell separator | ||
newline: '\n', // specify a newline character | ||
escape: '"', // specify optional escape character | ||
newline: '\n', // specify a newline character | ||
headers: ['index', 'message'] // Specifing the headers | ||
@@ -89,7 +91,14 @@ }) | ||
- `outputSeparator` - default `\n`, what to put between JSON items in the output | ||
- 'beforeOutput` - default empty, what to put at beginning of output | ||
- `beforeOutput` - default empty, what to put at beginning of output | ||
- `afterOutput` - default `\n`, what to put at end of output | ||
For example, to produce an object with a JSON array of items as output: | ||
``` | ||
--beforeOutput='{"items":[' --afterOutput=]} --outputSeparator=, | ||
``` | ||
## License | ||
MIT |
@@ -57,2 +57,13 @@ var test = require('tape') | ||
test('custom escaped character', function (t) { | ||
collect('custom_escaped_character.csv', {escape: '`'}, verify) | ||
function verify (err, lines) { | ||
t.false(err, 'no err') | ||
t.same(lines[0], {a: '1', b: 'ha `ha` ha'}, 'first row') | ||
t.same(lines[1], {a: '3', b: '4'}, 'second row') | ||
t.equal(lines.length, 2, '2 rows') | ||
t.end() | ||
} | ||
}) | ||
test('raw escaped quotes and newlines', function (t) { | ||
@@ -59,0 +70,0 @@ collect('quotes_and_newlines.csv', verify) |
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
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
20574
25
495
102