Skip to content

Commit

Permalink
fix: Rename doubleQuote to escapedQuote (#418)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Renamed `doubleQuote` to `escapedQuote`
  • Loading branch information
juanjoDiaz authored and knownasilya committed Sep 9, 2019
1 parent 39f303d commit f99408c
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Options:
-S, --flatten-separator <separator> Flattened keys separator. Defaults to '.'.
-v, --default-value [defaultValue] Default value to use for missing fields.
-q, --quote [value] Character(s) to use a quote mark. Defaults to '"'.
-Q, --double-quote [value] Character(s) to use as a escaped quote. Defaults to a double `quote`, '""'.
-Q, --escaped-quote [value] Character(s) to use as a escaped quote. Defaults to a double `quote`, '""'.
-d, --delimiter [delimiter] Character(s) to use as delimiter. Defaults to ','.
-e, --eol [value] Character(s) to use as End-of-Line for separating rows. Defaults to '\n'.
-E, --excel-strings Wraps string data to force Excel to interpret it as string even if it contains a number.
Expand Down Expand Up @@ -181,7 +181,7 @@ The programatic APIs take a configuration object very equivalent to the CLI opti
- `flattenSeparator` - String, separator to use between nested JSON keys when `flatten` option enabled. Defaults to `.` if not specified.
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)
- `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.
- `escapedQuote` - String, the value to replace escaped quotes in strings. Defaults to 2x`quotes` (for example `""`) if not specified.
- `delimiter` - String, delimiter of columns. Defaults to `,` if not specified.
- `eol` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
- `excelStrings` - Boolean, converts string data into normalized Excel style data.
Expand Down
4 changes: 2 additions & 2 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ program
.option('-S, --flatten-separator <separator>', 'Flattened keys separator. Defaults to \'.\'.')
.option('-v, --default-value [defaultValue]', 'Default value to use for missing fields.')
.option('-q, --quote [quote]', 'Character(s) to use as quote mark. Defaults to \'"\'.')
.option('-Q, --double-quote [doubleQuote]', 'Character(s) to use as a escaped quote. Defaults to a double `quote`, \'""\'.')
.option('-Q, --escaped-quote [escapedQuote]', 'Character(s) to use as a escaped quote. Defaults to a double `quote`, \'""\'.')
.option('-d, --delimiter [delimiter]', 'Character(s) to use as delimiter. Defaults to \',\'.')
.option('-e, --eol [eol]', 'Character(s) to use as End-of-Line for separating rows. Defaults to \'\\n\'.')
.option('-E, --excel-strings','Wraps string data to force Excel to interpret it as string even if it contains a number.')
Expand Down Expand Up @@ -160,7 +160,7 @@ Promise.resolve()
flattenSeparator: config.flattenSeparator,
defaultValue: config.defaultValue,
quote: config.quote,
doubleQuote: config.doubleQuote,
escapedQuote: config.escapedQuote,
delimiter: config.delimiter,
eol: config.eol,
excelStrings: config.excelStrings,
Expand Down
6 changes: 3 additions & 3 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class JSON2CSVBase {
processedOpts.quote = typeof processedOpts.quote === 'string'
? opts.quote
: '"';
processedOpts.doubleQuote = typeof processedOpts.doubleQuote === 'string'
? processedOpts.doubleQuote
processedOpts.escapedQuote = typeof processedOpts.escapedQuote === 'string'
? processedOpts.escapedQuote
: processedOpts.quote + processedOpts.quote;
processedOpts.header = processedOpts.header !== false;
processedOpts.includeEmptyRows = processedOpts.includeEmptyRows || false;
Expand Down Expand Up @@ -194,7 +194,7 @@ class JSON2CSVBase {

if (typeof value === 'string') {
if(value.includes(this.opts.quote)) {
value = value.replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote);
value = value.replace(new RegExp(this.opts.quote, 'g'), this.opts.escapedQuote);
}

value = `${this.opts.quote}${value}${this.opts.quote}`;
Expand Down
18 changes: 9 additions & 9 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,15 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => {
const opts = ' --quote "\'"';

child_process.exec(cli + '-i ' + getFixturePath('/json/doubleQuotes.json') + opts, (err, stdout, stderr) => {
child_process.exec(cli + '-i ' + getFixturePath('/json/escapedQuotes.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.doubleQuotesUnescaped);
t.equal(csv, csvFixtures.escapedQuotesUnescaped);
t.end();
});
});

// Double Quote
// Escaped Quote

testRunner.add('should escape quotes with double quotes', (t) => {
child_process.exec(cli + '-i ' + getFixturePath('/json/quotes.json'), (err, stdout, stderr) => {
Expand Down Expand Up @@ -458,13 +458,13 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should escape quotes with value in \'doubleQuote\'', (t) => {
const opts = ' --fields "a string" --double-quote "*"';
testRunner.add('should escape quotes with value in \'escapedQuote\'', (t) => {
const opts = ' --fields "a string" --escaped-quote "*"';

child_process.exec(cli + '-i ' + getFixturePath('/json/doubleQuotes.json') + opts, (err, stdout, stderr) => {
child_process.exec(cli + '-i ' + getFixturePath('/json/escapedQuotes.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.doubleQuotes);
t.equal(csv, csvFixtures.escapedQuotes);
t.end();
});
});
Expand Down Expand Up @@ -533,10 +533,10 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});

testRunner.add('should escape " when preceeded by \\', (t) => {
child_process.exec(cli + '-i ' + getFixturePath('/json/escapeDoubleBackslashedDoubleQuote.json'), (err, stdout, stderr) => {
child_process.exec(cli + '-i ' + getFixturePath('/json/escapeDoubleBackslashedEscapedQuote.json'), (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.escapeDoubleBackslashedDoubleQuote);
t.equal(csv, csvFixtures.escapeDoubleBackslashedEscapedQuote);
t.end();
});
});
Expand Down
20 changes: 10 additions & 10 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,13 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
};

const parser = new AsyncParser(opts);
parser.fromInput(jsonFixtures.doubleQuotes()).promise()
.then(csv => t.equal(csv, csvFixtures.doubleQuotesUnescaped))
parser.fromInput(jsonFixtures.escapedQuotes()).promise()
.then(csv => t.equal(csv, csvFixtures.escapedQuotesUnescaped))
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});

// Double Quote
// Escaped Quote

testRunner.add('should escape quotes with double quotes', (t) => {
const parser = new AsyncParser();
Expand All @@ -618,20 +618,20 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
.then(() => t.end());
});

testRunner.add('should escape quotes with value in \'doubleQuote\'', (t) => {
testRunner.add('should escape quotes with value in \'escapedQuote\'', (t) => {
const opts = {
fields: ['a string'],
doubleQuote: '*'
escapedQuote: '*'
};

const parser = new AsyncParser(opts);
parser.fromInput(jsonFixtures.doubleQuotes()).promise()
.then(csv => t.equal(csv, csvFixtures.doubleQuotes))
parser.fromInput(jsonFixtures.escapedQuotes()).promise()
.then(csv => t.equal(csv, csvFixtures.escapedQuotes))
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});

testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
testRunner.add('should escape quotes before new line with value in \'escapedQuote\'', (t) => {
const opts = {
fields: ['a string']
};
Expand Down Expand Up @@ -714,8 +714,8 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =

testRunner.add('should escape " when preceeded by \\', (t) => {
const parser = new AsyncParser();
parser.fromInput(jsonFixtures.escapeDoubleBackslashedDoubleQuote()).promise()
.then(csv => t.equal(csv, csvFixtures.escapeDoubleBackslashedDoubleQuote))
parser.fromInput(jsonFixtures.escapeDoubleBackslashedEscapedQuote()).promise()
.then(csv => t.equal(csv, csvFixtures.escapeDoubleBackslashedEscapedQuote))
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});
Expand Down
20 changes: 10 additions & 10 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
};

const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.doubleQuotes);
const csv = parser.parse(jsonFixtures.escapedQuotes);

t.equal(csv, csvFixtures.doubleQuotesUnescaped);
t.equal(csv, csvFixtures.escapedQuotesUnescaped);
t.end();
});

// Double Quote
// Escaped Quote

testRunner.add('should escape quotes with double quotes', (t) => {
const parser = new Json2csvParser();
Expand All @@ -529,20 +529,20 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should escape quotes with value in \'doubleQuote\'', (t) => {
testRunner.add('should escape quotes with value in \'escapedQuote\'', (t) => {
const opts = {
fields: ['a string'],
doubleQuote: '*'
escapedQuote: '*'
};

const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.doubleQuotes);
const csv = parser.parse(jsonFixtures.escapedQuotes);

t.equal(csv, csvFixtures.doubleQuotes);
t.equal(csv, csvFixtures.escapedQuotes);
t.end();
});

testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
testRunner.add('should escape quotes before new line with value in \'escapedQuote\'', (t) => {
const opts = {
fields: ['a string']
};
Expand Down Expand Up @@ -625,9 +625,9 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

testRunner.add('should escape " when preceeded by \\', (t) => {
const parser = new Json2csvParser();
const csv = parser.parse(jsonFixtures.escapeDoubleBackslashedDoubleQuote);
const csv = parser.parse(jsonFixtures.escapeDoubleBackslashedEscapedQuote);

t.equal(csv, csvFixtures.escapeDoubleBackslashedDoubleQuote);
t.equal(csv, csvFixtures.escapeDoubleBackslashedEscapedQuote);
t.end();
});

Expand Down
20 changes: 10 additions & 10 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,13 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.doubleQuotes().pipe(transform);
const processor = jsonFixtures.escapedQuotes().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.doubleQuotesUnescaped);
t.equal(csv, csvFixtures.escapedQuotesUnescaped);
t.end();
})
.on('error', err => {
Expand All @@ -815,7 +815,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
});
});

// Double Quote
// Escaped Quote

testRunner.add('should escape quotes with double quotes', (t) => {
const transform = new Json2csvTransform();
Expand Down Expand Up @@ -868,20 +868,20 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
});
});

testRunner.add('should escape quotes with value in \'doubleQuote\'', (t) => {
testRunner.add('should escape quotes with value in \'escapedQuote\'', (t) => {
const opts = {
fields: ['a string'],
doubleQuote: '*'
escapedQuote: '*'
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.doubleQuotes().pipe(transform);
const processor = jsonFixtures.escapedQuotes().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.doubleQuotes);
t.equal(csv, csvFixtures.escapedQuotes);
t.end();
})
.on('error', err => {
Expand All @@ -890,7 +890,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
});
});

testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
testRunner.add('should escape quotes before new line with value in \'escapedQuote\'', (t) => {
const opts = {
fields: ['a string']
};
Expand Down Expand Up @@ -1027,13 +1027,13 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =

testRunner.add('should escape " when preceeded by \\', (t) => {
const transform = new Json2csvTransform();
const processor = jsonFixtures.escapeDoubleBackslashedDoubleQuote().pipe(transform);
const processor = jsonFixtures.escapeDoubleBackslashedEscapedQuote().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.escapeDoubleBackslashedDoubleQuote);
t.equal(csv, csvFixtures.escapeDoubleBackslashedEscapedQuote);
t.end();
})
.on('error', err => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit f99408c

Please sign in to comment.