Skip to content

Commit 5453bf3

Browse files
committed
v0.2.1
* Fixed issue with \r line break in parser
1 parent 7d230e8 commit 5453bf3

File tree

4 files changed

+370
-167
lines changed

4 files changed

+370
-167
lines changed

lib/parser.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ var extended = require("./extended"),
22
isUndefinedOrNull = extended.isUndefinedOrNull,
33
trim = extended.trim,
44
trimLeft = extended.trimLeft,
5-
trimRight = extended.trimRight,
6-
LINE_BREAK = extended.LINE_BREAK;
5+
trimRight = extended.trimRight;
76

87
function createParser(options) {
98
options = options || {};
@@ -13,9 +12,10 @@ function createParser(options) {
1312
doTrim = options.trim || false,
1413
ESCAPE = options.quote || '"',
1514
VALUE_REGEXP = new RegExp("([^" + delimiter + "'\"\\s\\\\]*(?:\\s+[^" + delimiter + "'\"\\s\\\\]+)*)"),
16-
SEARCH_REGEXP = new RegExp("(?:\\n|" + delimiter + ")"),
15+
SEARCH_REGEXP = new RegExp("(?:\\n|\\r|" + delimiter + ")"),
1716
ESCAPE_CHAR = options.escape || '"',
18-
NEXT_TOKEN_REGEXP = new RegExp("([^\\s]|\\\n|" + delimiter + ")");
17+
NEXT_TOKEN_REGEXP = new RegExp("([^\\s]|\\n|\\r|" + delimiter + ")"),
18+
LINE_BREAK = /[\r\n]/;
1919

2020
function formatItem(item) {
2121
if (doTrim) {
@@ -67,11 +67,11 @@ function createParser(options) {
6767
if (hasMoreData) {
6868
cursor = null;
6969
} else {
70-
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor).replace(/\n/g, "\\n" + "'"));
70+
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor).replace(/[r\n]/g, "\\n" + "'"));
7171
}
7272
} else if ((!depth && nextToken && nextToken.search(SEARCH_REGEXP) === -1)) {
73-
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor, 10).replace(/\n/g, "\\n" + "'"));
74-
} else if (hasMoreData && (!nextToken || nextToken.search(LINE_BREAK) === -1)) {
73+
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor, 10).replace(/[\r\n]/g, "\\n" + "'"));
74+
} else if (hasMoreData && (!nextToken || !LINE_BREAK.test(nextToken))) {
7575
cursor = null;
7676
}
7777
if (cursor !== null) {
@@ -98,7 +98,7 @@ function createParser(options) {
9898
items.push(formatItem(searchStr.substr(0, nextIndex)));
9999
cursor += nextIndex + 1;
100100
}
101-
} else if (nextChar.search(LINE_BREAK) !== -1) {
101+
} else if (LINE_BREAK.test(nextChar)) {
102102
items.push(formatItem(searchStr.substr(0, nextIndex)));
103103
cursor += nextIndex;
104104
} else if (!hasMoreData) {
@@ -127,7 +127,7 @@ function createParser(options) {
127127
if (isUndefinedOrNull(token)) {
128128
i = lastLineI;
129129
break;
130-
} else if (token === LINE_BREAK) {
130+
} else if (LINE_BREAK.test(token)) {
131131
i = nextToken.cursor + 1;
132132
if (i < l) {
133133
rows.push(items);

lib/parser_stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extended(ParserStream).extend({
6161

6262
__handleLine: function __parseLineData(line, index, ignore) {
6363
var ignoreEmpty = this._ignoreEmpty;
64-
if (extended.isBoolean(ignoreEmpty) && ignoreEmpty && EMPTY.test(line.join(""))) {
64+
if (extended.isBoolean(ignoreEmpty) && ignoreEmpty && (!line || EMPTY.test(line.join("")))) {
6565
return null;
6666
}
6767
if (!ignore) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-csv",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "CSV parser and writer",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)