deno.land / x / rambda@v9.1.1 / source / assocPath.spec.js

assocPath.spec.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { assocPathFn } from './assocPath.js'
test.only('happy', () => { const path = 'a.c.1' const input = { a : { b : 1, c : [ 1, 2 ], }, } assocPathFn( path, 3, input ) expect(input).toEqual({ a : { b : 1, c : [ 1, 2 ], }, })})
test('string can be used as path input', () => { const testObj = { a : [ { b : 1 }, { b : 2 } ], d : 3, } const result1 = assocPathFn( [ 'a', 0, 'b' ], 10, testObj ) const result2 = assocPathFn( 'a.0.b', 10, testObj )
const expected = { a : [ { b : 10 }, { b : 2 } ], d : 3, } expect(result1).toEqual(expected) expect(result2).toEqual(expected)})
test('difference with ramda - doesn\'t overwrite primitive values with keys in the path', () => { const obj = { a : 'str' } const result = assocPath( [ 'a', 'b' ], 42, obj )
expect(result).toEqual({ a : { 0 : 's', 1 : 't', 2 : 'r', b : 42, }, })})
test('bug', () => { /* https://github.com/selfrefactor/rambda/issues/524 */ const state = {}
const withDateLike = assocPath( [ 'outerProp', '2020-03-10' ], { prop : 2 }, state ) const withNumber = assocPath( [ 'outerProp', '5' ], { prop : 2 }, state )
const withDateLikeExpected = { outerProp : { '2020-03-10' : { prop : 2 } } } const withNumberExpected = { outerProp : { 5 : { prop : 2 } } } expect(withDateLike).toEqual(withDateLikeExpected) expect(withNumber).toEqual(withNumberExpected)})
test('adds a key to an empty object', () => { expect(assocPath( [ 'a' ], 1, {} )).toEqual({ a : 1 })})
test('adds a key to a non-empty object', () => { expect(assocPath( 'b', 2, { a : 1 } )).toEqual({ a : 1, b : 2, })})
test('adds a nested key to a non-empty object', () => { expect(assocPath( 'b.c', 2, { a : 1 } )).toEqual({ a : 1, b : { c : 2 }, })})
test('adds a nested key to a nested non-empty object - curry case 1', () => { expect(assocPath('b.d', 3)({ a : 1, b : { c : 2 }, })).toEqual({ a : 1, b : { c : 2, d : 3, }, })})
test('adds a key to a non-empty object - curry case 1', () => { expect(assocPath('b', 2)({ a : 1 })).toEqual({ a : 1, b : 2, })})
test('adds a nested key to a non-empty object - curry case 1', () => { expect(assocPath('b.c', 2)({ a : 1 })).toEqual({ a : 1, b : { c : 2 }, })})
test('adds a key to a non-empty object - curry case 2', () => { expect(assocPath('b')(2, { a : 1 })).toEqual({ a : 1, b : 2, })})
test('adds a key to a non-empty object - curry case 3', () => { const result = assocPath('b')(2)({ a : 1 })
expect(result).toEqual({ a : 1, b : 2, })})
test('changes an existing key', () => { expect(assocPath( 'a', 2, { a : 1 } )).toEqual({ a : 2 })})
test('undefined is considered an empty object', () => { expect(assocPath( 'a', 1, undefined )).toEqual({ a : 1 })})
test('null is considered an empty object', () => { expect(assocPath( 'a', 1, null )).toEqual({ a : 1 })})
test('value can be null', () => { expect(assocPath( 'a', null, null )).toEqual({ a : null })})
test('value can be undefined', () => { expect(assocPath( 'a', undefined, null )).toEqual({ a : undefined })})
test('assignment is shallow', () => { expect(assocPath( 'a', { b : 2 }, { a : { c : 3 } } )).toEqual({ a : { b : 2 } })})
test('empty array as path', () => { const result = assocPath( [], 3, { a : 1, b : 2, } ) expect(result).toBe(3)})
test('happy', () => { const expected = { foo : { bar : { baz : 42 } } } const result = assocPath( [ 'foo', 'bar', 'baz' ], 42, { foo : null } ) expect(result).toEqual(expected)})
rambda

Version Info

Tagged at
2 months ago