From d28955a53b84747fbc94137f6f379b770c3c1865 Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Mon, 19 Nov 2018 14:55:42 +0200 Subject: [PATCH] fix: Remove lodash.clonedeep dependency (#339) 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). --- lib/JSON2CSVBase.js | 18 +++++++++++------- package-lock.json | 11 ----------- package.json | 1 - 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 841297c4..1ecf600d 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -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) { @@ -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 => { @@ -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), []); diff --git a/package-lock.json b/package-lock.json index c89559d3..35bf6d89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4970,12 +4970,6 @@ } } }, - "git-update-ghpages": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/git-update-ghpages/-/git-update-ghpages-1.3.0.tgz", - "integrity": "sha1-TP8lTiH2TVo6KYZaAfNv0WTYyiU=", - "dev": true - }, "gitconfiglocal": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/gitconfiglocal/-/gitconfiglocal-1.0.0.tgz", @@ -6405,11 +6399,6 @@ "integrity": "sha1-euMBfpOWIqwxt9fX3LGzTbFpDTU=", "dev": true }, - "lodash.clonedeep": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", - "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=" - }, "lodash.defaults": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", diff --git a/package.json b/package.json index 716b18b0..b6c6aab9 100644 --- a/package.json +++ b/package.json @@ -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" },