csv-stringify
Advanced tools
Comparing version 0.0.0 to 0.0.1
169
lib/index.js
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
var Stringifier, stream, util; | ||
@@ -8,3 +8,27 @@ | ||
/* | ||
`Stringifier([options])` | ||
----------------------- | ||
Options may include: | ||
* `delimiter` Set the field delimiter, one character only, defaults to `options.from.delimiter` which is a comma. | ||
* `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). | ||
* `quote` Defaults to the quote read option. | ||
* `quoted` Boolean, default to false, quote all the fields even if not required. | ||
* `escape` Defaults to the escape read option. | ||
* `columns` List of fields, applied when `transform` returns an object, order matters, read the transformer documentation for additionnal information. | ||
* `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). | ||
Todo: | ||
* `eof` Add a linebreak on the last line, default to false, expect a charactere or use '\n' if value is set to "true" | ||
*/ | ||
Stringifier = function(options) { | ||
var _base, _base1, _base2, _base3, _base4, _base5, _base6, _base7; | ||
if (options == null) { | ||
@@ -15,16 +39,45 @@ options = {}; | ||
this.options = options; | ||
this.options.delimiter = ','; | ||
this.options.quote = '"'; | ||
this.options.quoted = false; | ||
this.options.escape = '"'; | ||
this.options.columns = null; | ||
this.options.header = false; | ||
this.options.lineBreaks = null; | ||
this.options.flags = 'w'; | ||
this.options.encoding = 'utf8'; | ||
this.options.newColumns = false; | ||
this.options.end = true; | ||
this.options.eof = false; | ||
this.count = 0; | ||
this.countWriten = 0; | ||
if ((_base = this.options).delimiter == null) { | ||
_base.delimiter = ','; | ||
} | ||
if ((_base1 = this.options).quote == null) { | ||
_base1.quote = '"'; | ||
} | ||
if ((_base2 = this.options).quoted == null) { | ||
_base2.quoted = false; | ||
} | ||
if ((_base3 = this.options).escape == null) { | ||
_base3.escape = '"'; | ||
} | ||
if ((_base4 = this.options).columns == null) { | ||
_base4.columns = null; | ||
} | ||
if ((_base5 = this.options).header == null) { | ||
_base5.header = false; | ||
} | ||
if ((_base6 = this.options).lineBreaks == null) { | ||
_base6.lineBreaks = null; | ||
} | ||
if ((_base7 = this.options).rowDelimiter == null) { | ||
_base7.rowDelimiter = '\n'; | ||
} | ||
if (this.countWriten == null) { | ||
this.countWriten = 0; | ||
} | ||
switch (this.options.rowDelimiter) { | ||
case 'auto': | ||
this.options.rowDelimiter = null; | ||
break; | ||
case 'unix': | ||
this.options.rowDelimiter = "\n"; | ||
break; | ||
case 'mac': | ||
this.options.rowDelimiter = "\r"; | ||
break; | ||
case 'windows': | ||
this.options.rowDelimiter = "\r\n"; | ||
break; | ||
case 'unicode': | ||
this.options.rowDelimiter = "\u2028"; | ||
} | ||
return this; | ||
@@ -35,2 +88,27 @@ }; | ||
Stringifier.prototype.headers = function() { | ||
var k, label, labels; | ||
labels = this.options.columns; | ||
if (typeof labels === 'object') { | ||
labels = (function() { | ||
var _results; | ||
_results = []; | ||
for (k in labels) { | ||
label = labels[k]; | ||
_results.push(label); | ||
} | ||
return _results; | ||
})(); | ||
} | ||
labels = this.stringify(labels); | ||
return stream.Transform.prototype.write.call(this, labels); | ||
}; | ||
Stringifier.prototype.end = function(chunk, encoding, callback) { | ||
if (this.countWriten === 0) { | ||
this.headers(); | ||
} | ||
return stream.Transform.prototype.end.apply(this, arguments); | ||
}; | ||
Stringifier.prototype.write = function(chunk, encoding, callback) { | ||
@@ -44,3 +122,3 @@ var e, preserve; | ||
try { | ||
this.emit('record', chunk, this.count - 1); | ||
this.emit('record', chunk, this.countWriten); | ||
} catch (_error) { | ||
@@ -51,2 +129,5 @@ e = _error; | ||
chunk = this.stringify(chunk); | ||
if (this.options.header || this.countWriten) { | ||
chunk = this.options.rowDelimiter + chunk; | ||
} | ||
} | ||
@@ -56,2 +137,5 @@ if (typeof chunk === 'number') { | ||
} | ||
if (this.options.header && this.countWriten === 0) { | ||
this.headers(); | ||
} | ||
if (!preserve) { | ||
@@ -68,2 +152,3 @@ this.countWriten++; | ||
/* | ||
@@ -75,5 +160,4 @@ | ||
Convert a line to a string. Line may be an object, an array or a string. | ||
*/ | ||
*/ | ||
Stringifier.prototype.stringify = function(line) { | ||
@@ -109,3 +193,3 @@ var column, columns, containsLinebreak, containsQuote, containsdelimiter, delimiter, escape, field, i, newLine, quote, regexp, _i, _j, _line, _ref, _ref1; | ||
if (Array.isArray(line)) { | ||
newLine = this.countWriten ? this.options.rowDelimiter || "\n" : ''; | ||
newLine = ''; | ||
for (i = _j = 0, _ref1 = line.length; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) { | ||
@@ -125,3 +209,3 @@ field = line[i]; | ||
containsQuote = field.indexOf(quote) >= 0; | ||
containsLinebreak = field.indexOf("\r") >= 0 || field.indexOf("\n") >= 0; | ||
containsLinebreak = field.indexOf('\r') >= 0 || field.indexOf('\n') >= 0; | ||
if (containsQuote) { | ||
@@ -145,6 +229,49 @@ regexp = new RegExp(quote, 'g'); | ||
/* | ||
`stringify([options])` | ||
`stringify(data, [options], callback)` | ||
*/ | ||
module.exports = function() { | ||
return new Stringifier(); | ||
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.0", | ||
"version": "0.0.1", | ||
"name": "csv-stringify", | ||
@@ -9,7 +9,6 @@ "description": "CSV stringifier implementing the Node.js `stream.Transform` API", | ||
}, | ||
"dependencies": { | ||
"produce": "latest" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"coffee-script": "latest", | ||
"csv-generate": "latest", | ||
"mocha": "latest", | ||
@@ -19,3 +18,6 @@ "should": "latest" | ||
"optionalDependencies": {}, | ||
"main": "./lib" | ||
"main": "./lib", | ||
"scripts": { | ||
"test": "make test" | ||
} | ||
} |
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
33121
0
17
271
0
114
4
- Removedproduce@latest
- Removedproduce@0.4.1(transitive)