ts-data-forge 12.2.1 → 12.2.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.
- package/README.md +3 -6
- package/dist/array/impl/array-utils-creation.d.mts +1 -1
- package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts.map +1 -1
- package/dist/array/impl/array-utils-element-access.mjs +10 -13
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/array/impl/array-utils-iterators.d.mts +1 -1
- package/dist/array/impl/array-utils-iterators.d.mts.map +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts.map +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-cast.mjs +53 -53
- package/dist/array/impl/array-utils-length-bounded-array-cast.mjs.map +1 -1
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs +61 -70
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +37 -47
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-search.d.mts.map +1 -1
- package/dist/array/impl/array-utils-search.mjs +80 -92
- package/dist/array/impl/array-utils-search.mjs.map +1 -1
- package/dist/array/impl/array-utils-set-op.d.mts +1 -1
- package/dist/array/impl/array-utils-set-op.d.mts.map +1 -1
- package/dist/array/impl/array-utils-size.d.mts +1 -1
- package/dist/array/impl/array-utils-size.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slice-clamped.mjs +9 -10
- package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs +20 -32
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +41 -52
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-validation.mjs +10 -16
- package/dist/array/impl/array-utils-validation.mjs.map +1 -1
- package/dist/json/json.mjs +1 -1
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/branded-types/finite-number.d.mts +6 -3
- package/dist/number/branded-types/finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +8 -4
- package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +2 -1
- package/dist/number/branded-types/negative-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +2 -1
- package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +6 -3
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +6 -3
- package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +6 -3
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +6 -3
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
- package/dist/number/enum/int8.d.mts +26 -13
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/uint8.d.mts +24 -12
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/others/if-then.d.mts +1 -1
- package/dist/others/if-then.d.mts.map +1 -1
- package/dist/string/str.d.mts.map +1 -1
- package/dist/string/str.mjs +52 -52
- package/dist/string/str.mjs.map +1 -1
- package/package.json +4 -4
- package/src/array/impl/array-utils-element-access.mts +15 -16
- package/src/array/impl/array-utils-length-bounded-array-cast.mts +73 -60
- package/src/array/impl/array-utils-modification.mts +96 -94
- package/src/array/impl/array-utils-reducing-value.mts +68 -57
- package/src/array/impl/array-utils-search.mts +146 -148
- package/src/array/impl/array-utils-slice-clamped.mts +16 -14
- package/src/array/impl/array-utils-slicing.mts +37 -40
- package/src/array/impl/array-utils-transformation.mts +64 -62
- package/src/array/impl/array-utils-transformation.test.mts +1 -1
- package/src/array/impl/array-utils-validation.mts +18 -20
- package/src/collections/iset.test.mts +2 -2
- package/src/functional/pipe.test.mts +1 -1
- package/src/json/json.mts +1 -1
- package/src/string/str.mts +74 -61
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-data-forge",
|
|
3
|
-
"version": "12.2.
|
|
3
|
+
"version": "12.2.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@sindresorhus/is": "^8.1.0",
|
|
39
|
-
"ts-type-forge": "7.
|
|
39
|
+
"ts-type-forge": "~7.2.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@emotion/react": "11.14.0",
|
|
43
43
|
"@emotion/styled": "11.14.1",
|
|
44
44
|
"@mui/material": "9.2.0",
|
|
45
45
|
"@types/react": "19.2.17",
|
|
46
|
-
"immer": "11.1.
|
|
46
|
+
"immer": "11.1.15",
|
|
47
47
|
"react": "19.2.7",
|
|
48
|
-
"typedoc": "0.28.
|
|
48
|
+
"typedoc": "0.28.20",
|
|
49
49
|
"typedoc-github-theme": "0.4.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
@@ -45,26 +45,25 @@ export function at<E>(
|
|
|
45
45
|
| readonly [index: SizeType.ArgArrWithNegative]
|
|
46
46
|
): Optional<E> | ((array: readonly E[]) => Optional<E>) {
|
|
47
47
|
switch (args.length) {
|
|
48
|
-
case 2:
|
|
49
|
-
|
|
48
|
+
case 2:
|
|
49
|
+
return atImpl(...args);
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
normalizedIndex < 0 || normalizedIndex >= array.length
|
|
54
|
-
? Optional.none
|
|
55
|
-
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
56
|
-
Optional.some(array[normalizedIndex]!),
|
|
57
|
-
).value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
case 1: {
|
|
61
|
-
const [index] = args;
|
|
62
|
-
|
|
63
|
-
return (array) => at(array, index);
|
|
64
|
-
}
|
|
51
|
+
case 1:
|
|
52
|
+
return (array) => atImpl(array, ...args);
|
|
65
53
|
}
|
|
66
54
|
}
|
|
67
55
|
|
|
56
|
+
const atImpl = <E,>(
|
|
57
|
+
array: readonly E[],
|
|
58
|
+
index: SizeType.ArgArrWithNegative,
|
|
59
|
+
): Optional<E> =>
|
|
60
|
+
pipe(index < 0 ? array.length + index : index).map((normalizedIndex) =>
|
|
61
|
+
normalizedIndex < 0 || normalizedIndex >= array.length
|
|
62
|
+
? Optional.none
|
|
63
|
+
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
64
|
+
Optional.some(array[normalizedIndex]!),
|
|
65
|
+
).value;
|
|
66
|
+
|
|
68
67
|
/**
|
|
69
68
|
* Returns the first element of an array as an Optional.
|
|
70
69
|
*
|
|
@@ -66,25 +66,28 @@ export function asFixedLengthArray<E>(
|
|
|
66
66
|
| readonly [length: SupportedLength]
|
|
67
67
|
): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
|
|
68
68
|
switch (args.length) {
|
|
69
|
-
case 2:
|
|
70
|
-
|
|
69
|
+
case 2:
|
|
70
|
+
return asFixedLengthArrayImpl(...args);
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return xs;
|
|
79
|
-
}
|
|
72
|
+
case 1:
|
|
73
|
+
return (xs) => asFixedLengthArrayImpl(xs, ...args);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
const asFixedLengthArrayImpl = <E,>(
|
|
78
|
+
xs: readonly E[],
|
|
79
|
+
length: SupportedLength,
|
|
80
|
+
): readonly E[] => {
|
|
81
|
+
const actualLength = xs.length;
|
|
83
82
|
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
if (!isFixedLengthArray(xs, length)) {
|
|
84
|
+
throw new TypeError(
|
|
85
|
+
`Expected an array of length ${length}, got an array of length ${actualLength}`,
|
|
86
|
+
);
|
|
86
87
|
}
|
|
87
|
-
|
|
88
|
+
|
|
89
|
+
return xs;
|
|
90
|
+
};
|
|
88
91
|
|
|
89
92
|
/**
|
|
90
93
|
* Casts an array to `MinLengthArray<MinLength, E>` after checking that it has
|
|
@@ -139,25 +142,28 @@ export function asMinLengthArray<E>(
|
|
|
139
142
|
| readonly [minLength: SupportedLength]
|
|
140
143
|
): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
|
|
141
144
|
switch (args.length) {
|
|
142
|
-
case 2:
|
|
143
|
-
|
|
145
|
+
case 2:
|
|
146
|
+
return asMinLengthArrayImpl(...args);
|
|
144
147
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return xs;
|
|
152
|
-
}
|
|
148
|
+
case 1:
|
|
149
|
+
return (xs) => asMinLengthArrayImpl(xs, ...args);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
153
152
|
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
const asMinLengthArrayImpl = <E,>(
|
|
154
|
+
xs: readonly E[],
|
|
155
|
+
minLength: SupportedLength,
|
|
156
|
+
): readonly E[] => {
|
|
157
|
+
const actualLength = xs.length;
|
|
156
158
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
if (!isMinLengthArray(xs, minLength)) {
|
|
160
|
+
throw new TypeError(
|
|
161
|
+
`Expected an array of length >= ${minLength}, got an array of length ${actualLength}`,
|
|
162
|
+
);
|
|
159
163
|
}
|
|
160
|
-
|
|
164
|
+
|
|
165
|
+
return xs;
|
|
166
|
+
};
|
|
161
167
|
|
|
162
168
|
/**
|
|
163
169
|
* Casts an array to `MaxLengthArray<MaxLength, E>` after checking that it has
|
|
@@ -209,25 +215,28 @@ export function asMaxLengthArray<E>(
|
|
|
209
215
|
| readonly [maxLength: SupportedLength]
|
|
210
216
|
): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
|
|
211
217
|
switch (args.length) {
|
|
212
|
-
case 2:
|
|
213
|
-
|
|
218
|
+
case 2:
|
|
219
|
+
return asMaxLengthArrayImpl(...args);
|
|
214
220
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return xs;
|
|
222
|
-
}
|
|
221
|
+
case 1:
|
|
222
|
+
return (xs) => asMaxLengthArrayImpl(xs, ...args);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
223
225
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
+
const asMaxLengthArrayImpl = <E,>(
|
|
227
|
+
xs: readonly E[],
|
|
228
|
+
maxLength: SupportedLength,
|
|
229
|
+
): readonly E[] => {
|
|
230
|
+
const actualLength = xs.length;
|
|
226
231
|
|
|
227
|
-
|
|
228
|
-
|
|
232
|
+
if (!isMaxLengthArray(xs, maxLength)) {
|
|
233
|
+
throw new TypeError(
|
|
234
|
+
`Expected an array of length <= ${maxLength}, got an array of length ${actualLength}`,
|
|
235
|
+
);
|
|
229
236
|
}
|
|
230
|
-
|
|
237
|
+
|
|
238
|
+
return xs;
|
|
239
|
+
};
|
|
231
240
|
|
|
232
241
|
/**
|
|
233
242
|
* Casts an array to `BoundedLengthArray<MinLength, MaxLength, E>` after
|
|
@@ -298,25 +307,29 @@ export function asBoundedLengthArray<E>(
|
|
|
298
307
|
| readonly [minLength: SupportedLength, maxLength: SupportedLength]
|
|
299
308
|
): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
|
|
300
309
|
switch (args.length) {
|
|
301
|
-
case 3:
|
|
302
|
-
|
|
310
|
+
case 3:
|
|
311
|
+
return asBoundedLengthArrayImpl(...args);
|
|
303
312
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return xs;
|
|
311
|
-
}
|
|
313
|
+
case 2:
|
|
314
|
+
return (xs) => asBoundedLengthArrayImpl(xs, ...args);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
312
317
|
|
|
313
|
-
|
|
314
|
-
|
|
318
|
+
const asBoundedLengthArrayImpl = <E,>(
|
|
319
|
+
xs: readonly E[],
|
|
320
|
+
minLength: SupportedLength,
|
|
321
|
+
maxLength: SupportedLength,
|
|
322
|
+
): readonly E[] => {
|
|
323
|
+
const actualLength = xs.length;
|
|
315
324
|
|
|
316
|
-
|
|
317
|
-
|
|
325
|
+
if (!isBoundedLengthArray(xs, minLength, maxLength)) {
|
|
326
|
+
throw new TypeError(
|
|
327
|
+
`Expected an array of length in [${minLength}, ${maxLength}], got an array of length ${actualLength}`,
|
|
328
|
+
);
|
|
318
329
|
}
|
|
319
|
-
|
|
330
|
+
|
|
331
|
+
return xs;
|
|
332
|
+
};
|
|
320
333
|
|
|
321
334
|
/**
|
|
322
335
|
* Casts an array to `NonEmptyArray<E>` after checking that it has at least one
|
|
@@ -55,20 +55,20 @@ export function set<E, const V = E>(
|
|
|
55
55
|
| readonly [index: SizeType.ArgArr, newValue: V]
|
|
56
56
|
): readonly (E | V)[] | ((array: readonly E[]) => readonly (E | V)[]) {
|
|
57
57
|
switch (args.length) {
|
|
58
|
-
case 3:
|
|
59
|
-
|
|
58
|
+
case 3:
|
|
59
|
+
return setImpl(...args);
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
case 2: {
|
|
65
|
-
const [index, newValue] = args;
|
|
66
|
-
|
|
67
|
-
return (array) => set(array, index, newValue);
|
|
68
|
-
}
|
|
61
|
+
case 2:
|
|
62
|
+
return (array) => setImpl(array, ...args);
|
|
69
63
|
}
|
|
70
64
|
}
|
|
71
65
|
|
|
66
|
+
const setImpl = <E, const V = E>(
|
|
67
|
+
array: readonly E[],
|
|
68
|
+
index: SizeType.ArgArr,
|
|
69
|
+
newValue: V,
|
|
70
|
+
): readonly (E | V)[] => (array as readonly (E | V)[]).with(index, newValue);
|
|
71
|
+
|
|
72
72
|
/**
|
|
73
73
|
* Returns a new array with an element at the specified index updated by applying a function.
|
|
74
74
|
*
|
|
@@ -124,21 +124,22 @@ export function toUpdated<E, V = E>(
|
|
|
124
124
|
| readonly [index: SizeType.ArgArr, updater: (prev: E) => V]
|
|
125
125
|
): readonly (E | V)[] | ((array: readonly E[]) => readonly (E | V)[]) {
|
|
126
126
|
switch (args.length) {
|
|
127
|
-
case 3:
|
|
128
|
-
|
|
127
|
+
case 3:
|
|
128
|
+
return toUpdatedImpl(...args);
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
return (array
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
case 2: {
|
|
135
|
-
const [index, updater] = args;
|
|
136
|
-
|
|
137
|
-
return (array) => toUpdated(array, index, updater);
|
|
138
|
-
}
|
|
130
|
+
case 2:
|
|
131
|
+
return (array) => toUpdatedImpl(array, ...args);
|
|
139
132
|
}
|
|
140
133
|
}
|
|
141
134
|
|
|
135
|
+
const toUpdatedImpl = <E, V = E>(
|
|
136
|
+
array: readonly E[],
|
|
137
|
+
index: SizeType.ArgArr,
|
|
138
|
+
updater: (prev: E) => V,
|
|
139
|
+
): readonly (E | V)[] =>
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
141
|
+
(array as readonly (E | V)[]).with(index, updater(array[index]!));
|
|
142
|
+
|
|
142
143
|
/**
|
|
143
144
|
* Returns a new array with a value inserted at the specified index.
|
|
144
145
|
*
|
|
@@ -187,25 +188,26 @@ export function toInserted<E, const V = E>(
|
|
|
187
188
|
| readonly [index: SizeType.ArgArrWithNegative, newValue: V]
|
|
188
189
|
): NonEmptyArray<E | V> | ((array: readonly E[]) => NonEmptyArray<E | V>) {
|
|
189
190
|
switch (args.length) {
|
|
190
|
-
case 3:
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
return (array
|
|
195
|
-
index,
|
|
196
|
-
0,
|
|
197
|
-
newValue,
|
|
198
|
-
) as unknown as NonEmptyArray<E | V>;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
case 2: {
|
|
202
|
-
const [index, newValue] = args;
|
|
203
|
-
|
|
204
|
-
return (array) => toInserted(array, index, newValue);
|
|
205
|
-
}
|
|
191
|
+
case 3:
|
|
192
|
+
return toInsertedImpl(...args);
|
|
193
|
+
|
|
194
|
+
case 2:
|
|
195
|
+
return (array) => toInsertedImpl(array, ...args);
|
|
206
196
|
}
|
|
207
197
|
}
|
|
208
198
|
|
|
199
|
+
const toInsertedImpl = <E, const V = E>(
|
|
200
|
+
array: readonly E[],
|
|
201
|
+
index: SizeType.ArgArrWithNegative,
|
|
202
|
+
newValue: V,
|
|
203
|
+
): NonEmptyArray<E | V> =>
|
|
204
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
205
|
+
(array as readonly (E | V)[]).toSpliced(
|
|
206
|
+
index,
|
|
207
|
+
0,
|
|
208
|
+
newValue,
|
|
209
|
+
) as unknown as NonEmptyArray<E | V>;
|
|
210
|
+
|
|
209
211
|
type CastToNumber<T> = T extends number ? T : never;
|
|
210
212
|
|
|
211
213
|
/**
|
|
@@ -240,20 +242,19 @@ export function toRemoved<E>(
|
|
|
240
242
|
| readonly [index: SizeType.ArgArrWithNegative]
|
|
241
243
|
): readonly E[] | ((array: readonly E[]) => readonly E[]) {
|
|
242
244
|
switch (args.length) {
|
|
243
|
-
case 2:
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
return array.toSpliced(index, 1);
|
|
247
|
-
}
|
|
245
|
+
case 2:
|
|
246
|
+
return toRemovedImpl(...args);
|
|
248
247
|
|
|
249
|
-
case 1:
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return (array) => toRemoved(array, index);
|
|
253
|
-
}
|
|
248
|
+
case 1:
|
|
249
|
+
return (array) => toRemovedImpl(array, ...args);
|
|
254
250
|
}
|
|
255
251
|
}
|
|
256
252
|
|
|
253
|
+
const toRemovedImpl = <E,>(
|
|
254
|
+
array: readonly E[],
|
|
255
|
+
index: SizeType.ArgArrWithNegative,
|
|
256
|
+
): readonly E[] => array.toSpliced(index, 1);
|
|
257
|
+
|
|
257
258
|
/**
|
|
258
259
|
* Returns a new array with a value appended to the end.
|
|
259
260
|
*
|
|
@@ -291,24 +292,21 @@ export function toPushed<const Ar extends readonly unknown[], const V>(
|
|
|
291
292
|
...args: readonly [array: Ar, newValue: V] | readonly [newValue: V]
|
|
292
293
|
): readonly [...Ar, V] | ((array: Ar) => readonly [...Ar, V]) {
|
|
293
294
|
switch (args.length) {
|
|
294
|
-
case 2:
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
298
|
-
return array.toSpliced(array.length, 0, newValue) as unknown as readonly [
|
|
299
|
-
...Ar,
|
|
300
|
-
V,
|
|
301
|
-
];
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
case 1: {
|
|
305
|
-
const [newValue] = args;
|
|
295
|
+
case 2:
|
|
296
|
+
return toPushedImpl(...args);
|
|
306
297
|
|
|
307
|
-
|
|
308
|
-
|
|
298
|
+
case 1:
|
|
299
|
+
return (array) => toPushedImpl(array, ...args);
|
|
309
300
|
}
|
|
310
301
|
}
|
|
311
302
|
|
|
303
|
+
const toPushedImpl = <const Ar extends readonly unknown[], const V>(
|
|
304
|
+
array: Ar,
|
|
305
|
+
newValue: V,
|
|
306
|
+
): readonly [...Ar, V] =>
|
|
307
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
308
|
+
array.toSpliced(array.length, 0, newValue) as unknown as readonly [...Ar, V];
|
|
309
|
+
|
|
312
310
|
/**
|
|
313
311
|
* Returns a new array with a value prepended to the beginning.
|
|
314
312
|
*
|
|
@@ -346,21 +344,21 @@ export function toUnshifted<Ar extends readonly unknown[], const V>(
|
|
|
346
344
|
...args: readonly [array: Ar, newValue: V] | readonly [newValue: V]
|
|
347
345
|
): readonly [V, ...Ar] | ((array: Ar) => readonly [V, ...Ar]) {
|
|
348
346
|
switch (args.length) {
|
|
349
|
-
case 2:
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
353
|
-
return array.toSpliced(0, 0, newValue) as unknown as readonly [V, ...Ar];
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
case 1: {
|
|
357
|
-
const [newValue] = args;
|
|
347
|
+
case 2:
|
|
348
|
+
return toUnshiftedImpl(...args);
|
|
358
349
|
|
|
359
|
-
|
|
360
|
-
|
|
350
|
+
case 1:
|
|
351
|
+
return (array) => toUnshiftedImpl(array, ...args);
|
|
361
352
|
}
|
|
362
353
|
}
|
|
363
354
|
|
|
355
|
+
const toUnshiftedImpl = <Ar extends readonly unknown[], const V>(
|
|
356
|
+
array: Ar,
|
|
357
|
+
newValue: V,
|
|
358
|
+
): readonly [V, ...Ar] =>
|
|
359
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
360
|
+
array.toSpliced(0, 0, newValue) as unknown as readonly [V, ...Ar];
|
|
361
|
+
|
|
364
362
|
/**
|
|
365
363
|
* Returns a new array with all elements replaced by the specified value.
|
|
366
364
|
*
|
|
@@ -402,20 +400,17 @@ export function toFilled<E>(
|
|
|
402
400
|
...args: readonly [array: readonly E[], value: E] | readonly [value: E]
|
|
403
401
|
): readonly E[] | ((array: readonly E[]) => readonly E[]) {
|
|
404
402
|
switch (args.length) {
|
|
405
|
-
case 2:
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
return create(asPositiveUint32(array.length), value);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
case 1: {
|
|
412
|
-
const [value] = args;
|
|
403
|
+
case 2:
|
|
404
|
+
return toFilledImpl(...args);
|
|
413
405
|
|
|
414
|
-
|
|
415
|
-
|
|
406
|
+
case 1:
|
|
407
|
+
return (array) => toFilledImpl(array, ...args);
|
|
416
408
|
}
|
|
417
409
|
}
|
|
418
410
|
|
|
411
|
+
const toFilledImpl = <E,>(array: readonly E[], value: E): readonly E[] =>
|
|
412
|
+
create(asPositiveUint32(array.length), value);
|
|
413
|
+
|
|
419
414
|
/**
|
|
420
415
|
* Returns a new array with elements in the specified range replaced by the specified value.
|
|
421
416
|
*
|
|
@@ -480,20 +475,27 @@ export function toRangeFilled<E, const V>(
|
|
|
480
475
|
]
|
|
481
476
|
): readonly (E | V)[] | ((array: readonly E[]) => readonly (E | V)[]) {
|
|
482
477
|
switch (args.length) {
|
|
483
|
-
case 3:
|
|
484
|
-
|
|
478
|
+
case 3:
|
|
479
|
+
return toRangeFilledImpl(...args);
|
|
485
480
|
|
|
486
|
-
|
|
481
|
+
case 2:
|
|
482
|
+
return (array) => toRangeFilledImpl(array, ...args);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
487
485
|
|
|
488
|
-
|
|
486
|
+
const toRangeFilledImpl = <E, const V>(
|
|
487
|
+
array: readonly E[],
|
|
488
|
+
value: V,
|
|
489
|
+
fillRange: readonly [
|
|
490
|
+
start: SizeType.ArgArrWithNegative,
|
|
491
|
+
end: SizeType.ArgArrWithNegative,
|
|
492
|
+
],
|
|
493
|
+
): readonly (E | V)[] => {
|
|
494
|
+
const [start, end] = fillRange;
|
|
489
495
|
|
|
490
|
-
|
|
491
|
-
}
|
|
496
|
+
const mut_cp: (E | V)[] = castMutable(copy(array));
|
|
492
497
|
|
|
493
|
-
|
|
494
|
-
const [value, fillRange] = args;
|
|
498
|
+
mut_cp.fill(value, start, end);
|
|
495
499
|
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
+
return mut_cp;
|
|
501
|
+
};
|
|
@@ -239,24 +239,24 @@ export function count<E>(
|
|
|
239
239
|
| readonly [predicate: (value: E, index: SizeType.Arr) => boolean]
|
|
240
240
|
): SizeType.Arr | ((array: readonly E[]) => SizeType.Arr) {
|
|
241
241
|
switch (args.length) {
|
|
242
|
-
case 2:
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
return array.reduce<Uint32>(
|
|
246
|
-
(acc, curr, index) =>
|
|
247
|
-
predicate(curr, asUint32(index)) ? Uint32.add(acc, 1) : acc,
|
|
248
|
-
asUint32(0),
|
|
249
|
-
);
|
|
250
|
-
}
|
|
242
|
+
case 2:
|
|
243
|
+
return countImpl(...args);
|
|
251
244
|
|
|
252
|
-
case 1:
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
return (array) => count(array, predicate);
|
|
256
|
-
}
|
|
245
|
+
case 1:
|
|
246
|
+
return (array) => countImpl(array, ...args);
|
|
257
247
|
}
|
|
258
248
|
}
|
|
259
249
|
|
|
250
|
+
const countImpl = <E,>(
|
|
251
|
+
array: readonly E[],
|
|
252
|
+
predicate: (value: E, index: SizeType.Arr) => boolean,
|
|
253
|
+
): SizeType.Arr =>
|
|
254
|
+
array.reduce<Uint32>(
|
|
255
|
+
(acc, curr, index) =>
|
|
256
|
+
predicate(curr, asUint32(index)) ? Uint32.add(acc, 1) : acc,
|
|
257
|
+
asUint32(0),
|
|
258
|
+
);
|
|
259
|
+
|
|
260
260
|
/**
|
|
261
261
|
* Groups elements by a key and counts the elements in each group.
|
|
262
262
|
*
|
|
@@ -295,29 +295,30 @@ export function countBy<E, G extends MapSetKeyType>(
|
|
|
295
295
|
| readonly [grouper: (value: E, index: SizeType.Arr) => G]
|
|
296
296
|
): IMap<G, SizeType.Arr> | ((array: readonly E[]) => IMap<G, SizeType.Arr>) {
|
|
297
297
|
switch (args.length) {
|
|
298
|
-
case 2:
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const mut_groups = new Map<G, SizeType.Arr>();
|
|
302
|
-
|
|
303
|
-
for (const [index, e] of array.entries()) {
|
|
304
|
-
const key = grouper(e, asUint32(index));
|
|
298
|
+
case 2:
|
|
299
|
+
return countByImpl(...args);
|
|
305
300
|
|
|
306
|
-
|
|
301
|
+
case 1:
|
|
302
|
+
return (array) => countByImpl(array, ...args);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
307
305
|
|
|
308
|
-
|
|
309
|
-
|
|
306
|
+
const countByImpl = <E, G extends MapSetKeyType>(
|
|
307
|
+
array: readonly E[],
|
|
308
|
+
grouper: (value: E, index: SizeType.Arr) => G,
|
|
309
|
+
): IMap<G, SizeType.Arr> => {
|
|
310
|
+
const mut_groups = new Map<G, SizeType.Arr>();
|
|
310
311
|
|
|
311
|
-
|
|
312
|
-
|
|
312
|
+
for (const [index, e] of array.entries()) {
|
|
313
|
+
const key = grouper(e, asUint32(index));
|
|
313
314
|
|
|
314
|
-
|
|
315
|
-
const [grouper] = args;
|
|
315
|
+
const curr = mut_groups.get(key) ?? 0;
|
|
316
316
|
|
|
317
|
-
|
|
318
|
-
}
|
|
317
|
+
mut_groups.set(key, asUint32(curr + 1));
|
|
319
318
|
}
|
|
320
|
-
|
|
319
|
+
|
|
320
|
+
return IMap.create(mut_groups);
|
|
321
|
+
};
|
|
321
322
|
|
|
322
323
|
/**
|
|
323
324
|
* Reduces an array to a single value from left to right.
|
|
@@ -379,23 +380,28 @@ export function foldl<E, P>(
|
|
|
379
380
|
]
|
|
380
381
|
): P | ((array: readonly E[]) => P) {
|
|
381
382
|
switch (args.length) {
|
|
382
|
-
case 3:
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
return array.reduce(
|
|
386
|
-
(prev, curr, index) => callbackfn(prev, curr, asUint32(index)),
|
|
387
|
-
initialValue,
|
|
388
|
-
);
|
|
389
|
-
}
|
|
383
|
+
case 3:
|
|
384
|
+
return foldlImpl(...args);
|
|
390
385
|
|
|
391
|
-
case 2:
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
return (array) => foldl(array, callbackfn, initialValue);
|
|
395
|
-
}
|
|
386
|
+
case 2:
|
|
387
|
+
return (array) => foldlImpl(array, ...args);
|
|
396
388
|
}
|
|
397
389
|
}
|
|
398
390
|
|
|
391
|
+
const foldlImpl = <E, P>(
|
|
392
|
+
array: readonly E[],
|
|
393
|
+
callbackfn: (
|
|
394
|
+
previousValue: P,
|
|
395
|
+
currentValue: E,
|
|
396
|
+
currentIndex: SizeType.Arr,
|
|
397
|
+
) => P,
|
|
398
|
+
initialValue: P,
|
|
399
|
+
): P =>
|
|
400
|
+
array.reduce(
|
|
401
|
+
(prev, curr, index) => callbackfn(prev, curr, asUint32(index)),
|
|
402
|
+
initialValue,
|
|
403
|
+
);
|
|
404
|
+
|
|
399
405
|
/**
|
|
400
406
|
* Reduces an array to a single value from right to left.
|
|
401
407
|
*
|
|
@@ -456,23 +462,28 @@ export function foldr<E, P>(
|
|
|
456
462
|
]
|
|
457
463
|
): P | ((array: readonly E[]) => P) {
|
|
458
464
|
switch (args.length) {
|
|
459
|
-
case 3:
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return array.reduceRight(
|
|
463
|
-
(prev, curr, index) => callbackfn(prev, curr, asUint32(index)),
|
|
464
|
-
initialValue,
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
case 2: {
|
|
469
|
-
const [callbackfn, initialValue] = args;
|
|
465
|
+
case 3:
|
|
466
|
+
return foldrImpl(...args);
|
|
470
467
|
|
|
471
|
-
|
|
472
|
-
|
|
468
|
+
case 2:
|
|
469
|
+
return (array) => foldrImpl(array, ...args);
|
|
473
470
|
}
|
|
474
471
|
}
|
|
475
472
|
|
|
473
|
+
const foldrImpl = <E, P>(
|
|
474
|
+
array: readonly E[],
|
|
475
|
+
callbackfn: (
|
|
476
|
+
previousValue: P,
|
|
477
|
+
currentValue: E,
|
|
478
|
+
currentIndex: SizeType.Arr,
|
|
479
|
+
) => P,
|
|
480
|
+
initialValue: P,
|
|
481
|
+
): P =>
|
|
482
|
+
array.reduceRight(
|
|
483
|
+
(prev, curr, index) => callbackfn(prev, curr, asUint32(index)),
|
|
484
|
+
initialValue,
|
|
485
|
+
);
|
|
486
|
+
|
|
476
487
|
/**
|
|
477
488
|
* Calculates the sum of numbers in an array.
|
|
478
489
|
*
|