1
1
'use strict' ;
2
- var property = require ( 'nested-property' ) ;
3
- var keyBy = require ( 'lodash.keyby' ) ;
2
+ const property = require ( 'nested-property' ) ;
3
+ const keyBy = require ( 'lodash.keyby' ) ;
4
4
5
- var isArray = Array . isArray || function ( arg ) {
6
- return Object . prototype . toString . call ( arg ) === '[object Array]' ;
7
- } ;
8
-
9
- var createTree = function ( array , rootNodes , customID , childrenProperty ) {
10
- var tree = [ ] ;
5
+ const createTree = ( array , rootNodes , customID , childrenProperty ) => {
6
+ const tree = [ ] ;
11
7
12
- for ( var rootNode in rootNodes ) {
13
- var node = rootNodes [ rootNode ] ;
14
- var childNode = array [ node [ customID ] ] ;
8
+ for ( const rootNode in rootNodes ) {
9
+ const node = rootNodes [ rootNode ] ;
10
+ const childNode = array [ node [ customID ] ] ;
15
11
16
12
if ( ! node && ! rootNodes . hasOwnProperty ( rootNode ) ) {
17
13
continue ;
18
14
}
19
15
20
16
if ( childNode ) {
21
- node [ childrenProperty ] = createTree ( array , childNode , customID , childrenProperty ) ;
17
+ node [ childrenProperty ] = createTree (
18
+ array ,
19
+ childNode ,
20
+ customID ,
21
+ childrenProperty
22
+ ) ;
22
23
}
23
24
24
25
tree . push ( node ) ;
@@ -27,11 +28,11 @@ var createTree = function (array, rootNodes, customID, childrenProperty) {
27
28
return tree ;
28
29
} ;
29
30
30
- var groupByParents = function ( array , options ) {
31
- var arrayByID = keyBy ( array , options . customID ) ;
31
+ const groupByParents = ( array , options ) => {
32
+ const arrayByID = keyBy ( array , options . customID ) ;
32
33
33
- return array . reduce ( function ( prev , item ) {
34
- var parentID = property . get ( item , options . parentProperty ) ;
34
+ return array . reduce ( ( prev , item ) => {
35
+ let parentID = property . get ( item , options . parentProperty ) ;
35
36
if ( ! parentID || ! arrayByID . hasOwnProperty ( parentID ) ) {
36
37
parentID = options . rootID ;
37
38
}
@@ -58,24 +59,34 @@ var groupByParents = function (array, options) {
58
59
* @param {Object } options An object containing the following fields:
59
60
*
60
61
* - `parentProperty` (String): A name of a property where a link to
61
- * a parent node could be found. Default: 'parent_id'
62
+ * a parent node could be found. Default: 'parent_id'
62
63
* - `customID` (String): An unique node identifier. Default: 'id'
64
+ * - `childrenProperty` (String): A name of a property where children nodes
65
+ * are going to be stored. Default: 'children'.
63
66
*
64
67
* @return {Array } Result of transformation
65
68
*/
66
69
67
70
module . exports = function arrayToTree ( data , options ) {
68
- options = Object . assign ( {
69
- childrenProperty : 'children' ,
70
- parentProperty : 'parent_id' ,
71
- customID : 'id' ,
72
- rootID : '0'
73
- } , options ) ;
71
+ options = Object . assign (
72
+ {
73
+ parentProperty : 'parent_id' ,
74
+ childrenProperty : 'children' ,
75
+ customID : 'id' ,
76
+ rootID : '0'
77
+ } ,
78
+ options
79
+ ) ;
74
80
75
- if ( ! isArray ( data ) ) {
81
+ if ( ! Array . isArray ( data ) ) {
76
82
throw new TypeError ( 'Expected an object but got an invalid argument' ) ;
77
83
}
78
84
79
- var grouped = JSON . parse ( JSON . stringify ( groupByParents ( data , options ) ) ) ; // clone
80
- return createTree ( grouped , grouped [ options . rootID ] , options . customID , options . childrenProperty ) ;
85
+ const grouped = groupByParents ( data . slice ( ) , options ) ;
86
+ return createTree (
87
+ grouped ,
88
+ grouped [ options . rootID ] ,
89
+ options . customID ,
90
+ options . childrenProperty
91
+ ) ;
81
92
} ;
0 commit comments