2
2
var setPath = require ( './index.js' ) ;
3
3
var now = new Date ( ) ;
4
4
var obj ;
5
- var getDefaultObject = function ( ) {
5
+ var getDefaultObject = function ( ) {
6
6
return {
7
7
nested : {
8
8
thing : {
@@ -21,11 +21,11 @@ var getDefaultObject = function() {
21
21
} ;
22
22
} ;
23
23
24
- describe ( 'object-path-set' , function ( ) {
25
- beforeEach ( function ( ) {
24
+ describe ( 'object-path-set' , function ( ) {
25
+ beforeEach ( function ( ) {
26
26
obj = getDefaultObject ( ) ;
27
27
} ) ;
28
- it ( 'should be able to set and overwrite types' , function ( ) {
28
+ it ( 'should be able to set and overwrite types' , function ( ) {
29
29
var newValue = 'newValue' ;
30
30
31
31
obj = setPath ( obj , 'dataUndefined' , newValue ) ;
@@ -45,7 +45,7 @@ describe('object-path-set', function() {
45
45
expect ( typeof obj . nested . foo ) . toBe ( 'string' ) ;
46
46
expect ( obj . nested . foo ) . toBe ( newValue ) ;
47
47
} ) ;
48
- it ( 'should covert things to objects' , function ( ) {
48
+ it ( 'should covert things to objects' , function ( ) {
49
49
expect ( setPath ( 1234 , 'a' , 42 ) ) . toEqual ( { a : 42 } ) ;
50
50
expect ( setPath ( null , 'a' , 42 ) ) . toEqual ( { a : 42 } ) ;
51
51
expect ( setPath ( true , 'a' , 42 ) ) . toEqual ( { a : 42 } ) ;
@@ -54,26 +54,26 @@ describe('object-path-set', function() {
54
54
a : { b : { c : { d : null } } }
55
55
} ) ;
56
56
} ) ;
57
- it ( 'should be able to use custom delimiters' , function ( ) {
57
+ it ( 'should be able to use custom delimiters' , function ( ) {
58
58
expect ( setPath ( { } , 'a|b|c|d' , 42 ) ) . toEqual ( { 'a|b|c|d' : 42 } ) ;
59
59
expect ( setPath ( { } , 'a|b|c|d' , 42 , '|' ) ) . toEqual ( {
60
60
a : { b : { c : { d : 42 } } }
61
61
} ) ;
62
62
expect ( setPath ( { } , 'a.b.c.d' , 42 , '|' ) ) . toEqual ( { 'a.b.c.d' : 42 } ) ;
63
63
} ) ;
64
- it ( 'should set the correct values' , function ( ) {
64
+ it ( 'should set the correct values' , function ( ) {
65
65
expect ( setPath ( { } , 'a.b' , 42 ) ) . toEqual ( { a : { b : 42 } } ) ;
66
66
expect ( setPath ( { } , 'a.b' , undefined ) ) . toEqual ( { a : { b : undefined } } ) ;
67
67
expect ( setPath ( { } , 'a.b' , true ) ) . toEqual ( { a : { b : true } } ) ;
68
68
expect ( setPath ( { } , 'a.b' , 'wow' ) ) . toEqual ( { a : { b : 'wow' } } ) ;
69
69
} ) ;
70
- it ( 'should handle arrays as paths' , function ( ) {
70
+ it ( 'should handle arrays as paths' , function ( ) {
71
71
expect ( setPath ( { } , [ 'a' , 'b' ] , 42 ) ) . toEqual ( { a : { b : 42 } } ) ;
72
72
expect ( setPath ( { } , [ 'a' , 'b' ] , undefined ) ) . toEqual ( { a : { c : undefined } } ) ;
73
73
expect ( setPath ( { } , [ 'a' , 'b' ] , true ) ) . toEqual ( { a : { b : true } } ) ;
74
74
expect ( setPath ( { } , [ 'a' , 'b' ] , 'wow' ) ) . toEqual ( { a : { b : 'wow' } } ) ;
75
75
} ) ;
76
- it ( 'should be able to be called multiple times' , function ( ) {
76
+ it ( 'should be able to be called multiple times' , function ( ) {
77
77
obj = { } ;
78
78
obj = setPath ( obj , 'a' , 42 ) ;
79
79
obj = setPath ( obj , 'b' , true ) ;
@@ -82,41 +82,41 @@ describe('object-path-set', function() {
82
82
obj = setPath ( obj , 'c.d.f' , 'foo' ) ;
83
83
expect ( obj ) . toEqual ( { a : 42 , b : true , c : { d : { e : { } , f : 'foo' } } } ) ;
84
84
} ) ;
85
- it ( 'should return the default object when key is not a string or array' , function ( ) {
85
+ it ( 'should return the default object when key is not a string or array' , function ( ) {
86
86
var defaultValue = Math . random ( ) ;
87
- [ { } , null , 42 , undefined , true ] . forEach ( function ( path ) {
87
+ [ { } , null , 42 , undefined , true ] . forEach ( function ( path ) {
88
88
expect ( setPath ( getDefaultObject ( ) , path , defaultValue ) ) . toEqual (
89
89
getDefaultObject ( )
90
90
) ;
91
91
} ) ;
92
92
} ) ;
93
- it ( 'should return the default object when key is an empty array' , function ( ) {
93
+ it ( 'should return the default object when key is an empty array' , function ( ) {
94
94
var defaultValue = Math . random ( ) ;
95
95
expect ( setPath ( obj , [ ] , defaultValue ) ) . toEqual ( getDefaultObject ( ) ) ;
96
96
} ) ;
97
- it ( 'should allow empty strings as a path' , function ( ) {
97
+ it ( 'should allow empty strings as a path' , function ( ) {
98
98
var defaultValue = Math . random ( ) ;
99
99
var obj2 = getDefaultObject ( ) ;
100
100
obj2 [ '' ] = defaultValue ;
101
101
expect ( setPath ( obj , '' , defaultValue ) ) . toEqual ( obj2 ) ;
102
102
} ) ;
103
- it ( 'should not pollute __proto__' , function ( ) {
103
+ it ( 'should not pollute __proto__' , function ( ) {
104
104
var obj = { } ;
105
105
expect ( obj . polluted ) . toBeUndefined ( ) ;
106
106
setPath ( obj , '__proto__.polluted' , 'yes' ) ;
107
107
var obj2 = { } ;
108
108
expect ( obj . polluted ) . toBeUndefined ( ) ;
109
109
expect ( obj2 . polluted ) . toBeUndefined ( ) ;
110
110
} ) ;
111
- it ( 'should not pollute constructor' , function ( ) {
111
+ it ( 'should not pollute constructor' , function ( ) {
112
112
var obj = { } ;
113
113
expect ( obj . polluted ) . toBeUndefined ( ) ;
114
114
setPath ( obj , 'constructor.polluted' , 'yes' ) ;
115
115
var obj2 = { } ;
116
116
expect ( obj . polluted ) . toBeUndefined ( ) ;
117
117
expect ( obj2 . polluted ) . toBeUndefined ( ) ;
118
118
} ) ;
119
- it ( 'should not pollute prototype' , function ( ) {
119
+ it ( 'should not pollute prototype' , function ( ) {
120
120
var obj = { } ;
121
121
expect ( obj . polluted ) . toBeUndefined ( ) ;
122
122
setPath ( obj , 'prototype.polluted' , 'yes' ) ;
0 commit comments