Skip to content

Commit

Permalink
fix: Remove lodash.clonedeep dependency (#339)
Browse files Browse the repository at this point in the history
This should resolve #333

Since lodash modules don't seem to have a reliable release cycle and we don't really need clonedeep. Let's just get rid of it altogether.

This should also have a good impact on performance since now we only do shallow clones where needed instead of lodash.deepclone (which is famously solid and infamously slow).
  • Loading branch information
juanjoDiaz authored and knownasilya committed Nov 19, 2018
1 parent 9d8f917 commit d28955a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
18 changes: 11 additions & 7 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const os = require('os');
const lodashGet = require('lodash.get');
const lodashSet = require('lodash.set');
const lodashCloneDeep = require('lodash.clonedeep');

class JSON2CSVBase {
constructor(opts) {
Expand Down Expand Up @@ -255,9 +254,14 @@ class JSON2CSVBase {
*/
unwindData(dataRow, unwindPaths) {
const unwind = (rows, unwindPath) => {
const clone = unwindPath.indexOf('.') !== -1
? o => lodashCloneDeep(o)
: o => Object.assign({}, o);
const pathAndField = unwindPath.split(/\.(?=[^.]+$)/);
const setUnwoundValue = pathAndField.length === 2
? (() => {
const parentPath = pathAndField[0];
const unwindField = pathAndField[1];
return (row, value) => lodashSet(Object.assign({}, row), parentPath, Object.assign({}, lodashGet(row, parentPath), { [unwindField]: value }));
})()
: (row, value) => Object.assign({}, row, { [unwindPath]: value });

return rows
.map(row => {
Expand All @@ -268,15 +272,15 @@ class JSON2CSVBase {
}

if (!unwindArray.length) {
return lodashSet(clone(row), unwindPath, undefined);
return setUnwoundValue(row, undefined);
}

return unwindArray.map((unwindRow, index) => {
const clonedRow = (this.opts.unwindBlank && index > 0)
? {}
: clone(row);
: row;

return lodashSet(clonedRow, unwindPath, unwindRow);
return setUnwoundValue(clonedRow, unwindRow);
});
})
.reduce((a, e) => a.concat(e), []);
Expand Down
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dependencies": {
"commander": "^2.15.1",
"jsonparse": "^1.3.1",
"lodash.clonedeep": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2"
},
Expand Down

0 comments on commit d28955a

Please sign in to comment.