@@ -47,22 +47,28 @@ describe('object-path-set', function () {
47
47
expect ( obj . nested . foo ) . to . equal ( newValue ) ;
48
48
} ) ;
49
49
it ( 'should covert things to objects' , function ( ) {
50
- expect ( setPath ( 1234 , 'a' , 42 ) ) . to . deep . equal ( { a : 42 } ) ;
51
- expect ( setPath ( null , 'a' , 42 ) ) . to . deep . equal ( { a : 42 } ) ;
52
- expect ( setPath ( true , 'a' , 42 ) ) . to . deep . equal ( { a : 42 } ) ;
53
- expect ( setPath ( { a : 123 } , 'a.b' , 42 ) ) . to . deep . equal ( { a : { b : 42 } } ) ;
54
- expect ( setPath ( null , 'a.b.c.d' , null ) ) . to . deep . equal ( { a :{ b :{ c :{ d :null } } } } ) ;
50
+ expect ( setPath ( 1234 , 'a' , 42 ) ) . to . eql ( { a : 42 } ) ;
51
+ expect ( setPath ( null , 'a' , 42 ) ) . to . eql ( { a : 42 } ) ;
52
+ expect ( setPath ( true , 'a' , 42 ) ) . to . eql ( { a : 42 } ) ;
53
+ expect ( setPath ( { a : 123 } , 'a.b' , 42 ) ) . to . eql ( { a : { b : 42 } } ) ;
54
+ expect ( setPath ( null , 'a.b.c.d' , null ) ) . to . eql ( { a :{ b :{ c :{ d :null } } } } ) ;
55
55
} ) ;
56
56
it ( 'should be able to use custom delimiters' , function ( ) {
57
- expect ( setPath ( { } , 'a|b|c|d' , 42 ) ) . to . deep . equal ( { 'a|b|c|d' : 42 } ) ;
58
- expect ( setPath ( { } , 'a|b|c|d' , 42 , '|' ) ) . to . deep . equal ( { a :{ b :{ c :{ d :42 } } } } ) ;
59
- expect ( setPath ( { } , 'a.b.c.d' , 42 , '|' ) ) . to . deep . equal ( { 'a.b.c.d' : 42 } ) ;
57
+ expect ( setPath ( { } , 'a|b|c|d' , 42 ) ) . to . eql ( { 'a|b|c|d' : 42 } ) ;
58
+ expect ( setPath ( { } , 'a|b|c|d' , 42 , '|' ) ) . to . eql ( { a :{ b :{ c :{ d :42 } } } } ) ;
59
+ expect ( setPath ( { } , 'a.b.c.d' , 42 , '|' ) ) . to . eql ( { 'a.b.c.d' : 42 } ) ;
60
60
} ) ;
61
- it ( 'should set the correct values' , function ( ) {
62
- expect ( setPath ( { } , 'a.b' , 42 ) ) . to . deep . equal ( { a :{ b : 42 } } ) ;
63
- expect ( setPath ( { } , 'a.b' , undefined ) ) . to . deep . equal ( { a :{ b : undefined } } ) ;
64
- expect ( setPath ( { } , 'a.b' , true ) ) . to . deep . equal ( { a :{ b : true } } ) ;
65
- expect ( setPath ( { } , 'a.b' , 'wow' ) ) . to . deep . equal ( { a :{ b : 'wow' } } ) ;
61
+ it ( 'should set the correct values' , function ( ) {
62
+ expect ( setPath ( { } , 'a.b' , 42 ) ) . to . eql ( { a :{ b : 42 } } ) ;
63
+ expect ( setPath ( { } , 'a.b' , undefined ) ) . to . eql ( { a :{ b : undefined } } ) ;
64
+ expect ( setPath ( { } , 'a.b' , true ) ) . to . eql ( { a :{ b : true } } ) ;
65
+ expect ( setPath ( { } , 'a.b' , 'wow' ) ) . to . eql ( { a :{ b : 'wow' } } ) ;
66
+ } ) ;
67
+ it ( 'should handle arrays as paths' , function ( ) {
68
+ expect ( setPath ( { } , [ 'a' , 'b' ] , 42 ) ) . to . eql ( { a :{ b : 42 } } ) ;
69
+ expect ( setPath ( { } , [ 'a' , 'b' ] , undefined ) ) . to . eql ( { a :{ b : undefined } } ) ;
70
+ expect ( setPath ( { } , [ 'a' , 'b' ] , true ) ) . to . eql ( { a :{ b : true } } ) ;
71
+ expect ( setPath ( { } , [ 'a' , 'b' ] , 'wow' ) ) . to . eql ( { a :{ b : 'wow' } } ) ;
66
72
} ) ;
67
73
it ( 'should be able to be called multiple times' , function ( ) {
68
74
obj = { } ;
@@ -71,14 +77,22 @@ describe('object-path-set', function () {
71
77
obj = setPath ( obj , 'c.d' , { } ) ;
72
78
obj = setPath ( obj , 'c.d.e' , { } ) ;
73
79
obj = setPath ( obj , 'c.d.f' , 'foo' ) ;
74
- expect ( obj ) . to . deep . equal ( { a : 42 , b : true , c :{ d :{ e :{ } , f :'foo' } } } ) ;
80
+ expect ( obj ) . to . eql ( { a : 42 , b : true , c :{ d :{ e :{ } , f :'foo' } } } ) ;
75
81
} ) ;
76
- it ( 'should return the default object when key is not a string' , function ( ) {
82
+ it ( 'should return the default object when key is not a string or array ' , function ( ) {
77
83
var defaultValue = Math . random ( ) ;
78
- expect ( setPath ( obj , { } , defaultValue ) ) . to . deep . equal ( obj ) ;
79
- expect ( setPath ( obj , [ ] , defaultValue ) ) . to . deep . equal ( obj ) ;
80
- expect ( setPath ( obj , null , defaultValue ) ) . to . deep . equal ( obj ) ;
81
- expect ( setPath ( obj , 11 , defaultValue ) ) . to . deep . equal ( obj ) ;
82
- expect ( setPath ( obj , undefined , defaultValue ) ) . to . deep . equal ( obj ) ;
84
+ [ { } , null , 42 , undefined , true ] . forEach ( function ( path ) {
85
+ expect ( setPath ( getDefaultObject ( ) , path , defaultValue ) ) . to . eql ( getDefaultObject ( ) ) ;
86
+ } ) ;
83
87
} ) ;
88
+ it ( 'should return the default object when key is an empty array' , function ( ) {
89
+ var defaultValue = Math . random ( ) ;
90
+ expect ( setPath ( obj , [ ] , defaultValue ) ) . to . eql ( getDefaultObject ( ) ) ;
91
+ } ) ;
92
+ it ( 'should allow empty strings as a path' , function ( ) {
93
+ var defaultValue = Math . random ( ) ;
94
+ var obj2 = getDefaultObject ( ) ;
95
+ obj2 [ '' ] = defaultValue ;
96
+ expect ( setPath ( obj , '' , defaultValue ) ) . to . eql ( obj2 ) ;
97
+ } ) ;
84
98
} ) ;
0 commit comments