-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from doug-martin/master
v0.2.2
- Loading branch information
Showing
3 changed files
with
205 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,7 +200,6 @@ it.describe("fast-csv parser", function (it) { | |
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
it.describe("with \\r", function (it) { | ||
|
@@ -402,4 +401,203 @@ it.describe("fast-csv parser", function (it) { | |
|
||
}); | ||
|
||
it.describe("with \\r\\n", function (it) { | ||
|
||
it.describe("unescaped data", function (it) { | ||
|
||
it.should("parse a block of CSV text", function () { | ||
var data = "first_name,last_name,email_address\r\nFirst1,Last1,[email protected]"; | ||
var myParser = parser({delimiter: ","}); | ||
assert.deepEqual(myParser(data, false), {"line": "", "rows": [ | ||
["first_name", "last_name", "email_address"], | ||
["First1", "Last1", "[email protected]"] | ||
]}); | ||
}); | ||
|
||
it.should("return the rest of the line if there is more data", function () { | ||
var data = "first_name,last_name,email_address\r\nFirst1,Last1,[email protected]"; | ||
var myParser = parser({delimiter: ","}); | ||
assert.deepEqual(myParser(data, true), { | ||
"line": "First1,Last1,[email protected]", | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
}); | ||
|
||
it.should("accept new data and return the result", function () { | ||
var data = "first_name,last_name,email_address\r\nFirst1,Last1,[email protected],"; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": "First1,Last1,[email protected],", | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
assert.deepEqual(myParser(parsedData.line + "\r\nFirst2,Last2,[email protected]", false), {"line": "", "rows": [ | ||
["First1", "Last1", "[email protected]"], | ||
["First2", "Last2", "[email protected]"] | ||
]}); | ||
}); | ||
|
||
it.should("not parse a row if a new line is not found and there is more data", function () { | ||
var data = "first_name,last_name,email_address"; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": "first_name,last_name,email_address", | ||
"rows": [] | ||
}); | ||
}); | ||
|
||
it.should("not parse a row if there is a trailing delimiter and there is more data", function () { | ||
var data = "first_name,last_name,email_address,"; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": "first_name,last_name,email_address,", | ||
"rows": [] | ||
}); | ||
}); | ||
|
||
it.should("parse a row if a new line is found and there is more data", function () { | ||
var data = "first_name,last_name,email_address\r\n"; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": "", | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
it.describe("escaped values", function (it) { | ||
|
||
it.should("parse a block of CSV text", function () { | ||
var data = 'first_name,last_name,email_address\r\n"First,1","Last,1","[email protected]"'; | ||
var myParser = parser({delimiter: ","}); | ||
assert.deepEqual(myParser(data, false), {"line": "", "rows": [ | ||
["first_name", "last_name", "email_address"], | ||
["First,1", "Last,1", "[email protected]"] | ||
]}); | ||
}); | ||
|
||
it.should("parse a block of CSV text with escaped escaped char", function () { | ||
var data = 'first_name,last_name,email_address\r\n"First,""1""","Last,""1""","[email protected]"'; | ||
var myParser = parser({delimiter: ","}); | ||
assert.deepEqual(myParser(data, false), {"line": "", "rows": [ | ||
["first_name", "last_name", "email_address"], | ||
["First,\"1\"", "Last,\"1\"", "[email protected]"] | ||
]}); | ||
}); | ||
|
||
it.should("parse a block of CSV text with alternate escape char", function () { | ||
var data = 'first_name,last_name,email_address\r\n"First,\\"1\\"","Last,\\"1\\"","[email protected]"'; | ||
var myParser = parser({delimiter: ",", escape: "\\"}); | ||
assert.deepEqual(myParser(data, false), {"line": "", "rows": [ | ||
["first_name", "last_name", "email_address"], | ||
["First,\"1\"", "Last,\"1\"", "[email protected]"] | ||
]}); | ||
}); | ||
|
||
it.should("return the rest of the line if a complete value is not found", function () { | ||
var data = 'first_name,last_name,email_address\r\n"First,""1""","Last,""1""","[email protected]'; | ||
var myParser = parser({delimiter: ","}); | ||
assert.deepEqual(myParser(data, true), { | ||
"line": '"First,""1""","Last,""1""","[email protected]', | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
}); | ||
|
||
it.should("accept more data appended to the returned line with escaped values", function () { | ||
var data = 'first_name,last_name,email_address\r\n"First,""1""","Last,""1""","[email protected]'; | ||
var myParser = parser({delimiter: ","}), | ||
parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": '"First,""1""","Last,""1""","[email protected]', | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
assert.deepEqual(myParser(parsedData.line + '"\r\n"First,""2""","Last,""2""","[email protected]"', false), { | ||
line: "", | ||
rows: [ | ||
["First,\"1\"", "Last,\"1\"", "[email protected]"], | ||
["First,\"2\"", "Last,\"2\"", "[email protected]"] | ||
] | ||
}); | ||
}); | ||
|
||
it.should("throw an error if there is not more data and there is an invalid escape sequence", function () { | ||
var data = 'first_name,last_name,email_address\r\n"First,""1""","Last,""1""","[email protected]'; | ||
var myParser = parser({delimiter: ","}), | ||
parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": '"First,""1""","Last,""1""","[email protected]', | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
assert.throws(function () { | ||
assert.deepEqual(myParser(parsedData.line + '\r\n"First,"",2""","Last""2""","[email protected]"', false), { | ||
line: "", | ||
rows: [ | ||
["First,\"1\"", "Last,\"1\"", "[email protected]"], | ||
["First,\"2\"", "Last,\"2\"", "[email protected]"] | ||
] | ||
}); | ||
}, Error, ' Parse Error: expected: \'"\' got: \'F\'. at \'First,""2""","Last""2""","[email protected]"'); | ||
}); | ||
|
||
it.should("handle empty values properly", function () { | ||
var data = '"","",""\r\n,Last4,[email protected]'; | ||
var myParser = parser({delimiter: ","}), | ||
parsedData = myParser(data, false); | ||
assert.deepEqual(parsedData, {"line": "", "rows": [ | ||
["", "", ""], | ||
["", "Last4", "[email protected]"] | ||
]}); | ||
}); | ||
|
||
it.should("not parse a row if a new line is not found and there is more data", function () { | ||
var data = '"first_name","last_name","email_address"'; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": '"first_name","last_name","email_address"', | ||
"rows": [] | ||
}); | ||
}); | ||
|
||
it.should("not parse a row if there is a trailing delimiter and there is more data", function () { | ||
var data = '"first_name","last_name","email_address",'; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": '"first_name","last_name","email_address",', | ||
"rows": [] | ||
}); | ||
}); | ||
|
||
it.should("parse a row if a new line is found and there is more data", function () { | ||
var data = '"first_name","last_name","email_address"\r\n'; | ||
var myParser = parser({delimiter: ","}); | ||
var parsedData = myParser(data, true); | ||
assert.deepEqual(parsedData, { | ||
"line": "", | ||
"rows": [ | ||
["first_name", "last_name", "email_address"] | ||
] | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); |