ts-data-forge 14.10.0 → 14.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/README.md +5 -5
  2. package/dist/array/impl/array-utils-element-access.d.mts.map +1 -1
  3. package/dist/array/impl/array-utils-element-access.mjs +4 -4
  4. package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
  5. package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts +8 -8
  6. package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts.map +1 -1
  7. package/dist/array/impl/array-utils-length-bounded-array-cast.mjs +28 -28
  8. package/dist/array/impl/array-utils-length-bounded-array-cast.mjs.map +1 -1
  9. package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts.map +1 -1
  10. package/dist/array/impl/array-utils-length-bounded-array-guard.mjs +4 -4
  11. package/dist/array/impl/array-utils-length-bounded-array-guard.mjs.map +1 -1
  12. package/dist/array/impl/array-utils-length-bounded-tuple-cast.d.mts +3 -4
  13. package/dist/array/impl/array-utils-length-bounded-tuple-cast.d.mts.map +1 -1
  14. package/dist/array/impl/array-utils-length-bounded-tuple-cast.mjs +28 -29
  15. package/dist/array/impl/array-utils-length-bounded-tuple-cast.mjs.map +1 -1
  16. package/dist/array/impl/array-utils-length-bounded-tuple-guard.d.mts.map +1 -1
  17. package/dist/array/impl/array-utils-length-bounded-tuple-guard.mjs +3 -3
  18. package/dist/array/impl/array-utils-length-bounded-tuple-guard.mjs.map +1 -1
  19. package/dist/array/impl/array-utils-modification.d.mts +1 -1
  20. package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
  21. package/dist/array/impl/array-utils-modification.mjs +15 -15
  22. package/dist/array/impl/array-utils-modification.mjs.map +1 -1
  23. package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
  24. package/dist/array/impl/array-utils-reducing-value.mjs +12 -12
  25. package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
  26. package/dist/array/impl/array-utils-search.d.mts.map +1 -1
  27. package/dist/array/impl/array-utils-search.mjs +56 -56
  28. package/dist/array/impl/array-utils-search.mjs.map +1 -1
  29. package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
  30. package/dist/array/impl/array-utils-slicing.mjs +3 -3
  31. package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
  32. package/dist/array/impl/array-utils-transformation.d.mts +25 -25
  33. package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
  34. package/dist/array/impl/array-utils-transformation.mjs +22 -22
  35. package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
  36. package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
  37. package/dist/array/impl/array-utils-validation.mjs +2 -2
  38. package/dist/array/impl/array-utils-validation.mjs.map +1 -1
  39. package/dist/collections/imap-mapped.d.mts +2 -2
  40. package/dist/collections/imap-mapped.mjs +6 -3
  41. package/dist/collections/imap-mapped.mjs.map +1 -1
  42. package/dist/collections/imap.d.mts +3 -4
  43. package/dist/collections/imap.d.mts.map +1 -1
  44. package/dist/collections/imap.mjs +6 -4
  45. package/dist/collections/imap.mjs.map +1 -1
  46. package/dist/collections/iset-mapped.d.mts +1 -2
  47. package/dist/collections/iset-mapped.d.mts.map +1 -1
  48. package/dist/collections/iset-mapped.mjs.map +1 -1
  49. package/dist/collections/iset.d.mts +4 -5
  50. package/dist/collections/iset.d.mts.map +1 -1
  51. package/dist/collections/iset.mjs +0 -1
  52. package/dist/collections/iset.mjs.map +1 -1
  53. package/dist/json/json.d.mts.map +1 -1
  54. package/dist/json/json.mjs +13 -47
  55. package/dist/json/json.mjs.map +1 -1
  56. package/dist/number/num.d.mts +12 -8
  57. package/dist/number/num.d.mts.map +1 -1
  58. package/dist/number/num.mjs +5 -1
  59. package/dist/number/num.mjs.map +1 -1
  60. package/dist/object/object.d.mts +10 -2
  61. package/dist/object/object.d.mts.map +1 -1
  62. package/dist/object/object.mjs +16 -4
  63. package/dist/object/object.mjs.map +1 -1
  64. package/dist/others/fast-deep-equal.d.mts.map +1 -1
  65. package/dist/others/fast-deep-equal.mjs +10 -7
  66. package/dist/others/fast-deep-equal.mjs.map +1 -1
  67. package/dist/others/replace-equal-deep.d.mts +9 -9
  68. package/dist/others/replace-equal-deep.mjs +22 -15
  69. package/dist/others/replace-equal-deep.mjs.map +1 -1
  70. package/dist/string/str.d.mts +2 -2
  71. package/package.json +12 -12
  72. package/src/array/impl/array-utils-element-access.mts +11 -11
  73. package/src/array/impl/array-utils-length-bounded-array-cast.mts +69 -69
  74. package/src/array/impl/array-utils-length-bounded-array-guard.mts +21 -21
  75. package/src/array/impl/array-utils-length-bounded-array-guard.test.mts +2 -2
  76. package/src/array/impl/array-utils-length-bounded-tuple-cast.mts +64 -65
  77. package/src/array/impl/array-utils-length-bounded-tuple-guard.mts +15 -15
  78. package/src/array/impl/array-utils-modification.mts +54 -54
  79. package/src/array/impl/array-utils-reducing-value.mts +55 -55
  80. package/src/array/impl/array-utils-search.mts +114 -114
  81. package/src/array/impl/array-utils-slicing.mts +16 -16
  82. package/src/array/impl/array-utils-transformation.mts +96 -96
  83. package/src/array/impl/array-utils-transformation.test.mts +54 -5
  84. package/src/array/impl/array-utils-validation.mts +19 -19
  85. package/src/array/impl/array-utils-validation.test.mts +1 -1
  86. package/src/collections/imap-mapped.mts +8 -6
  87. package/src/collections/imap-mapped.test.mts +27 -0
  88. package/src/collections/imap.mts +9 -8
  89. package/src/collections/imap.test.mts +14 -1
  90. package/src/collections/iset-mapped.mts +1 -2
  91. package/src/collections/iset.mts +4 -5
  92. package/src/collections/iset.test.mts +1 -1
  93. package/src/json/json.mts +17 -57
  94. package/src/json/json.test.mts +32 -0
  95. package/src/number/num.mts +21 -17
  96. package/src/number/num.test.mts +13 -1
  97. package/src/object/object.mts +34 -6
  98. package/src/object/object.test.mts +55 -0
  99. package/src/others/fast-deep-equal.mts +17 -10
  100. package/src/others/fast-deep-equal.test.mts +141 -29
  101. package/src/others/replace-equal-deep.mts +48 -41
  102. package/src/others/replace-equal-deep.test.mts +39 -7
  103. package/src/string/str.mts +2 -2
