Skip to content

Commit

Permalink
feat: add doubleQuote to cli, rename other options to line up with th…
Browse files Browse the repository at this point in the history
…e cli

BREAKING CHANGE: Rename del to delimiter to match the cli flag

BREAKING CHANGE: Rename quotes to quote to match the cli flag

* Remove unused double quotes comment

* Fix noHeader in CLI

* Revert "Remove unused double quotes comment"

This reverts commit 250d3e6.

* Add doubleQuote to CLI
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 19, 2018
1 parent f053c8b commit 5e402dc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ try {
- `fields` - Array of Objects/Strings. Defaults to toplevel JSON attributes. See example below.
- `fieldNames` Array of Strings, names for the fields at the same indexes.
Must be the same length as `fields` array. (Optional. Maintained for backwards compatibility. Use `fields` config object for more features)
- `del` - String, delimiter of columns. Defaults to `,` if not specified.
- `delimiter` - String, delimiter of columns. Defaults to `,` if not specified.
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)
- `quotes` - String, quotes around cell values and column names. Defaults to `"` if not specified.
- `doubleQuotes` - String, the value to replace double quotes in strings. Defaults to 2x`quotes` (for example `""`) if not specified.
- `quote` - String, quote around cell values and column names. Defaults to `"` if not specified.
- `doubleQuote` - String, the value to replace double quote in strings. Defaults to 2x`quotes` (for example `""`) if not specified.
- `noHeader` - Boolean, determines whether or not CSV file will contain a title column. Defaults to `true` if not specified.
- `eol` - String, it gets added to each row of data. Defaults to `` if not specified.
- `newLine` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
Expand Down Expand Up @@ -169,12 +169,12 @@ car, color

### Example 3

Use a custom delimiter to create tsv files. Add it as the value of the del property on the parameters:
Use a custom delimiter to create tsv files. Add it as the value of the delimiter property on the parameters:

```javascript
var json2csv = require('json2csv');
var fields = ['car', 'price', 'color'];
var tsv = json2csv({ data: myCars, fields: fields, del: '\t' });
var tsv = json2csv({ data: myCars, fields: fields, delimiter: '\t' });

console.log(tsv);
```
Expand Down Expand Up @@ -216,7 +216,7 @@ var opts = {
data: myCars,
fields: fields,
fieldNames: fieldNames,
quotes: ''
quote: ''
};
var csv = json2csv(opts);

