Skip to content

Commit

Permalink
fix: bug that modifies opts after parsing an object/stream (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Aug 6, 2018
1 parent ee3d181 commit f0a4830
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class JSON2CSVBase {
* delimiter, default value, quote mark, header, etc.
*/
preprocessOpts(opts) {
const processedOpts = opts || {};
const processedOpts = Object.assign({}, opts);
processedOpts.unwind = !Array.isArray(processedOpts.unwind)
? (processedOpts.unwind ? [processedOpts.unwind] : [])
: processedOpts.unwind
Expand Down
21 changes: 21 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const json2csv = require('../lib/json2csv');
const Json2csvParser = json2csv.Parser;

module.exports = (testRunner, jsonFixtures, csvFixtures) => {
testRunner.add('should not modify the opts passed using parse method', (t) => {
const opts = {};
const csv = json2csv.parse(jsonFixtures.default);

t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.default);
t.deepEqual(opts, {});
t.end();
});

testRunner.add('should parse json to csv and infer the fields automatically using parse method', (t) => {
const csv = json2csv.parse(jsonFixtures.default);

Expand All @@ -12,6 +22,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should not modify the opts passed', (t) => {
const opts = {};
const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.default);

t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.default);
t.deepEqual(opts, {});
t.end();
});

testRunner.add('should error if input data is not an object', (t) => {
const input = 'not an object';
try {
Expand Down
17 changes: 17 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should not modify the opts passed', (t) => {
const opts = {};
const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.default().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.defaultStream);
t.deepEqual(opts, {});
t.end();
})
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should error if input data is not an object', (t) => {
const input = new Readable();
input._read = () => {};
Expand Down

0 comments on commit f0a4830

Please sign in to comment.