@@ -3,7 +3,7 @@ var assign = require('lodash.assign');
3
3
var property = require ( 'nested-property' ) ;
4
4
var keyBy = require ( 'lodash.keyby' ) ;
5
5
6
- var createTree = function ( array , rootNodes , customID ) {
6
+ var createTree = function ( array , rootNodes , customID , childrenProperty ) {
7
7
var tree = [ ] ;
8
8
9
9
for ( var rootNode in rootNodes ) {
@@ -15,7 +15,12 @@ var createTree = function(array, rootNodes, customID) {
15
15
}
16
16
17
17
if ( childNode ) {
18
- node . children = createTree ( array , childNode , customID ) ;
18
+ node [ childrenProperty ] = createTree (
19
+ array ,
20
+ childNode ,
21
+ customID ,
22
+ childrenProperty
23
+ ) ;
19
24
}
20
25
21
26
tree . push ( node ) ;
@@ -55,8 +60,10 @@ var groupByParents = function(array, options) {
55
60
* @param {Object } options An object containing the following fields:
56
61
*
57
62
* - `parentProperty` (String): A name of a property where a link to
58
- * a parent node could be found. Default: 'parent_id'
63
+ * a parent node could be found. Default: 'parent_id'
59
64
* - `customID` (String): An unique node identifier. Default: 'id'
65
+ * - `childrenProperty` (String): A name of a property where chilren nodes
66
+ * are going to be stored. Default: 'children'.
60
67
*
61
68
* @return {Array } Result of transformation
62
69
*/
@@ -65,6 +72,7 @@ module.exports = function arrayToTree(data, options) {
65
72
options = assign (
66
73
{
67
74
parentProperty : 'parent_id' ,
75
+ childrenProperty : 'children' ,
68
76
customID : 'id' ,
69
77
rootID : '0'
70
78
} ,
@@ -76,5 +84,10 @@ module.exports = function arrayToTree(data, options) {
76
84
}
77
85
78
86
var grouped = groupByParents ( data , options ) ;
79
- return createTree ( grouped , grouped [ options . rootID ] , options . customID ) ;
87
+ return createTree (
88
+ grouped ,
89
+ grouped [ options . rootID ] ,
90
+ options . customID ,
91
+ options . childrenProperty
92
+ ) ;
80
93
} ;
0 commit comments