Expand Down
8 changes: 5 additions & 3 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ program
.option('-e, --eol [value]', 'Specify an EOL value after each row.')
.option('-z, --newLine [value]', 'Specify an new line value for separating rows.')
.option('-q, --quote [value]', 'Specify an alternate quote value.')
.option('-dq, --doubleQuotes [value]', 'Specify a value to replace double quote in strings')
.option('-n, --no-header', 'Disable the column name header')
.option('-F, --flatten', 'Flatten nested objects')
.option('-u, --unwindPath <paths>', 'Creates multiple rows from a single JSON document similar to MongoDB unwind.')
Expand Down Expand Up @@ -109,16 +110,17 @@ getFields(function (err, fields) {
var opts = {
data: input,
fields: fields,
noHeader: program.header,
quotes: program.quote,
noHeader: program.noHeader,
quote: program.quote,
doubleQuotes: doubleQuotes,
defaultValue: program.defaultValue,
flatten: program.flatten,
includeEmptyRows: program.includeEmptyRows,
withBOM: program.withBom
};

if (program.delimiter) {
opts.del = program.delimiter;
opts.delimiter = program.delimiter;
}

if (program.eol) {
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ declare namespace json2csv {
data: T[];
fields?: (string | Field | CallbackField<T>)[];
fieldNames?: string[];
del?: string;
delimiter?: string;
defaultValue?: string;
quotes?: string;
doubleQuotes?: string;
quote?: string;
doubleQuote?: string;
noHeader?: boolean;
eol?: string;
newLine?: string;
Expand Down
42 changes: 21 additions & 21 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ var flatten = require('flat');
* @property {Array} [fields] - see documentation for details
* @property {String[]} [fieldNames] - names for fields at the same indexes. Must be same length as fields array
* (Optional. Maintained for backwards compatibility. Use fields config object for more features)
* @property {String} [del=","] - delimiter of columns
* @property {String} [delimiter=","] - delimiter of columns
* @property {String} [defaultValue="<empty>"] - default value to use when missing data
* @property {String} [quotes='"'] - quotes around cell values and column names
* @property {String} [doubleQuotes='""'] - the value to replace double quotes in strings
* @property {String} [quote='"'] - quote around cell values and column names
* @property {String} [doubleQuote='""'] - the value to replace double quote in strings
* @property {Boolean} [noHeader=true] - determines whether or not CSV file will contain a title column
* @property {String} [eol=''] - it gets added to each row of data
* @property {String} [newLine] - overrides the default OS line ending (\n on Unix \r\n on Windows)
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = function (params, callback) {
* Note that this modifies params.
*
* @param {Json2CsvParams} params Function parameters containing data, fields,
* delimiter, default value, mark quotes and noHeader
* delimiter, default value, mark quote and noHeader
*/
function checkParams(params) {
params.data = params.data || [];
Expand Down Expand Up @@ -119,16 +119,16 @@ function checkParams(params) {
});

//#check delimiter
params.del = params.del || ',';
params.delimiter = params.delimiter || ',';

//#check end of line character
params.eol = params.eol || '';

//#check quotation mark
params.quotes = typeof params.quotes === 'string' ? params.quotes : '"';
params.quote = typeof params.quote === 'string' ? params.quote : '"';

//#check double quotes
params.doubleQuotes = typeof params.doubleQuotes === 'string' ? params.doubleQuotes : Array(3).join(params.quotes);
//#check double quote
params.doubleQuote = typeof params.doubleQuote === 'string' ? params.doubleQuote : Array(3).join(params.quote);

//#check default value
params.defaultValue = params.defaultValue;
Expand Down Expand Up @@ -164,9 +164,9 @@ function createColumnTitles(params) {
if (params.noHeader) {
params.fieldNames.forEach(function (element) {
if (str !== '') {
str += params.del;
str += params.delimiter;
}
str += JSON.stringify(element).replace(/\"/g, params.quotes);
str += JSON.stringify(element).replace(/\"/g, params.quote);
});
}

Expand All @@ -177,9 +177,9 @@ function createColumnTitles(params) {
* Replace the quotation marks of the field element if needed (can be a not string-like item)
*
* @param {string} stringifiedElement The field element after JSON.stringify()
* @param {string} quotes The params.quotes value. At this point we know that is not equal to double (")
* @param {string} quote The params.quote value. At this point we know that is not equal to double (")
*/
function replaceQuotationMarks(stringifiedElement, quotes) {
function replaceQuotationMarks(stringifiedElement, quote) {
var lastCharIndex = stringifiedElement.length - 1;

//check if it's an string-like element
Expand All @@ -188,8 +188,8 @@ function replaceQuotationMarks(stringifiedElement, quotes) {
var splitElement = stringifiedElement.split('');

//replace the quotation marks
splitElement[0] = quotes;
splitElement[lastCharIndex] = quotes;
splitElement[0] = quote;
splitElement[lastCharIndex] = quote;

//join again
stringifiedElement = splitElement.join('');
Expand Down Expand Up @@ -258,16 +258,16 @@ function createColumnContent(params, str) {

if (typeof val === 'object') {
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
// Strip the leading and trailing quotes if so, so we don't end up double-quoting it
// Strip the leading and trailing quote if so, so we don't end up double-quoting it
stringifiedElement = replaceQuotationMarks(stringifiedElement, '');

// If val is a normal object, we want to escape its JSON so any commas etc
// don't get interpreted as field separators
stringifiedElement = JSON.stringify(stringifiedElement);
}

if (params.quotes !== '"') {
stringifiedElement = replaceQuotationMarks(stringifiedElement, params.quotes);
if (params.quote !== '"') {
stringifiedElement = replaceQuotationMarks(stringifiedElement, params.quote);
}

//JSON.stringify('\\') results in a string with two backslash
Expand All @@ -278,18 +278,18 @@ function createColumnContent(params, str) {
stringifiedElement = '"="' + stringifiedElement + '""';
}

//Replace single quotes with double quotes. Single quotes are preceeded by
//Replace single quote with double quote. Single quote are preceeded by
//a backslash, and it's not at the end of the stringifiedElement.
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuotes);
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuote);

line += stringifiedElement;
}

line += params.del;
line += params.delimiter;
});

//remove last delimeter by its length
line = line.substring(0, line.length - params.del.length);
line = line.substring(0, line.length - params.delimiter.length);

//Remove the final excess backslashes from the stringified value.
line = line.replace(/\\\\/g,'\\');
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
{ firstname: 'foo', lastname: 'bar', email: '[email protected]' },
{ firstname: 'bar', lastname: 'foo', email: '[email protected]' }
],
del: '|@|'
delimiter: '|@|'
}, function (err, csv) {
t.equal(csv, csvFixtures.delimiter);
t.end();
Expand Down Expand Up @@ -224,35 +224,35 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
});
});

test('should use a custom delimiter when \'quotes\' property is present', function (t) {
test('should use a custom delimiter when \'quote\' property is present', function (t) {
json2csv({
data: jsonDefault,
fields: ['carModel', 'price'],
quotes: '\''
quote: '\''
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.withSimpleQuotes);
t.end();
});
});

test('should be able to don\'t output quotes when using \'quotes\' property', function (t) {
test('should be able to don\'t output quotes when using \'quote\' property', function (t) {
json2csv({
data: jsonDefault,
fields: ['carModel', 'price'],
quotes: ''
quote: ''
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.withoutQuotes);
t.end();
});
});

test('should use a custom delimiter when \'del\' property is present', function (t) {
test('should use a custom delimiter when \'delimiter\' property is present', function (t) {
json2csv({
data: jsonDefault,
fields: ['carModel', 'price', 'color'],
del: '\t'
delimiter: '\t'
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.tsv);
Expand Down

0 comments on commit 5e402dc

Please sign in to comment.