From 257673e944ce061ee4a7150c1398d1d695546d21 Mon Sep 17 00:00:00 2001 From: timwis Date: Wed, 23 Aug 2017 06:33:29 -0400 Subject: [PATCH 1/2] Treat orphan nodes as root nodes --- index.js | 8 +++++++- package.json | 1 + test/fixtures/expected-orphan.fixture.js | 15 +++++++++++++++ test/fixtures/initial-orphan.fixture.js | 13 +++++++++++++ test/test.js | 8 ++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/expected-orphan.fixture.js create mode 100644 test/fixtures/initial-orphan.fixture.js diff --git a/index.js b/index.js index 8779643..d010139 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ var isArray = require('lodash.isarray'); var assign = require('lodash.assign'); var property = require('nested-property'); +var keyBy = require('lodash.keyby'); var createTree = function(array, rootNodes, customID) { var tree = []; @@ -25,8 +26,13 @@ var createTree = function(array, rootNodes, customID) { }; var groupByParents = function(array, options) { + var arrayByID = keyBy(array, options.customID); + return array.reduce(function(prev, item) { - var parentID = property.get(item, options.parentProperty) || options.rootID; + var parentID = property.get(item, options.parentProperty); + if (!parentID || !(parentID in arrayByID)) { + parentID = options.rootID; + } if (parentID && prev.hasOwnProperty(parentID)) { prev[parentID].push(item); diff --git a/package.json b/package.json index 80bedf5..f19864a 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "dependencies": { "lodash.assign": "^4.0.6", "lodash.isarray": "^4.0.0", + "lodash.keyby": "^4.6.0", "nested-property": "^0.0.7" } } diff --git a/test/fixtures/expected-orphan.fixture.js b/test/fixtures/expected-orphan.fixture.js new file mode 100644 index 0000000..6ad4fcf --- /dev/null +++ b/test/fixtures/expected-orphan.fixture.js @@ -0,0 +1,15 @@ +module.exports = [{ + id: 1, + parent_id: null, + children: [{ + id: 2, + parent_id: 1, + children: [{ + id: 3, + parent_id: 2 + }] + }] +}, { + id: 4, + parent_id: 5 +}]; diff --git a/test/fixtures/initial-orphan.fixture.js b/test/fixtures/initial-orphan.fixture.js new file mode 100644 index 0000000..1a497bd --- /dev/null +++ b/test/fixtures/initial-orphan.fixture.js @@ -0,0 +1,13 @@ +module.exports = [{ + id: 1, + parent_id: null +}, { + id: 2, + parent_id: 1 +}, { + id: 3, + parent_id: 2 +}, { + id: 4, + parent_id: 5 +}]; diff --git a/test/test.js b/test/test.js index 2d8fcd0..e4308a7 100644 --- a/test/test.js +++ b/test/test.js @@ -8,6 +8,8 @@ var customExpected = require('./fixtures/expected-custom.fixture.js'); var customInitial = require('./fixtures/initial-custom.fixture.js'); var nestedInitial = require('./fixtures/initial-nested.fixture.js'); var nestedExpected = require('./fixtures/expected-nested.fixture.js'); +var orphanInitial = require('./fixtures/initial-orphan.fixture.js'); +var orphanExpected = require('./fixtures/expected-orphan.fixture.js'); var current; @@ -89,5 +91,11 @@ describe('array-to-tree', function() { expect(current) .to.be.deep.equal(nestedExpected); }); + it('should work with orphan nodes', function() { + current = toTree(orphanInitial); + + expect(current) + .to.be.deep.equal(orphanExpected); + }); }); }); From 1e1373126ec94ea4b0460eff05edaaf9637ef816 Mon Sep 17 00:00:00 2001 From: timwis Date: Wed, 23 Aug 2017 06:38:51 -0400 Subject: [PATCH 2/2] Orphans: use Object.prototype.hasOwnProperty instead of in --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d010139..4d75e21 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ var groupByParents = function(array, options) { return array.reduce(function(prev, item) { var parentID = property.get(item, options.parentProperty); - if (!parentID || !(parentID in arrayByID)) { + if (!parentID || !arrayByID.hasOwnProperty(parentID)) { parentID = options.rootID; }