Skip to content

Commit

Permalink
fix: Remove stringify option (#419)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove `stringify` option
  • Loading branch information
juanjoDiaz authored and knownasilya committed Sep 3, 2019
1 parent e0baab8 commit 39f303d
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 93 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(',')}"`
},
]
}
Expand Down
17 changes: 3 additions & 14 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand All @@ -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,
};
}

Expand All @@ -82,7 +80,6 @@ class JSON2CSVBase {
? defaultValue
: value;
},
stringify: fieldInfo.stringify !== undefined ? fieldInfo.stringify : true,
}
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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}""`;
Expand Down
11 changes: 0 additions & 11 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
20 changes: 2 additions & 18 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: [] }
]
};
Expand Down Expand Up @@ -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: [{
Expand Down
20 changes: 2 additions & 18 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: [] }
]
};
Expand Down Expand Up @@ -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: [{
Expand Down
29 changes: 2 additions & 27 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: [] }
]
};
Expand Down Expand Up @@ -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: [{
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/fields/functionNoStringify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
fields: [{
label: 'Value1',
value: row => row.value1.toLocaleString(),
stringify: false
value: row => row.value1.toLocaleString()
}]
};

0 comments on commit 39f303d

Please sign in to comment.