Comparing version 0.1.0 to 0.1.1
var fastCsv = require("../lib"), | ||
csv = require("csv"), | ||
path = require("path"), | ||
COUNT = 1000000, | ||
COUNT = 20000, | ||
TEST_FILE = path.resolve(__dirname, "./assets/" + COUNT + ".csv"); | ||
@@ -6,0 +6,0 @@ |
@@ -25,3 +25,8 @@ /** | ||
* * **NOTE** When specifying an alternate `delimiter` you may only pass in a single character delimeter | ||
* * The following are options for parsing only. | ||
* * `trim=false`: If you want to trim all values parsed set to true. | ||
* * `rtrim=false`: If you want to right trim all values parsed set to true. | ||
* * `ltrim=false`: If you want to left trim all values parsed set to true. | ||
* | ||
* | ||
* **events** | ||
@@ -28,0 +33,0 @@ * |
@@ -30,3 +30,4 @@ var extended = require("./extended"), | ||
} | ||
this.parser = createParser(delimiter); | ||
options.delimiter = delimiter; | ||
this.parser = createParser(options); | ||
this._headers = options.headers; | ||
@@ -33,0 +34,0 @@ this._ignoreEmpty = options.ignoreEmpty; |
@@ -1,10 +0,30 @@ | ||
var SINGLE_QUOTE = "'", | ||
var extended = require("./extended"), | ||
trim = extended.trim, | ||
trimLeft = extended.trimLeft, | ||
trimRight = extended.trimRight, | ||
SINGLE_QUOTE = "'", | ||
DOUBLE_QUOTE = '"'; | ||
function createParser(delimiter) { | ||
var VALUE_REGEXP = new RegExp("([^" + delimiter + "'\"\\s\\\\]*(?:\\s+[^" + delimiter + "'\"\\s\\\\]+)*)"), | ||
function createParser(options) { | ||
options = options || {}; | ||
var delimiter = options.delimiter || ",", | ||
doLtrim = options.ltrim || false, | ||
doRtrim = options.rtrim || false, | ||
doTrim = options.trim || false, | ||
VALUE_REGEXP = new RegExp("([^" + delimiter + "'\"\\s\\\\]*(?:\\s+[^" + delimiter + "'\"\\s\\\\]+)*)"), | ||
SEARCH_REGEXP = new RegExp("[^\\\\]" + delimiter), | ||
ESCAPE_CHAR = "\\"; | ||
ESCAPE_CHAR = "\\", | ||
WHITE_SPACE = /\s/; | ||
function formatItem(item) { | ||
if (doTrim) { | ||
item = trim(item); | ||
} else if (doLtrim) { | ||
item = trimLeft(item); | ||
} else if (doRtrim) { | ||
item = trimRight(item); | ||
} | ||
return item; | ||
} | ||
function getTokensBetween(str, start, items, cursor) { | ||
@@ -38,6 +58,6 @@ var depth = 0, ret = []; | ||
} | ||
if (++cursor < str.length && str[cursor].search(delimiter) !== 0) { | ||
if (++cursor < str.length && getNextToken(str, cursor).token.search(delimiter) !== 0) { | ||
throw new Error("Invalid row " + str); | ||
} | ||
items.push(ret.join("")); | ||
items.push(formatItem(ret.join(""))); | ||
return ++cursor; | ||
@@ -56,3 +76,3 @@ } | ||
} | ||
items.push(searchStr.substr(0, nextIndex + 1)); | ||
items.push(formatItem(searchStr.substr(0, nextIndex + 1))); | ||
return cursor + (nextIndex + 2); | ||
@@ -74,6 +94,24 @@ } | ||
function getNextToken(line, cursor) { | ||
var l = line.length, ret, token; | ||
do { | ||
token = line[cursor]; | ||
if (token === delimiter || !WHITE_SPACE.test(token)) { | ||
ret = token; | ||
} else { | ||
token = null; | ||
} | ||
} while (!token && cursor++ < l); | ||
if (!token) { | ||
throw new Error("Invalid row " + line); | ||
} | ||
return {token: token, cursor: cursor}; | ||
} | ||
return function parseLine(line) { | ||
var i = 0, l = line.length, items = [], token; | ||
var i = 0, l = line.length, items = [], token, nextToken; | ||
while (i < l) { | ||
token = line[i]; | ||
nextToken = getNextToken(line, i); | ||
token = nextToken.token; | ||
if (token === delimiter) { | ||
@@ -83,8 +121,9 @@ items.push(""); | ||
} else if (token === SINGLE_QUOTE) { | ||
i = parseSingleQuoteItem(line, items, i); | ||
i = parseSingleQuoteItem(line, items, nextToken.cursor); | ||
} else if (token === DOUBLE_QUOTE) { | ||
i = parseDoubleQuoteItem(line, items, i); | ||
i = parseDoubleQuoteItem(line, items, nextToken.cursor); | ||
} else { | ||
i = parseItem(line, items, i); | ||
} | ||
} | ||
@@ -91,0 +130,0 @@ return items; |
{ | ||
"name": "fast-csv", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "CSV parser for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -24,3 +24,8 @@ <a name="top"></a> | ||
* **NOTE** When specifying an alternate `delimiter` you may only pass in a single character delimeter | ||
* The following are options for parsing only. | ||
* `trim=false`: If you want to trim all values parsed set to true. | ||
* `rtrim=false`: If you want to right trim all values parsed set to true. | ||
* `ltrim=false`: If you want to left trim all values parsed set to true. | ||
**events** | ||
@@ -227,8 +232,8 @@ | ||
csv | ||
.write([ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}) | ||
.pipe(ws); | ||
.write([ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}) | ||
.pipe(ws); | ||
``` | ||
@@ -239,7 +244,7 @@ | ||
csv | ||
.write([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.pipe(ws); | ||
.write([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.pipe(ws); | ||
``` | ||
@@ -253,7 +258,7 @@ | ||
csv | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}); | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}); | ||
``` | ||
@@ -263,7 +268,7 @@ | ||
csv | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.pipe(ws); | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.pipe(ws); | ||
``` | ||
@@ -277,10 +282,10 @@ | ||
csv | ||
.writeToPath("my.csv", [ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
.writeToPath("my.csv", [ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
``` | ||
@@ -290,9 +295,9 @@ | ||
csv | ||
.writeToStream("my.csv", [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
.writeToStream("my.csv", [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
``` | ||
@@ -304,5 +309,5 @@ | ||
csv.writeToString([ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}); //"a,b\na1,b1\na2,b2\n" | ||
@@ -313,4 +318,4 @@ ``` | ||
csv.writeToString([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}); //"a,b\na1,b1\na2,b2\n" | ||
@@ -317,0 +322,0 @@ ``` |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
59472732
896
370
0