csv-stringify
Advanced tools
Comparing version 4.0.1 to 4.1.0
# Changelog | ||
## Version 4.1.0 | ||
* columns: support array with column definition objects | ||
* travis: support Node.js 10 | ||
* samples: new formatters script | ||
* samples: update syntax | ||
* package: improve ignore files | ||
## Version 4.0.1 | ||
@@ -5,0 +13,0 @@ |
@@ -10,3 +10,3 @@ 'use strict'; | ||
// information. | ||
var Stringifier, get, stream, util; | ||
var _Stringifier, get, stream, util; | ||
@@ -55,3 +55,3 @@ stream = require('stream'); | ||
} | ||
stringifier = new Stringifier(options); | ||
stringifier = new _Stringifier(options); | ||
if (data) { | ||
@@ -103,6 +103,6 @@ process.nextTick(function () { | ||
// Options are documented [here](http://csv.adaltas.com/stringify/). | ||
Stringifier = function Stringifier() { | ||
_Stringifier = function Stringifier() { | ||
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var base, base1, base10, base11, base12, base13, base14, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v; | ||
var base, base1, base10, base11, base12, base13, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v; | ||
// Immutable options | ||
@@ -138,11 +138,10 @@ options = {}; | ||
} | ||
if ((base7 = this.options).columns == null) { | ||
base7.columns = null; | ||
if ((base7 = this.options).header == null) { | ||
base7.header = false; | ||
} | ||
if ((base8 = this.options).header == null) { | ||
base8.header = false; | ||
// Normalize the columns option | ||
this.options.columns = _Stringifier.normalize_columns(this.options.columns); | ||
if ((base8 = this.options).formatters == null) { | ||
base8.formatters = {}; | ||
} | ||
if ((base9 = this.options).formatters == null) { | ||
base9.formatters = {}; | ||
} | ||
if (this.options.formatters.bool) { | ||
@@ -153,4 +152,4 @@ // Backward compatibility | ||
// Custom formatters | ||
if ((base10 = this.options.formatters).date == null) { | ||
base10.date = function (value) { | ||
if ((base9 = this.options.formatters).date == null) { | ||
base9.date = function (value) { | ||
// Cast date to timestamp string by default | ||
@@ -160,4 +159,4 @@ return '' + value.getTime(); | ||
} | ||
if ((base11 = this.options.formatters).boolean == null) { | ||
base11.boolean = function (value) { | ||
if ((base10 = this.options.formatters).boolean == null) { | ||
base10.boolean = function (value) { | ||
// Cast boolean to string by default | ||
@@ -171,4 +170,4 @@ if (value) { | ||
} | ||
if ((base12 = this.options.formatters).number == null) { | ||
base12.number = function (value) { | ||
if ((base11 = this.options.formatters).number == null) { | ||
base11.number = function (value) { | ||
// Cast number to string using native casting by default | ||
@@ -178,4 +177,4 @@ return '' + value; | ||
} | ||
if ((base13 = this.options.formatters).object == null) { | ||
base13.object = function (value) { | ||
if ((base12 = this.options.formatters).object == null) { | ||
base12.object = function (value) { | ||
// Stringify object as JSON by default | ||
@@ -185,4 +184,4 @@ return JSON.stringify(value); | ||
} | ||
if ((base14 = this.options).rowDelimiter == null) { | ||
base14.rowDelimiter = '\n'; | ||
if ((base13 = this.options).rowDelimiter == null) { | ||
base13.rowDelimiter = '\n'; | ||
} | ||
@@ -215,5 +214,5 @@ // Internal usage, state related | ||
util.inherits(Stringifier, stream.Transform); | ||
util.inherits(_Stringifier, stream.Transform); | ||
module.exports.Stringifier = Stringifier; | ||
module.exports.Stringifier = _Stringifier; | ||
@@ -223,4 +222,4 @@ // ## `Stringifier.prototype.headers` | ||
// Print the header line if the option "header" is "true". | ||
Stringifier.prototype.headers = function () { | ||
var k, label, labels; | ||
_Stringifier.prototype.headers = function () { | ||
var headers; | ||
if (!this.options.header) { | ||
@@ -232,24 +231,14 @@ return; | ||
} | ||
labels = this.options.columns; | ||
// If columns is an object, keys are fields and values are labels | ||
if ((typeof labels === 'undefined' ? 'undefined' : _typeof(labels)) === 'object') { | ||
labels = function () { | ||
var results; | ||
results = []; | ||
for (k in labels) { | ||
label = labels[k]; | ||
results.push(label); | ||
} | ||
return results; | ||
}(); | ||
} | ||
headers = this.options.columns.map(function (column) { | ||
return column.header; | ||
}); | ||
if (this.options.eof) { | ||
labels = this.stringify(labels) + this.options.rowDelimiter; | ||
headers = this.stringify(headers) + this.options.rowDelimiter; | ||
} else { | ||
labels = this.stringify(labels); | ||
headers = this.stringify(headers); | ||
} | ||
return stream.Transform.prototype.write.call(this, labels); | ||
return stream.Transform.prototype.write.call(this, headers); | ||
}; | ||
Stringifier.prototype.end = function (chunk, encoding, callback) { | ||
_Stringifier.prototype.end = function (chunk, encoding, callback) { | ||
if (this.countWriten === 0) { | ||
@@ -261,4 +250,5 @@ this.headers(); | ||
Stringifier.prototype.write = function (chunk, encoding, callback) { | ||
_Stringifier.prototype.write = function (chunk, encoding, callback) { | ||
var base, e, preserve; | ||
// Nothing to do if null or undefined | ||
if (chunk == null) { | ||
@@ -268,7 +258,8 @@ return; | ||
preserve = (typeof chunk === 'undefined' ? 'undefined' : _typeof(chunk)) !== 'object'; | ||
// Emit and stringify the record | ||
// Emit and stringify the record if an object or an array | ||
if (!preserve) { | ||
// Detect columns from the first record | ||
if (this.countWriten === 0 && !Array.isArray(chunk)) { | ||
if ((base = this.options).columns == null) { | ||
base.columns = Object.keys(chunk); | ||
base.columns = _Stringifier.normalize_columns(Object.keys(chunk)); | ||
} | ||
@@ -306,3 +297,3 @@ } | ||
// ## `Stringifier.prototype._transform(line)` | ||
Stringifier.prototype._transform = function (chunk, encoding, callback) { | ||
_Stringifier.prototype._transform = function (chunk, encoding, callback) { | ||
this.push(chunk); | ||
@@ -315,29 +306,25 @@ return callback(); | ||
// Convert a line to a string. Line may be an object, an array or a string. | ||
Stringifier.prototype.stringify = function (line) { | ||
var _line, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, escape, field, i, j, l, newLine, quote, ref, ref1, regexp, shouldQuote, type, value; | ||
if ((typeof line === 'undefined' ? 'undefined' : _typeof(line)) !== 'object') { | ||
return line; | ||
_Stringifier.prototype.stringify = function (record) { | ||
var _record, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, escape, field, i, j, l, newrecord, quote, ref, ref1, regexp, shouldQuote, type, value; | ||
if ((typeof record === 'undefined' ? 'undefined' : _typeof(record)) !== 'object') { | ||
return record; | ||
} | ||
columns = this.options.columns; | ||
if ((typeof columns === 'undefined' ? 'undefined' : _typeof(columns)) === 'object' && columns !== null && !Array.isArray(columns)) { | ||
columns = Object.keys(columns); | ||
} | ||
delimiter = this.options.delimiter; | ||
quote = this.options.quote; | ||
escape = this.options.escape; | ||
if (!Array.isArray(line)) { | ||
_line = []; | ||
if (!Array.isArray(record)) { | ||
_record = []; | ||
if (columns) { | ||
for (i = j = 0, ref = columns.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { | ||
column = columns[i]; | ||
value = get(line, column); | ||
_line[i] = typeof value === 'undefined' || value === null ? '' : value; | ||
value = get(record, columns[i].key); | ||
_record[i] = typeof value === 'undefined' || value === null ? '' : value; | ||
} | ||
} else { | ||
for (column in line) { | ||
_line.push(line[column]); | ||
for (column in record) { | ||
_record.push(record[column]); | ||
} | ||
} | ||
line = _line; | ||
_line = null; | ||
record = _record; | ||
_record = null; | ||
} else if (columns) { | ||
@@ -347,8 +334,8 @@ // Note, we used to have @options.columns | ||
// this case, we respect the columns indexes | ||
line.splice(columns.length); | ||
record.splice(columns.length); | ||
} | ||
if (Array.isArray(line)) { | ||
newLine = ''; | ||
for (i = l = 0, ref1 = line.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) { | ||
field = line[i]; | ||
if (Array.isArray(record)) { | ||
newrecord = ''; | ||
for (i = l = 0, ref1 = record.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) { | ||
field = record[i]; | ||
type = typeof field === 'undefined' ? 'undefined' : _typeof(field); | ||
@@ -376,3 +363,3 @@ if (type === 'string') { | ||
containsRowDelimiter = field.indexOf(this.options.rowDelimiter) >= 0; | ||
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || this.options.quoted || this.options.quotedString && typeof line[i] === 'string'; | ||
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || this.options.quoted || this.options.quotedString && typeof record[i] === 'string'; | ||
if (shouldQuote && containsEscape) { | ||
@@ -389,17 +376,69 @@ regexp = escape === '\\' ? new RegExp(escape + escape, 'g') : new RegExp(escape, 'g'); | ||
} | ||
newLine += field; | ||
} else if (this.options.quotedEmpty || this.options.quotedEmpty == null && line[i] === '' && this.options.quotedString) { | ||
newLine += quote + quote; | ||
newrecord += field; | ||
} else if (this.options.quotedEmpty || this.options.quotedEmpty == null && record[i] === '' && this.options.quotedString) { | ||
newrecord += quote + quote; | ||
} | ||
if (i !== line.length - 1) { | ||
newLine += delimiter; | ||
if (i !== record.length - 1) { | ||
newrecord += delimiter; | ||
} | ||
} | ||
line = newLine; | ||
record = newrecord; | ||
} | ||
return line; | ||
return record; | ||
}; | ||
_Stringifier.normalize_columns = function (columns) { | ||
var column, k, v; | ||
if (columns == null) { | ||
return null; | ||
} | ||
if (columns != null) { | ||
if ((typeof columns === 'undefined' ? 'undefined' : _typeof(columns)) !== 'object') { | ||
throw Error('Invalid option "columns": expect an array or an object'); | ||
} | ||
if (!Array.isArray(columns)) { | ||
columns = function () { | ||
var results; | ||
results = []; | ||
for (k in columns) { | ||
v = columns[k]; | ||
results.push({ | ||
key: k, | ||
header: v | ||
}); | ||
} | ||
return results; | ||
}(); | ||
} else { | ||
columns = function () { | ||
var j, len, results; | ||
results = []; | ||
for (j = 0, len = columns.length; j < len; j++) { | ||
column = columns[j]; | ||
if (typeof column === 'string') { | ||
results.push({ | ||
key: column, | ||
header: column | ||
}); | ||
} else if ((typeof column === 'undefined' ? 'undefined' : _typeof(column)) === 'object' && column != null && !Array.isArray(column)) { | ||
if (!column.key) { | ||
throw Error('Invalid column definition: property "key" is required'); | ||
} | ||
if (column.header == null) { | ||
column.header = column.key; | ||
} | ||
results.push(column); | ||
} else { | ||
throw Error('Invalid column definition: expect a string or an object'); | ||
} | ||
} | ||
return results; | ||
}(); | ||
} | ||
} | ||
return columns; | ||
}; | ||
// [readme]: https://github.com/wdavidw/node-csv-stringify | ||
// [samples]: https://github.com/wdavidw/node-csv-stringify/tree/master/samples | ||
// [tests]: https://github.com/wdavidw/node-csv-stringify/tree/master/test |
171
lib/index.js
@@ -98,3 +98,3 @@ // Generated by CoffeeScript 2.0.3 | ||
Stringifier = function(opts = {}) { | ||
var base, base1, base10, base11, base12, base13, base14, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v; | ||
var base, base1, base10, base11, base12, base13, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v; | ||
// Immutable options | ||
@@ -130,11 +130,10 @@ options = {}; | ||
} | ||
if ((base7 = this.options).columns == null) { | ||
base7.columns = null; | ||
if ((base7 = this.options).header == null) { | ||
base7.header = false; | ||
} | ||
if ((base8 = this.options).header == null) { | ||
base8.header = false; | ||
// Normalize the columns option | ||
this.options.columns = Stringifier.normalize_columns(this.options.columns); | ||
if ((base8 = this.options).formatters == null) { | ||
base8.formatters = {}; | ||
} | ||
if ((base9 = this.options).formatters == null) { | ||
base9.formatters = {}; | ||
} | ||
if (this.options.formatters.bool) { | ||
@@ -145,4 +144,4 @@ // Backward compatibility | ||
// Custom formatters | ||
if ((base10 = this.options.formatters).date == null) { | ||
base10.date = function(value) { | ||
if ((base9 = this.options.formatters).date == null) { | ||
base9.date = function(value) { | ||
// Cast date to timestamp string by default | ||
@@ -152,4 +151,4 @@ return '' + value.getTime(); | ||
} | ||
if ((base11 = this.options.formatters).boolean == null) { | ||
base11.boolean = function(value) { | ||
if ((base10 = this.options.formatters).boolean == null) { | ||
base10.boolean = function(value) { | ||
// Cast boolean to string by default | ||
@@ -163,4 +162,4 @@ if (value) { | ||
} | ||
if ((base12 = this.options.formatters).number == null) { | ||
base12.number = function(value) { | ||
if ((base11 = this.options.formatters).number == null) { | ||
base11.number = function(value) { | ||
// Cast number to string using native casting by default | ||
@@ -170,4 +169,4 @@ return '' + value; | ||
} | ||
if ((base13 = this.options.formatters).object == null) { | ||
base13.object = function(value) { | ||
if ((base12 = this.options.formatters).object == null) { | ||
base12.object = function(value) { | ||
// Stringify object as JSON by default | ||
@@ -177,4 +176,4 @@ return JSON.stringify(value); | ||
} | ||
if ((base14 = this.options).rowDelimiter == null) { | ||
base14.rowDelimiter = '\n'; | ||
if ((base13 = this.options).rowDelimiter == null) { | ||
base13.rowDelimiter = '\n'; | ||
} | ||
@@ -215,3 +214,3 @@ // Internal usage, state related | ||
Stringifier.prototype.headers = function() { | ||
var k, label, labels; | ||
var headers; | ||
if (!this.options.header) { | ||
@@ -223,21 +222,11 @@ return; | ||
} | ||
labels = this.options.columns; | ||
// If columns is an object, keys are fields and values are labels | ||
if (typeof labels === 'object') { | ||
labels = (function() { | ||
var results; | ||
results = []; | ||
for (k in labels) { | ||
label = labels[k]; | ||
results.push(label); | ||
} | ||
return results; | ||
})(); | ||
} | ||
headers = this.options.columns.map(function(column) { | ||
return column.header; | ||
}); | ||
if (this.options.eof) { | ||
labels = this.stringify(labels) + this.options.rowDelimiter; | ||
headers = this.stringify(headers) + this.options.rowDelimiter; | ||
} else { | ||
labels = this.stringify(labels); | ||
headers = this.stringify(headers); | ||
} | ||
return stream.Transform.prototype.write.call(this, labels); | ||
return stream.Transform.prototype.write.call(this, headers); | ||
}; | ||
@@ -254,2 +243,3 @@ | ||
var base, e, preserve; | ||
// Nothing to do if null or undefined | ||
if (chunk == null) { | ||
@@ -259,7 +249,8 @@ return; | ||
preserve = typeof chunk !== 'object'; | ||
// Emit and stringify the record | ||
// Emit and stringify the record if an object or an array | ||
if (!preserve) { | ||
// Detect columns from the first record | ||
if (this.countWriten === 0 && !Array.isArray(chunk)) { | ||
if ((base = this.options).columns == null) { | ||
base.columns = Object.keys(chunk); | ||
base.columns = Stringifier.normalize_columns(Object.keys(chunk)); | ||
} | ||
@@ -305,38 +296,34 @@ } | ||
// Convert a line to a string. Line may be an object, an array or a string. | ||
Stringifier.prototype.stringify = function(line) { | ||
var _line, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, escape, field, i, j, l, newLine, quote, ref, ref1, regexp, shouldQuote, type, value; | ||
if (typeof line !== 'object') { | ||
return line; | ||
Stringifier.prototype.stringify = function(record) { | ||
var _record, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, escape, field, i, j, l, newrecord, quote, ref, ref1, regexp, shouldQuote, type, value; | ||
if (typeof record !== 'object') { | ||
return record; | ||
} | ||
columns = this.options.columns; | ||
if (typeof columns === 'object' && columns !== null && !Array.isArray(columns)) { | ||
columns = Object.keys(columns); | ||
} | ||
delimiter = this.options.delimiter; | ||
quote = this.options.quote; | ||
escape = this.options.escape; | ||
if (!Array.isArray(line)) { | ||
_line = []; | ||
if (!Array.isArray(record)) { | ||
_record = []; | ||
if (columns) { | ||
for (i = j = 0, ref = columns.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { | ||
column = columns[i]; | ||
value = get(line, column); | ||
_line[i] = (typeof value === 'undefined' || value === null) ? '' : value; | ||
value = get(record, columns[i].key); | ||
_record[i] = (typeof value === 'undefined' || value === null) ? '' : value; | ||
} | ||
} else { | ||
for (column in line) { | ||
_line.push(line[column]); | ||
for (column in record) { | ||
_record.push(record[column]); | ||
} | ||
} | ||
line = _line; | ||
_line = null; | ||
record = _record; | ||
_record = null; | ||
} else if (columns) { // Note, we used to have @options.columns | ||
// We are getting an array but the user want specified output columns. In | ||
// this case, we respect the columns indexes | ||
line.splice(columns.length); | ||
record.splice(columns.length); | ||
} | ||
if (Array.isArray(line)) { | ||
newLine = ''; | ||
for (i = l = 0, ref1 = line.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) { | ||
field = line[i]; | ||
if (Array.isArray(record)) { | ||
newrecord = ''; | ||
for (i = l = 0, ref1 = record.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) { | ||
field = record[i]; | ||
type = typeof field; | ||
@@ -364,3 +351,3 @@ if (type === 'string') { | ||
containsRowDelimiter = field.indexOf(this.options.rowDelimiter) >= 0; | ||
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || this.options.quoted || (this.options.quotedString && typeof line[i] === 'string'); | ||
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || this.options.quoted || (this.options.quotedString && typeof record[i] === 'string'); | ||
if (shouldQuote && containsEscape) { | ||
@@ -377,17 +364,69 @@ regexp = escape === '\\' ? new RegExp(escape + escape, 'g') : new RegExp(escape, 'g'); | ||
} | ||
newLine += field; | ||
} else if (this.options.quotedEmpty || ((this.options.quotedEmpty == null) && line[i] === '' && this.options.quotedString)) { | ||
newLine += quote + quote; | ||
newrecord += field; | ||
} else if (this.options.quotedEmpty || ((this.options.quotedEmpty == null) && record[i] === '' && this.options.quotedString)) { | ||
newrecord += quote + quote; | ||
} | ||
if (i !== line.length - 1) { | ||
newLine += delimiter; | ||
if (i !== record.length - 1) { | ||
newrecord += delimiter; | ||
} | ||
} | ||
line = newLine; | ||
record = newrecord; | ||
} | ||
return line; | ||
return record; | ||
}; | ||
Stringifier.normalize_columns = function(columns) { | ||
var column, k, v; | ||
if (columns == null) { | ||
return null; | ||
} | ||
if (columns != null) { | ||
if (typeof columns !== 'object') { | ||
throw Error('Invalid option "columns": expect an array or an object'); | ||
} | ||
if (!Array.isArray(columns)) { | ||
columns = (function() { | ||
var results; | ||
results = []; | ||
for (k in columns) { | ||
v = columns[k]; | ||
results.push({ | ||
key: k, | ||
header: v | ||
}); | ||
} | ||
return results; | ||
})(); | ||
} else { | ||
columns = (function() { | ||
var j, len, results; | ||
results = []; | ||
for (j = 0, len = columns.length; j < len; j++) { | ||
column = columns[j]; | ||
if (typeof column === 'string') { | ||
results.push({ | ||
key: column, | ||
header: column | ||
}); | ||
} else if (typeof column === 'object' && (column != null) && !Array.isArray(column)) { | ||
if (!column.key) { | ||
throw Error('Invalid column definition: property "key" is required'); | ||
} | ||
if (column.header == null) { | ||
column.header = column.key; | ||
} | ||
results.push(column); | ||
} else { | ||
throw Error('Invalid column definition: expect a string or an object'); | ||
} | ||
} | ||
return results; | ||
})(); | ||
} | ||
} | ||
return columns; | ||
}; | ||
// [readme]: https://github.com/wdavidw/node-csv-stringify | ||
// [samples]: https://github.com/wdavidw/node-csv-stringify/tree/master/samples | ||
// [tests]: https://github.com/wdavidw/node-csv-stringify/tree/master/test |
{ | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"name": "csv-stringify", | ||
@@ -4,0 +4,0 @@ "description": "CSV stringifier implementing the Node.js `stream.Transform` API", |
@@ -1,3 +0,4 @@ | ||
[![Build Status](https://secure.travis-ci.org/adaltas/node-csv-stringify.svg)][travis] | ||
[![Build Status](https://api.travis-ci.org/adaltas/node-csv-stringify.svg)](https://travis-ci.org/#!/adaltas/node-csv-stringify) | ||
Part of the [CSV module][csv_home], this project is a stringifier converting | ||
@@ -12,15 +13,14 @@ arrays or objects input into a CSV text. It implements the Node.js | ||
## Features | ||
## Main features | ||
* Follow the Node.js streaming API | ||
* Simplicity with the optional callback API | ||
* Support for custom formatters, delimiters, quotes, escape characters and header | ||
* Support big datasets | ||
* Complete test coverage and samples for inspiration | ||
* no external dependencies | ||
* to be used conjointly with `csv-generate`, `csv-parse` and `stream-transform` | ||
* BSD License | ||
* Follow the Node.js streaming API | ||
* Simplicity with the optional callback API | ||
* Support for custom formatters, delimiters, quotes, escape characters and header | ||
* Support big datasets | ||
* Complete test coverage and samples for inspiration | ||
* Only 1 external dependency | ||
* to be used conjointly with `csv-generate`, `csv-parse` and `stream-transform` | ||
* BSD License | ||
Usage | ||
----- | ||
## Usage | ||
@@ -43,4 +43,3 @@ Refer to the [project webpage][home] for [an exhaustive list of options][home] | ||
Development | ||
----------- | ||
## Development | ||
@@ -54,7 +53,5 @@ Tests are executed with mocha. To install it, run `npm install` | ||
The test suite is run online with [Travis][travis] against the versions | ||
0.10, 0.11 and 0.12 of Node.js. | ||
The test suite is run online with [Travis](https://travis-ci.org/#!/adaltas/node-csv-stringify). See the [Travis definition file](https://github.com/adaltas/node-csv-stringify/blob/master/.travis.yml) to view the tested Node.js version. | ||
Contributors | ||
------------ | ||
## Contributors | ||
@@ -68,2 +65,1 @@ * David Worms: <https://github.com/wdavidw> | ||
[csv]: https://github.com/adaltas/node-csv | ||
[travis]: https://travis-ci.org/#!/adaltas/node-csv-stringify |
should = require('should'); | ||
stringify = require('../lib'); | ||
const stringify = require('../lib') | ||
const assert = require('assert') | ||
input = [ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]; | ||
input = [ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ] | ||
stringify(input, function(err, output){ | ||
output.should.eql('1,2,3,4\na,b,c,d\n'); | ||
}); | ||
assert.equal(output, '1,2,3,4\na,b,c,d\n') | ||
}) |
var stringify = require('../lib'); | ||
var generate = require('csv-generate'); | ||
const stringify = require('../lib') | ||
const generate = require('csv-generate') | ||
generator = generate({objectMode: true, seed: 1, headers: 2}); | ||
stringifier = stringify(); | ||
generator.pipe(stringifier).pipe(process.stdout); | ||
generator = generate({ | ||
objectMode: true, | ||
seed: 1, | ||
headers: 2 | ||
}) | ||
stringifier = stringify() | ||
generator.pipe(stringifier).pipe(process.stdout) |
require('should'); | ||
var stringify = require('../lib'); | ||
const stringify = require('../lib') | ||
const assert = require('assert') | ||
data = ''; | ||
stringifier = stringify({delimiter: ':'}) | ||
const data = [] | ||
const stringifier = stringify({ | ||
delimiter: ':' | ||
}) | ||
stringifier.write([ 'root','x','0','0','root','/root','/bin/bash' ]) | ||
stringifier.write([ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]) | ||
stringifier.end() | ||
stringifier.on('readable', function(){ | ||
let row; | ||
while(row = stringifier.read()){ | ||
data += row; | ||
data.push(row) | ||
} | ||
}); | ||
}) | ||
stringifier.on('error', function(err){ | ||
console.log(err.message); | ||
}); | ||
console.error(err.message) | ||
}) | ||
stringifier.on('finish', function(){ | ||
data.should.eql( | ||
assert.equal( | ||
data.join('\n'), | ||
"root:x:0:0:root:/root:/bin/bash\n" + | ||
"someone:x:1022:1022:a funny cat:/home/someone:/bin/bash\n" | ||
); | ||
}); | ||
stringifier.write([ 'root','x','0','0','root','/root','/bin/bash' ]); | ||
stringifier.write([ 'someone','x','1022','1022','a funny cat','/home/someone','/bin/bash' ]); | ||
stringifier.end(); | ||
"someone:x:1022:1022::/home/someone:/bin/bash\n" | ||
) | ||
}) |
@@ -6,13 +6,16 @@ | ||
var stringify = require('../lib'); | ||
var generate = require('csv-generate'); | ||
var generator = generate({objectMode: true, seed: 1, headers: 2}); | ||
var columns = { | ||
year: 'birthYear', | ||
phone: 'phone' | ||
}; | ||
var stringifier = stringify({ header: true, columns: columns }); | ||
generator.pipe(stringifier).pipe(process.stdout); | ||
const stringify = require('../lib') | ||
const generate = require('csv-generate') | ||
const generator = generate({ | ||
objectMode: true, | ||
seed: 1, | ||
headers: 2 | ||
}) | ||
const stringifier = stringify({ | ||
header: true, | ||
columns: { | ||
year: 'birthYear', | ||
phone: 'phone' | ||
} | ||
}) | ||
generator.pipe(stringifier).pipe(process.stdout) |
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
1009
39858
62