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
|
@@ -142,31 +142,33 @@ export function scan<E, S>(
|
|
|
142
142
|
]
|
|
143
143
|
): NonEmptyTuple<S> | ((array: readonly E[]) => NonEmptyTuple<S>) {
|
|
144
144
|
switch (args.length) {
|
|
145
|
-
case 3:
|
|
146
|
-
|
|
145
|
+
case 3:
|
|
146
|
+
return scanImpl(...args);
|
|
147
147
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
let mut_acc = init;
|
|
153
|
-
|
|
154
|
-
for (const [index, value] of array.entries()) {
|
|
155
|
-
mut_acc = reducer(mut_acc, value, asUint32(index));
|
|
148
|
+
case 2:
|
|
149
|
+
return (array) => scanImpl(array, ...args);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
156
152
|
|
|
157
|
-
|
|
158
|
-
|
|
153
|
+
const scanImpl = <E, S>(
|
|
154
|
+
array: readonly E[],
|
|
155
|
+
reducer: (accumulator: S, currentValue: E, currentIndex: SizeType.Arr) => S,
|
|
156
|
+
init: S,
|
|
157
|
+
): NonEmptyTuple<S> => {
|
|
158
|
+
const mut_result = castMutable<NonEmptyTuple<S>>(
|
|
159
|
+
newArray<PositiveUint32, S>(asPositiveUint32(array.length + 1), init),
|
|
160
|
+
);
|
|
159
161
|
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
+
let mut_acc = init;
|
|
162
163
|
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
for (const [index, value] of array.entries()) {
|
|
165
|
+
mut_acc = reducer(mut_acc, value, asUint32(index));
|
|
165
166
|
|
|
166
|
-
|
|
167
|
-
}
|
|
167
|
+
mut_result[index + 1] = mut_acc;
|
|
168
168
|
}
|
|
169
|
-
|
|
169
|
+
|
|
170
|
+
return mut_result;
|
|
171
|
+
};
|
|
170
172
|
|
|
171
173
|
/**
|
|
172
174
|
* Reverses an array.
|
|
@@ -362,20 +364,19 @@ export function filter<E>(
|
|
|
362
364
|
| readonly [predicate: (a: E, index: SizeType.Arr) => boolean]
|
|
363
365
|
): readonly E[] | ((array: readonly E[]) => readonly E[]) {
|
|
364
366
|
switch (args.length) {
|
|
365
|
-
case 2:
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
return array.filter((a, i) => predicate(a, asUint32(i)));
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
case 1: {
|
|
372
|
-
const [predicate] = args;
|
|
367
|
+
case 2:
|
|
368
|
+
return filterImpl(...args);
|
|
373
369
|
|
|
374
|
-
|
|
375
|
-
|
|
370
|
+
case 1:
|
|
371
|
+
return (array) => filterImpl(array, ...args);
|
|
376
372
|
}
|
|
377
373
|
}
|
|
378
374
|
|
|
375
|
+
const filterImpl = <E,>(
|
|
376
|
+
array: readonly E[],
|
|
377
|
+
predicate: (a: E, index: SizeType.Arr) => boolean,
|
|
378
|
+
): readonly E[] => array.filter((a, i) => predicate(a, asUint32(i)));
|
|
379
|
+
|
|
379
380
|
/**
|
|
380
381
|
* Filters an array by excluding elements for which the predicate returns true.
|
|
381
382
|
*
|
|
@@ -411,20 +412,19 @@ export function filterNot<E>(
|
|
|
411
412
|
| readonly [predicate: (a: E, index: SizeType.Arr) => boolean]
|
|
412
413
|
): readonly E[] | ((array: readonly E[]) => readonly E[]) {
|
|
413
414
|
switch (args.length) {
|
|
414
|
-
case 2:
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
return array.filter((a, i) => !predicate(a, asUint32(i)));
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
case 1: {
|
|
421
|
-
const [predicate] = args;
|
|
415
|
+
case 2:
|
|
416
|
+
return filterNotImpl(...args);
|
|
422
417
|
|
|
423
|
-
|
|
424
|
-
|
|
418
|
+
case 1:
|
|
419
|
+
return (array) => filterNotImpl(array, ...args);
|
|
425
420
|
}
|
|
426
421
|
}
|
|
427
422
|
|
|
423
|
+
const filterNotImpl = <E,>(
|
|
424
|
+
array: readonly E[],
|
|
425
|
+
predicate: (a: E, index: SizeType.Arr) => boolean,
|
|
426
|
+
): readonly E[] => array.filter((a, i) => !predicate(a, asUint32(i)));
|
|
427
|
+
|
|
428
428
|
/**
|
|
429
429
|
* Creates a new array with unique elements.
|
|
430
430
|
*
|
|
@@ -533,11 +533,8 @@ export function flat<E, D extends SafeUintWithSmallInt = 1>(
|
|
|
533
533
|
...args: readonly [array: readonly E[], depth?: D] | readonly [depth?: D]
|
|
534
534
|
): readonly unknown[] | ((array: readonly unknown[]) => readonly unknown[]) {
|
|
535
535
|
switch (args.length) {
|
|
536
|
-
case 2:
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
return array.flat(depth);
|
|
540
|
-
}
|
|
536
|
+
case 2:
|
|
537
|
+
return flatImpl(...args);
|
|
541
538
|
|
|
542
539
|
case 1: {
|
|
543
540
|
const [arrayOrDepth] = args;
|
|
@@ -545,11 +542,11 @@ export function flat<E, D extends SafeUintWithSmallInt = 1>(
|
|
|
545
542
|
if (typeof arrayOrDepth === 'number') {
|
|
546
543
|
const depth = arrayOrDepth as SafeUintWithSmallInt | undefined;
|
|
547
544
|
|
|
548
|
-
return (array) =>
|
|
545
|
+
return (array) => flatImpl(array, depth);
|
|
549
546
|
}
|
|
550
547
|
|
|
551
548
|
if (arrayOrDepth === undefined) {
|
|
552
|
-
return (array) =>
|
|
549
|
+
return (array) => flatImpl(array, 1);
|
|
553
550
|
}
|
|
554
551
|
|
|
555
552
|
expectType<typeof arrayOrDepth, readonly E[]>('=');
|
|
@@ -558,10 +555,15 @@ export function flat<E, D extends SafeUintWithSmallInt = 1>(
|
|
|
558
555
|
}
|
|
559
556
|
|
|
560
557
|
case 0:
|
|
561
|
-
return (array) =>
|
|
558
|
+
return (array) => flatImpl(array, 1);
|
|
562
559
|
}
|
|
563
560
|
}
|
|
564
561
|
|
|
562
|
+
const flatImpl = (
|
|
563
|
+
array: readonly unknown[],
|
|
564
|
+
depth?: SafeUintWithSmallInt,
|
|
565
|
+
): readonly unknown[] => array.flat(depth);
|
|
566
|
+
|
|
565
567
|
/**
|
|
566
568
|
* Maps each element and flattens the result.
|
|
567
569
|
*
|
|
@@ -662,25 +664,25 @@ export function partition<
|
|
|
662
664
|
| readonly (readonly E[])[]
|
|
663
665
|
| ((array: readonly E[]) => readonly (readonly E[])[]) {
|
|
664
666
|
switch (args.length) {
|
|
665
|
-
case 2:
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
return chunkSize < 2
|
|
669
|
-
? []
|
|
670
|
-
: // eslint-disable-next-line total-functions/no-partial-division
|
|
671
|
-
seq(asUint32(Math.ceil(array.length / chunkSize))).map((i: Uint32) =>
|
|
672
|
-
array.slice(chunkSize * i, chunkSize * (i + 1)),
|
|
673
|
-
);
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
case 1: {
|
|
677
|
-
const [chunkSize] = args;
|
|
667
|
+
case 2:
|
|
668
|
+
return partitionImpl(...args);
|
|
678
669
|
|
|
679
|
-
|
|
680
|
-
|
|
670
|
+
case 1:
|
|
671
|
+
return (array) => partitionImpl(array, ...args);
|
|
681
672
|
}
|
|
682
673
|
}
|
|
683
674
|
|
|
675
|
+
const partitionImpl = <N extends WithSmallInt<PositiveInt & SizeType.Arr>, E>(
|
|
676
|
+
array: readonly E[],
|
|
677
|
+
chunkSize: N,
|
|
678
|
+
): readonly (readonly E[])[] =>
|
|
679
|
+
chunkSize < 2
|
|
680
|
+
? ([] as const)
|
|
681
|
+
: // eslint-disable-next-line total-functions/no-partial-division
|
|
682
|
+
seq(asUint32(Math.ceil(array.length / chunkSize))).map((i: Uint32) =>
|
|
683
|
+
array.slice(chunkSize * i, chunkSize * (i + 1)),
|
|
684
|
+
);
|
|
685
|
+
|
|
684
686
|
/**
|
|
685
687
|
* Concatenates two arrays.
|
|
686
688
|
*
|
|
@@ -35,7 +35,7 @@ describe('Arr transformations', () => {
|
|
|
35
35
|
test('should work with empty tuple', () => {
|
|
36
36
|
const empty = [] as const;
|
|
37
37
|
|
|
38
|
-
const mappedEmpty = map(empty,
|
|
38
|
+
const mappedEmpty = map(empty, String);
|
|
39
39
|
|
|
40
40
|
expectType<typeof mappedEmpty, readonly []>('=');
|
|
41
41
|
|
|
@@ -240,20 +240,19 @@ export function every<E>(
|
|
|
240
240
|
| readonly [predicate: (a: E, index: SizeType.Arr) => boolean]
|
|
241
241
|
): boolean | ((array: readonly E[]) => boolean) {
|
|
242
242
|
switch (args.length) {
|
|
243
|
-
case 2:
|
|
244
|
-
|
|
243
|
+
case 2:
|
|
244
|
+
return everyImpl(...args);
|
|
245
245
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
case 1: {
|
|
250
|
-
const [predicate] = args;
|
|
251
|
-
|
|
252
|
-
return (array) => every(array, predicate);
|
|
253
|
-
}
|
|
246
|
+
case 1:
|
|
247
|
+
return (array) => everyImpl(array, ...args);
|
|
254
248
|
}
|
|
255
249
|
}
|
|
256
250
|
|
|
251
|
+
const everyImpl = <E,>(
|
|
252
|
+
array: readonly E[],
|
|
253
|
+
predicate: (a: E, index: SizeType.Arr) => boolean,
|
|
254
|
+
): boolean => array.every((a, i) => predicate(a, asUint32(i)));
|
|
255
|
+
|
|
257
256
|
/**
|
|
258
257
|
* Tests whether at least one element in an array passes a test implemented by a predicate.
|
|
259
258
|
*
|
|
@@ -291,20 +290,19 @@ export function some<E>(
|
|
|
291
290
|
| readonly [predicate: (a: E, index: SizeType.Arr) => boolean]
|
|
292
291
|
): boolean | ((array: readonly E[]) => boolean) {
|
|
293
292
|
switch (args.length) {
|
|
294
|
-
case 2:
|
|
295
|
-
|
|
293
|
+
case 2:
|
|
294
|
+
return someImpl(...args);
|
|
296
295
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
case 1: {
|
|
301
|
-
const [predicate] = args;
|
|
302
|
-
|
|
303
|
-
return (array) => some(array, predicate);
|
|
304
|
-
}
|
|
296
|
+
case 1:
|
|
297
|
+
return (array) => someImpl(array, ...args);
|
|
305
298
|
}
|
|
306
299
|
}
|
|
307
300
|
|
|
301
|
+
const someImpl = <E,>(
|
|
302
|
+
array: readonly E[],
|
|
303
|
+
predicate: (a: E, index: SizeType.Arr) => boolean,
|
|
304
|
+
): boolean => array.some((a, i) => predicate(a, asUint32(i)));
|
|
305
|
+
|
|
308
306
|
/**
|
|
309
307
|
* Checks if an index is within the valid range of an array.
|
|
310
308
|
*
|
|
@@ -1229,7 +1229,7 @@ describe('iterable functionality', () => {
|
|
|
1229
1229
|
const array = Array.from(set);
|
|
1230
1230
|
|
|
1231
1231
|
assert.deepStrictEqual(
|
|
1232
|
-
|
|
1232
|
+
array.toSorted((a, b) => a - b),
|
|
1233
1233
|
[1, 2, 3],
|
|
1234
1234
|
);
|
|
1235
1235
|
});
|
|
@@ -1240,7 +1240,7 @@ describe('iterable functionality', () => {
|
|
|
1240
1240
|
const array = Array.from(set);
|
|
1241
1241
|
|
|
1242
1242
|
assert.deepStrictEqual(
|
|
1243
|
-
|
|
1243
|
+
array.toSorted((a, b) => a - b),
|
|
1244
1244
|
[1, 2, 3],
|
|
1245
1245
|
);
|
|
1246
1246
|
});
|
|
@@ -142,7 +142,7 @@ describe(pipe, () => {
|
|
|
142
142
|
): D | undefined => {
|
|
143
143
|
const v = pipe(input)
|
|
144
144
|
.map(parse)
|
|
145
|
-
.mapNullable(
|
|
145
|
+
.mapNullable(validate<D>)
|
|
146
146
|
.mapNullable((res) => Result.unwrapOk(res)).value;
|
|
147
147
|
|
|
148
148
|
expectType<typeof v, D | undefined>('=');
|
package/src/json/json.mts
CHANGED
|
@@ -327,7 +327,7 @@ export namespace Json {
|
|
|
327
327
|
space?: UintRangeInclusive<1, 10> | string,
|
|
328
328
|
): Result<string | undefined, string> => {
|
|
329
329
|
const allKeys = pipe(keysDeep(value))
|
|
330
|
-
.map(
|
|
330
|
+
.map(Arr.uniq)
|
|
331
331
|
.map((ks) => ks.toSorted()).value;
|
|
332
332
|
|
|
333
333
|
return stringifySelected(value, allKeys, space);
|
package/src/string/str.mts
CHANGED
|
@@ -244,25 +244,28 @@ export namespace Str {
|
|
|
244
244
|
| readonly [minLength: SupportedLength]
|
|
245
245
|
): string | ((s: string) => string) {
|
|
246
246
|
switch (args.length) {
|
|
247
|
-
case 2:
|
|
248
|
-
|
|
247
|
+
case 2:
|
|
248
|
+
return asMinLengthStringImpl(...args);
|
|
249
249
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return s;
|
|
257
|
-
}
|
|
250
|
+
case 1:
|
|
251
|
+
return (s) => asMinLengthStringImpl(s, ...args);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
258
254
|
|
|
259
|
-
|
|
260
|
-
|
|
255
|
+
const asMinLengthStringImpl = (
|
|
256
|
+
s: string,
|
|
257
|
+
minLength: SupportedLength,
|
|
258
|
+
): string => {
|
|
259
|
+
const actualLength = s.length;
|
|
261
260
|
|
|
262
|
-
|
|
263
|
-
|
|
261
|
+
if (!isMinLengthString(s, minLength)) {
|
|
262
|
+
throw new TypeError(
|
|
263
|
+
`Expected a string of length >= ${minLength}, got a string of length ${actualLength}`,
|
|
264
|
+
);
|
|
264
265
|
}
|
|
265
|
-
|
|
266
|
+
|
|
267
|
+
return s;
|
|
268
|
+
};
|
|
266
269
|
|
|
267
270
|
/**
|
|
268
271
|
* Casts a string to `MaxLengthString<MaxLength>` after checking that it has
|
|
@@ -315,25 +318,28 @@ export namespace Str {
|
|
|
315
318
|
| readonly [maxLength: SupportedLength]
|
|
316
319
|
): string | ((s: string) => string) {
|
|
317
320
|
switch (args.length) {
|
|
318
|
-
case 2:
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
if (!isMaxLengthString(s, maxLength)) {
|
|
322
|
-
throw new TypeError(
|
|
323
|
-
`Expected a string of length <= ${maxLength}, got a string of length ${s.length}`,
|
|
324
|
-
);
|
|
325
|
-
}
|
|
321
|
+
case 2:
|
|
322
|
+
return asMaxLengthStringImpl(...args);
|
|
326
323
|
|
|
327
|
-
|
|
328
|
-
|
|
324
|
+
case 1:
|
|
325
|
+
return (s) => asMaxLengthStringImpl(s, ...args);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
329
328
|
|
|
330
|
-
|
|
331
|
-
|
|
329
|
+
const asMaxLengthStringImpl = (
|
|
330
|
+
s: string,
|
|
331
|
+
maxLength: SupportedLength,
|
|
332
|
+
): string => {
|
|
333
|
+
const actualLength = s.length;
|
|
332
334
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
+
if (!isMaxLengthString(s, maxLength)) {
|
|
336
|
+
throw new TypeError(
|
|
337
|
+
`Expected a string of length <= ${maxLength}, got a string of length ${actualLength}`,
|
|
338
|
+
);
|
|
335
339
|
}
|
|
336
|
-
|
|
340
|
+
|
|
341
|
+
return s;
|
|
342
|
+
};
|
|
337
343
|
|
|
338
344
|
/**
|
|
339
345
|
* Casts a string to `BoundedLengthString<MinLength, MaxLength>` after
|
|
@@ -403,26 +409,30 @@ export namespace Str {
|
|
|
403
409
|
| readonly [minLength: SupportedLength, maxLength: SupportedLength]
|
|
404
410
|
): string | ((s: string) => string) {
|
|
405
411
|
switch (args.length) {
|
|
406
|
-
case 3:
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
if (!isBoundedLengthString(s, minLength, maxLength)) {
|
|
410
|
-
throw new TypeError(
|
|
411
|
-
`Expected a string of length in [${minLength}, ${maxLength}], got a string of length ${s.length}`,
|
|
412
|
-
);
|
|
413
|
-
}
|
|
412
|
+
case 3:
|
|
413
|
+
return asBoundedLengthStringImpl(...args);
|
|
414
414
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
case 2: {
|
|
419
|
-
const [minLength, maxLength] = args;
|
|
420
|
-
|
|
421
|
-
return (s) => asBoundedLengthString(s, minLength, maxLength);
|
|
422
|
-
}
|
|
415
|
+
case 2:
|
|
416
|
+
return (s) => asBoundedLengthStringImpl(s, ...args);
|
|
423
417
|
}
|
|
424
418
|
}
|
|
425
419
|
|
|
420
|
+
const asBoundedLengthStringImpl = (
|
|
421
|
+
s: string,
|
|
422
|
+
minLength: SupportedLength,
|
|
423
|
+
maxLength: SupportedLength,
|
|
424
|
+
): string => {
|
|
425
|
+
const actualLength = s.length;
|
|
426
|
+
|
|
427
|
+
if (!isBoundedLengthString(s, minLength, maxLength)) {
|
|
428
|
+
throw new TypeError(
|
|
429
|
+
`Expected a string of length in [${minLength}, ${maxLength}], got a string of length ${actualLength}`,
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return s;
|
|
434
|
+
};
|
|
435
|
+
|
|
426
436
|
/**
|
|
427
437
|
* Casts a string to `FixedLengthString<Length>` after checking that it has
|
|
428
438
|
* exactly `length` characters.
|
|
@@ -474,25 +484,28 @@ export namespace Str {
|
|
|
474
484
|
| readonly [length: SupportedLength]
|
|
475
485
|
): string | ((s: string) => string) {
|
|
476
486
|
switch (args.length) {
|
|
477
|
-
case 2:
|
|
478
|
-
|
|
487
|
+
case 2:
|
|
488
|
+
return asFixedLengthStringImpl(...args);
|
|
479
489
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
return s;
|
|
487
|
-
}
|
|
490
|
+
case 1:
|
|
491
|
+
return (s) => asFixedLengthStringImpl(s, ...args);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
488
494
|
|
|
489
|
-
|
|
490
|
-
|
|
495
|
+
const asFixedLengthStringImpl = (
|
|
496
|
+
s: string,
|
|
497
|
+
length: SupportedLength,
|
|
498
|
+
): string => {
|
|
499
|
+
const actualLength = s.length;
|
|
491
500
|
|
|
492
|
-
|
|
493
|
-
|
|
501
|
+
if (!isFixedLengthString(s, length)) {
|
|
502
|
+
throw new TypeError(
|
|
503
|
+
`Expected a string of length ${length}, got a string of length ${actualLength}`,
|
|
504
|
+
);
|
|
494
505
|
}
|
|
495
|
-
|
|
506
|
+
|
|
507
|
+
return s;
|
|
508
|
+
};
|
|
496
509
|
|
|
497
510
|
/**
|
|
498
511
|
* Casts a string to `NonEmptyString` after checking that it has at least one
|