Skip to content

Commit d5c5262

Browse files
author
Philipp Alferov
authored
Merge pull request #3 from timwis/orphans
Treat orphan nodes as root nodes
2 parents 4a1e79c + 1e13731 commit d5c5262

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var isArray = require('lodash.isarray');
33
var assign = require('lodash.assign');
44
var property = require('nested-property');
5+
var keyBy = require('lodash.keyby');
56

67
var createTree = function(array, rootNodes, customID) {
78
var tree = [];
@@ -25,8 +26,13 @@ var createTree = function(array, rootNodes, customID) {
2526
};
2627

2728
var groupByParents = function(array, options) {
29+
var arrayByID = keyBy(array, options.customID);
30+
2831
return array.reduce(function(prev, item) {
29-
var parentID = property.get(item, options.parentProperty) || options.rootID;
32+
var parentID = property.get(item, options.parentProperty);
33+
if (!parentID || !arrayByID.hasOwnProperty(parentID)) {
34+
parentID = options.rootID;
35+
}
3036

3137
if (parentID && prev.hasOwnProperty(parentID)) {
3238
prev[parentID].push(item);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"lodash.assign": "^4.0.6",
3737
"lodash.isarray": "^4.0.0",
38+
"lodash.keyby": "^4.6.0",
3839
"nested-property": "^0.0.7"
3940
}
4041
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = [{
2+
id: 1,
3+
parent_id: null,
4+
children: [{
5+
id: 2,
6+
parent_id: 1,
7+
children: [{
8+
id: 3,
9+
parent_id: 2
10+
}]
11+
}]
12+
}, {
13+
id: 4,
14+
parent_id: 5
15+
}];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = [{
2+
id: 1,
3+
parent_id: null
4+
}, {
5+
id: 2,
6+
parent_id: 1
7+
}, {
8+
id: 3,
9+
parent_id: 2
10+
}, {
11+
id: 4,
12+
parent_id: 5
13+
}];

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var customExpected = require('./fixtures/expected-custom.fixture.js');
88
var customInitial = require('./fixtures/initial-custom.fixture.js');
99
var nestedInitial = require('./fixtures/initial-nested.fixture.js');
1010
var nestedExpected = require('./fixtures/expected-nested.fixture.js');
11+
var orphanInitial = require('./fixtures/initial-orphan.fixture.js');
12+
var orphanExpected = require('./fixtures/expected-orphan.fixture.js');
1113

1214
var current;
1315

@@ -89,5 +91,11 @@ describe('array-to-tree', function() {
8991
expect(current)
9092
.to.be.deep.equal(nestedExpected);
9193
});
94+
it('should work with orphan nodes', function() {
95+
current = toTree(orphanInitial);
96+
97+
expect(current)
98+
.to.be.deep.equal(orphanExpected);
99+
});
92100
});
93101
});

0 commit comments

Comments
 (0)