Skip to content

Commit

Permalink
Support nested parent properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Alferov committed Jan 13, 2017
1 parent 04f8d26 commit 1245209
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var isArray = require('lodash.isarray');
var assign = require('lodash.assign');
var property = require('nested-property');

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

var groupByParents = function(array, options) {
return array.reduce(function(prev, item) {
var parentID = item[options.parentProperty] || options.rootID;
var parentID = property.get(item, options.parentProperty) || options.rootID;

if (parentID && prev.hasOwnProperty(parentID)) {
prev[parentID].push(item);
Expand Down
2 changes: 1 addition & 1 deletion license.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright © 2016 sPhilipp Alferov.
Copyright © 2016 Philipp Alferov.

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:

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"license": "MIT",
"dependencies": {
"lodash.assign": "^4.0.6",
"lodash.isarray": "^4.0.0"
"lodash.isarray": "^4.0.0",
"nested-property": "^0.0.7"
}
}
20 changes: 20 additions & 0 deletions test/fixtures/expected-nested.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = [{
id: 1,
attributes: {
name: 'Parent',
parent_id: null
},
children: [{
id: 2,
attributes: {
name: 'Child One',
parent_id: 1
}
}, {
id: 3,
attributes: {
name: 'Child Two',
parent_id: 1
}
}]
}];
19 changes: 19 additions & 0 deletions test/fixtures/initial-nested.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = [{
id: 1,
attributes: {
name: 'Parent',
parent_id: null
}
}, {
id: 2,
attributes: {
name: 'Child One',
parent_id: 1
}
}, {
id: 3,
attributes: {
name: 'Child Two',
parent_id: 1
}
}];
13 changes: 12 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
var chai = require('chai');
var expect = chai.expect;
var toTree = require('../index.js');
var initial = require('./fixtures/initial.fixture.js');
var expected = require('./fixtures/expected.fixture.js');
var customExpected = require('./fixtures/expected-custom.fixture.js');
var customInitial = require('./fixtures/initial-custom.fixture.js');
var initial = require('./fixtures/initial.fixture.js');
var nestedInitial = require('./fixtures/initial-nested.fixture.js');
var nestedExpected = require('./fixtures/expected-nested.fixture.js');

var current;

describe('array-to-tree', function() {
Expand Down Expand Up @@ -78,5 +81,13 @@ describe('array-to-tree', function() {
expect(current)
.to.be.deep.equal(customExpected);
});
it('should work with nested parent id', function() {
current = toTree(nestedInitial, {
parentProperty: 'attributes.parent_id'
});

expect(current)
.to.be.deep.equal(nestedExpected);
});
});
});

0 comments on commit 1245209

Please sign in to comment.