Comparing version 1.0.2 to 1.0.3
@@ -64,2 +64,6 @@ { | ||
"type": "boolean" | ||
}, | ||
"--escape":{ | ||
"desc":"escape character used in quoted column. Default is double quote (\") according to RFC4108. Change to back slash (\\) or other chars for your own case.", | ||
"type":"string" | ||
} | ||
@@ -66,0 +70,0 @@ }, |
@@ -18,2 +18,3 @@ var util = require("util"); | ||
quote: '"', //quote for a column containing delimiter. | ||
escape:'"', //escape char for quoted column | ||
trim: true, //trim column's space charcters | ||
@@ -20,0 +21,0 @@ checkType: true, //whether check column type |
@@ -5,6 +5,5 @@ | ||
rowSplit: rowSplit, //Split a csv row to an array based on delimiter and quote | ||
isToogleQuote: isToogleQuote, //returns if a segmenthas even number of quotes | ||
twoDoubleQuote: twoDoubleQuote //converts two double quotes to one | ||
isToogleQuote: isToogleQuote //returns if a segmenthas even number of quotes | ||
} | ||
var cachedRegExp = {}; | ||
var defaulDelimiters=[",","|","\t",";",":"]; | ||
@@ -33,3 +32,6 @@ function getDelimiter(rowStr,param) { | ||
var quote=param.quote; | ||
return str[0] === quote && (str[1]!==quote || str[1]===quote && (str[2] === quote || str.length ===2)); | ||
var escape=param.escape; | ||
return str[0] === quote && ( | ||
str[1]!==quote || | ||
str[1]===escape && (str[2] === quote || str.length ===2)); | ||
} | ||
@@ -40,3 +42,4 @@ function isQuoteClose(str,param){ | ||
var idx=str.length-1; | ||
while (str[idx] === quote){ | ||
var escape=param.escape; | ||
while (str[idx] === quote || str[idx]===escape){ | ||
idx--; | ||
@@ -53,2 +56,3 @@ count++; | ||
var trim=param.trim; | ||
var escape=param.escape; | ||
if (param.needCheckDelimiter===true){ | ||
@@ -77,3 +81,3 @@ param.delimiter=getDelimiter(rowStr,param); | ||
e=e.substring(0,e.length-1); | ||
e=twoDoubleQuote(e,quote); | ||
e=_escapeQuote(e,quote,escape); | ||
row.push(e); | ||
@@ -95,3 +99,3 @@ continue; | ||
quoteBuff+=delimiter+e; | ||
quoteBuff=twoDoubleQuote(quoteBuff,quote); | ||
quoteBuff=_escapeQuote(quoteBuff,quote,escape); | ||
if (trim){ | ||
@@ -134,16 +138,11 @@ quoteBuff=quoteBuff.trimRight(); | ||
function _getRegExpObj(quote) { | ||
if (cachedRegExp[quote]) { | ||
return cachedRegExp[quote]; | ||
} else { | ||
cachedRegExp[quote] = { | ||
single: new RegExp(quote, 'g'), | ||
double: new RegExp(quote + quote, 'g') | ||
} | ||
return _getRegExpObj(quote); | ||
} | ||
} | ||
var cachedRegExp = {}; | ||
function isToogleQuote(segment, quote) { | ||
var reg = _getRegExpObj(quote).single; | ||
var key="s|"+quote; | ||
if ( cachedRegExp[key]=== undefined){ | ||
cachedRegExp[key]=new RegExp(quote, 'g'); | ||
} | ||
var reg = cachedRegExp[key]; | ||
var match = segment.match(reg); | ||
@@ -153,5 +152,13 @@ return match && match.length % 2 !== 0; | ||
function twoDoubleQuote(segment, quote) { | ||
var regExp = _getRegExpObj(quote).double; | ||
function _escapeQuote(segment, quote,escape) { | ||
var key="es|"+quote+"|"+escape; | ||
if (cachedRegExp[key] === undefined){ | ||
if (escape ==="\\"){ | ||
escape="\\\\"; | ||
} | ||
cachedRegExp[key]=new RegExp(escape+quote,'g'); | ||
} | ||
var regExp = cachedRegExp[key]; | ||
return segment.replace(regExp, quote); | ||
} |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"keywords": [ | ||
@@ -24,0 +24,0 @@ "csv", |
@@ -163,2 +163,3 @@ [![Build Status](https://travis-ci.org/Keyang/node-csvtojson.svg?branch=master)](https://travis-ci.org/Keyang/node-csvtojson) | ||
* **eol**: End of line character. If omitted, parser will attempt retrieve it from first chunk of CSV data. If no valid eol found, then operation system eol will be used. | ||
* **escape**: escape character used in quoted column. Default is double quote (") according to RFC4108. Change to back slash (\) or other chars for your own case. | ||
@@ -165,0 +166,0 @@ All parameters can be used in Command Line tool. see |
@@ -243,2 +243,16 @@ var Converter = require("../libs/core/Converter.js"); | ||
}); | ||
it ("should process escape chars",function(done){ | ||
var test_converter = new Converter({ | ||
escape:"\\" | ||
}); | ||
var testData = __dirname + "/data/dataWithSlashEscape"; | ||
var rs = fs.createReadStream(testData); | ||
test_converter.on("end_parsed",function(res){ | ||
assert.equal(res[0].raw.hello,"world"); | ||
assert.equal(res[0].raw.test,true); | ||
done(); | ||
}); | ||
rs.pipe(test_converter); | ||
}); | ||
// it ("should convert big csv",function(done){ | ||
@@ -245,0 +259,0 @@ // // var rs=fs.createReadStream(__dirname+"/data/large-csv-sample.csv"); |
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
797889
12281
911