-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.js
More file actions
754 lines (594 loc) · 23.9 KB
/
index.test.js
File metadata and controls
754 lines (594 loc) · 23.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
const { set, get, parse, stringify, tokenize, jsonClone, EMPTY_OBJ, EMPTY_ARR } = require("./");
const assert = require("node:assert");
const { test } = require("node:test");
function hasOwn(o, k) {
return Object.prototype.hasOwnProperty.call(o, k);
}
test("sets values for arrays", () => {
const val1 = {};
const val2 = set(val1, "pastAddresses[1].street", "someStreet");
assert.deepStrictEqual(val2, {
pastAddresses: [
null,
{
street: "someStreet",
},
],
});
});
test("undefined set", () => {
const val1 = { locks: undefined, replicas: ["dev"] };
const val2 = set(
val1,
undefined,
{ locks: undefined, replicas: ["dev"], error: undefined },
false,
);
assert.deepStrictEqual(val2, { replicas: ["dev"] });
});
test("undefined set2", () => {
const val1 = {};
const val2 = set(
val1,
undefined,
{ locks: undefined, replicas: ["dev"], error: undefined },
false,
);
assert.deepStrictEqual(val2, { replicas: ["dev"] });
});
test("undefined set3", () => {
const val1 = { replicas: ["dev1"] };
const val2 = set(val1, null, { replicas: ["dev1"], locks: undefined }, false);
assert.equal(val1, val2);
});
test("undefined set4", () => {
const val1 = { replicas: ["dev1"] };
const val2 = set(
val1,
null,
{ locks: undefined, replicas: ["dev1"], error: undefined },
false,
);
assert.equal(val1, val2);
});
test("result shape equals newValue keys (drops unspecified keys)", () => {
const oldData = { a: 1, b: 2, c: 3 };
const next = set(oldData, undefined, { b: 20 }, true);
// Under "replace shape" semantics, only "b" remains
assert.deepEqual(next, { b: 20 });
assert.equal(hasOwn(next, "a"), false);
assert.equal(hasOwn(next, "c"), false);
});
test("undefined values are omitted from result", () => {
const oldData = { a: 1, b: 2, c: 3 };
const next = set(oldData, undefined, { a: 1, b: undefined }, true);
// only "a" remains; "b" explicitly removed; "c" dropped because not present in patch
assert.deepEqual(next, { a: 1 });
assert.equal(hasOwn(next, "b"), false);
assert.equal(hasOwn(next, "c"), false);
});
test("empty object becomes EMPTY_OBJ (or preserves ref if already empty)", () => {
const oldNonEmpty = { a: 1 };
const next1 = set(oldNonEmpty, undefined, {}, true);
assert.strictEqual(next1, EMPTY_OBJ);
const oldEmpty = EMPTY_OBJ; // already empty sentinel
const next2 = set(oldEmpty, undefined, {}, true);
assert.strictEqual(next2, oldEmpty);
});
test("EMPTY_OBJ forces EMPTY_OBJ", () => {
const oldData = { a: 1, b: 2 };
const next = set(oldData, undefined, EMPTY_OBJ, true);
assert.strictEqual(next, EMPTY_OBJ);
});
test("no-op returns same reference when newValue matches oldValue shape+values", () => {
const oldData = { a: 1, b: 2 };
const next = set(oldData, undefined, { a: 1, b: 2 }, true);
// key order matches and values match => should be ref-equal
assert.strictEqual(next, oldData);
});
test("should update if newValue key order differs", () => {
const oldData = { a: 1, b: 2, c: 3 };
// Same key/value pairs but different insertion order
const newValue = {};
newValue.c = 3;
newValue.a = 1;
newValue.b = 2;
const next = set(oldData, undefined, newValue, true);
// Semantically identical — if your "no changes" optimization is correct,
// this should still return oldData.
//
// If you do NOT want this optimization to be order-insensitive, change this
// to deepEqual instead of strictEqual.
assert.deepStrictEqual(next, newValue);
});
test("delete-via-undefined should not affect no-op detection", () => {
const oldData = { a: 1 };
const newValue = { a: 1, b: undefined }; // b doesn't exist; should be omitted => result still {a:1}
const next = set(oldData, undefined, newValue, true);
// should be no-op (result equals oldData)
assert.strictEqual(next, oldData);
});
test("when result differs, must not accidentally return oldValue", () => {
const oldData = { a: 1, b: 2, c: 3 };
// Change b, keep a, drop c by omission
const next = set(oldData, undefined, { a: 1, b: 999 }, true);
assert.notStrictEqual(next, oldData);
assert.deepEqual(next, { a: 1, b: 999 });
});
test("should not include undefined keys and should keep later keys", () => {
const oldData = { a: 1, b: 2, c: 3 };
// insertion order matters: a, x, b
const newValue = {
a: 1,
x: undefined,
b: 2,
};
const next = set(oldData, undefined, newValue, true);
// Expected (replace-shape semantics):
// - keep a and b (same values)
// - omit x (undefined)
// - drop c (not present in patch)
assert.deepEqual(next, { a: 1, b: 2 });
// and x must not exist as an own property
assert.equal(Object.prototype.hasOwnProperty.call(next, "x"), false);
});
test("should not include undefined keys and should keep later keys", () => {
const oldData = { a: 1, b: 2, c: 3 };
// insertion order matters: a, x, b
const newValue = {
a: 1,
b: 2,
c: 3,
d: undefined,
};
const next = set(oldData, undefined, newValue, true);
// Expected (replace-shape semantics):
// - keep a and b (same values)
// - omit x (undefined)
// - drop c (not present in patch)
assert.deepEqual(next, { a: 1, b: 2, c: 3 });
// and x must not exist as an own property
assert.equal(Object.prototype.hasOwnProperty.call(next, "x"), false);
});
test("fallback uses idx and can drop later keys / include undefined keys", () => {
// Important: insertion order is a, c, b (NOT a, b, c)
const oldData = {};
oldData.a = 1;
oldData.c = 3;
oldData.b = 2;
// Patch insertion order: a, x, b
// x is undefined and not present in oldData => should be omitted
// Under your replace-shape semantics, result should be { a: 1, b: 2 }
const patch = {};
patch.a = 1;
patch.x = undefined;
patch.b = 2;
const next = set(oldData, undefined, patch, true);
// Expected: x omitted, b kept
assert.deepEqual(next, { a: 1, b: 2 });
});
test("patch with array of undefined is noop", () => {
const oldData = [1];
const patch = [1, undefined, undefined];
const next = set(oldData, undefined, patch, true);
assert.strictEqual(next, oldData);
});
test("set property on array", () => {
const oldData = [1];
const next = set(oldData, "asd", "foo", true);
assert.strictEqual(next, oldData);
});
test("remove property on undefined", () => {
const y = {
time: {},
};
const res = set(y, "time", undefined);
assert.deepStrictEqual(res, {});
});
test("numeric root path creates object root", () => {
const next = set({}, "0", "a");
assert.deepStrictEqual(next, { 0: "a" });
assert.equal(Array.isArray(next), false);
});
test("numeric falsy path treated as path", () => {
const next = set({ a: 1 }, 0, "x");
assert.deepStrictEqual(next, { a: 1, 0: "x" });
});
test("array undefined", () => {
const next = set([1, 2, 3], undefined, [1, undefined, 3], true);
assert.deepStrictEqual(next, [1, null, 3]);
});
test("array undefined removes element in nested patch", () => {
const next = set({ items: [1, 2, 3] }, "items", [1, undefined, 3], true);
assert.deepStrictEqual(next, { items: [1, null, 3] });
});
test("false path treated as path", () => {
const next = set({ a: 1 }, false, "x");
assert.deepStrictEqual(next, { a: 1, false: "x" });
});
test("array index set to undefined", () => {
const next = set(["a", "b", "c"], "1", undefined);
assert.deepStrictEqual(next, ["a", null, "c"]);
});
test("set new Date", () => {
const date = new Date();
const next = set({}, "time", date);
assert.deepStrictEqual(next, { time: date.toISOString() });
});
// Regression tests: no-op detection when newValue has leading undefined keys
// Bug: using loop index `i` instead of non-undefined count `idx-1` for position
// check caused unnecessary allocations when undefined keys preceded real keys
// within the bounds of oldKeys.
test("noop: leading undefined key before matching keys within oldKeys bounds", () => {
const old = { a: 1, b: 2 };
const next = set(old, null, { x: undefined, a: 1, b: 2 }, true);
assert.strictEqual(next, old);
});
test("noop: multiple leading undefined keys before matching keys", () => {
const old = { a: 1, b: 2, c: 3 };
const next = set(old, null, { x: undefined, y: undefined, a: 1, b: 2, c: 3 }, true);
assert.strictEqual(next, old);
});
test("noop: undefined key interspersed, all real keys within oldKeys bounds", () => {
const old = { a: 1, b: 2, c: 3 };
const next = set(old, null, { x: undefined, a: 1, y: undefined, b: 2, c: 3 }, true);
assert.strictEqual(next, old);
});
// ---------------------------------------------------------------------------
// get
// ---------------------------------------------------------------------------
test("get: null data with non-empty path returns undefined", () => {
assert.strictEqual(get(null, "a"), undefined);
});
test("get: undefined data with non-empty path returns undefined", () => {
assert.strictEqual(get(undefined, "a"), undefined);
});
test("get: null path returns data as-is", () => {
const obj = { a: 1 };
assert.strictEqual(get(obj, null), obj);
});
test("get: empty string path returns data as-is", () => {
const obj = { a: 1 };
assert.strictEqual(get(obj, ""), obj);
});
test("get: traverses dot-separated path", () => {
assert.strictEqual(get({ a: { b: 42 } }, "a.b"), 42);
});
test("get: returns undefined for missing key", () => {
assert.strictEqual(get({ a: 1 }, "b"), undefined);
});
test("get: returns undefined when intermediate is a primitive", () => {
assert.strictEqual(get({ a: 1 }, "a.b"), undefined);
});
test("get: traverses array index via bracket notation", () => {
assert.strictEqual(get({ a: [10, 20, 30] }, "a[1]"), 20);
});
test("get: accepts token array as path", () => {
assert.strictEqual(get({ a: { b: 99 } }, ["a", "b"]), 99);
});
// ---------------------------------------------------------------------------
// tokenize
// ---------------------------------------------------------------------------
test("tokenize: null returns EMPTY_ARR sentinel", () => {
assert.strictEqual(tokenize(null), EMPTY_ARR);
});
test("tokenize: empty string returns EMPTY_ARR sentinel", () => {
assert.strictEqual(tokenize(""), EMPTY_ARR);
});
test("tokenize: empty array returns EMPTY_ARR sentinel", () => {
assert.strictEqual(tokenize([]), EMPTY_ARR);
});
test("tokenize: dot-separated string", () => {
assert.deepStrictEqual(tokenize("a.b.c"), ["a", "b", "c"]);
});
test("tokenize: bracket notation becomes string index", () => {
assert.deepStrictEqual(tokenize("a[0]"), ["a", "0"]);
});
test("tokenize: array path passes through fp.toPath", () => {
assert.deepStrictEqual(tokenize(["a", "b"]), ["a", "b"]);
});
// ---------------------------------------------------------------------------
// parse / stringify
// ---------------------------------------------------------------------------
test("parse: '{}' returns EMPTY_OBJ sentinel", () => {
assert.strictEqual(parse("{}"), EMPTY_OBJ);
});
test("parse: '[]' returns EMPTY_ARR sentinel", () => {
assert.strictEqual(parse("[]"), EMPTY_ARR);
});
test("parse: regular JSON string", () => {
assert.deepStrictEqual(parse('{"a":1}'), { a: 1 });
});
test("stringify: EMPTY_OBJ returns '{}'", () => {
assert.strictEqual(stringify(EMPTY_OBJ), "{}");
});
test("stringify: EMPTY_ARR returns '[]'", () => {
assert.strictEqual(stringify(EMPTY_ARR), "[]");
});
test("stringify: regular object", () => {
assert.strictEqual(stringify({ a: 1 }), '{"a":1}');
});
test("stringify: null", () => {
assert.strictEqual(stringify(null), "null");
});
test("parse/stringify round-trip", () => {
const obj = { x: [1, 2, null], y: true };
assert.deepStrictEqual(parse(stringify(obj)), obj);
});
// ---------------------------------------------------------------------------
// jsonClone
// ---------------------------------------------------------------------------
test("jsonClone: null returns null", () => {
assert.strictEqual(jsonClone(null), null);
});
test("jsonClone: string returns same string", () => {
assert.strictEqual(jsonClone("hello"), "hello");
});
test("jsonClone: empty array returns EMPTY_ARR sentinel", () => {
assert.strictEqual(jsonClone([]), EMPTY_ARR);
});
test("jsonClone: empty object returns EMPTY_OBJ sentinel", () => {
assert.strictEqual(jsonClone({}), EMPTY_OBJ);
});
test("jsonClone: deep-clones a non-empty object", () => {
const obj = { a: { b: 1 } };
const clone = jsonClone(obj);
assert.deepStrictEqual(clone, obj);
assert.notStrictEqual(clone, obj);
assert.notStrictEqual(clone.a, obj.a);
});
test("jsonClone: Date serialises to ISO string", () => {
const d = new Date("2024-01-01T00:00:00.000Z");
assert.deepStrictEqual(jsonClone(d), "2024-01-01T00:00:00.000Z");
});
// ---------------------------------------------------------------------------
// set — null / missing data
// ---------------------------------------------------------------------------
test("set: null data is treated as empty object", () => {
assert.deepStrictEqual(set(null, "a", 1), { a: 1 });
});
test("set: null data with null path patches against empty object", () => {
assert.deepStrictEqual(set(null, null, { a: 1 }, true), { a: 1 });
});
// ---------------------------------------------------------------------------
// set — path variants
// ---------------------------------------------------------------------------
test("set: token array accepted as path", () => {
assert.deepStrictEqual(set({ a: { b: 1 } }, ["a", "b"], 2), { a: { b: 2 } });
});
test("set: creates deep nested intermediate objects", () => {
assert.deepStrictEqual(set({}, "a.b.c", 42), { a: { b: { c: 42 } } });
});
test("set: creates intermediate array when next token is numeric", () => {
assert.deepStrictEqual(set({}, "a[0]", "x"), { a: ["x"] });
});
test("set: overwrites primitive intermediate node with object", () => {
assert.deepStrictEqual(set({ a: 1 }, "a.b", 2), { a: { b: 2 } });
});
test("set: updates array element in-place via path", () => {
assert.deepStrictEqual(set([1, 2, 3], "1", "x"), [1, "x", 3]);
});
test("set: fills gaps with null when setting sparse array index", () => {
assert.deepStrictEqual(set([], "3", "x"), [null, null, null, "x"]);
});
// ---------------------------------------------------------------------------
// set — undefined / null values at a path
// ---------------------------------------------------------------------------
test("set: stores null at an existing path", () => {
assert.deepStrictEqual(set({ a: 1 }, "a", null), { a: null });
});
test("set: removing a non-existent nested key is a noop", () => {
const obj = { a: 1 };
assert.strictEqual(set(obj, "b.c", undefined), obj);
});
test("set: removing an existing key leaves siblings intact", () => {
assert.deepStrictEqual(set({ a: 1, b: 2 }, "a", undefined), { b: 2 });
});
// ---------------------------------------------------------------------------
// set — isPlainJSON cloning behaviour
// ---------------------------------------------------------------------------
test("isPlainJSON=true: nested value is NOT deep-cloned (same reference)", () => {
const inner = { b: 2 };
const next = set({}, "a", inner, true);
assert.strictEqual(next.a, inner);
});
test("isPlainJSON=false: nested value IS deep-cloned (different reference)", () => {
const inner = { b: 2 };
const next = set({}, "a", inner, false);
assert.deepStrictEqual(next.a, inner);
assert.notStrictEqual(next.a, inner);
});
// ---------------------------------------------------------------------------
// _patch — array behaviours
// ---------------------------------------------------------------------------
test("patch array: noop returns same reference when all elements match", () => {
const arr = [1, 2, 3];
assert.strictEqual(set(arr, null, [1, 2, 3], true), arr);
});
test("patch array: shorter newValue truncates result", () => {
assert.deepStrictEqual(set([1, 2, 3], null, [1, 2], true), [1, 2]);
});
test("patch array: trailing undefineds in newValue are trimmed (noop when rest matches)", () => {
const arr = [1, 2];
assert.strictEqual(set(arr, null, [1, 2, undefined, undefined], true), arr);
});
test("patch array: trailing undefineds trimmed leaving shorter array", () => {
assert.deepStrictEqual(set([1, 2, 3], null, [1, 2, undefined], true), [1, 2]);
});
test("patch array: non-empty old to empty new returns EMPTY_ARR sentinel", () => {
assert.strictEqual(set([1, 2], null, [], true), EMPTY_ARR);
});
test("patch array: empty old to empty new returns same empty reference", () => {
const arr = [];
assert.strictEqual(set(arr, null, [], true), arr);
});
test("patch array: undefined mid-element becomes null", () => {
assert.deepStrictEqual(set([1, 2, 3], null, [1, undefined, 3], true), [
1, null, 3,
]);
});
// ---------------------------------------------------------------------------
// _patch — type-mismatch (object ↔ array)
// ---------------------------------------------------------------------------
test("patch: object replaced by array is deep-cloned", () => {
const arr = [1, 2, 3];
const next = set({ a: 1 }, null, arr, false);
assert.deepStrictEqual(next, arr);
assert.notStrictEqual(next, arr);
});
test("patch: array replaced by object is deep-cloned", () => {
const obj = { a: 1 };
const next = set([1, 2], null, obj, false);
assert.deepStrictEqual(next, obj);
assert.notStrictEqual(next, obj);
});
// ---------------------------------------------------------------------------
// deepFreeze — active in non-production (current config: !isProduction)
// ---------------------------------------------------------------------------
test("set: result is frozen in non-production mode", () => {
const next = set({ a: 1 }, "b", 2);
assert.ok(Object.isFrozen(next));
});
test("set: noop result is frozen in non-production mode", () => {
const obj = { a: 1 };
const next = set(obj, "a", 1);
assert.strictEqual(next, obj);
assert.ok(Object.isFrozen(next));
});
test("set: nested result objects are deeply frozen", () => {
const next = set({}, "a.b", 1);
assert.ok(Object.isFrozen(next));
assert.ok(Object.isFrozen(next.a));
});
// ---------------------------------------------------------------------------
// Regression: _patch fallback path must preserve references from oldValue
// ---------------------------------------------------------------------------
test("patch object fallback preserves oldValue references when trimming extra keys", () => {
const innerA = { x: 1 };
const old = { a: innerA, b: 2, c: 3 };
// newValue has same keys a,b but a is a different (deep-equal) object
const next = set(old, null, { a: { x: 1 }, b: 2 }, true);
// _patch(innerA, {x:1}) should return innerA; fallback should use it
assert.strictEqual(next.a, innerA, "inner reference from oldValue must be preserved");
assert.deepStrictEqual(next, { a: { x: 1 }, b: 2 });
});
test("patch object fallback preserves deeply nested references", () => {
const deep = { y: { z: 42 } };
const old = { a: deep, b: "keep", extra1: 1, extra2: 2 };
const next = set(old, null, { a: { y: { z: 42 } }, b: "keep" }, true);
assert.strictEqual(next.a, deep);
assert.strictEqual(next.a.y, deep.y);
});
// ---------------------------------------------------------------------------
// get() edge cases
// ---------------------------------------------------------------------------
test("get: undefined data with null path returns undefined", () => {
assert.strictEqual(get(undefined, null), undefined);
});
test("get: null data with null path returns null", () => {
assert.strictEqual(get(null, null), null);
});
test("get: 0 data with null path returns 0", () => {
assert.strictEqual(get(0, null), 0);
});
test("get: false data with non-empty path returns undefined", () => {
assert.strictEqual(get(false, "a"), undefined);
});
test("get: deeply nested traversal", () => {
assert.strictEqual(get({ a: { b: { c: { d: 99 } } } }, "a.b.c.d"), 99);
});
test("get: array root with bracket path", () => {
assert.strictEqual(get([10, 20, 30], "[1]"), 20);
});
// ---------------------------------------------------------------------------
// set() — nested arrays
// ---------------------------------------------------------------------------
test("set: creates nested array via path a[0][1]", () => {
const next = set({}, "a[0][1]", "x");
assert.deepStrictEqual(next, { a: [[null, "x"]] });
});
test("set: updates nested array element", () => {
const next = set({ a: [[1, 2], [3, 4]] }, "a[1][0]", 99);
assert.deepStrictEqual(next, { a: [[1, 2], [99, 4]] });
});
test("set: preserves sibling when updating nested path", () => {
const old = { a: 1, b: { c: 2, d: 3 } };
const next = set(old, "b.c", 99);
assert.deepStrictEqual(next, { a: 1, b: { c: 99, d: 3 } });
// sibling unchanged
assert.strictEqual(next.a, old.a);
});
// ---------------------------------------------------------------------------
// tokenize() edge cases
// ---------------------------------------------------------------------------
test("tokenize: single key with brackets", () => {
assert.deepStrictEqual(tokenize("a[0].b[1]"), ["a", "0", "b", "1"]);
});
// ---------------------------------------------------------------------------
// jsonClone with isPlainJSON
// ---------------------------------------------------------------------------
test("jsonClone: isPlainJSON=true treats non-lodash-plain objects as plain", () => {
const obj = { a: 1 };
const clone = jsonClone(obj, true);
assert.deepStrictEqual(clone, obj);
assert.notStrictEqual(clone, obj);
});
test("jsonClone: empty object with isPlainJSON=true returns EMPTY_OBJ", () => {
assert.strictEqual(jsonClone({}, true), EMPTY_OBJ);
});
test("jsonClone: undefined returns undefined", () => {
assert.strictEqual(jsonClone(undefined), undefined);
});
// ---------------------------------------------------------------------------
// __proto__ handling
// ---------------------------------------------------------------------------
test("set: __proto__ in path does not pollute prototype", () => {
const data = {};
const next = set(data, "__proto__.polluted", true);
assert.strictEqual(({}).polluted, undefined, "Object.prototype must not be polluted");
});
test("patch: __proto__ key in value is handled safely", () => {
const old = { a: 1 };
const patch = JSON.parse('{"a": 1, "__proto__": {"polluted": true}}');
const next = set(old, null, patch, true);
assert.strictEqual(({}).polluted, undefined, "Object.prototype must not be polluted");
});
// ---------------------------------------------------------------------------
// set() — additional no-op / reference preservation
// ---------------------------------------------------------------------------
test("set: nested path no-op returns same root reference", () => {
const old = { a: { b: 1 } };
const next = set(old, "a.b", 1);
assert.strictEqual(next, old);
});
test("set: setting same array element is a no-op", () => {
const old = [1, 2, 3];
const next = set(old, "1", 2);
assert.strictEqual(next, old);
});
test("set: setting undefined on non-existent key is a no-op", () => {
const old = { a: 1 };
const next = set(old, "nonexistent", undefined);
assert.strictEqual(next, old);
});
// ---------------------------------------------------------------------------
// _patch — additional array edge cases
// ---------------------------------------------------------------------------
test("patch array: all-undefined newValue trims to empty array", () => {
const next = set([1, 2, 3], null, [undefined, undefined, undefined], true);
assert.deepStrictEqual(next, []);
});
test("patch array: single element noop", () => {
const arr = [42];
assert.strictEqual(set(arr, null, [42], true), arr);
});
test("patch array: growing array", () => {
assert.deepStrictEqual(set([1], null, [1, 2, 3], true), [1, 2, 3]);
});
// ---------------------------------------------------------------------------
// _patch — object with all undefined values
// ---------------------------------------------------------------------------
test("patch object: all undefined values returns EMPTY_OBJ", () => {
const next = set({ a: 1 }, null, { a: undefined, b: undefined }, true);
assert.strictEqual(next, EMPTY_OBJ);
});