@@ -48,8 +48,8 @@ export function map<const Ar extends readonly unknown[] & UnknownBrand, B>(
48
48
  mapFn: (a: Ar[number], index: ArrayIndex<Ar>) => B,
49
49
  ): ChangeArrayElement<Ar, B>;
50
50
 
51
- // A plain array or tuple carries no length brand, so the homomorphic mapping
52
- // is the whole answer. Stating it directly — rather than going through
51
+ // A plain array or tuple carries no length brand, so the homomorphic mapping is
52
+ // the whole answer. Stating it directly — rather than going through
53
53
  // `ChangeArrayElement`, whose `HasLengthConstraint` test a *generic* `Ar`
54
54
  // cannot decide — is what keeps `map` usable inside a function that is itself
55
55
  // generic over the tuple: a deferred conditional is not assignable to the
@@ -68,8 +68,8 @@ export function map<const Ar extends readonly unknown[], B>(
68
68
  //
69
69
  // Spelled as an intersection of two function types rather than one object type
70
70
  // carrying two call signatures — the `convert-to-readonly` codemod wraps an
71
- // inline object type in `Readonly<…>`, and a mapped type drops call
72
- // signatures, which would leave the curried form with none at all.
71
+ // inline object type in `Readonly<…>`, and a mapped type drops call signatures,
72
+ // which would leave the curried form with none at all.
73
73
  export function map<A, B>(
74
74
  mapFn: (a: A, index: SizeType.Arr) => B,
75
75
  ): (<const Ar extends readonly A[] & UnknownBrand>(
@@ -175,26 +175,6 @@ export function scan<E, S>(
175
175
  }
176
176
  }
177
177
 
178
- const scanImpl = <E, S>(
179
- array: readonly E[],
180
- reducer: (accumulator: S, currentValue: E, currentIndex: SizeType.Arr) => S,
181
- init: S,
182
- ): NonEmptyTuple<S> => {
183
- const mut_result = castMutable<NonEmptyTuple<S>>(
184
- newArray<PositiveUint32, S>(asPositiveUint32(array.length + 1), init),
185
- );
186
-
187
- let mut_acc = init;
188
-
189
- for (const [index, value] of array.entries()) {
190
- mut_acc = reducer(mut_acc, value, asUint32(index));
191
-
192
- mut_result[index + 1] = mut_acc;
193
- }
194
-
195
- return mut_result;
196
- };
197
-
198
178
  /**
199
179
  * Reverses an array.
200
180
  *
@@ -277,16 +257,6 @@ export function toSorted(
277
257
  );
278
258
  }
279
259
 
280
- /** The result of sorting `Ar`: same elements, same length, same bounds. */
281
- type SortedResult<Ar extends readonly unknown[]> =
282
- HasLengthConstraint<Ar> extends true
283
- ? ChangeArrayElement<Ar, Ar[number]>
284
- : IsFixedLengthList<Ar> extends true
285
- ? FixedLengthTuple<Ar['length'], Ar[number]>
286
- : Ar extends NonEmptyTuple<unknown>
287
- ? NonEmptyArray<Ar[number]>
288
- : readonly Ar[number][];
289
-
290
260
  /**
291
261
  * Sorts an array by a mapped value.
292
262
  *
@@ -426,11 +396,6 @@ export function filter<E>(
426
396
  }
427
397
  }
428
398
 
429
- const filterImpl = <E,>(
430
- array: readonly E[],
431
- predicate: (a: E, index: SizeType.Arr) => boolean,
432
- ): readonly E[] => array.filter((a, i) => predicate(a, asUint32(i)));
433
-
434
399
  /**
435
400
  * Filters an array by excluding elements for which the predicate returns true.
436
401
  *
@@ -474,11 +439,6 @@ export function filterNot<E>(
474
439
  }
475
440
  }
476
441
 
477
- const filterNotImpl = <E,>(
478
- array: readonly E[],
479
- predicate: (a: E, index: SizeType.Arr) => boolean,
480
- ): readonly E[] => array.filter((a, i) => !predicate(a, asUint32(i)));
481
-
482
442
  /**
483
443
  * Creates a new array with unique elements.
484
444
  *
@@ -615,11 +575,6 @@ export function flat<E, D extends SafeUintWithSmallInt = 1>(
615
575
  }
616
576
  }
617
577
 
618
- const flatImpl = (
619
- array: readonly unknown[],
620
- depth?: SafeUintWithSmallInt,
621
- ): readonly unknown[] => array.flat(depth);
622
-
623
578
  /**
624
579
  * Maps each element and flattens the result.
625
580
  *
@@ -734,37 +689,6 @@ export function partition<
734
689
  }
735
690
  }
736
691
 
737
- /**
738
- * Element type of {@link partition}'s result: a chunk holds between 1 and `N`
739
- * elements. The lower bound is what makes this worth spelling out — `partition`
740
- * never emits an empty chunk, so indexed access into a chunk is not
741
- * `undefined` at position `0`, unlike with a plain `readonly E[]`.
742
- *
743
- * Falls back to {@link NonEmptyArray} when `N` is not a bound the brand can
744
- * express, which keeps that lower bound in every case.
745
- */
746
- type PartitionChunk<N, E> = N extends SupportedLength
747
- ? BoundedLengthArray<1, N, E>
748
- : NonEmptyArray<E>;
749
-
750
- const partitionImpl = <E, N extends WithSmallInt<PositiveInt & SizeType.Arr>>(
751
- array: readonly E[],
752
- chunkSize: N,
753
- ): readonly (readonly E[])[] =>
754
- chunkSize < 2
755
- ? ([] as const)
756
- : // eslint-disable-next-line total-functions/no-partial-division
757
- seq(asUint32(Math.ceil(array.length / chunkSize))).map((i: Uint32) =>
758
- array.slice(chunkSize * i, chunkSize * (i + 1)),
759
- );
760
-
761
- /**
762
- * Alias for `partition`.
763
- *
764
- * @see {@link partition}
765
- */
766
- export const chunk = partition;
767
-
768
692
  /**
769
693
  * Concatenates two arrays.
770
694
  *
@@ -805,22 +729,6 @@ export const concat = <
805
729
  // eslint-disable-next-line total-functions/no-unsafe-type-assertion
806
730
  [...array1, ...array2] as never;
807
731
 
808
- /**
809
- * Return type of {@link concat}: the concatenated tuple `readonly [...Ar1,
810
- * ...Ar2]`, additionally branded as {@link NonEmptyArray} when either input is
811
- * a non-empty tuple (a plain non-empty tuple or a branded `NonEmptyArray`,
812
- * both of which extend `NonEmptyTuple<unknown>`).
813
- */
814
- type ConcatResult<
815
- Ar1 extends readonly unknown[],
816
- Ar2 extends readonly unknown[],
817
- > =
818
- Ar1 extends NonEmptyTuple<unknown>
819
- ? readonly [...Ar1, ...Ar2] & NonEmptyArray<Ar1[number] | Ar2[number]>
820
- : Ar2 extends NonEmptyTuple<unknown>
821
- ? readonly [...Ar1, ...Ar2] & NonEmptyArray<Ar1[number] | Ar2[number]>
822
- : readonly [...Ar1, ...Ar2];
823
-
824
732
  /**
825
733
  * Groups elements by a key derived from each element.
826
734
  *
@@ -985,3 +893,95 @@ export const cartesianProduct = <T,>(
985
893
 
986
894
  return first.flatMap((item) => restProduct.map((prod) => [item, ...prod]));
987
895
  };
896
+
897
+ const scanImpl = <E, S>(
898
+ array: readonly E[],
899
+ reducer: (accumulator: S, currentValue: E, currentIndex: SizeType.Arr) => S,
900
+ init: S,
901
+ ): NonEmptyTuple<S> => {
902
+ const mut_result = castMutable<NonEmptyTuple<S>>(
903
+ newArray<PositiveUint32, S>(asPositiveUint32(array.length + 1), init),
904
+ );
905
+
906
+ let mut_acc = init;
907
+
908
+ for (const [index, value] of array.entries()) {
909
+ mut_acc = reducer(mut_acc, value, asUint32(index));
910
+
911
+ mut_result[index + 1] = mut_acc;
912
+ }
913
+
914
+ return mut_result;
915
+ };
916
+
917
+ /** The result of sorting `Ar`: same elements, same length, same bounds. */
918
+ type SortedResult<Ar extends readonly unknown[]> =
919
+ HasLengthConstraint<Ar> extends true
920
+ ? ChangeArrayElement<Ar, Ar[number]>
921
+ : IsFixedLengthList<Ar> extends true
922
+ ? FixedLengthTuple<Ar['length'], Ar[number]>
923
+ : Ar extends NonEmptyTuple<unknown>
924
+ ? NonEmptyArray<Ar[number]>
925
+ : readonly Ar[number][];
926
+
927
+ const filterImpl = <E,>(
928
+ array: readonly E[],
929
+ predicate: (a: E, index: SizeType.Arr) => boolean,
930
+ ): readonly E[] => array.filter((a, i) => predicate(a, asUint32(i)));
931
+
932
+ const filterNotImpl = <E,>(
933
+ array: readonly E[],
934
+ predicate: (a: E, index: SizeType.Arr) => boolean,
935
+ ): readonly E[] => array.filter((a, i) => !predicate(a, asUint32(i)));
936
+
937
+ const flatImpl = (
938
+ array: readonly unknown[],
939
+ depth?: SafeUintWithSmallInt,
940
+ ): readonly unknown[] => array.flat(depth);
941
+
942
+ /**
943
+ * Element type of {@link partition}'s result: a chunk holds between 1 and `N`
944
+ * elements. The lower bound is what makes this worth spelling out — `partition`
945
+ * never emits an empty chunk, so indexed access into a chunk is not `undefined`
946
+ * at position `0`, unlike with a plain `readonly E[]`.
947
+ *
948
+ * Falls back to {@link NonEmptyArray} when `N` is not a bound the brand can
949
+ * express, which keeps that lower bound in every case.
950
+ */
951
+ type PartitionChunk<N, E> = N extends SupportedLength
952
+ ? BoundedLengthArray<1, N, E>
953
+ : NonEmptyArray<E>;
954
+
955
+ const partitionImpl = <E, N extends WithSmallInt<PositiveInt & SizeType.Arr>>(
956
+ array: readonly E[],
957
+ chunkSize: N,
958
+ ): readonly (readonly E[])[] =>
959
+ chunkSize < 1
960
+ ? ([] as const)
961
+ : // eslint-disable-next-line total-functions/no-partial-division
962
+ seq(asUint32(Math.ceil(array.length / chunkSize))).map((i: Uint32) =>
963
+ array.slice(chunkSize * i, chunkSize * (i + 1)),
964
+ );
965
+
966
+ /**
967
+ * Alias for `partition`.
968
+ *
969
+ * @see {@link partition}
970
+ */
971
+ export const chunk = partition;
972
+
973
+ /**
974
+ * Return type of {@link concat}: the concatenated tuple `readonly [...Ar1,
975
+ * ...Ar2]`, additionally branded as {@link NonEmptyArray} when either input is
976
+ * a non-empty tuple (a plain non-empty tuple or a branded `NonEmptyArray`, both
977
+ * of which extend `NonEmptyTuple<unknown>`).
978
+ */
979
+ type ConcatResult<
980
+ Ar1 extends readonly unknown[],
981
+ Ar2 extends readonly unknown[],
982
+ > =
983
+ Ar1 extends NonEmptyTuple<unknown>
984
+ ? readonly [...Ar1, ...Ar2] & NonEmptyArray<Ar1[number] | Ar2[number]>
985
+ : Ar2 extends NonEmptyTuple<unknown>
986
+ ? readonly [...Ar1, ...Ar2] & NonEmptyArray<Ar1[number] | Ar2[number]>
987
+ : readonly [...Ar1, ...Ar2];
@@ -12,6 +12,7 @@ import { SafeUint } from '../../number/index.mjs';
12
12
  import { asMinLengthArray } from './array-utils-length-bounded-array-cast.mjs';
13
13
  import {
14
14
  cartesianProduct,
15
+ chunk,
15
16
  concat,
16
17
  filter,
17
18
  filterNot,
@@ -71,8 +72,8 @@ describe('Arr transformations', () => {
71
72
 
72
73
  test('should preserve the tuple shape under a generic parameter', () => {
73
74
  // A caller that is itself generic over the tuple cannot resolve a
74
- // conditional return type, so `map` has to report the homomorphic
75
- // mapping directly for a parameter that carries no length brand.
75
+ // conditional return type, so `map` has to report the homomorphic mapping
76
+ // directly for a parameter that carries no length brand.
76
77
  const mapValues = <const T extends readonly Readonly<{ v: unknown }>[]>(
77
78
  boxes: T,
78
79
  ): Readonly<{ [K in keyof T]: unknown }> => map(boxes, (b) => b.v);
@@ -759,13 +760,61 @@ describe('Arr transformations', () => {
759
760
  ]);
760
761
  });
761
762
 
762
- test('should work with chunk size < 2 (returns empty)', () => {
763
+ test('returns singleton chunks for size one', () => {
763
764
  const numbers = [1, 2, 3] as const;
764
765
 
765
766
  const result = partition(numbers, 1);
766
767
 
767
- // According to docs, returns empty array if chunkSize < 2
768
- assert.deepStrictEqual(result, []);
768
+ expectType<typeof result, readonly BoundedLengthArray<1, 1, 1 | 2 | 3>[]>(
769
+ '~=',
770
+ );
771
+
772
+ assert.deepStrictEqual<readonly (readonly number[])[]>(result, [
773
+ [1],
774
+ [2],
775
+ [3],
776
+ ]);
777
+ });
778
+
779
+ test('preserves singleton chunks through the alias and curried overloads', () => {
780
+ const numbers = [1, 2, 3] as const;
781
+
782
+ const expected = [[1], [2], [3]] as const;
783
+
784
+ assert.deepStrictEqual<readonly (readonly number[])[]>(
785
+ partition(1)(numbers),
786
+ expected,
787
+ );
788
+
789
+ assert.deepStrictEqual<readonly (readonly number[])[]>(
790
+ chunk(numbers, 1),
791
+ expected,
792
+ );
793
+
794
+ assert.deepStrictEqual<readonly (readonly number[])[]>(
795
+ chunk(1)(numbers),
796
+ expected,
797
+ );
798
+ });
799
+
800
+ test('size one produces no chunk for an empty array', () => {
801
+ assert.deepStrictEqual(partition([], 1), []);
802
+
803
+ assert.deepStrictEqual(partition(1)([]), []);
804
+
805
+ assert.deepStrictEqual(chunk([], 1), []);
806
+
807
+ assert.deepStrictEqual(chunk(1)([]), []);
808
+ });
809
+
810
+ test('a size below one, which the types reject, produces no chunk', () => {
811
+ const numbers = [1, 2, 3] as const;
812
+
813
+ // @ts-expect-error - a chunk size of 0 is not a positive integer
814
+ assert.deepStrictEqual(partition(numbers, 0), []);
815
+
816
+ // @ts-expect-error - a negative chunk size is not a positive integer
817
+ assert.deepStrictEqual(partition(-1)(numbers), []);
769
818
  });
770
819
 
771
820
  test('should work with chunk size larger than array', () => {
@@ -63,11 +63,11 @@ type Cast<A, B> = A extends B ? A : never;
63
63
  */
64
64
  // Type guard overloads - narrow the entire array type.
65
65
  //
66
- // Both narrowed types are intersected with `Ar`: a type predicate's type has
67
- // to be assignable to the parameter's type, and neither the deferred
68
- // conditional `ChangeArrayElement<Ar, S>` nor the mapped type over a generic
69
- // `Ar` is (TS2677). The intersection also keeps whatever the caller already
70
- // knew about the array — a branded five-tuple stays a five-tuple.
66
+ // Both narrowed types are intersected with `Ar`: a type predicate's type has to
67
+ // be assignable to the parameter's type, and neither the deferred conditional
68
+ // `ChangeArrayElement<Ar, S>` nor the mapped type over a generic `Ar` is
69
+ // (TS2677). The intersection also keeps whatever the caller already knew about
70
+ // the array — a branded five-tuple stays a five-tuple.
71
71
  export function every<
72
72
  const Ar extends readonly unknown[] & UnknownBrand,
73
73
  S extends Ar[number],
@@ -76,8 +76,8 @@ export function every<
76
76
  predicate: (a: Ar[number], index: ArrayIndex<Ar>) => a is S,
77
77
  ): array is ChangeArrayElement<Ar, S> & Ar;
78
78
 
79
- // A plain array or tuple carries no length brand, so the homomorphic mapping
80
- // is the whole answer — same reasoning as in `map`.
79
+ // A plain array or tuple carries no length brand, so the homomorphic mapping is
80
+ // the whole answer — same reasoning as in `map`.
81
81
  export function every<
82
82
  const Ar extends readonly unknown[],
83
83
  S extends Ar[number],
@@ -98,8 +98,8 @@ export function every<const Ar extends readonly unknown[]>(
98
98
  // returned function*, not two overloads of `every` itself: both curried
99
99
  // signatures take the same single predicate, so overload resolution would
100
100
  // always pick the first and the second would be dead — which is what left a
101
- // plain `readonly unknown[]` unable to satisfy the `UnknownBrand` constraint
102
- // at the call site of the returned guard.
101
+ // plain `readonly unknown[]` unable to satisfy the `UnknownBrand` constraint at
102
+ // the call site of the returned guard.
103
103
  //
104
104
  // The intersection is also what keeps the group compatible with the
105
105
  // implementation signature. A lone `<Ar extends readonly E[] & UnknownBrand>`
@@ -136,11 +136,6 @@ export function every<E>(
136
136
  }
137
137
  }
138
138
 
139
- const everyImpl = <E,>(
140
- array: readonly E[],
141
- predicate: (a: E, index: SizeType.Arr) => boolean,
142
- ): boolean => array.every((a, i) => predicate(a, asUint32(i)));
143
-
144
139
  /**
145
140
  * Tests whether at least one element in an array passes a test implemented by a predicate.
146
141
  *
@@ -186,11 +181,6 @@ export function some<E>(
186
181
  }
187
182
  }
188
183
 
189
- const someImpl = <E,>(
190
- array: readonly E[],
191
- predicate: (a: E, index: SizeType.Arr) => boolean,
192
- ): boolean => array.some((a, i) => predicate(a, asUint32(i)));
193
-
194
184
  /**
195
185
  * Checks if an index is within the valid range of an array.
196
186
  *
@@ -212,3 +202,13 @@ export const indexIsInRange = <E,>(
212
202
  array: readonly E[],
213
203
  index: SizeType.ArgArr,
214
204
  ): boolean => Num.isInRange(0, array.length)(index);
205
+
206
+ const everyImpl = <E,>(
207
+ array: readonly E[],
208
+ predicate: (a: E, index: SizeType.Arr) => boolean,
209
+ ): boolean => array.every((a, i) => predicate(a, asUint32(i)));
210
+
211
+ const someImpl = <E,>(
212
+ array: readonly E[],
213
+ predicate: (a: E, index: SizeType.Arr) => boolean,
214
+ ): boolean => array.some((a, i) => predicate(a, asUint32(i)));
@@ -39,7 +39,7 @@ describe('Arr validations', () => {
39
39
 
40
40
  assert.isFalse(isArray({}));
41
41
 
42
- assert.isFalse(isArray(new Set()));
42
+ assert.isFalse(isArray(new Set<unknown>()));
43
43
  });
44
44
 
45
45
  test('should refine union types correctly', () => {
@@ -125,8 +125,8 @@ type IMapMappedInterface<K, V, KM extends MapSetKeyType> = Readonly<{
125
125
  // Reducing a value
126
126
 
127
127
  /**
128
- * Checks if all elements in the map satisfy a predicate. Also supports a
129
- * type predicate overload that narrows the type of values in the map if the
128
+ * Checks if all elements in the map satisfy a predicate. Also supports a type
129
+ * predicate overload that narrows the type of values in the map if the
130
130
  * predicate returns true for all elements.
131
131
  *
132
132
  * @example
@@ -1161,9 +1161,9 @@ class IMapMappedClass<K, V, KM extends MapSetKeyType>
1161
1161
  break;
1162
1162
 
1163
1163
  case 'update': {
1164
- const curr = mut_result.get(key);
1165
-
1166
- if (curr === undefined || !mut_result.has(key)) {
1164
+ // Presence is `has` alone, as in `update`: a stored value may itself
1165
+ // be `undefined`.
1166
+ if (!mut_result.has(key)) {
1167
1167
  if (this.#showNotFoundMessage) {
1168
1168
  console.warn(
1169
1169
  `IMapMapped.withMutations::update: key not found: ${unknownToString(key)}`,
@@ -1173,7 +1173,9 @@ class IMapMappedClass<K, V, KM extends MapSetKeyType>
1173
1173
  break;
1174
1174
  }
1175
1175
 
1176
- mut_result.set(key, action.updater(curr));
1176
+ // `has` held, so `get` returns the stored `V`, `undefined` included.
1177
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
1178
+ mut_result.set(key, action.updater(mut_result.get(key) as V));
1177
1179
 
1178
1180
  break;
1179
1181
  }
@@ -705,6 +705,33 @@ describe('IMapMapped.withMutations', () => {
705
705
  expect(Optional.unwrap(updated.get({ id: 2, type: 'admin' }))).toBe('Bob');
706
706
  });
707
707
 
708
+ test('update calls the updater for a key whose value is undefined, as update() does', () => {
709
+ const map = IMapMapped.create<TestKey, string | undefined, string>(
710
+ [[{ id: 1, type: 'user' }, undefined]],
711
+ testKeyToString,
712
+ stringToTestKey,
713
+ );
714
+
715
+ const updater = (x: string | undefined): string | undefined =>
716
+ x ?? 'filled';
717
+
718
+ const batched = map.withMutations([
719
+ { type: 'update', key: { id: 1, type: 'user' }, updater },
720
+ ]);
721
+
722
+ expect(Optional.unwrap(batched.get({ id: 1, type: 'user' }))).toBe(
723
+ 'filled',
724
+ );
725
+
726
+ expect(
727
+ Optional.unwrap(
728
+ map
729
+ .update({ id: 1, type: 'user' }, updater)
730
+ .get({ id: 1, type: 'user' }),
731
+ ),
732
+ ).toBe('filled');
733
+ });
734
+
708
735
  test('should handle empty mutations array', () => {
709
736
  const map = IMapMapped.create(
710
737
  [[{ id: 1, type: 'user' }, 'Alice']],
@@ -96,8 +96,8 @@ type IMapInterface<K extends MapSetKeyType, V> = Readonly<{
96
96
  // Reducing a value
97
97
 
98
98
  /**
99
- * Checks if all elements in the map satisfy a predicate. Also supports a
100
- * type predicate overload that narrows the type of values in the map if the
99
+ * Checks if all elements in the map satisfy a predicate. Also supports a type
100
+ * predicate overload that narrows the type of values in the map if the
101
101
  * predicate returns true for all elements.
102
102
  *
103
103
  * @example
@@ -656,8 +656,7 @@ export namespace IMap {
656
656
  *
657
657
  * Two IMaps are considered equal if they have the same size and contain
658
658
  * exactly the same key-value pairs. The order of entries does not matter for
659
- * equality comparison. Values are compared using JavaScript's `===`
660
- * operator.
659
+ * equality comparison. Values are compared using JavaScript's `===` operator.
661
660
  *
662
661
  * **Performance:** O(n) where n is the size of the smaller map.
663
662
  *
@@ -881,9 +880,9 @@ class IMapClass<K extends MapSetKeyType, V>
881
880
  case 'update': {
882
881
  const { key } = action;
883
882
 
884
- const curr = mut_result.get(key);
885
-
886
- if (curr === undefined || !mut_result.has(key)) {
883
+ // Presence is `has` alone, as in `update`: a stored value may itself
884
+ // be `undefined`.
885
+ if (!mut_result.has(key)) {
887
886
  if (this.#showNotFoundMessage) {
888
887
  const keyStr = unknownToString(key);
889
888
 
@@ -893,7 +892,9 @@ class IMapClass<K extends MapSetKeyType, V>
893
892
  break;
894
893
  }
895
894
 
896
- mut_result.set(key, action.updater(curr));
895
+ // `has` held, so `get` returns the stored `V`, `undefined` included.
896
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
897
+ mut_result.set(key, action.updater(mut_result.get(key) as V));
897
898
 
898
899
  break;
899
900
  }
@@ -81,7 +81,7 @@ describe('IMap.create', () => {
81
81
  });
82
82
 
83
83
  test('should create map from JavaScript Map', () => {
84
- const jsMap = new Map([
84
+ const jsMap = new Map<string, number>([
85
85
  ['a', 1],
86
86
  ['b', 2],
87
87
  ]);
@@ -754,6 +754,19 @@ describe('IMap.withMutations', () => {
754
754
  assert.isTrue(Optional.isNone(updated.get('nonexistent')));
755
755
  });
756
756
 
757
+ test('update calls the updater for a key whose value is undefined, as update() does', () => {
758
+ const map = IMap.create<string, number | undefined>([['a', undefined]]);
759
+
760
+ const updater = (x: number | undefined): number | undefined =>
761
+ x === undefined ? 1 : x + 1;
762
+
763
+ const batched = map.withMutations([{ type: 'update', key: 'a', updater }]);
764
+
765
+ expect(Optional.unwrap(batched.get('a'))).toBe(1);
766
+
767
+ expect(Optional.unwrap(map.update('a', updater).get('a'))).toBe(1);
768
+ });
769
+
757
770
  test('should handle mixed operations', () => {
758
771
  const map = IMap.create([['a', 1]]);
759
772
 
@@ -138,8 +138,7 @@ type ISetMappedInterface<K, KM extends MapSetKeyType> = Readonly<{
138
138
 
139
139
  /**
140
140
  * Checks if all elements in the set satisfy a type predicate. Narrows the
141
- * type of elements in the set if the predicate returns true for all
142
- * elements.
141
+ * type of elements in the set if the predicate returns true for all elements.
143
142
  *
144
143
  *
145
144
  * @example
@@ -89,9 +89,9 @@ type ISetInterface<K extends MapSetKeyType> = Readonly<{
89
89
  // Reducing a value
90
90
 
91
91
  /**
92
- * Checks if all elements in the set satisfy a predicate. Also supports a
93
- * type predicate overload that narrows the type of elements in the set if
94
- * the predicate returns true for all elements.
92
+ * Checks if all elements in the set satisfy a predicate. Also supports a type
93
+ * predicate overload that narrows the type of elements in the set if the
94
+ * predicate returns true for all elements.
95
95
  *
96
96
  * @example
97
97
  *
@@ -539,8 +539,7 @@ export namespace ISet {
539
539
  *
540
540
  * This factory function accepts any iterable of elements, including arrays,
541
541
  * JavaScript Sets, other ISets, or custom iterables. Duplicate elements in
542
- * the input iterable will be automatically deduplicated in the resulting
543
- * set.
542
+ * the input iterable will be automatically deduplicated in the resulting set.
544
543
  *
545
544
  * **Performance:** O(n) where n is the number of elements in the iterable.
546
545
  *
@@ -39,7 +39,7 @@ describe('ISet.create', () => {
39
39
  });
40
40
 
41
41
  test('should create set from JavaScript Set', () => {
42
- const jsSet = new Set(['a', 'b', 'c']);
42
+ const jsSet = new Set<string>(['a', 'b', 'c']);
43
43
 
44
44
  const set = ISet.create(jsSet);
45
45