-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest.js
More file actions
1852 lines (1537 loc) · 55.7 KB
/
Copy pathtest.js
File metadata and controls
1852 lines (1537 loc) · 55.7 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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import test from 'ava';
import {
getProperty,
setProperty,
hasProperty,
deleteProperty,
escapePath,
deepKeys,
unflatten,
parsePath,
stringifyPath,
} from './index.js';
test('getProperty', t => {
const fixture1 = {foo: {bar: 1}};
t.is(getProperty(fixture1), fixture1);
fixture1[''] = 'foo';
t.is(getProperty(fixture1, ''), 'foo');
t.is(getProperty(fixture1, 'foo'), fixture1.foo);
t.is(getProperty({foo: 1}, 'foo'), 1);
t.is(getProperty({foo: null}, 'foo'), null);
t.is(getProperty({foo: undefined}, 'foo'), undefined);
t.true(getProperty({foo: {bar: true}}, 'foo.bar'));
t.true(getProperty({foo: {bar: {baz: true}}}, 'foo.bar.baz'));
t.is(getProperty({foo: {bar: {baz: null}}}, 'foo.bar.baz'), null);
t.is(getProperty({foo: {bar: 'a'}}, 'foo.fake'), undefined);
t.is(getProperty({foo: {bar: 'a'}}, 'foo.fake.fake2'), undefined);
t.is(getProperty({foo: {bar: 'a'}}, 'foo.fake.fake2', 'some value'), 'some value');
t.is(getProperty({foo: {}}, 'foo.fake', 'some value'), 'some value');
t.true(getProperty({'\\': true}, '\\'));
t.true(getProperty({foo: true}, String.raw`\foo`)); // \f is escaped to f
t.true(getProperty({'\\foo': true}, String.raw`\\foo`)); // \\f is escaped backslash + f
t.true(getProperty({'foo\\': true}, 'foo\\\\'));
t.true(getProperty({'bar\\': true}, 'bar\\'));
t.true(getProperty({foobar: true}, String.raw`foo\bar`)); // \b is escaped to b
t.true(getProperty({'\\': {foo: true}}, String.raw`\\.foo`));
t.true(getProperty({'bar\\.': true}, String.raw`bar\\\.`));
t.true(getProperty({
'foo\\': {
bar: true,
},
}, String.raw`foo\\.bar`));
t.is(getProperty({foo: 1}, 'foo.bar'), undefined);
const fixture2 = {};
Object.defineProperty(fixture2, 'foo', {
value: 'bar',
enumerable: false,
});
t.is(getProperty(fixture2, 'foo'), 'bar');
t.is(getProperty({}, 'hasOwnProperty'), Object.prototype.hasOwnProperty);
function function_() {}
function_.foo = {bar: 1};
t.is(getProperty(function_), function_);
t.is(getProperty(function_, 'foo'), function_.foo);
t.is(getProperty(function_, 'foo.bar'), 1);
const f3 = {foo: null};
t.is(getProperty(f3, 'foo.bar'), undefined);
t.is(getProperty(f3, 'foo.bar', 'some value'), 'some value');
t.true(getProperty({'foo.baz': {bar: true}}, String.raw`foo\.baz.bar`));
t.true(getProperty({'fo.ob.az': {bar: true}}, String.raw`fo\.ob\.az.bar`));
t.false(getProperty(null, 'foo.bar', false));
t.false(getProperty('foo', 'foo.bar', false));
t.false(getProperty([], 'foo.bar', false));
t.false(getProperty(undefined, 'foo.bar', false));
class F4Class {}
F4Class.prototype.foo = 1;
const f4 = new F4Class();
t.is(getProperty(f4, 'foo'), 1); // #46
t.true(getProperty({'': {'': {'': true}}}, '..'));
t.true(getProperty({'': {'': true}}, '.'));
});
test('getProperty - with array indexes', t => {
t.true(getProperty([true, false, false], '[0]'));
t.true(getProperty([[false, true, false], false, false], '[0][1]'));
t.true(getProperty([{foo: [true]}], '[0].foo[0]'));
t.true(getProperty({foo: [0, {bar: true}]}, 'foo[1].bar'));
t.false(getProperty(['a', 'b', 'c'], '[3]', false));
t.false(getProperty([{foo: [1]}], '[0].bar[0]', false));
t.false(getProperty([{foo: [1]}], '[0].foo[1]', false));
t.false(getProperty({foo: [0, {bar: 2}]}, 'foo[0].bar', false));
t.false(getProperty({foo: [0, {bar: 2}]}, 'foo[2].bar', false));
t.false(getProperty({foo: [0, {bar: 2}]}, 'foo[1].biz', false));
t.false(getProperty({foo: [0, {bar: 2}]}, 'bar[0].bar', false));
t.true(getProperty({
bar: {
'[0]': true,
},
}, String.raw`bar.\[0]`));
t.true(getProperty({
bar: {
'': [true],
},
}, 'bar.[0]'));
t.throws(() => getProperty({
'foo[5[': true,
}, 'foo[5['), {
message: 'Invalid character \'[\' in an index at position 6',
});
t.throws(() => getProperty({
'foo[5': {
bar: true,
},
}, 'foo[5.bar'), {
message: 'Invalid character \'.\' in an index at position 6',
});
t.true(getProperty({
'foo[5]': {
bar: true,
},
}, String.raw`foo\[5].bar`));
t.throws(() => getProperty({
'foo[5\\]': {
bar: true,
},
}, String.raw`foo[5\].bar`), {
message: String.raw`Invalid character '\' in an index at position 6`,
});
t.throws(() => getProperty({
'foo[5': true,
}, 'foo[5'), {
message: 'Index was not closed',
});
t.throws(() => getProperty({
'foo[bar]': true,
}, 'foo[bar]'), {
message: 'Invalid character \'b\' in an index at position 5',
});
t.false(getProperty({}, 'constructor[0]', false));
t.throws(() => getProperty({}, 'foo[constructor]'), {
message: 'Invalid character \'c\' in an index at position 5',
});
t.false(getProperty([], 'foo[0].bar', false));
t.true(getProperty({foo: [{bar: true}]}, 'foo[0].bar'));
t.false(getProperty({foo: ['bar']}, 'foo[1]', false));
t.true(getProperty([true], '0', false));
t.true(getProperty({foo: [true]}, 'foo.0'));
t.true(getProperty({
foo: {
0: true,
},
}, 'foo.0'));
t.true(getProperty([{
'[1]': true,
}, false, false], String.raw`[0].\[1]`));
t.true(getProperty({foo: {'[0]': true}}, String.raw`foo.\[0]`));
t.throws(() => getProperty({foo: {'[0]': true}}, String.raw`foo.[0\]`), {
message: String.raw`Invalid character '\' in an index at position 7`,
});
t.true(getProperty({foo: {'\\': [true]}}, String.raw`foo.\\[0]`));
t.throws(() => getProperty({foo: {'[0]': true}}, String.raw`foo.[0\]`), {
message: String.raw`Invalid character '\' in an index at position 7`,
});
t.throws(() => getProperty({'foo[0': {'9]': true}}, 'foo[0.9]'), {
message: 'Invalid character \'.\' in an index at position 6',
});
t.throws(() => getProperty({'foo[-1]': true}, 'foo[-1]'), {
message: 'Invalid character \'-\' in an index at position 5',
});
});
test('setProperty', t => {
const function_ = () => 'test';
let fixture1 = {};
const o1 = setProperty(fixture1, 'foo', 2);
t.is(fixture1.foo, 2);
t.is(o1, fixture1);
fixture1 = {foo: {bar: 1}};
setProperty(fixture1, 'foo.bar', 2);
t.is(fixture1.foo.bar, 2);
setProperty(fixture1, 'foo.bar.baz', 3);
t.is(fixture1.foo.bar.baz, 3);
setProperty(fixture1, 'foo.bar', 'test');
t.is(fixture1.foo.bar, 'test');
setProperty(fixture1, 'foo.bar', null);
t.is(fixture1.foo.bar, null);
setProperty(fixture1, 'foo.bar', false);
t.is(fixture1.foo.bar, false);
setProperty(fixture1, 'foo.bar', undefined);
t.is(fixture1.foo.bar, undefined);
setProperty(fixture1, 'foo.fake.fake2', 'fake');
t.is(fixture1.foo.fake.fake2, 'fake');
setProperty(fixture1, 'foo.function', function_);
t.is(fixture1.foo.function, function_);
function function__() {}
setProperty(function__, 'foo.bar', 1);
t.is(function__.foo.bar, 1);
fixture1.fn = function__;
setProperty(fixture1, 'fn.bar.baz', 2);
t.is(fixture1.fn.bar.baz, 2);
const fixture2 = {foo: null};
setProperty(fixture2, 'foo.bar', 2);
t.is(fixture2.foo.bar, 2);
const fixture3 = {};
setProperty(fixture3, '', 3);
t.is(fixture3[''], 3);
setProperty(fixture1, String.raw`foo\.bar.baz`, true);
t.is(fixture1['foo.bar'].baz, true);
setProperty(fixture1, String.raw`fo\.ob\.ar.baz`, true);
t.true(fixture1['fo.ob.ar'].baz);
const fixture4 = 'noobject';
const output4 = setProperty(fixture4, 'foo.bar', 2);
t.is(fixture4, 'noobject');
t.is(output4, fixture4);
const fixture5 = [];
setProperty(fixture5, '[1]', true);
t.is(fixture5[1], true);
setProperty(fixture5, '[0].foo[0]', true);
t.is(fixture5[0].foo[0], true);
setProperty(fixture5, '1', true);
t.is(fixture5[1], true);
setProperty(fixture5, '0.foo.0', true);
t.is(fixture5[0].foo[0], true);
const fixture6 = {};
setProperty(fixture6, 'foo[0].bar', true);
t.true(fixture6.foo[0].bar);
t.deepEqual(fixture6, {
foo: [{
bar: true,
}],
});
const fixture7 = {foo: ['bar', 'baz']};
setProperty(fixture7, 'foo.length', 1);
t.is(fixture7.foo.length, 1);
t.deepEqual(fixture7, {foo: ['bar']});
});
test('deleteProperty', t => {
const function_ = () => 'test';
function_.foo = 'bar';
const inner = {
a: 'a',
b: 'b',
c: 'c',
func: function_,
};
const fixture1 = {
foo: {
bar: {
baz: inner,
},
},
top: {
dog: 'sindre',
},
};
t.is(fixture1.foo.bar.baz.c, 'c');
t.true(deleteProperty(fixture1, 'foo.bar.baz.c'));
t.is(fixture1.foo.bar.baz.c, undefined);
t.is(fixture1.top.dog, 'sindre');
t.true(deleteProperty(fixture1, 'top'));
t.is(fixture1.top, undefined);
t.is(fixture1.foo.bar.baz.func.foo, 'bar');
t.true(deleteProperty(fixture1, 'foo.bar.baz.func.foo'));
t.is(fixture1.foo.bar.baz.func.foo, undefined);
t.is(fixture1.foo.bar.baz.func, function_);
t.true(deleteProperty(fixture1, 'foo.bar.baz.func'));
t.is(fixture1.foo.bar.baz.func, undefined);
setProperty(fixture1, String.raw`foo\.bar.baz`, true);
t.true(fixture1['foo.bar'].baz);
t.true(deleteProperty(fixture1, String.raw`foo\.bar.baz`));
t.is(fixture1['foo.bar'].baz, undefined);
const fixture2 = {};
setProperty(fixture2, String.raw`foo.bar\.baz`, true);
t.true(fixture2.foo['bar.baz']);
t.true(deleteProperty(fixture2, String.raw`foo.bar\.baz`));
t.is(fixture2.foo['bar.baz'], undefined);
fixture2.dotted = {
sub: {
'dotted.prop': 'foo',
other: 'prop',
},
};
t.true(deleteProperty(fixture2, String.raw`dotted.sub.dotted\.prop`));
t.is(fixture2.dotted.sub['dotted.prop'], undefined);
t.is(fixture2.dotted.sub.other, 'prop');
const fixture3 = {foo: null};
t.false(deleteProperty(fixture3, 'foo.bar'));
t.deepEqual(fixture3, {foo: null});
const fixture4 = [{
top: {
dog: 'sindre',
},
}];
t.true(deleteProperty(fixture4, '0.top.dog'));
t.deepEqual(fixture4, [{top: {}}]);
// Test bracket notation deletion with fresh fixture
const fixture4b = [{
top: {
dog: 'sindre',
},
}];
t.true(deleteProperty(fixture4b, '[0].top.dog'));
t.deepEqual(fixture4b, [{top: {}}]);
const fixture5 = {
foo: [{
bar: ['foo', 'bar'],
}],
};
deleteProperty(fixture5, 'foo[0].bar[0]');
const fixtureArray = [];
fixtureArray[1] = 'bar';
t.deepEqual(fixture5, {
foo: [{
bar: fixtureArray,
}],
});
});
test('hasProperty', t => {
const fixture1 = {foo: {bar: 1}};
t.false(hasProperty(fixture1));
t.true(hasProperty(fixture1, 'foo'));
t.true(hasProperty({foo: 1}, 'foo'));
t.true(hasProperty({foo: null}, 'foo'));
t.true(hasProperty({foo: undefined}, 'foo'));
t.true(hasProperty({foo: {bar: true}}, 'foo.bar'));
t.true(hasProperty({foo: {bar: {baz: true}}}, 'foo.bar.baz'));
t.true(hasProperty({foo: {bar: {baz: null}}}, 'foo.bar.baz'));
t.false(hasProperty({foo: {bar: 'a'}}, 'foo.fake.fake2'));
t.false(hasProperty({foo: null}, 'foo.bar'));
t.false(hasProperty({foo: ''}, 'foo.bar'));
t.false(hasProperty({foo: 0}, 'foo.bar'));
t.false(hasProperty({foo: false}, 'foo.bar'));
function function_() {}
function_.foo = {bar: 1};
t.false(hasProperty(function_));
t.true(hasProperty(function_, 'foo'));
t.true(hasProperty(function_, 'foo.bar'));
t.true(hasProperty({'foo.baz': {bar: true}}, String.raw`foo\.baz.bar`));
t.true(hasProperty({'fo.ob.az': {bar: true}}, String.raw`fo\.ob\.az.bar`));
t.false(hasProperty(undefined, String.raw`fo\.ob\.az.bar`));
t.true(hasProperty({
foo: [{bar: ['bar', 'bizz']}],
}, 'foo[0].bar.1'));
t.false(hasProperty({
foo: [{bar: ['bar', 'bizz']}],
}, 'foo[0].bar.2'));
t.false(hasProperty({
foo: [{bar: ['bar', 'bizz']}],
}, 'foo[1].bar.1'));
t.true(hasProperty({
foo: [{
bar: {
1: 'bar',
},
}],
}, 'foo[0].bar.1'));
});
test('escapePath', t => {
t.is(escapePath('foo.bar[0]'), String.raw`foo\.bar\[0]`);
t.is(escapePath(String.raw`foo\.bar[0]`), String.raw`foo\\\.bar\[0]`);
t.is(escapePath('foo\\\.bar[0]'), String.raw`foo\\\.bar\[0]`); // eslint-disable-line no-useless-escape
t.is(escapePath(String.raw`foo\\.bar[0]`), String.raw`foo\\\\\.bar\[0]`);
t.is(escapePath(String.raw`foo\\.bar\\[0]`), String.raw`foo\\\\\.bar\\\\\[0]`);
t.is(escapePath('foo[0].bar'), String.raw`foo\[0]\.bar`);
t.is(escapePath('foo.bar[0].baz'), String.raw`foo\.bar\[0]\.baz`);
t.is(escapePath('[0].foo'), String.raw`\[0]\.foo`);
// These tests verify that all backslashes are escaped, even those without special meaning
t.is(escapePath(String.raw`\foo`), String.raw`\\foo`);
t.is(escapePath('foo\\'), 'foo\\\\');
t.is(escapePath('foo\\\\'), 'foo\\\\\\\\');
t.is(escapePath(''), '');
t.throws(() => {
escapePath(0);
}, {
instanceOf: TypeError,
message: /Expected a string, got number/,
});
});
test('deepKeys', t => {
const object = {
eo: {},
ea: [],
'a.b': {
c: {
d: [1, 2, {
g: 3,
}],
e: '🦄',
f: 0,
h: {},
i: [],
nu: null,
na: Number.NaN,
un: undefined,
},
'': {
a: 0,
},
},
'': {
a: 0,
},
};
const keys = deepKeys(object);
t.deepEqual(keys, [
'eo',
'ea',
String.raw`a\.b.c.d[0]`,
String.raw`a\.b.c.d[1]`,
String.raw`a\.b.c.d[2].g`,
String.raw`a\.b.c.e`,
String.raw`a\.b.c.f`,
String.raw`a\.b.c.h`,
String.raw`a\.b.c.i`,
String.raw`a\.b.c.nu`,
String.raw`a\.b.c.na`,
String.raw`a\.b.c.un`,
String.raw`a\.b..a`,
'.a',
]);
for (const key of keys) {
t.true(hasProperty(object, key));
}
t.deepEqual(deepKeys([]), []);
t.deepEqual(deepKeys(0), []);
});
test('deepKeys - does not throw on sparse array', t => {
const object = {
sparse: [1,,3], // eslint-disable-line no-sparse-arrays
};
const keys = deepKeys(object);
t.deepEqual(keys, [
'sparse[0]',
'sparse[2]',
]);
});
test('prevent setting/getting `__proto__`', t => {
setProperty({}, '__proto__.unicorn', '🦄');
t.not({}.unicorn, '🦄'); // eslint-disable-line no-use-extend-native/no-use-extend-native
t.is(getProperty({}, '__proto__'), undefined);
});
test('prevent setting/getting `prototype`', t => {
const object = {};
setProperty(object, 'prototype.unicorn', '🦄');
t.is(object.prototype, undefined);
t.is(getProperty({}, 'prototype'), undefined);
t.is(getProperty({}, 'prototype', 'default'), 'default');
t.false(hasProperty({}, 'prototype'));
});
test('return default value if path is invalid', t => {
t.is(getProperty({}, 'constructor', '🦄'), '🦄');
});
test('deepKeys with Symbol properties', t => {
const symbol = Symbol('test');
const object = {
foo: 'bar',
[symbol]: 'value',
};
const keys = deepKeys(object);
t.deepEqual(keys, ['foo']);
// Symbols are not included in deepKeys
});
test('deepKeys with cyclic references', t => {
const object = {foo: {}};
object.foo.bar = object;
// DeepKeys now handles cyclic references gracefully
// Since foo contains a non-empty object (even with cycle), it doesn't yield 'foo'
// It tries to recurse but stops due to cycle detection
const keys = deepKeys(object);
t.deepEqual(keys, []);
});
test('edge cases for mixed array and object paths', t => {
const fixture = {
foo: [
{bar: {baz: [1, {qux: 'value'}]}},
],
};
t.is(getProperty(fixture, 'foo[0].bar.baz[1].qux'), 'value');
t.true(hasProperty(fixture, 'foo[0].bar.baz[1].qux'));
setProperty(fixture, 'foo[0].bar.baz[2]', 'new');
t.is(fixture.foo[0].bar.baz[2], 'new');
t.true(deleteProperty(fixture, 'foo[0].bar.baz[1].qux'));
t.false(hasProperty(fixture, 'foo[0].bar.baz[1].qux'));
});
test('escaped paths with special characters', t => {
const fixture = {};
// Test escaping of dots, brackets, and backslashes
setProperty(fixture, String.raw`foo\.bar\[0]`, 'value');
t.is(fixture['foo.bar[0]'], 'value');
t.is(getProperty(fixture, String.raw`foo\.bar\[0]`), 'value');
// Test multiple escaped characters
setProperty(fixture, String.raw`a\.b\.c`, 'test');
t.is(fixture['a.b.c'], 'test');
});
test('setProperty on non-objects returns original value', t => {
t.is(setProperty(null, 'foo', 'bar'), null);
t.is(setProperty(undefined, 'foo', 'bar'), undefined);
t.is(setProperty(42, 'foo', 'bar'), 42);
t.is(setProperty('string', 'foo', 'bar'), 'string');
t.is(setProperty(true, 'foo', 'bar'), true);
});
test('deleteProperty on non-objects returns false', t => {
t.false(deleteProperty(null, 'foo'));
t.false(deleteProperty(undefined, 'foo'));
t.false(deleteProperty(42, 'foo'));
t.false(deleteProperty('string', 'foo'));
t.false(deleteProperty(true, 'foo'));
});
test('hasProperty on non-objects returns false', t => {
t.false(hasProperty(null, 'foo'));
t.false(hasProperty(42, 'foo'));
t.false(hasProperty('string', 'foo'));
t.false(hasProperty(true, 'foo'));
});
test('empty path edge cases', t => {
const fixture = {'': {'': 'value'}};
t.is(getProperty(fixture, '.'), fixture['']['']);
t.true(hasProperty(fixture, '.'));
setProperty(fixture, '..', 'new');
t.is(fixture[''][''][''], 'new');
});
test('array length property', t => {
const fixture = {foo: [1, 2, 3]};
t.is(getProperty(fixture, 'foo.length'), 3);
t.true(hasProperty(fixture, 'foo.length'));
// Trying to delete array length throws an error
t.throws(() => {
deleteProperty(fixture, 'foo.length');
}, {message: 'Cannot delete property \'length\' of [object Array]'});
t.is(fixture.foo.length, 3);
});
test('deepKeys with nested empty objects and arrays', t => {
const object = {
a: {},
b: [],
c: {
d: {},
e: [],
f: null,
g: {
h: 'value',
},
},
};
const keys = deepKeys(object);
t.deepEqual(keys, [
'a',
'b',
'c.d',
'c.e',
'c.f',
'c.g.h',
]);
});
test('deepKeys with function values', t => {
const function_ = () => {};
function_.prop = 'value';
const object = {
a: function_,
b: {
c: function_,
},
};
const keys = deepKeys(object);
t.deepEqual(keys, [
'a.prop',
'b.c.prop',
]);
});
test('getProperty with inherited properties', t => {
class Parent {
constructor() {
this.instanceProp = 'instance';
}
}
Parent.prototype.protoProp = 'proto';
const child = new Parent();
t.is(getProperty(child, 'instanceProp'), 'instance');
t.is(getProperty(child, 'protoProp'), 'proto');
});
test('setting array elements creates sparse arrays correctly', t => {
const fixture = {};
setProperty(fixture, 'arr[2]', 'value');
t.is(fixture.arr.length, 3);
t.is(fixture.arr[0], undefined);
t.is(fixture.arr[1], undefined);
t.is(fixture.arr[2], 'value');
// Check sparse array in deepKeys
const keys = deepKeys(fixture);
t.deepEqual(keys, ['arr[2]']);
});
test('invalid path edge cases for indexEnd state', t => {
// Test invalid character after closing bracket with backslash
t.throws(() => {
getProperty({}, String.raw`[0]\x`);
}, {message: String.raw`Invalid character '\' after an index at position 4`});
// Test closing bracket followed by another closing bracket
t.throws(() => {
getProperty({}, '[0]]');
}, {message: 'Invalid character \']\' after an index at position 4'});
// Test invalid character after index with default character
t.throws(() => {
getProperty({}, '[0]a');
}, {message: 'Invalid character \'a\' after an index at position 4'});
});
test('unflatten - basic dotted keys (issue #116)', t => {
const flat = {
'user.name': 'Gummy Bear',
'user.email': 'gummybear@candymountain.com',
'user.professional.title': 'King',
'user.professional.employer': 'Candy Mountain',
};
const result = unflatten(flat);
t.deepEqual(result, {
user: {
name: 'Gummy Bear',
email: 'gummybear@candymountain.com',
professional: {
title: 'King',
employer: 'Candy Mountain',
},
},
});
});
test('unflatten - bracket indexes and escapes', t => {
const flat = {
'users[0].name': 'Ada',
'users[1].name': 'Linus',
'a\\.b.c': 1,
};
const result = unflatten(flat);
t.is(result.users[0].name, 'Ada');
t.is(result.users[1].name, 'Linus');
t.is(result['a.b'].c, 1);
// Ensure getProperty agrees with produced structure
t.is(getProperty(result, 'users[0].name'), 'Ada');
t.is(getProperty(result, String.raw`a\.b.c`), 1);
});
test('unflatten - disallowed keys are ignored safely', t => {
const flat = {
'__proto__.polluted': 'nope',
'constructor.prototype.bad': true,
'valid.path': 1,
};
const result = unflatten(flat);
t.is(result.valid.path, 1);
// Ensure prototype is not polluted via inherited property
const empty = {};
t.false('polluted' in empty);
});
test('unflatten - non-object input returns empty object', t => {
t.deepEqual(unflatten(undefined), {});
t.deepEqual(unflatten(null), {});
t.deepEqual(unflatten('unicorn'), {});
t.deepEqual(unflatten(1), {});
});
test('unflatten - last write wins on conflicts', t => {
const flat = {
a: 1,
'a.b': 2,
'a.c': 3,
};
const result = unflatten(flat);
// Primitive at 'a' is replaced by object writes
t.deepEqual(result, {a: {b: 2, c: 3}});
});
test('unflatten - creates sparse arrays where appropriate', t => {
const result = unflatten({'arr[2]': 'value'});
t.is(result.arr.length, 3);
t.is(result.arr[0], undefined);
t.is(result.arr[1], undefined);
t.is(result.arr[2], 'value');
});
test('dot notation for array indices support', t => {
// GetProperty with dot notation for array indices
const object = {users: [{name: 'Alice', roles: ['admin']}, {name: 'Bob'}]};
t.is(getProperty(object, 'users.0.name'), 'Alice');
t.is(getProperty(object, 'users.1.name'), 'Bob');
t.is(getProperty(object, 'users.0.roles.0'), 'admin');
// SetProperty with dot notation for array indices
const data = {};
setProperty(data, 'items.0.title', 'First Item');
setProperty(data, 'items.1.title', 'Second Item');
setProperty(data, 'items.0.tags.0', 'important');
t.true(Array.isArray(data.items));
t.is(data.items[0].title, 'First Item');
t.is(data.items[1].title, 'Second Item');
t.true(Array.isArray(data.items[0].tags));
t.is(data.items[0].tags[0], 'important');
// HasProperty with dot notation for array indices
t.true(hasProperty(data, 'items.0.title'));
t.true(hasProperty(data, 'items.0.tags.0'));
t.false(hasProperty(data, 'items.2.title'));
// DeleteProperty with dot notation for array indices
t.true(deleteProperty(data, 'items.0.tags.0'));
t.is(data.items[0].tags[0], undefined);
// Mixed bracket and dot notation should work
t.is(getProperty(object, 'users[0].name'), 'Alice');
t.is(getProperty(object, 'users.0.roles[0]'), 'admin');
});
test('dot notation array indices - edge cases and mixed syntax', t => {
// Test with sparse arrays
const sparseObject = {};
setProperty(sparseObject, 'items.5.name', 'item5');
t.is(sparseObject.items[5].name, 'item5');
t.is(sparseObject.items[0], undefined);
t.true(hasProperty(sparseObject, 'items.5.name'));
t.false(hasProperty(sparseObject, 'items.0.name'));
// Deep nesting with mixed syntax
const deepObject = {
level1: [{
level2: {
arr: [
{level4: ['deep', 'array']},
],
},
}],
};
t.is(getProperty(deepObject, 'level1.0.level2.arr[0].level4.1'), 'array');
t.is(getProperty(deepObject, 'level1[0].level2.arr.0.level4[1]'), 'array');
// Setting complex nested structures
setProperty(deepObject, 'level1.0.level2.newArr.0.value', 'test');
t.is(deepObject.level1[0].level2.newArr[0].value, 'test');
// Zero-based indices
const zeroObject = {};
setProperty(zeroObject, 'arr.0', 'first');
setProperty(zeroObject, 'arr.00', 'zero-prefixed'); // Should be treated as string
t.is(zeroObject.arr[0], 'first');
t.is(zeroObject.arr['00'], 'zero-prefixed');
t.true(hasProperty(zeroObject, 'arr.0'));
// Large indices
const largeIndexObject = {};
setProperty(largeIndexObject, 'arr.100', 'large');
t.is(largeIndexObject.arr[100], 'large');
t.is(largeIndexObject.arr.length, 101);
// Mixed object and array operations
const mixedObject = {complex: {data: [{items: ['a', 'b']}, {items: ['c', 'd']}]}};
t.is(getProperty(mixedObject, 'complex.data.0.items.1'), 'b');
t.is(getProperty(mixedObject, 'complex.data.1.items.0'), 'c');
setProperty(mixedObject, 'complex.data.0.items.2', 'e');
t.is(mixedObject.complex.data[0].items[2], 'e');
t.true(deleteProperty(mixedObject, 'complex.data.0.items.1'));
t.is(mixedObject.complex.data[0].items[1], undefined);
});
test('dot notation array indices - boundary conditions', t => {
// Negative indices should be treated as strings (not valid array indices)
const object = {};
setProperty(object, 'arr.-1', 'negative');
t.is(object.arr['-1'], 'negative');
t.false(Array.isArray(object.arr)); // Should create object, not array
// Non-numeric strings with dots should work normally
setProperty(object, 'data.abc.def', 'string-keys');
t.is(object.data.abc.def, 'string-keys');
// Leading zeros are treated as strings
const leadingZeroObject = {};
setProperty(leadingZeroObject, 'arr.01', 'leading-zero');
setProperty(leadingZeroObject, 'arr.1', 'one');
t.is(leadingZeroObject.arr['01'], 'leading-zero');
t.is(leadingZeroObject.arr[1], 'one');
t.not(leadingZeroObject.arr['01'], leadingZeroObject.arr[1]);
// Empty arrays with dot notation
const emptyArrayObject = {arr: []};
t.false(hasProperty(emptyArrayObject, 'arr.0'));
setProperty(emptyArrayObject, 'arr.0', 'first');
t.true(hasProperty(emptyArrayObject, 'arr.0'));
t.is(emptyArrayObject.arr[0], 'first');
// Very long numeric strings (should still work as indices)
const longIndexObject = {};
setProperty(longIndexObject, 'arr.999999999999999', 'huge-index');
t.is(longIndexObject.arr[999_999_999_999_999], 'huge-index');
// Unflatten with dot array notation
const flatWithDotIndices = {
'users.0.name': 'Alice',
'users.0.age': 30,
'users.1.name': 'Bob',
'tags.0': 'important',
'tags.1': 'urgent',
};
const unflattened = unflatten(flatWithDotIndices);
t.true(Array.isArray(unflattened.users));
t.true(Array.isArray(unflattened.tags));
t.is(unflattened.users[0].name, 'Alice');
t.is(unflattened.users[1].name, 'Bob');
t.is(unflattened.tags[0], 'important');
t.is(unflattened.tags[1], 'urgent');
});
test('deleteProperty - correct boolean returns', t => {
// Should return false when property doesn't exist
t.false(deleteProperty({}, 'a'));
t.false(deleteProperty({a: {}}, 'a.b'));
t.false(deleteProperty({foo: [1, 2, 3]}, 'foo.5'));
t.false(deleteProperty({}, 'nonexistent.path'));
// Should return true when property exists and is deleted
const object = {a: {b: 'value'}, foo: 'bar'};
t.true(deleteProperty(object, 'a.b'));
t.false('b' in object.a);
t.true(deleteProperty(object, 'foo'));
t.false('foo' in object);
// Array indices
const arrayObject = {items: ['a', 'b', 'c']};
t.true(deleteProperty(arrayObject, 'items[1]'));
t.is(arrayObject.items[1], undefined);
t.is(arrayObject.items.length, 3); // Length preserved
// Dot notation array indices
const dotArrayObject = {items: ['x', 'y', 'z']};
t.true(deleteProperty(dotArrayObject, 'items.0'));
t.is(dotArrayObject.items[0], undefined);
});
test('unified numeric behavior', t => {
// Numeric strings in dot notation become numbers
const array = [undefined, 'value', undefined];
// '1' in dot notation becomes number 1 and sets the array element
setProperty(array, '1', 'updated');
t.is(array[1], 'updated');
// Leading zeros stay as string keys (not valid array indices)
const object = {arr: []};
setProperty(object, 'arr.01', 'leading-zero'); // '01' stays as string
setProperty(object, 'arr.00', 'double-zero'); // '00' stays as string
t.is(object.arr['01'], 'leading-zero');
t.is(object.arr['00'], 'double-zero');
t.not(object.arr[1], 'leading-zero'); // Should not affect actual index 1
// Direct numeric strings are converted to numbers
const array2 = ['a', 'b', 'c'];
setProperty(array2, '0', 'updated-a');
setProperty(array2, '2', 'updated-c');
t.deepEqual(array2, ['updated-a', 'b', 'updated-c']);
});
test('string index edge cases - leading zeros treated as strings', t => {
// 'users.00' should be treated as string key, not array index
const object = {};
setProperty(object, 'users.00.name', 'x');
t.deepEqual(object, {users: {'00': {name: 'x'}}});
t.false(Array.isArray(object.users)); // Should be object, not array
// hasProperty should work with leading zero string keys
t.true(hasProperty({users: {'00': {name: 'x'}}}, 'users.00.name'));
// Multiple leading zeros
setProperty(object, 'users.000.id', 'y');
t.is(object.users['000'].id, 'y');
// Mixed canonical and non-canonical
const mixed = {};
setProperty(mixed, 'array.0', 'zero'); // Canonical -> array index
setProperty(mixed, 'array.00', 'double'); // Non-canonical -> string key
t.true(Array.isArray(mixed.array));
t.is(mixed.array[0], 'zero');
t.is(mixed.array['00'], 'double');
});
test('edge cases - empty strings and special characters', t => {
// Empty string segments
const object = {};
setProperty(object, '.foo', 'empty-start');
setProperty(object, 'foo.', 'empty-end');
setProperty(object, 'bar..baz', 'double-dot');