File tree 2 files changed +11
-7
lines changed
2 files changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -33,11 +33,15 @@ function orderByParents(list) {
33
33
return parents ;
34
34
}
35
35
36
- module . exports = function ( obj ) {
37
- if ( ! util . isArray ( obj ) ) {
36
+ module . exports = function ( options ) {
37
+ var config = util . _extend ( { parentProperty : 'parent_id' , data : [ ] } , options ) ;
38
+ var data = config . data ;
39
+
40
+ if ( ! util . isArray ( data ) ) {
38
41
throw new Error ( 'Expected an object but got an invalid argument' ) ;
39
42
}
40
- var cloned = obj . slice ( ) ;
43
+
44
+ var cloned = data . slice ( ) ;
41
45
var ordered = orderByParents ( cloned ) ;
42
46
return createTree ( ordered , ordered [ 0 ] ) ;
43
47
} ;
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ describe('parent pointer array to tree', function() {
10
10
describe ( 'expected behavior' , function ( ) {
11
11
12
12
before ( function ( ) {
13
- current = toTree ( initial ) ;
13
+ current = toTree ( { data : initial } ) ;
14
14
} ) ;
15
15
16
16
it ( 'should not modify passed object' , function ( ) {
@@ -44,12 +44,12 @@ describe('parent pointer array to tree', function() {
44
44
45
45
describe ( 'with incorrect arguments' , function ( ) {
46
46
it ( 'should return an empty array if the empty array passed' , function ( ) {
47
- expect ( toTree ( [ ] ) ) . to . be . deep . equal ( [ ] ) ;
47
+ expect ( toTree ( { data : [ ] } ) ) . to . be . deep . equal ( [ ] ) ;
48
48
} ) ;
49
49
50
50
it ( 'should throw an error if wrong arguments passed' , function ( ) {
51
- expect ( toTree . bind ( null , 'string' ) ) . to . throw ( / i n v a l i d a r g u m e n t / ) ;
52
- expect ( toTree . bind ( null , { } ) ) . to . throw ( / i n v a l i d a r g u m e n t / ) ;
51
+ expect ( toTree . bind ( null , { data : 'string' } ) ) . to . throw ( / i n v a l i d a r g u m e n t / ) ;
52
+ expect ( toTree . bind ( null , { data : { } } ) ) . to . throw ( / i n v a l i d a r g u m e n t / ) ;
53
53
} ) ;
54
54
55
55
} )
You can’t perform that action at this time.
0 commit comments