From 39f303d1aa41d1c5ea4a5bb050cf564e0ffb4b0d Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Tue, 3 Sep 2019 17:42:28 +0300 Subject: [PATCH] fix: Remove stringify option (#419) BREAKING CHANGE: remove `stringify` option --- README.md | 5 ++-- lib/JSON2CSVBase.js | 17 +++--------- test/CLI.js | 11 -------- test/JSON2CSVAsyncParser.js | 20 ++------------ test/JSON2CSVParser.js | 20 ++------------ test/JSON2CSVTransform.js | 29 ++------------------- test/fixtures/fields/functionNoStringify.js | 3 +-- 7 files changed, 12 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 15aa2c01..dae6a884 100644 --- a/README.md +++ b/README.md @@ -368,7 +368,7 @@ The stream API can also work on object mode. This is useful when you have an inp { label: 'some label', // Optional, column will be labeled with the function name or empty if the function is anonymous value: (row, field) => row[field.label].toLowerCase() ||field.default, - default: 'NULL', // default if value function returns null or undefined + default: 'NULL' // default if value function returns null or undefined }, // Supports label -> derived value @@ -378,8 +378,7 @@ The stream API can also work on object mode. This is useful when you have an inp // Supports label -> derived value { - value: (row) => `"${row.arrayField.join(',')}"`, - stringify: false // This flag signals if the resulting string should be quoted (stringified) or not (optional, default: true) + value: (row) => `"${row.arrayField.join(',')}"` }, ] } diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 42bff389..c55ea5c8 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -52,7 +52,6 @@ class JSON2CSVBase { value: (fieldInfo.includes('.') || fieldInfo.includes('[')) ? row => lodashGet(row, fieldInfo, this.opts.defaultValue) : row => getProp(row, fieldInfo, this.opts.defaultValue), - stringify: true, }; } @@ -67,7 +66,6 @@ class JSON2CSVBase { value: (fieldInfo.value.includes('.') || fieldInfo.value.includes('[')) ? row => lodashGet(row, fieldInfo.value, defaultValue) : row => getProp(row, fieldInfo.value, defaultValue), - stringify: fieldInfo.stringify !== undefined ? fieldInfo.stringify : true, }; } @@ -82,7 +80,6 @@ class JSON2CSVBase { ? defaultValue : value; }, - stringify: fieldInfo.stringify !== undefined ? fieldInfo.stringify : true, } } } @@ -168,17 +165,16 @@ class JSON2CSVBase { * @returns {String} CSV string (cell) */ processCell(row, fieldInfo) { - return this.processValue(fieldInfo.value(row), fieldInfo.stringify); + return this.processValue(fieldInfo.value(row)); } /** * Create the content of a specfic CSV row cell * * @param {Any} value Value to be included in a CSV cell - * @param {Boolean} stringify Details of the field to process to be a CSV cell * @returns {String} Value stringified and processed */ - processValue(value, stringify) { + processValue(value) { if (value === null || value === undefined) { return undefined; } @@ -201,14 +197,7 @@ class JSON2CSVBase { value = value.replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote); } - // This should probably be remove together with the whole strignify option - if (stringify) { - value = `${this.opts.quote}${value}${this.opts.quote}`; - } else { - value = value - .replace(new RegExp(`^${this.opts.doubleQuote}`), this.opts.quote) - .replace(new RegExp(`${this.opts.doubleQuote}$`), this.opts.quote); - } + value = `${this.opts.quote}${value}${this.opts.quote}`; if (this.opts.excelStrings) { value = `"="${value}""`; diff --git a/test/CLI.js b/test/CLI.js index f466a925..99598209 100644 --- a/test/CLI.js +++ b/test/CLI.js @@ -264,17 +264,6 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { }); }); - testRunner.add('field.value function should not stringify if stringify is selected to false', (t) => { - const opts = ' --config ' + getFixturePath('/fields/functionNoStringify.js'); - - child_process.exec(cli + '-i ' + getFixturePath('/json/functionNoStringify.json') + opts, (err, stdout, stderr) => { - t.notOk(stderr); - const csv = stdout; - t.equal(csv, csvFixtures.functionNoStringify); - t.end(); - }); - }); - testRunner.add('should process different combinations in fields option', (t) => { const opts = ' --config ' + getFixturePath('/fields/fancyfields.js') + ' --default-value NULL'; diff --git a/test/JSON2CSVAsyncParser.js b/test/JSON2CSVAsyncParser.js index db0cff73..adcb7e93 100644 --- a/test/JSON2CSVAsyncParser.js +++ b/test/JSON2CSVAsyncParser.js @@ -273,7 +273,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = testRunner.add('should error on invalid \'fields\' property', (t) => { const opts = { - fields: [ { value: 'price', stringify: true }, () => {} ] + fields: [ { value: 'price' }, () => {} ] }; try { @@ -293,7 +293,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = testRunner.add('should error on invalid \'fields.value\' property', (t) => { const opts = { fields: [ - { value: row => row.price, stringify: true }, + { value: row => row.price }, { label: 'Price USD', value: [] } ] }; @@ -373,22 +373,6 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = .then(() => t.end()); }); - testRunner.add('field.value function should not stringify if stringify is selected to false', (t) => { - const opts = { - fields: [{ - label: 'Value1', - value: row => row.value1.toLocaleString(), - stringify: false - }] - }; - - const parser = new AsyncParser(opts); - parser.fromInput(jsonFixtures.functionNoStringify()).promise() - .then(csv => t.equal(csv, csvFixtures.functionNoStringify)) - .catch(err => t.notOk(true, err.message)) - .then(() => t.end()); - }); - testRunner.add('should process different combinations in fields option', (t) => { const opts = { fields: [{ diff --git a/test/JSON2CSVParser.js b/test/JSON2CSVParser.js index 9e62f0be..6e01501f 100644 --- a/test/JSON2CSVParser.js +++ b/test/JSON2CSVParser.js @@ -177,7 +177,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { testRunner.add('should error on invalid \'fields\' property', (t) => { const opts = { - fields: [ { value: 'price', stringify: true }, () => {} ] + fields: [ { value: 'price' }, () => {} ] }; try { @@ -194,7 +194,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { testRunner.add('should error on invalid \'fields.value\' property', (t) => { const opts = { fields: [ - { value: row => row.price, stringify: true }, + { value: row => row.price }, { label: 'Price USD', value: [] } ] }; @@ -271,22 +271,6 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => { t.end(); }); - testRunner.add('field.value function should not stringify if stringify is selected to false', (t) => { - const opts = { - fields: [{ - label: 'Value1', - value: row => row.value1.toLocaleString(), - stringify: false - }] - }; - - const parser = new Json2csvParser(opts); - const csv = parser.parse(jsonFixtures.functionNoStringify); - - t.equal(csv, csvFixtures.functionNoStringify); - t.end(); - }); - testRunner.add('should process different combinations in fields option', (t) => { const opts = { fields: [{ diff --git a/test/JSON2CSVTransform.js b/test/JSON2CSVTransform.js index 3a5c23d8..973074eb 100644 --- a/test/JSON2CSVTransform.js +++ b/test/JSON2CSVTransform.js @@ -347,7 +347,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = testRunner.add('should error on invalid \'fields\' property', (t) => { const opts = { - fields: [ { value: 'price', stringify: true }, () => {} ] + fields: [ { value: 'price' }, () => {} ] }; try { @@ -364,7 +364,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = testRunner.add('should error on invalid \'fields.value\' property', (t) => { const opts = { fields: [ - { value: row => row.price, stringify: true }, + { value: row => row.price }, { label: 'Price USD', value: [] } ] }; @@ -468,31 +468,6 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) = }); }); - testRunner.add('field.value function should not stringify if stringify is selected to false', (t) => { - const opts = { - fields: [{ - label: 'Value1', - value: row => row.value1.toLocaleString(), - stringify: false - }] - }; - - const transform = new Json2csvTransform(opts); - const processor = jsonFixtures.functionNoStringify().pipe(transform); - - let csv = ''; - processor - .on('data', chunk => (csv += chunk.toString())) - .on('end', () => { - t.equal(csv, csvFixtures.functionNoStringify); - t.end(); - }) - .on('error', err => { - t.notOk(true, err.message) - t.end(); - }); - }); - testRunner.add('should process different combinations in fields option', (t) => { const opts = { fields: [{ diff --git a/test/fixtures/fields/functionNoStringify.js b/test/fixtures/fields/functionNoStringify.js index 511ff164..0c796ed5 100644 --- a/test/fixtures/fields/functionNoStringify.js +++ b/test/fixtures/fields/functionNoStringify.js @@ -1,7 +1,6 @@ module.exports = { fields: [{ label: 'Value1', - value: row => row.value1.toLocaleString(), - stringify: false + value: row => row.value1.toLocaleString() }] }; \ No newline at end of file