@@ -96,6 +96,43 @@ describe('setBy', () => {
96
96
kids : [ ...structure . kids , { name : 'John' } ] ,
97
97
} )
98
98
} )
99
+
100
+ it ( 'should preserve arrays when setting them within other arrays' , ( ) => {
101
+ const table : { field : { value : number } [ ] [ ] } = {
102
+ field : [
103
+ [
104
+ {
105
+ value : 0 ,
106
+ } ,
107
+ {
108
+ value : 1 ,
109
+ } ,
110
+ ] ,
111
+ [
112
+ {
113
+ value : 2 ,
114
+ } ,
115
+ ] ,
116
+ ] ,
117
+ }
118
+
119
+ const newTable = setBy ( table , 'field[0][1].value' , 2 )
120
+ expect ( newTable . field ) . toStrictEqual ( [
121
+ [
122
+ {
123
+ value : 0 ,
124
+ } ,
125
+ {
126
+ value : 2 ,
127
+ } ,
128
+ ] ,
129
+ [
130
+ {
131
+ value : 2 ,
132
+ } ,
133
+ ] ,
134
+ ] )
135
+ } )
99
136
} )
100
137
101
138
describe ( 'deleteBy' , ( ) => {
@@ -138,27 +175,54 @@ describe('deleteBy', () => {
138
175
} )
139
176
140
177
describe ( 'makePathArray' , ( ) => {
141
- it ( 'should convert dot notation to array' , ( ) => {
142
- expect ( makePathArray ( 'name' ) ) . toEqual ( [ 'name' ] )
143
- expect ( makePathArray ( 'mother.name' ) ) . toEqual ( [ 'mother' , 'name' ] )
144
- expect ( makePathArray ( 'kids[0].name' ) ) . toEqual ( [ 'kids' , 0 , 'name' ] )
145
- expect ( makePathArray ( 'kids[0].name[1]' ) ) . toEqual ( [ 'kids' , 0 , 'name' , 1 ] )
146
- expect ( makePathArray ( 'kids[0].name[1].age' ) ) . toEqual ( [
147
- 'kids' ,
148
- 0 ,
149
- 'name' ,
150
- 1 ,
151
- 'age' ,
152
- ] )
153
- expect ( makePathArray ( 'kids[0].name[1].age[2]' ) ) . toEqual ( [
154
- 'kids' ,
178
+ it ( 'should convert chained property access' , ( ) => {
179
+ expect ( makePathArray ( 'a' ) ) . toEqual ( [ 'a' ] )
180
+ expect ( makePathArray ( 'a.b' ) ) . toEqual ( [ 'a' , 'b' ] )
181
+ expect ( makePathArray ( 'foo.bar.baz' ) ) . toEqual ( [ 'foo' , 'bar' , 'baz' ] )
182
+ } )
183
+
184
+ it ( 'should convert property access followed by array indeces' , ( ) => {
185
+ expect ( makePathArray ( 'a[0]' ) ) . toEqual ( [ 'a' , 0 ] )
186
+ expect ( makePathArray ( 'foo[1]' ) ) . toEqual ( [ 'foo' , 1 ] )
187
+ expect ( makePathArray ( 'a.b[2]' ) ) . toEqual ( [ 'a' , 'b' , 2 ] )
188
+ } )
189
+
190
+ it ( 'should convert chained array indeces' , ( ) => {
191
+ expect ( makePathArray ( 'a[0][1]' ) ) . toEqual ( [ 'a' , 0 , 1 ] )
192
+ expect ( makePathArray ( 'foo[3][4][5]' ) ) . toEqual ( [ 'foo' , 3 , 4 , 5 ] )
193
+ } )
194
+
195
+ it ( 'should convert array indeces followed by property access' , ( ) => {
196
+ expect ( makePathArray ( 'a[0].b' ) ) . toEqual ( [ 'a' , 0 , 'b' ] )
197
+ expect ( makePathArray ( 'foo[1].bar' ) ) . toEqual ( [ 'foo' , 1 , 'bar' ] )
198
+ expect ( makePathArray ( '[2].bar' ) ) . toEqual ( [ 2 , 'bar' ] )
199
+ expect ( makePathArray ( '[1][5].baz' ) ) . toEqual ( [ 1 , 5 , 'baz' ] )
200
+ } )
201
+
202
+ it ( 'should convert mixed chains of access' , ( ) => {
203
+ expect ( makePathArray ( 'a.b[0].c[1].d' ) ) . toEqual ( [ 'a' , 'b' , 0 , 'c' , 1 , 'd' ] )
204
+ expect ( makePathArray ( 'x[0].y[1].z' ) ) . toEqual ( [ 'x' , 0 , 'y' , 1 , 'z' ] )
205
+ } )
206
+
207
+ it ( 'should handle deeply nested paths' , ( ) => {
208
+ expect ( makePathArray ( 'a.b[0][1].c.d[2][3].e' ) ) . toEqual ( [
209
+ 'a' ,
210
+ 'b' ,
155
211
0 ,
156
- 'name' ,
157
212
1 ,
158
- 'age' ,
213
+ 'c' ,
214
+ 'd' ,
159
215
2 ,
216
+ 3 ,
217
+ 'e' ,
160
218
] )
161
219
} )
220
+
221
+ it ( 'should convert paths starting with multiple array indeces' , ( ) => {
222
+ expect ( makePathArray ( '[0][1]' ) ) . toEqual ( [ 0 , 1 ] )
223
+ expect ( makePathArray ( '[2][3].a' ) ) . toEqual ( [ 2 , 3 , 'a' ] )
224
+ expect ( makePathArray ( '[4][5][6].b[7]' ) ) . toEqual ( [ 4 , 5 , 6 , 'b' , 7 ] )
225
+ } )
162
226
} )
163
227
164
228
describe ( 'determineFormLevelErrorSourceAndValue' , ( ) => {
0 commit comments