-
-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathutils.spec.ts
More file actions
959 lines (817 loc) · 27.9 KB
/
utils.spec.ts
File metadata and controls
959 lines (817 loc) · 27.9 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
import { describe, expect, expectTypeOf, it } from 'vitest'
import {
concatenatePaths,
createFieldMap,
deleteBy,
determineFieldLevelErrorSourceAndValue,
determineFormLevelErrorSourceAndValue,
evaluate,
getBy,
makePathArray,
mergeOpts,
setBy,
uuid,
} from '../src/index'
describe('getBy', () => {
const structure = {
name: 'Marc',
kids: [
{ name: 'Stephen', age: 10, hobbies: ['soccer', 'reading'] },
{ name: 'Taylor', age: 15, hobbies: ['swimming', 'gaming'] },
],
mother: {
name: 'Lisa',
},
siblings: [
{
name: undefined,
},
],
}
it('should get subfields by path', () => {
expect(getBy(structure, 'name')).toBe(structure.name)
expect(getBy(structure, 'mother.name')).toBe(structure.mother.name)
})
it('should get array subfields by path', () => {
expect(getBy(structure, 'kids[0].name')).toBe(structure.kids[0]!.name)
expect(getBy(structure, 'kids[0].age')).toBe(structure.kids[0]!.age)
})
it('should get nested array subfields by path', () => {
expect(getBy(structure, 'kids[0].hobbies[0]')).toBe(
structure.kids[0]!.hobbies[0],
)
expect(getBy(structure, 'kids[0].hobbies[1]')).toBe(
structure.kids[0]!.hobbies[1],
)
})
})
describe('setBy', () => {
const structure = {
name: 'Marc',
kids: [
{ name: 'Stephen', age: 10, hobbies: ['soccer', 'reading'] },
{ name: 'Taylor', age: 15, hobbies: ['swimming', 'gaming'] },
],
mother: {
name: 'Lisa',
},
}
it('should set subfields by path', () => {
expect(setBy(structure, 'name', 'Lisa').name).toBe('Lisa')
expect(setBy(structure, 'mother.name', 'Tina').mother.name).toBe('Tina')
})
it('should set array subfields by path', () => {
expect(setBy(structure, 'kids[0].name', 'Taylor').kids[0].name).toBe(
'Taylor',
)
expect(setBy(structure, 'kids[0].age', 20).kids[0].age).toBe(20)
})
it('should set nested array subfields by path', () => {
expect(
setBy(structure, 'kids[0].hobbies[0]', 'swimming').kids[0].hobbies[0],
).toBe('swimming')
expect(
setBy(structure, 'kids[0].hobbies[1]', 'gaming').kids[0].hobbies[1],
).toBe('gaming')
})
it("should create an object if it doesn't exist", () => {
expect(setBy(structure, 'father.name', 'John').father.name).toBe('John')
})
it('should create an array if it doesnt exist', () => {
expect(setBy(structure, 'kids[2].name', 'John').kids[2].name).toBe('John')
})
it('should set a value in an object if the key is a number but the parent is an object', () => {
const newStructure = setBy(structure, '5.name', 'John')
expect(newStructure['5'].name).toBe('John')
expect(newStructure).toStrictEqual({ ...structure, 5: { name: 'John' } })
})
it('should set a value in an array if the key is a number and the parent is an array', () => {
const newStructure = setBy(structure, 'kids.2.name', 'John')
expect(newStructure.kids[2].name).toBe('John')
expect(newStructure).toStrictEqual({
...structure,
kids: [...structure.kids, { name: 'John' }],
})
})
it('should preserve arrays when setting them within other arrays', () => {
const table: { field: { value: number }[][] } = {
field: [
[
{
value: 0,
},
{
value: 1,
},
],
[
{
value: 2,
},
],
],
}
const newTable = setBy(table, 'field[0][1].value', 2)
expect(newTable.field).toStrictEqual([
[
{
value: 0,
},
{
value: 2,
},
],
[
{
value: 2,
},
],
])
})
it('should correctly set a value on a key with leading zeros', () => {
const initial = { name: 'test' }
const result = setBy(initial, '01234', 'some-value')
expect(result).toHaveProperty('01234')
expect(result['01234']).toBe('some-value')
expect(result).not.toHaveProperty('1234')
})
})
describe('deleteBy', () => {
const structure = {
name: 'Marc',
kids: [
{ name: 'Stephen', age: 10, hobbies: ['soccer', 'reading'] },
{ name: 'Taylor', age: 15, hobbies: ['swimming', 'gaming'] },
],
mother: {
name: 'Lisa',
},
}
it('should delete subfields by path', () => {
expect(deleteBy(structure, 'name').name).not.toBeDefined()
expect(deleteBy(structure, 'mother.name').mother.name).not.toBeDefined()
})
it('should delete array subfields by path', () => {
expect(deleteBy(structure, 'kids[0].name').kids[0].name).not.toBeDefined()
expect(deleteBy(structure, 'kids[0].age').kids[0].age).not.toBeDefined()
})
it('should delete nested array subfields by path', () => {
expect(deleteBy(structure, 'kids[0].hobbies[0]').kids[0].hobbies[0]).toBe(
'reading',
)
expect(
deleteBy(structure, 'kids[0].hobbies[1]').kids[0].hobbies[1],
).not.toBeDefined()
})
it('should delete non-existent paths like a noop', () => {
expect(deleteBy(structure, 'nonexistent')).toEqual(structure)
expect(deleteBy(structure, 'nonexistent.nonexistent')).toEqual(structure)
expect(deleteBy(structure, 'kids[3].name')).toEqual(structure)
expect(deleteBy(structure, 'nonexistent[3].nonexistent')).toEqual(structure)
})
it('should now throw an error when deleting a path with numeric keys', () => {
const structure2 = {
123: 'a',
b: {
234: 'c',
},
d: {
456: {
e: 'f',
},
},
}
expect(() => deleteBy(structure2, '123')).not.toThrow()
expect(() => deleteBy(structure2, 'b.234')).not.toThrow()
expect(() => deleteBy(structure2, 'd.456.e')).not.toThrow()
})
})
describe('makePathArray', () => {
it('should convert chained property access', () => {
expect(makePathArray('a')).toEqual(['a'])
expect(makePathArray('a.b')).toEqual(['a', 'b'])
expect(makePathArray('foo.bar.baz')).toEqual(['foo', 'bar', 'baz'])
})
it('should convert property access followed by array indeces', () => {
expect(makePathArray('a[0]')).toEqual(['a', 0])
expect(makePathArray('foo[1]')).toEqual(['foo', 1])
expect(makePathArray('a.b[2]')).toEqual(['a', 'b', 2])
})
it('should convert chained array indeces', () => {
expect(makePathArray('a[0][1]')).toEqual(['a', 0, 1])
expect(makePathArray('foo[3][4][5]')).toEqual(['foo', 3, 4, 5])
})
it('should convert array indeces followed by property access', () => {
expect(makePathArray('a[0].b')).toEqual(['a', 0, 'b'])
expect(makePathArray('foo[1].bar')).toEqual(['foo', 1, 'bar'])
expect(makePathArray('[2].bar')).toEqual([2, 'bar'])
expect(makePathArray('[1][5].baz')).toEqual([1, 5, 'baz'])
})
it('should convert mixed chains of access', () => {
expect(makePathArray('a.b[0].c[1].d')).toEqual(['a', 'b', 0, 'c', 1, 'd'])
expect(makePathArray('x[0].y[1].z')).toEqual(['x', 0, 'y', 1, 'z'])
})
it('should handle deeply nested paths', () => {
expect(makePathArray('a.b[0][1].c.d[2][3].e')).toEqual([
'a',
'b',
0,
1,
'c',
'd',
2,
3,
'e',
])
})
it('should convert paths starting with multiple array indeces', () => {
expect(makePathArray('[0][1]')).toEqual([0, 1])
expect(makePathArray('[2][3].a')).toEqual([2, 3, 'a'])
expect(makePathArray('[4][5][6].b[7]')).toEqual([4, 5, 6, 'b', 7])
})
it('should preserve leading zeros on purely numeric strings', () => {
expect(makePathArray('01234')).toEqual(['01234'])
expect(makePathArray('007')).toEqual(['007'])
})
it('should still convert non-leading-zero numbers to number types', () => {
expect(makePathArray('12345')).toEqual([12345])
})
it('should treat lone "0" as the number 0', () => {
expect(makePathArray('0')).toEqual([0])
expect(makePathArray('a.0.b')).toEqual(['a', 0, 'b'])
})
it('should preserve leading zeros mid-path in both notations', () => {
expect(makePathArray('a.01.b')).toEqual(['a', '01', 'b'])
expect(makePathArray('a[01]')).toEqual(['a', '01'])
})
it('should return a defensive copy when given an array', () => {
const input: Array<string | number> = ['a', 0, 'b']
const out = makePathArray(input)
expect(out).toEqual(input)
expect(out).not.toBe(input)
})
it('should throw on non-string non-array input', () => {
expect(() => makePathArray(null as any)).toThrow('Path must be a string.')
expect(() => makePathArray(42 as any)).toThrow('Path must be a string.')
expect(() => makePathArray({} as any)).toThrow('Path must be a string.')
})
it('should handle malformed input', () => {
// Backwards compatible:
// Previous output: ['a', 'b']
expect(makePathArray('a..b')).toEqual(['a', 'b'])
// Previous output: ['a']
expect(makePathArray(']a')).toEqual(['a'])
// Previous output: ['a']
expect(makePathArray('a]')).toEqual(['a'])
// Previous output: ['a', 'b', 'c']
expect(makePathArray('a[b[c')).toEqual(['a', 'b', 'c'])
// Previous output: ['a', 'b', 'c']
expect(makePathArray('a[b[c]')).toEqual(['a', 'b', 'c'])
// Previous output: ['']
expect(makePathArray('')).toEqual([''])
// Previous output: ['', '']
expect(makePathArray('.')).toEqual(['', ''])
// Previous output: ['']
expect(makePathArray('[')).toEqual([''])
// Previous output: ['']
expect(makePathArray('[]')).toEqual([''])
// Previous output: ['', 'a']
expect(makePathArray('.a')).toEqual(['', 'a'])
// Previous output: ['a', '']
expect(makePathArray('a.')).toEqual(['a', ''])
// Previous output: ['a', '']
expect(makePathArray('a[')).toEqual(['a', ''])
// Previous output: ['', 'a']
expect(makePathArray('..a')).toEqual(['', 'a'])
// Previous output: ['a', '']
expect(makePathArray('a..')).toEqual(['a', ''])
// Previous output: ['a', '']
expect(makePathArray('a[[')).toEqual(['a', ''])
// Previous output: ['']
expect(makePathArray(']')).toEqual([''])
// Previous output: ['', '']
expect(makePathArray('[[')).toEqual(['', ''])
// Previous output: ['', 0]
expect(makePathArray('[[0]')).toEqual(['', 0])
// Breaking changes:
// This case is impossible to reproduce without allocating a new string that
// completely elides `]` or maintaining an array buffer in the loop to do so.
// Previous output: ['ab']
expect(makePathArray('a]b')).toEqual(['a', 'b'])
})
})
describe('determineFormLevelErrorSourceAndValue', () => {
describe('when a new form validator error exists', () => {
it('should return the new form error with source "form"', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: 'Form error',
isPreviousErrorFromFormValidator: false,
previousErrorValue: 'Field error',
})
expect(result).toEqual({
newErrorValue: 'Form error',
newSource: 'form',
})
})
it('should return the new form error even if previous error was from form', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: 'New form error',
isPreviousErrorFromFormValidator: true,
previousErrorValue: 'Old form error',
})
expect(result).toEqual({
newErrorValue: 'New form error',
newSource: 'form',
})
})
})
describe('when no new form validator error exists', () => {
it('should clear the error if previous error was from form validator', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: true,
previousErrorValue: 'Old form error',
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should clear the error if new error is null and previous error was from form', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: null,
isPreviousErrorFromFormValidator: true,
previousErrorValue: 'Old form error',
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should retain field error if previous error was from field', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: false,
previousErrorValue: 'Field error',
})
expect(result).toEqual({
newErrorValue: 'Field error',
newSource: 'field',
})
})
it('should retain field error if new error is null and previous error was from field', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: null,
isPreviousErrorFromFormValidator: false,
previousErrorValue: 'Field error',
})
expect(result).toEqual({
newErrorValue: 'Field error',
newSource: 'field',
})
})
it('should handle case when previous error is null', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: false,
previousErrorValue: undefined,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should handle case when previous error is undefined', () => {
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: false,
previousErrorValue: undefined,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
})
describe('edge cases', () => {
it('should handle complex previous error objects', () => {
const complexError = { message: 'Complex field error', code: 456 }
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: false,
previousErrorValue: complexError,
})
expect(result).toEqual({
newErrorValue: complexError,
newSource: 'field',
})
})
it('should treat falsy values as not errors', () => {
// This is consistent with current behavior as of v1.1.2
// Test with empty string
const result1 = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: true,
previousErrorValue: '',
})
expect(result1).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
// Test with 0
const result2 = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: true,
previousErrorValue: 0,
})
expect(result2).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
// Test with false
const result3 = determineFormLevelErrorSourceAndValue({
newFormValidatorError: undefined,
isPreviousErrorFromFormValidator: true,
previousErrorValue: false,
})
expect(result3).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should prioritize form validator errors over field errors', () => {
// Note that field level validation will prioritize field errors over form errors, this function is only used for form level validation
const result = determineFormLevelErrorSourceAndValue({
newFormValidatorError: 'Form error',
isPreviousErrorFromFormValidator: false,
previousErrorValue: 'Field error',
})
expect(result).toEqual({
newErrorValue: 'Form error',
newSource: 'form',
})
})
})
})
describe('determineFieldLevelErrorSourceAndValue', () => {
describe('when a field level error exists', () => {
it('should prioritize field error over form error', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: 'Field error',
formLevelError: 'Form error',
})
expect(result).toEqual({
newErrorValue: 'Field error',
newSource: 'field',
})
})
it('should return the field error when no form error exists', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: 'Field error',
formLevelError: undefined,
})
expect(result).toEqual({
newErrorValue: 'Field error',
newSource: 'field',
})
})
it('should handle complex field error objects', () => {
const complexError = { message: 'Complex field error', code: 123 }
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: complexError,
formLevelError: 'Form error',
})
expect(result).toEqual({
newErrorValue: complexError,
newSource: 'field',
})
})
})
describe('when no field level error exists', () => {
it('should use form error when field error is undefined', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: undefined,
formLevelError: 'Form error',
})
expect(result).toEqual({
newErrorValue: 'Form error',
newSource: 'form',
})
})
it('should use form error when field error is null', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: null,
formLevelError: 'Form error',
})
expect(result).toEqual({
newErrorValue: 'Form error',
newSource: 'form',
})
})
it('should handle complex form error objects', () => {
const complexError = { message: 'Complex form error', code: 789 }
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: undefined,
formLevelError: complexError,
})
expect(result).toEqual({
newErrorValue: complexError,
newSource: 'form',
})
})
})
describe('when neither field nor form level errors exist', () => {
it('should clear the error when both are undefined', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: undefined,
formLevelError: undefined,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should clear the error when both are null', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: null,
formLevelError: null,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
})
describe('edge cases', () => {
it('should treat empty string as not an error', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: '',
formLevelError: undefined,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should treat zero as not an error', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: 0,
formLevelError: undefined,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should treat false as not an error', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: false,
formLevelError: undefined,
})
expect(result).toEqual({
newErrorValue: undefined,
newSource: undefined,
})
})
it('should prioritize field level errors over form level errors', () => {
const result = determineFieldLevelErrorSourceAndValue({
fieldLevelError: 'Field error',
formLevelError: 'Form error',
})
expect(result).toEqual({
newErrorValue: 'Field error',
newSource: 'field',
})
})
})
})
describe('evaluate', () => {
it('should test equality between primitives', () => {
const numbersTrue = evaluate(1, 1)
expect(numbersTrue).toEqual(true)
const stringFalse = evaluate('uh oh', '')
expect(stringFalse).toEqual(false)
const boolTrue = evaluate(true, true)
expect(boolTrue).toEqual(true)
const nullFalse = evaluate(null, {})
expect(nullFalse).toEqual(false)
const undefinedFalse = evaluate(undefined, null)
expect(undefinedFalse).toEqual(false)
})
it('should test equality between arrays', () => {
const arrayTrue = evaluate([], [])
expect(arrayTrue).toEqual(true)
const arrayDeepSearchTrue = evaluate([[1]], [[1]])
expect(arrayDeepSearchTrue).toEqual(true)
const arrayFalse = evaluate([], [''])
expect(arrayFalse).toEqual(false)
const arrayDeepFalse = evaluate([[1]], [])
expect(arrayDeepFalse).toEqual(false)
const arrayComplexFalse = evaluate([[{ test: 'true' }], null], [[1], {}])
expect(arrayComplexFalse).toEqual(false)
const arrayComplexTrue = evaluate(
[[{ test: 'true' }], null],
[[{ test: 'true' }], null],
)
expect(arrayComplexTrue).toEqual(true)
})
it('should test equality between objects', () => {
const objTrue = evaluate({ test: 'same' }, { test: 'same' })
expect(objTrue).toEqual(true)
const objFalse = evaluate({ test: 'not' }, { test: 'same' })
expect(objFalse).toEqual(false)
const objDeepFalse = evaluate({ test: 'not' }, { test: { test: 'same' } })
expect(objDeepFalse).toEqual(false)
const objDeepArrFalse = evaluate({ test: [] }, { test: [[]] })
expect(objDeepArrFalse).toEqual(false)
const objNullFalse = evaluate({ test: '' }, null)
expect(objNullFalse).toEqual(false)
const objComplexFalse = evaluate(
{ test: { testTwo: '' }, arr: [[1]] },
{ test: { testTwo: false }, arr: [[1], [0]] },
)
expect(objComplexFalse).toEqual(false)
const objComplexTrue = evaluate(
{ test: { testTwo: '' }, arr: [[1]] },
{ test: { testTwo: '' }, arr: [[1]] },
)
expect(objComplexTrue).toEqual(true)
})
it('should test equality between Date objects', () => {
const date1 = new Date('2025-01-01T00:00:00.000Z')
const date2 = new Date('2025-01-01T00:00:00.000Z')
const date3 = new Date('2025-01-02T00:00:00.000Z')
const dateTrue = evaluate(date1, date2)
expect(dateTrue).toEqual(true)
const dateFalse = evaluate(date1, date3)
expect(dateFalse).toEqual(false)
const dateObjectTrue = evaluate({ date: date1 }, { date: date2 })
expect(dateObjectTrue).toEqual(true)
const dateObjectFalse = evaluate({ date: date1 }, { date: date3 })
expect(dateObjectFalse).toEqual(false)
})
it('should test equality between Map objects', () => {
const map1 = new Map([
['a', 1],
['b', 2],
])
const map2 = new Map([
['a', 1],
['b', 2],
])
expect(evaluate(map1, map2)).toEqual(true)
const emptyMap1 = new Map()
const emptyMap2 = new Map()
expect(evaluate(emptyMap1, emptyMap2)).toEqual(true)
const mapSmall = new Map([['a', 1]])
const mapLarge = new Map([
['a', 1],
['b', 2],
])
expect(evaluate(mapSmall, mapLarge)).toEqual(false)
const mapA = new Map([
['a', 1],
['b', 2],
])
const mapC = new Map([
['a', 1],
['c', 2],
])
expect(evaluate(mapA, mapC)).toEqual(false)
const mapVal1 = new Map([
['a', 1],
['b', 2],
])
const mapVal2 = new Map([
['a', 1],
['b', 3],
])
expect(evaluate(mapVal1, mapVal2)).toEqual(false)
})
it('should test equality between Set objects', () => {
const set1 = new Set([1, 2, 3])
const set2 = new Set([1, 2, 3])
expect(evaluate(set1, set2)).toEqual(true)
const emptySet1 = new Set()
const emptySet2 = new Set()
expect(evaluate(emptySet1, emptySet2)).toEqual(true)
const setSmall = new Set([1, 2])
const setLarge = new Set([1, 2, 3])
expect(evaluate(setSmall, setLarge)).toEqual(false)
const setA = new Set([1, 2, 3])
const setB = new Set([1, 2, 4])
expect(evaluate(setA, setB)).toEqual(false)
})
})
describe('concatenatePaths', () => {
it('should concatenate two object accessors with dot', () => {
expect(concatenatePaths('user', 'name')).toBe('user.name')
})
it('should join array accessor and object path directly', () => {
expect(concatenatePaths('users', '[0]')).toBe('users[0]')
})
it('should join object accessor after array accessor with dot', () => {
expect(concatenatePaths('users[0]', 'name')).toBe('users[0].name')
})
it('should append array accessor after array accessor directly', () => {
expect(concatenatePaths('users[0]', '[1]')).toBe('users[0][1]')
})
it('should join object accessor after object accessor with dot', () => {
expect(concatenatePaths('profile', 'settings.theme')).toBe(
'profile.settings.theme',
)
expect(concatenatePaths('settings.theme', 'profile')).toBe(
'settings.theme.profile',
)
})
it('should handle empty paths', () => {
expect(concatenatePaths('', 'name')).toBe('name')
expect(concatenatePaths('user', '')).toBe('user')
expect(concatenatePaths('', '')).toBe('')
})
it('should handle complex nesting with array and object accessors', () => {
expect(concatenatePaths('data[0].items[2]', 'value')).toBe(
'data[0].items[2].value',
)
expect(concatenatePaths('data', '[1].value')).toBe('data[1].value')
})
it('should not duplicate dots if the second path starts with one', () => {
expect(concatenatePaths('foo', '.bar')).toBe('foo.bar')
})
})
describe('createFieldMap', () => {
it('should return an empty object when given an empty object', () => {
const result = createFieldMap({})
expect(result).toEqual({})
expectTypeOf(result).toEqualTypeOf<{}>()
})
it('should map each key to its own name as a string', () => {
const input = { a: 1, b: 2 }
const result = createFieldMap(input)
expect(result).toEqual({ a: 'a', b: 'b' })
expectTypeOf(result).toEqualTypeOf<{ a: 'a'; b: 'b' }>()
})
it('should handle keys with special characters or numbers', () => {
const input = { '1key': 42, 'space key': 'x' }
const result = createFieldMap(input)
expect(result).toEqual({ '1key': '1key', 'space key': 'space key' })
expectTypeOf(result).toEqualTypeOf<{
'1key': '1key'
'space key': 'space key'
}>()
})
it('should not mutate the input object', () => {
const input = { a: 1 }
const copy = { ...input }
createFieldMap(input)
expect(input).toEqual(copy)
})
})
describe('mergeOpts', () => {
type SomeOpts = {
foo?: string
bar?: boolean
}
it('should return the overrides if original object is undefined', () => {
expect(mergeOpts<SomeOpts>(undefined, { foo: 'test' })).toEqual({
foo: 'test',
})
expect(mergeOpts<SomeOpts>(null, { foo: 'test' })).toEqual({ foo: 'test' })
})
it('should preserve properties that were not overwritten', () => {
const original: SomeOpts = {
foo: 'test',
}
expect(mergeOpts(original, { bar: true })).toEqual({
foo: 'test',
bar: true,
})
expect(mergeOpts(original, {})).toEqual({ foo: 'test' })
})
})
describe('uuid', () => {
it('should return a string', () => {
const id = uuid()
expect(typeof id).toBe('string')
})
it('should match UUID v4 format', () => {
const id = uuid()
const uuidV4Regex =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
expect(id).toMatch(uuidV4Regex)
})
it('should generate different values on multiple calls', () => {
const ids = new Set(Array.from({ length: 100 }, () => uuid()))
expect(ids.size).toBe(100)
})
it('should always produce 36 characters', () => {
const id = uuid()
expect(id.length).toBe(36)
})
it('should set correct version (4) and variant bits', () => {
const id = uuid()
const parts = id.split('-')
expect(parts[2]?.[0]).toBe('4')
expect(['8', '9', 'a', 'b']).toContain(parts[3]?.[0])
})
})