Skip to content

Commit 1245209

Browse files
author
Philipp Alferov
committed
Support nested parent properties
1 parent 04f8d26 commit 1245209

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var isArray = require('lodash.isarray');
33
var assign = require('lodash.assign');
4+
var property = require('nested-property');
45

56
var createTree = function(array, rootNodes, customID) {
67
var tree = [];
@@ -25,7 +26,7 @@ var createTree = function(array, rootNodes, customID) {
2526

2627
var groupByParents = function(array, options) {
2728
return array.reduce(function(prev, item) {
28-
var parentID = item[options.parentProperty] || options.rootID;
29+
var parentID = property.get(item, options.parentProperty) || options.rootID;
2930

3031
if (parentID && prev.hasOwnProperty(parentID)) {
3132
prev[parentID].push(item);

license.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright © 2016 sPhilipp Alferov.
1+
Copyright © 2016 Philipp Alferov.
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"license": "MIT",
3535
"dependencies": {
3636
"lodash.assign": "^4.0.6",
37-
"lodash.isarray": "^4.0.0"
37+
"lodash.isarray": "^4.0.0",
38+
"nested-property": "^0.0.7"
3839
}
3940
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = [{
2+
id: 1,
3+
attributes: {
4+
name: 'Parent',
5+
parent_id: null
6+
},
7+
children: [{
8+
id: 2,
9+
attributes: {
10+
name: 'Child One',
11+
parent_id: 1
12+
}
13+
}, {
14+
id: 3,
15+
attributes: {
16+
name: 'Child Two',
17+
parent_id: 1
18+
}
19+
}]
20+
}];
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = [{
2+
id: 1,
3+
attributes: {
4+
name: 'Parent',
5+
parent_id: null
6+
}
7+
}, {
8+
id: 2,
9+
attributes: {
10+
name: 'Child One',
11+
parent_id: 1
12+
}
13+
}, {
14+
id: 3,
15+
attributes: {
16+
name: 'Child Two',
17+
parent_id: 1
18+
}
19+
}];

test/test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
var chai = require('chai');
33
var expect = chai.expect;
44
var toTree = require('../index.js');
5+
var initial = require('./fixtures/initial.fixture.js');
56
var expected = require('./fixtures/expected.fixture.js');
67
var customExpected = require('./fixtures/expected-custom.fixture.js');
78
var customInitial = require('./fixtures/initial-custom.fixture.js');
8-
var initial = require('./fixtures/initial.fixture.js');
9+
var nestedInitial = require('./fixtures/initial-nested.fixture.js');
10+
var nestedExpected = require('./fixtures/expected-nested.fixture.js');
11+
912
var current;
1013

1114
describe('array-to-tree', function() {
@@ -78,5 +81,13 @@ describe('array-to-tree', function() {
7881
expect(current)
7982
.to.be.deep.equal(customExpected);
8083
});
84+
it('should work with nested parent id', function() {
85+
current = toTree(nestedInitial, {
86+
parentProperty: 'attributes.parent_id'
87+
});
88+
89+
expect(current)
90+
.to.be.deep.equal(nestedExpected);
91+
});
8192
});
8293
});

0 commit comments

Comments
 (0)