csv-stringify
Advanced tools
Comparing version 0.0.2 to 0.0.3
190
lib/index.js
@@ -8,24 +8,64 @@ // Generated by CoffeeScript 1.7.1 | ||
module.exports = function() { | ||
var callback, chunks, data, options, stringifier; | ||
if (arguments.length === 3) { | ||
data = arguments[0]; | ||
options = arguments[1]; | ||
callback = arguments[2]; | ||
} else if (arguments.length === 2) { | ||
if (Array.isArray(arguments[0])) { | ||
data = arguments[0]; | ||
} else { | ||
options = arguments[0]; | ||
} | ||
if (typeof arguments[1] === 'function') { | ||
callback = arguments[1]; | ||
} else { | ||
options = arguments[1]; | ||
} | ||
} else if (arguments.length === 1) { | ||
if (typeof arguments[0] === 'function') { | ||
callback = arguments[0]; | ||
} else if (Array.isArray(arguments[0])) { | ||
data = arguments[0]; | ||
} else { | ||
options = arguments[0]; | ||
} | ||
} | ||
if (options == null) { | ||
options = {}; | ||
} | ||
stringifier = new Stringifier(options); | ||
if (data) { | ||
process.nextTick(function() { | ||
var d, _i, _len; | ||
for (_i = 0, _len = data.length; _i < _len; _i++) { | ||
d = data[_i]; | ||
stringifier.write(d); | ||
} | ||
return stringifier.end(); | ||
}); | ||
} | ||
if (callback) { | ||
chunks = []; | ||
stringifier.on('readable', function() { | ||
var chunk, _results; | ||
_results = []; | ||
while (chunk = stringifier.read()) { | ||
_results.push(chunks.push(chunk)); | ||
} | ||
return _results; | ||
}); | ||
stringifier.on('error', function(err) { | ||
return callback(err); | ||
}); | ||
stringifier.on('finish', function() { | ||
return callback(null, chunks.join('')); | ||
}); | ||
} | ||
return stringifier; | ||
}; | ||
/* | ||
`Stringifier([options])` | ||
----------------------- | ||
Options may include: | ||
* `columns` List of fields, applied when `transform` returns an object, order matters, read the transformer documentation for additionnal information. | ||
* `delimiter` Set the field delimiter, one character only, defaults to `options.from.delimiter` which is a comma. | ||
* `eof` Add a linebreak on the last line, default to false, expect a charactere or use '\n' if value is set to "true" | ||
* `escape` Defaults to the escape read option. | ||
* `header` Display the column names on the first line if the columns option is provided. | ||
OR create objects with properties named by header titles (when using to.array) | ||
* `lineBreaks` String used to delimit record rows or a special value; special values are 'auto', 'unix', 'mac', 'windows', 'unicode'; defaults to 'auto' (discovered in source or 'unix' if no source is specified). | ||
* `quote` Defaults to the quote read option. | ||
* `quoted` Boolean, default to false, quote all the fields even if not required. | ||
* `rowDelimiter` String used to delimit record rows or a special value; special values are 'auto', 'unix', 'mac', 'windows', 'unicode'; defaults to 'auto' (discovered in source or 'unix' if no source is specified). | ||
*/ | ||
Stringifier = function(options) { | ||
var _base, _base1, _base2, _base3, _base4, _base5, _base6, _base7; | ||
var _base, _base1, _base2, _base3, _base4, _base5, _base6, _base7, _base8; | ||
if (options == null) { | ||
@@ -45,17 +85,20 @@ options = {}; | ||
} | ||
if ((_base3 = this.options).escape == null) { | ||
_base3.escape = '"'; | ||
if ((_base3 = this.options).eof == null) { | ||
_base3.eof = true; | ||
} | ||
if ((_base4 = this.options).columns == null) { | ||
_base4.columns = null; | ||
if ((_base4 = this.options).escape == null) { | ||
_base4.escape = '"'; | ||
} | ||
if ((_base5 = this.options).header == null) { | ||
_base5.header = false; | ||
if ((_base5 = this.options).columns == null) { | ||
_base5.columns = null; | ||
} | ||
if ((_base6 = this.options).lineBreaks == null) { | ||
_base6.lineBreaks = null; | ||
if ((_base6 = this.options).header == null) { | ||
_base6.header = false; | ||
} | ||
if ((_base7 = this.options).rowDelimiter == null) { | ||
_base7.rowDelimiter = '\n'; | ||
if ((_base7 = this.options).lineBreaks == null) { | ||
_base7.lineBreaks = null; | ||
} | ||
if ((_base8 = this.options).rowDelimiter == null) { | ||
_base8.rowDelimiter = '\n'; | ||
} | ||
if (this.countWriten == null) { | ||
@@ -85,4 +128,12 @@ this.countWriten = 0; | ||
module.exports.Stringifier = Stringifier; | ||
Stringifier.prototype.headers = function() { | ||
var k, label, labels; | ||
if (!this.options.header) { | ||
return; | ||
} | ||
if (!this.options.columns) { | ||
return; | ||
} | ||
labels = this.options.columns; | ||
@@ -100,3 +151,7 @@ if (typeof labels === 'object') { | ||
} | ||
labels = this.stringify(labels); | ||
if (this.options.eof) { | ||
labels = this.stringify(labels) + this.options.rowDelimiter; | ||
} else { | ||
labels = this.stringify(labels); | ||
} | ||
return stream.Transform.prototype.write.call(this, labels); | ||
@@ -109,5 +164,2 @@ }; | ||
} | ||
if (this.options.eof) { | ||
this.write(this.options.rowDelimiter); | ||
} | ||
return stream.Transform.prototype.end.apply(this, arguments); | ||
@@ -134,5 +186,9 @@ }; | ||
} | ||
chunk = this.stringify(chunk); | ||
if (this.options.header || this.countWriten) { | ||
chunk = this.options.rowDelimiter + chunk; | ||
if (this.options.eof) { | ||
chunk = this.stringify(chunk) + this.options.rowDelimiter; | ||
} else { | ||
chunk = this.stringify(chunk); | ||
if (this.options.header || this.countWriten) { | ||
chunk = this.options.rowDelimiter + chunk; | ||
} | ||
} | ||
@@ -143,3 +199,3 @@ } | ||
} | ||
if (this.options.header && this.countWriten === 0) { | ||
if (this.countWriten === 0) { | ||
this.headers(); | ||
@@ -158,11 +214,2 @@ } | ||
/* | ||
`Stringifier(line)` | ||
------------------- | ||
Convert a line to a string. Line may be an object, an array or a string. | ||
*/ | ||
Stringifier.prototype.stringify = function(line) { | ||
@@ -231,50 +278,1 @@ var column, columns, containsLinebreak, containsQuote, containsdelimiter, delimiter, escape, field, i, newLine, quote, regexp, _i, _j, _line, _ref, _ref1; | ||
}; | ||
/* | ||
`stringify([options])` | ||
`stringify(data, [options], callback)` | ||
*/ | ||
module.exports = function() { | ||
var callback, chunks, d, data, options, stringifier, _i, _len; | ||
if (arguments.length === 3) { | ||
data = arguments[0]; | ||
options = arguments[1]; | ||
callback = arguments[2]; | ||
} else if (arguments.length === 2) { | ||
data = arguments[0]; | ||
callback = arguments[1]; | ||
} else if (arguments.length === 1) { | ||
options = arguments[0]; | ||
} | ||
if (options == null) { | ||
options = {}; | ||
} | ||
stringifier = new Stringifier(options); | ||
if (data && callback) { | ||
chunks = []; | ||
for (_i = 0, _len = data.length; _i < _len; _i++) { | ||
d = data[_i]; | ||
stringifier.write(d); | ||
} | ||
stringifier.on('readable', function() { | ||
var chunk, _results; | ||
_results = []; | ||
while (chunk = stringifier.read()) { | ||
_results.push(chunks.push(chunk)); | ||
} | ||
return _results; | ||
}); | ||
stringifier.on('error', function(err) { | ||
return callback(err); | ||
}); | ||
stringifier.on('finish', function() { | ||
return callback(null, chunks.join('')); | ||
}); | ||
stringifier.end(); | ||
} | ||
return stringifier; | ||
}; | ||
module.exports.Stringifier = Stringifier; |
{ | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"name": "csv-stringify", | ||
"description": "CSV stringifier implementing the Node.js `stream.Transform` API", | ||
"keywords": [ "csv", "stringify", "stringifier" ], | ||
"licenses": [{ | ||
"type": "BSD", | ||
"url": "https://github.com/wdavidw/node-csv-stringify/blob/master/LICENSE" | ||
}], | ||
"repository": { | ||
@@ -6,0 +11,0 @@ "type": "git", |
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
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
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
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
40498
23
0
287