Socket
Socket
Sign inDemoInstall

csv-stringify

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csv-stringify - npm Package Compare versions

Comparing version 4.3.1 to 5.0.0

samples/option.formatters.js

25

CHANGELOG.md
# Changelog
## Version 5.0.0
Breaking changes:
* cast: was formatters
* record_delimiter: was row_delimiter
* options: instance options stored in underscore form
* nodejs: drop support for version 7, use './lib/es5'
New features:
* quoted_match: new option
* options: accept underscore and camelcase forms
Minor enhancements:
* stream: pass all options to the transform stream
* stream: use writableObjectMode
Project Management:
* package: update license to MIT
* travis: test agains Node.js 11
* samples: improve some scripts
## Version 4.3.1

@@ -5,0 +30,0 @@

204

lib/es5/index.js

@@ -5,2 +5,6 @@ "use strict";

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
// Generated by CoffeeScript 2.3.2

@@ -10,3 +14,3 @@ // # CSV Stringifier

// information.
var _Stringifier, get, stream, util;
var _Stringifier, get, stream, underscore, util;

@@ -106,63 +110,83 @@ stream = require('stream');

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; // Immutable options
var base, base1, base2, base3, base4, isRegExp, isString, j, k, len, options, quoted_match, ref, v;
stream.Transform.call(this, _objectSpread({}, options, {
writableObjectMode: true
}));
options = {};
for (k in opts) {
v = opts[k];
options[k] = v;
}
v = opts[k]; // Immutable options and camelcase conversion
options.objectMode = true;
stream.Transform.call(this, options); //# Default options
options[underscore(k)] = v;
} //# Default options
this.options = options;
if ((base = this.options).delimiter == null) {
base.delimiter = ',';
if (options.delimiter == null) {
options.delimiter = ',';
}
if ((base1 = this.options).quote == null) {
base1.quote = '"';
if (options.quote == null) {
options.quote = '"';
}
if ((base2 = this.options).quoted == null) {
base2.quoted = false;
if (options.quoted == null) {
options.quoted = false;
}
if ((base3 = this.options).quotedEmpty == null) {
base3.quotedEmpty = void 0;
if (options.quoted_empty == null) {
options.quoted_empty = void 0;
}
if ((base4 = this.options).quotedString == null) {
base4.quotedString = false;
if (options.quoted_string == null) {
options.quoted_string = false;
}
if ((base5 = this.options).eof == null) {
base5.eof = true;
if (options.eof == null) {
options.eof = true;
}
if ((base6 = this.options).escape == null) {
base6.escape = '"';
if (options.escape == null) {
options.escape = '"';
}
if ((base7 = this.options).header == null) {
base7.header = false;
if (options.header == null) {
options.header = false;
} // Normalize the columns option
this.options.columns = _Stringifier.normalize_columns(this.options.columns);
options.columns = _Stringifier.normalize_columns(options.columns);
if ((base8 = this.options).formatters == null) {
base8.formatters = {};
if (options.cast == null) {
options.cast = {};
} // Normalize option `quoted_match`
if (options.quoted_match === void 0 || options.quoted_match === null || options.quoted_match === false) {
options.quoted_match = null;
} else if (!Array.isArray(options.quoted_match)) {
options.quoted_match = [options.quoted_match];
}
if (this.options.formatters.bool) {
if (options.quoted_match) {
ref = options.quoted_match;
for (j = 0, len = ref.length; j < len; j++) {
quoted_match = ref[j];
isString = typeof quoted_match === 'string';
isRegExp = quoted_match instanceof RegExp;
if (!isString && !isRegExp) {
throw Error("Invalid Option: quoted_match must be a string or a regex, got ".concat(JSON.stringify(quoted_match)));
}
}
}
if (options.cast.bool) {
// Backward compatibility
this.options.formatters.boolean = this.options.formatters.bool;
} // Custom formatters
options.cast.boolean = options.cast.bool;
} // Custom cast
if ((base9 = this.options.formatters).string == null) {
base9.string = function (value) {
if ((base = options.cast).string == null) {
base.string = function (value) {
return value;

@@ -172,4 +196,4 @@ };

if ((base10 = this.options.formatters).date == null) {
base10.date = function (value) {
if ((base1 = options.cast).date == null) {
base1.date = function (value) {
// Cast date to timestamp string by default

@@ -180,4 +204,4 @@ return '' + value.getTime();

if ((base11 = this.options.formatters).boolean == null) {
base11.boolean = function (value) {
if ((base2 = options.cast).boolean == null) {
base2.boolean = function (value) {
// Cast boolean to string by default

@@ -192,4 +216,4 @@ if (value) {

if ((base12 = this.options.formatters).number == null) {
base12.number = function (value) {
if ((base3 = options.cast).number == null) {
base3.number = function (value) {
// Cast number to string using native casting by default

@@ -200,4 +224,4 @@ return '' + value;

if ((base13 = this.options.formatters).object == null) {
base13.object = function (value) {
if ((base4 = options.cast).object == null) {
base4.object = function (value) {
// Stringify object as JSON by default

@@ -208,36 +232,44 @@ return JSON.stringify(value);

if ((base14 = this.options).rowDelimiter == null) {
base14.rowDelimiter = '\n';
} // Internal usage, state related
if (options.record_delimiter === void 0 || options.record_delimiter === null || options.record_delimiter === false) {
if (options.record_delimiter == null) {
options.record_delimiter = '\n';
}
} else if (typeof options.record_delimiter === 'string') {
switch (options.record_delimiter) {
case 'auto':
options.record_delimiter = null;
break;
case 'unix':
options.record_delimiter = "\n";
break;
if (this.countWriten == null) {
this.countWriten = 0;
}
case 'mac':
options.record_delimiter = "\r";
break;
switch (this.options.rowDelimiter) {
case 'auto':
this.options.rowDelimiter = null;
break;
case 'windows':
options.record_delimiter = "\r\n";
break;
case 'unix':
this.options.rowDelimiter = "\n";
break;
case 'ascii':
options.record_delimiter = "\x1E";
break;
case 'mac':
this.options.rowDelimiter = "\r";
break;
case 'unicode':
options.record_delimiter = "\u2028";
}
} else if (Buffer.isBuffer(options.record_delimiter)) {
options.record_delimiter = options.record_delimiter.toString();
} else {
throw Error("Invalid Option: record_delimiter must be a string or a buffer, got ".concat(JSON.stringify(options.record_delimiter)));
} // Internal usage, state related
case 'windows':
this.options.rowDelimiter = "\r\n";
break;
case 'ascii':
this.options.rowDelimiter = "\x1E";
break;
if (this.countWriten == null) {
this.countWriten = 0;
} // Expose options
case 'unicode':
this.options.rowDelimiter = "\u2028";
}
this.options = options;
return this;

@@ -282,3 +314,3 @@ };

chunk = chunk + this.options.rowDelimiter;
chunk = chunk + this.options.record_delimiter;
} else {

@@ -292,3 +324,3 @@ chunk = this.stringify(chunk);

if (this.options.header || this.countWriten) {
chunk = this.options.rowDelimiter + chunk;
chunk = this.options.record_delimiter + chunk;
}

@@ -328,3 +360,3 @@ }

_Stringifier.prototype.stringify = function (record) {
var _record, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, err, escape, field, i, j, l, newrecord, quote, ref, ref1, regexp, shouldQuote, type, value;
var _record, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, err, escape, field, i, j, l, newrecord, quote, quoted, quotedMatch, quotedString, ref, ref1, regexp, shouldQuote, type, value;

@@ -373,11 +405,11 @@ if (_typeof(record) !== 'object') {

// fine 99% of the cases
field = this.options.formatters.string(field);
field = this.options.cast.string(field);
} else if (type === 'number') {
field = this.options.formatters.number(field);
field = this.options.cast.number(field);
} else if (type === 'boolean') {
field = this.options.formatters.boolean(field);
field = this.options.cast.boolean(field);
} else if (field instanceof Date) {
field = this.options.formatters.date(field);
field = this.options.cast.date(field);
} else if (type === 'object' && field !== null) {
field = this.options.formatters.object(field);
field = this.options.cast.object(field);
}

@@ -399,4 +431,14 @@ } catch (error) {

containsEscape = field.indexOf(escape) >= 0 && escape !== quote;
containsRowDelimiter = field.indexOf(this.options.rowDelimiter) >= 0;
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || this.options.quoted || this.options.quotedString && typeof record[i] === 'string';
containsRowDelimiter = field.indexOf(this.options.record_delimiter) >= 0;
quoted = this.options.quoted;
quotedString = this.options.quoted_string && typeof record[i] === 'string';
quotedMatch = this.options.quoted_match && typeof record[i] === 'string' && this.options.quoted_match.filter(function (quoted_match) {
if (typeof quoted_match === 'string') {
return record[i].indexOf(quoted_match) !== -1;
} else {
return quoted_match.test(record[i]);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || quoted || quotedString || quotedMatch;

@@ -418,3 +460,3 @@ if (shouldQuote && containsEscape) {

newrecord += field;
} else if (this.options.quotedEmpty || this.options.quotedEmpty == null && record[i] === '' && this.options.quotedString) {
} else if (this.options.quoted_empty || this.options.quoted_empty == null && record[i] === '' && this.options.quoted_string) {
newrecord += quote + quote;

@@ -452,3 +494,3 @@ }

if (this.options.eof) {
headers = this.stringify(headers) + this.options.rowDelimiter;
headers = this.stringify(headers) + this.options.record_delimiter;
} else {

@@ -524,2 +566,8 @@ headers = this.stringify(headers);

return columns;
};
underscore = function underscore(str) {
return str.replace(/([A-Z])/g, function (_, match, index) {
return '_' + match.toLowerCase();
});
};

@@ -6,3 +6,3 @@ // Generated by CoffeeScript 2.3.2

// information.
var Stringifier, get, stream, util;
var Stringifier, get, stream, underscore, util;

@@ -99,54 +99,71 @@ stream = require('stream');

Stringifier = function(opts = {}) {
var base, base1, base10, base11, base12, base13, base14, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v;
// Immutable options
var base, base1, base2, base3, base4, isRegExp, isString, j, k, len, options, quoted_match, ref, v;
stream.Transform.call(this, {...options, ...{
writableObjectMode: true
}});
options = {};
for (k in opts) {
v = opts[k];
options[k] = v;
// Immutable options and camelcase conversion
options[underscore(k)] = v;
}
options.objectMode = true;
stream.Transform.call(this, options);
//# Default options
this.options = options;
if ((base = this.options).delimiter == null) {
base.delimiter = ',';
if (options.delimiter == null) {
options.delimiter = ',';
}
if ((base1 = this.options).quote == null) {
base1.quote = '"';
if (options.quote == null) {
options.quote = '"';
}
if ((base2 = this.options).quoted == null) {
base2.quoted = false;
if (options.quoted == null) {
options.quoted = false;
}
if ((base3 = this.options).quotedEmpty == null) {
base3.quotedEmpty = void 0;
if (options.quoted_empty == null) {
options.quoted_empty = void 0;
}
if ((base4 = this.options).quotedString == null) {
base4.quotedString = false;
if (options.quoted_string == null) {
options.quoted_string = false;
}
if ((base5 = this.options).eof == null) {
base5.eof = true;
if (options.eof == null) {
options.eof = true;
}
if ((base6 = this.options).escape == null) {
base6.escape = '"';
if (options.escape == null) {
options.escape = '"';
}
if ((base7 = this.options).header == null) {
base7.header = false;
if (options.header == null) {
options.header = false;
}
// Normalize the columns option
this.options.columns = Stringifier.normalize_columns(this.options.columns);
if ((base8 = this.options).formatters == null) {
base8.formatters = {};
options.columns = Stringifier.normalize_columns(options.columns);
if (options.cast == null) {
options.cast = {};
}
if (this.options.formatters.bool) {
// Normalize option `quoted_match`
if (options.quoted_match === void 0 || options.quoted_match === null || options.quoted_match === false) {
options.quoted_match = null;
} else if (!Array.isArray(options.quoted_match)) {
options.quoted_match = [options.quoted_match];
}
if (options.quoted_match) {
ref = options.quoted_match;
for (j = 0, len = ref.length; j < len; j++) {
quoted_match = ref[j];
isString = typeof quoted_match === 'string';
isRegExp = quoted_match instanceof RegExp;
if (!isString && !isRegExp) {
throw Error(`Invalid Option: quoted_match must be a string or a regex, got ${JSON.stringify(quoted_match)}`);
}
}
}
if (options.cast.bool) {
// Backward compatibility
this.options.formatters.boolean = this.options.formatters.bool;
options.cast.boolean = options.cast.bool;
}
// Custom formatters
if ((base9 = this.options.formatters).string == null) {
base9.string = function(value) {
// Custom cast
if ((base = options.cast).string == null) {
base.string = function(value) {
return value;
};
}
if ((base10 = this.options.formatters).date == null) {
base10.date = function(value) {
if ((base1 = options.cast).date == null) {
base1.date = function(value) {
// Cast date to timestamp string by default

@@ -156,4 +173,4 @@ return '' + value.getTime();

}
if ((base11 = this.options.formatters).boolean == null) {
base11.boolean = function(value) {
if ((base2 = options.cast).boolean == null) {
base2.boolean = function(value) {
// Cast boolean to string by default

@@ -167,4 +184,4 @@ if (value) {

}
if ((base12 = this.options.formatters).number == null) {
base12.number = function(value) {
if ((base3 = options.cast).number == null) {
base3.number = function(value) {
// Cast number to string using native casting by default

@@ -174,4 +191,4 @@ return '' + value;

}
if ((base13 = this.options.formatters).object == null) {
base13.object = function(value) {
if ((base4 = options.cast).object == null) {
base4.object = function(value) {
// Stringify object as JSON by default

@@ -181,4 +198,30 @@ return JSON.stringify(value);

}
if ((base14 = this.options).rowDelimiter == null) {
base14.rowDelimiter = '\n';
if (options.record_delimiter === void 0 || options.record_delimiter === null || options.record_delimiter === false) {
if (options.record_delimiter == null) {
options.record_delimiter = '\n';
}
} else if (typeof options.record_delimiter === 'string') {
switch (options.record_delimiter) {
case 'auto':
options.record_delimiter = null;
break;
case 'unix':
options.record_delimiter = "\n";
break;
case 'mac':
options.record_delimiter = "\r";
break;
case 'windows':
options.record_delimiter = "\r\n";
break;
case 'ascii':
options.record_delimiter = "\u001e";
break;
case 'unicode':
options.record_delimiter = "\u2028";
}
} else if (Buffer.isBuffer(options.record_delimiter)) {
options.record_delimiter = options.record_delimiter.toString();
} else {
throw Error(`Invalid Option: record_delimiter must be a string or a buffer, got ${JSON.stringify(options.record_delimiter)}`);
}

@@ -189,21 +232,4 @@ // Internal usage, state related

}
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 'ascii':
this.options.rowDelimiter = "\u001e";
break;
case 'unicode':
this.options.rowDelimiter = "\u2028";
}
// Expose options
this.options = options;
return this;

@@ -246,3 +272,3 @@ };

}
chunk = chunk + this.options.rowDelimiter;
chunk = chunk + this.options.record_delimiter;
} else {

@@ -254,3 +280,3 @@ chunk = this.stringify(chunk);

if (this.options.header || this.countWriten) {
chunk = this.options.rowDelimiter + chunk;
chunk = this.options.record_delimiter + chunk;
}

@@ -287,3 +313,3 @@ }

Stringifier.prototype.stringify = function(record) {
var _record, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, err, escape, field, i, j, l, newrecord, quote, ref, ref1, regexp, shouldQuote, type, value;
var _record, column, columns, containsEscape, containsQuote, containsRowDelimiter, containsdelimiter, delimiter, err, escape, field, i, j, l, newrecord, quote, quoted, quotedMatch, quotedString, ref, ref1, regexp, shouldQuote, type, value;
if (typeof record !== 'object') {

@@ -323,11 +349,11 @@ return record;

// fine 99% of the cases
field = this.options.formatters.string(field);
field = this.options.cast.string(field);
} else if (type === 'number') {
field = this.options.formatters.number(field);
field = this.options.cast.number(field);
} else if (type === 'boolean') {
field = this.options.formatters.boolean(field);
field = this.options.cast.boolean(field);
} else if (field instanceof Date) {
field = this.options.formatters.date(field);
field = this.options.cast.date(field);
} else if (type === 'object' && field !== null) {
field = this.options.formatters.object(field);
field = this.options.cast.object(field);
}

@@ -347,4 +373,14 @@ } catch (error) {

containsEscape = field.indexOf(escape) >= 0 && (escape !== quote);
containsRowDelimiter = field.indexOf(this.options.rowDelimiter) >= 0;
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || this.options.quoted || (this.options.quotedString && typeof record[i] === 'string');
containsRowDelimiter = field.indexOf(this.options.record_delimiter) >= 0;
quoted = this.options.quoted;
quotedString = this.options.quoted_string && typeof record[i] === 'string';
quotedMatch = this.options.quoted_match && typeof record[i] === 'string' && this.options.quoted_match.filter(function(quoted_match) {
if (typeof quoted_match === 'string') {
return record[i].indexOf(quoted_match) !== -1;
} else {
return quoted_match.test(record[i]);
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
shouldQuote = containsQuote || containsdelimiter || containsRowDelimiter || quoted || quotedString || quotedMatch;
if (shouldQuote && containsEscape) {

@@ -362,3 +398,3 @@ regexp = escape === '\\' ? new RegExp(escape + escape, 'g') : new RegExp(escape, 'g');

newrecord += field;
} else if (this.options.quotedEmpty || ((this.options.quotedEmpty == null) && record[i] === '' && this.options.quotedString)) {
} else if (this.options.quoted_empty || ((this.options.quoted_empty == null) && record[i] === '' && this.options.quoted_string)) {
newrecord += quote + quote;

@@ -390,3 +426,3 @@ }

if (this.options.eof) {
headers = this.stringify(headers) + this.options.rowDelimiter;
headers = this.stringify(headers) + this.options.record_delimiter;
} else {

@@ -452,1 +488,7 @@ headers = this.stringify(headers);

};
underscore = function(str) {
return str.replace(/([A-Z])/g, function(_, match, index) {
return '_' + match.toLowerCase();
});
};
{
"version": "4.3.1",
"version": "5.0.0",
"name": "csv-stringify",

@@ -4,0 +4,0 @@ "description": "CSV stringifier implementing the Node.js `stream.Transform` API",

[![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 arrays or objects into a CSV text. It implements the Node.js [`stream.Transform` API][stream_transform]. It also provides a simple callback-based API for convenience. It is both extremely easy to use and powerful. It was first released in 2010 and is used against big data sets by a large community.
This package is a stringifier converting records into a CSV text and implementing the
Node.js [`stream.Transform` API](https://nodejs.org/api/stream.html). It also provides the easier synchronous and
callback-based APIs for conveniency. It is both extremely easy to use and
powerful. It was first released in 2010 and is tested against big data
sets by a large community.

@@ -6,0 +10,0 @@ ## Documentation

@@ -5,5 +5,7 @@

input = [ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]
stringify(input, function(err, output){
stringify([
[ '1', '2', '3', '4' ],
[ 'a', 'b', 'c', 'd' ]
], function(err, output){
assert.equal(output, '1,2,3,4\na,b,c,d\n')
})

@@ -5,3 +5,3 @@

generator = generate({
generate({
objectMode: true,

@@ -11,3 +11,9 @@ seed: 1,

})
stringifier = stringify()
generator.pipe(stringifier).pipe(process.stdout)
.pipe(stringify({
header: true,
columns: {
year: 'birthYear',
phone: 'phone'
}
}))
.pipe(process.stdout)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc