Comparing version 0.0.11 to 0.0.12
200
lib/csv.js
@@ -1,2 +0,1 @@ | ||
// Module CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed) | ||
@@ -52,2 +51,4 @@ | ||
escape: null, | ||
columns: null, | ||
header: false, | ||
lineBreaks: null, | ||
@@ -59,2 +60,8 @@ flags: 'w', | ||
}; | ||
// A boolean that is true by default, but turns false after an 'error' occurred, | ||
// the stream came to an 'end', or destroy() was called. | ||
this.readable = true; | ||
// A boolean that is true by default, but turns false after an 'error' occurred | ||
// or end() / destroy() was called. | ||
this.writable = true; | ||
} | ||
@@ -73,6 +80,6 @@ CSV.prototype.__proto__ = EventEmitter.prototype; | ||
} | ||
data.forEach(function(line){ | ||
state.line = line; | ||
for(var i=0; i<data.length; i++){ | ||
state.line = data[i]; | ||
flush(); | ||
}) | ||
} | ||
}else{ | ||
@@ -98,2 +105,3 @@ try{ | ||
self.emit('error', e); | ||
// Destroy the input stream | ||
this.destroy(); | ||
@@ -157,5 +165,7 @@ } | ||
csv.emit('end', state.count); | ||
csv.readable = false; | ||
} | ||
}else{ | ||
csv.emit('end', state.count); | ||
csv.readable = false; | ||
} | ||
@@ -186,3 +196,5 @@ } | ||
writeStream.on('close', function(){ | ||
self.emit('end',state.count); | ||
self.emit('end', state.count); | ||
self.readable = false; | ||
self.writable = false; | ||
}) | ||
@@ -219,80 +231,2 @@ this.writeStream = writeStream; | ||
/** | ||
* Write a line to the written stream. | ||
* Line may be an object, an array or a string | ||
* Preserve is for line which are not considered as CSV data | ||
*/ | ||
function write(line, preserve){ | ||
if(typeof line === 'undefined' || line === null){ | ||
return; | ||
} | ||
if(!preserve){ | ||
csv.emit('data', line, state.count); | ||
} | ||
if(typeof line === 'object'){ | ||
if(!(line instanceof Array)){ | ||
var columns = csv.writeOptions.columns || csv.readOptions.columns; | ||
var _line = []; | ||
if(columns){ | ||
columns.forEach(function(column, i){ | ||
_line[i] = (typeof line[column] === 'undefined' || line[column] === null) ? '' : line[column]; | ||
}) | ||
}else{ | ||
for(var column in line){ | ||
_line.push(line[column]); | ||
} | ||
} | ||
line = _line; | ||
_line = null; | ||
} | ||
if(line instanceof Array){ | ||
var newLine = state.countWriten ? csv.writeOptions.lineBreaks || "\r" : ''; | ||
line.forEach(function(field,i){ | ||
if(typeof field === 'string'){ | ||
// fine 99% of the cases, keep going | ||
}else if(typeof field === 'number'){ | ||
// Cast number to string | ||
field = '' + field; | ||
}else if(typeof field === 'boolean'){ | ||
// Cast number to string | ||
field = field ? '1' : ''; | ||
}else if(field instanceof Date){ | ||
// Cast date to timestamp string | ||
field = '' + field.getTime(); | ||
} | ||
if(field){ | ||
var containsdelimiter = field.indexOf(csv.writeOptions.delimiter || csv.readOptions.delimiter) >= 0; | ||
var containsQuote = field.indexOf(csv.writeOptions.quote || csv.readOptions.quote) >= 0; | ||
var containsLinebreak = field.indexOf("\r") >= 0 || field.indexOf("\n") >= 0; | ||
if(containsQuote){ | ||
field = field.replace( | ||
new RegExp(csv.writeOptions.quote || csv.readOptions.quote,'g') | ||
, (csv.writeOptions.escape || csv.readOptions.escape) | ||
+ (csv.writeOptions.quote || csv.readOptions.quote)); | ||
} | ||
if(containsQuote || containsdelimiter || containsLinebreak){ | ||
field = (csv.writeOptions.quote || csv.readOptions.quote) + field + (csv.writeOptions.quote || csv.readOptions.quote); | ||
} | ||
newLine += field; | ||
} | ||
if(i!==line.length-1){ | ||
newLine += csv.writeOptions.delimiter || csv.readOptions.delimiter; | ||
} | ||
}); | ||
line = newLine; | ||
} | ||
} | ||
if(state.buffer){ | ||
if(state.bufferPosition + Buffer.byteLength(line,'utf8') > csv.readOptions.bufferSize){ | ||
csv.writeStream.write(state.buffer.slice(0, state.bufferPosition)); | ||
state.buffer = new Buffer(csv.readOptions.bufferSize); | ||
state.bufferPosition = 0; | ||
} | ||
state.bufferPosition += state.buffer.write(line, state.bufferPosition,'utf8'); | ||
} | ||
if(!preserve){ | ||
state.countWriten++; | ||
} | ||
} | ||
/** | ||
* Parse a string which may hold multiple lines. | ||
@@ -393,3 +327,7 @@ * Private state object is enriched on each character until | ||
// Called by the `parse` function on each line. It will then call `write` | ||
function flush(){ | ||
if(state.count === 0 && csv.writeOptions.header === true){ | ||
write(csv.writeOptions.columns || csv.readOptions.columns); | ||
} | ||
if(csv.readOptions.columns){ | ||
@@ -403,5 +341,6 @@ if(state.count === 0 && csv.readOptions.columns === true){ | ||
var line = {}; | ||
csv.readOptions.columns.forEach(function(column, i){ | ||
for(var i=0; i<csv.readOptions.columns.length; i++){ | ||
var column = csv.readOptions.columns[i]; | ||
line[column] = state.line[i]||null; | ||
}) | ||
} | ||
state.line = line; | ||
@@ -424,3 +363,94 @@ line = null; | ||
/** | ||
* Write a line to the written stream. | ||
* Line may be an object, an array or a string | ||
* Preserve is for line which are not considered as CSV data | ||
*/ | ||
function write(line, preserve){ | ||
if(typeof line === 'undefined' || line === null){ | ||
return; | ||
} | ||
if(!preserve){ | ||
try { | ||
csv.emit('data', line, state.count); | ||
}catch(e){ | ||
csv.emit('error', e); | ||
csv.readable = false; | ||
csv.writable = false; | ||
} | ||
} | ||
if(typeof line === 'object'){ | ||
if(!(line instanceof Array)){ | ||
var columns = csv.writeOptions.columns || csv.readOptions.columns; | ||
var _line = []; | ||
if(columns){ | ||
for(var i=0; i<columns.length; i++){ | ||
var column = columns[i]; | ||
_line[i] = (typeof line[column] === 'undefined' || line[column] === null) ? '' : line[column]; | ||
} | ||
}else{ | ||
for(var column in line){ | ||
_line.push(line[column]); | ||
} | ||
} | ||
line = _line; | ||
_line = null; | ||
}else if(csv.writeOptions.columns){ | ||
// We are getting an array but the user want specified output columns. In | ||
// this case, we respect the columns indexes | ||
line.splice(csv.writeOptions.columns.length); | ||
} | ||
if(line instanceof Array){ | ||
var newLine = state.countWriten ? csv.writeOptions.lineBreaks || "\r" : ''; | ||
for(var i=0; i<line.length; i++){ | ||
var field = line[i]; | ||
if(typeof field === 'string'){ | ||
// fine 99% of the cases, keep going | ||
}else if(typeof field === 'number'){ | ||
// Cast number to string | ||
field = '' + field; | ||
}else if(typeof field === 'boolean'){ | ||
// Cast boolean to string | ||
field = field ? '1' : ''; | ||
}else if(field instanceof Date){ | ||
// Cast date to timestamp string | ||
field = '' + field.getTime(); | ||
} | ||
if(field){ | ||
var containsdelimiter = field.indexOf(csv.writeOptions.delimiter || csv.readOptions.delimiter) >= 0; | ||
var containsQuote = field.indexOf(csv.writeOptions.quote || csv.readOptions.quote) >= 0; | ||
var containsLinebreak = field.indexOf("\r") >= 0 || field.indexOf("\n") >= 0; | ||
if(containsQuote){ | ||
field = field.replace( | ||
new RegExp(csv.writeOptions.quote || csv.readOptions.quote,'g') | ||
, (csv.writeOptions.escape || csv.readOptions.escape) | ||
+ (csv.writeOptions.quote || csv.readOptions.quote)); | ||
} | ||
if(containsQuote || containsdelimiter || containsLinebreak){ | ||
field = (csv.writeOptions.quote || csv.readOptions.quote) + field + (csv.writeOptions.quote || csv.readOptions.quote); | ||
} | ||
newLine += field; | ||
} | ||
if(i!==line.length-1){ | ||
newLine += csv.writeOptions.delimiter || csv.readOptions.delimiter; | ||
} | ||
} | ||
line = newLine; | ||
} | ||
} | ||
if(state.buffer){ | ||
if(state.bufferPosition + Buffer.byteLength(line,'utf8') > csv.readOptions.bufferSize){ | ||
csv.writeStream.write(state.buffer.slice(0, state.bufferPosition)); | ||
state.buffer = new Buffer(csv.readOptions.bufferSize); | ||
state.bufferPosition = 0; | ||
} | ||
state.bufferPosition += state.buffer.write(line, state.bufferPosition,'utf8'); | ||
} | ||
if(!preserve){ | ||
state.countWriten++; | ||
} | ||
return true; | ||
} | ||
return csv; | ||
}; |
{ | ||
"name": "csv", | ||
"version": "0.0.11", | ||
"description": "CSV parser with simple api, full of options and tested against large datasets.", | ||
"author": "David Worms <david@adaltas.com>", | ||
"contributors": [ | ||
{ "name": "David Worms", "email": "david@adaltas.com" }, | ||
{ "name": "Will White", "email": "https://github.com/willwhite" }, | ||
{ "name": "Justin Latimer", "email": "https://github.com/justinlatimer" } | ||
], | ||
"engines": { "node": ">= 0.1.90" }, | ||
"keywords": ["parser", "csv"], | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/wdavidw/node-csv-parser.git" | ||
} | ||
"name": "csv", | ||
"version": "0.0.12", | ||
"description": "CSV parser with simple api, full of options and tested against large datasets.", | ||
"author": "David Worms <david@adaltas.com>", | ||
"contributors": [ | ||
"David Worms <david@adaltas.com>", | ||
"Will White <https://github.com/willwhite>", | ||
"Justin Latimer <https://github.com/justinlatimer>" | ||
], | ||
"engines": { | ||
"node": ">= 0.1.90" | ||
}, | ||
"keywords": [ | ||
"parser", | ||
"csv" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/wdavidw/node-csv-parser.git" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "latest", | ||
"mocha": "latest", | ||
"should": "latest" | ||
}, | ||
"dependencies": {}, | ||
"optionalDependencies": {}, | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"homepage": "https://github.com/wdavidw/node-csv-parser" | ||
} |
@@ -98,2 +98,5 @@ <pre> | ||
- *encoding* | ||
Default to 'utf8', apply when a readable stream is created. | ||
- *trim* | ||
@@ -142,2 +145,5 @@ If true, ignore whitespace immediately around the delimiter, default to false. | ||
- *header* | ||
Display the column names on the first line if the columns option is provided. | ||
- *lineBreaks* | ||
@@ -199,3 +205,3 @@ String used to delimit record rows or a special value; special values are 'auto', 'unix', 'mac', 'windows', 'unicode'; default to 'auto' (discovered in source). | ||
Columns names may be provided or discovered in the first line with the read options `columns`. If defined as an array, the order must match the input source. If set to `true`, the fields are expected to be present in the first line of the input source. | ||
Columns names may be provided or discovered in the first line with the read options `columns`. If defined as an array, the order must match the one of the input source. If set to `true`, the fields are expected to be present in the first line of the input source. | ||
@@ -202,0 +208,0 @@ You can define a different order and even different columns in the read options and in the write options. If the `columns` is not defined in the write options, it will default to the one present in the read options. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
92362
98
1
252
1
0
3
547