Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ var extended = require("./extended"),
isUndefinedOrNull = extended.isUndefinedOrNull,
trim = extended.trim,
trimLeft = extended.trimLeft,
trimRight = extended.trimRight,
LINE_BREAK = extended.LINE_BREAK;
trimRight = extended.trimRight;

function createParser(options) {
options = options || {};
Expand All @@ -13,9 +12,10 @@ function createParser(options) {
doTrim = options.trim || false,
ESCAPE = options.quote || '"',
VALUE_REGEXP = new RegExp("([^" + delimiter + "'\"\\s\\\\]*(?:\\s+[^" + delimiter + "'\"\\s\\\\]+)*)"),
SEARCH_REGEXP = new RegExp("(?:\\n|" + delimiter + ")"),
SEARCH_REGEXP = new RegExp("(?:\\n|\\r|" + delimiter + ")"),
ESCAPE_CHAR = options.escape || '"',
NEXT_TOKEN_REGEXP = new RegExp("([^\\s]|\\\n|" + delimiter + ")");
NEXT_TOKEN_REGEXP = new RegExp("([^\\s]|\\n|\\r|" + delimiter + ")"),
LINE_BREAK = /[\r\n]/;

function formatItem(item) {
if (doTrim) {
Expand Down Expand Up @@ -67,11 +67,11 @@ function createParser(options) {
if (hasMoreData) {
cursor = null;
} else {
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor).replace(/\n/g, "\\n" + "'"));
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor).replace(/[r\n]/g, "\\n" + "'"));
}
} else if ((!depth && nextToken && nextToken.search(SEARCH_REGEXP) === -1)) {
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor, 10).replace(/\n/g, "\\n" + "'"));
} else if (hasMoreData && (!nextToken || nextToken.search(LINE_BREAK) === -1)) {
throw new Error("Parse Error: expected: '" + ESCAPE + "' got: '" + nextToken + "'. at '" + str.substr(cursor, 10).replace(/[\r\n]/g, "\\n" + "'"));
} else if (hasMoreData && (!nextToken || !LINE_BREAK.test(nextToken))) {
cursor = null;
}
if (cursor !== null) {
Expand All @@ -98,7 +98,7 @@ function createParser(options) {
items.push(formatItem(searchStr.substr(0, nextIndex)));
cursor += nextIndex + 1;
}
} else if (nextChar.search(LINE_BREAK) !== -1) {
} else if (LINE_BREAK.test(nextChar)) {
items.push(formatItem(searchStr.substr(0, nextIndex)));
cursor += nextIndex;
} else if (!hasMoreData) {
Expand Down Expand Up @@ -127,7 +127,7 @@ function createParser(options) {
if (isUndefinedOrNull(token)) {
i = lastLineI;
break;
} else if (token === LINE_BREAK) {
} else if (LINE_BREAK.test(token)) {
i = nextToken.cursor + 1;
if (i < l) {
rows.push(items);
Expand Down
2 changes: 1 addition & 1 deletion lib/parser_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extended(ParserStream).extend({

__handleLine: function __parseLineData(line, index, ignore) {
var ignoreEmpty = this._ignoreEmpty;
if (extended.isBoolean(ignoreEmpty) && ignoreEmpty && EMPTY.test(line.join(""))) {
if (extended.isBoolean(ignoreEmpty) && ignoreEmpty && (!line || EMPTY.test(line.join("")))) {
return null;
}
if (!ignore) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast-csv",
"version": "0.2.0",
"version": "0.2.1",
"description": "CSV parser and writer",
"main": "index.js",
"scripts": {
Expand Down
Loading