File tree Expand file tree Collapse file tree 2 files changed +11
-7
lines changed
Expand file tree Collapse file tree 2 files changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -33,11 +33,15 @@ function orderByParents(list) {
3333 return parents ;
3434}
3535
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 ) ) {
3841 throw new Error ( 'Expected an object but got an invalid argument' ) ;
3942 }
40- var cloned = obj . slice ( ) ;
43+
44+ var cloned = data . slice ( ) ;
4145 var ordered = orderByParents ( cloned ) ;
4246 return createTree ( ordered , ordered [ 0 ] ) ;
4347} ;
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ describe('parent pointer array to tree', function() {
1010 describe ( 'expected behavior' , function ( ) {
1111
1212 before ( function ( ) {
13- current = toTree ( initial ) ;
13+ current = toTree ( { data : initial } ) ;
1414 } ) ;
1515
1616 it ( 'should not modify passed object' , function ( ) {
@@ -44,12 +44,12 @@ describe('parent pointer array to tree', function() {
4444
4545 describe ( 'with incorrect arguments' , function ( ) {
4646 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 ( [ ] ) ;
4848 } ) ;
4949
5050 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 / ) ;
5353 } ) ;
5454
5555 } )
You can’t perform that action at this time.
0 commit comments