diff --git a/index.js b/index.js index 90d653d..8779643 100644 --- a/index.js +++ b/index.js @@ -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 = []; @@ -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); diff --git a/license.md b/license.md index 3739790..a0ab099 100644 --- a/license.md +++ b/license.md @@ -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: diff --git a/package.json b/package.json index 9fcd602..1f832fb 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/fixtures/expected-nested.fixture.js b/test/fixtures/expected-nested.fixture.js new file mode 100644 index 0000000..834770e --- /dev/null +++ b/test/fixtures/expected-nested.fixture.js @@ -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 + } + }] +}]; diff --git a/test/fixtures/initial-nested.fixture.js b/test/fixtures/initial-nested.fixture.js new file mode 100644 index 0000000..3022042 --- /dev/null +++ b/test/fixtures/initial-nested.fixture.js @@ -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 + } +}]; diff --git a/test/test.js b/test/test.js index 7a8d4a6..2d8fcd0 100644 --- a/test/test.js +++ b/test/test.js @@ -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() { @@ -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); + }); }); });