ts-data-forge 12.2.2 → 14.0.0
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 +1 -1
- package/dist/array/impl/array-utils-creation.d.mts +2 -2
- package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-creation.mjs.map +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts +56 -29
- 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 +23 -15
- package/dist/array/impl/array-utils-length-bounded-array-cast.mjs.map +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts +126 -28
- package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts.map +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-guard.mjs +39 -5
- package/dist/array/impl/array-utils-length-bounded-array-guard.mjs.map +1 -1
- package/dist/array/impl/array-utils-length-bounded-tuple-cast.d.mts +167 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-cast.d.mts.map +1 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-cast.mjs +87 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-cast.mjs.map +1 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-guard.d.mts +135 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-guard.d.mts.map +1 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-guard.mjs +41 -0
- package/dist/array/impl/array-utils-length-bounded-tuple-guard.mjs.map +1 -0
- package/dist/array/impl/array-utils-modification.d.mts +32 -18
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts +3 -3
- package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts +11 -11
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs +1 -1
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts +35 -23
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +2 -2
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/array-utils-validation.d.mts +2 -122
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-validation.mjs +1 -7
- package/dist/array/impl/array-utils-validation.mjs.map +1 -1
- package/dist/array/impl/index.d.mts +2 -0
- package/dist/array/impl/index.d.mts.map +1 -1
- package/dist/array/impl/index.mjs +5 -3
- package/dist/array/impl/index.mjs.map +1 -1
- package/dist/object/object.mjs +1 -1
- package/dist/string/str.d.mts +44 -44
- package/dist/string/str.d.mts.map +1 -1
- package/dist/string/str.mjs +17 -17
- package/dist/string/str.mjs.map +1 -1
- package/package.json +6 -2
- package/src/array/impl/array-utils-creation.mts +7 -3
- package/src/array/impl/array-utils-length-bounded-array-cast.mts +88 -50
- package/src/array/impl/array-utils-length-bounded-array-cast.test.mts +40 -13
- package/src/array/impl/array-utils-length-bounded-array-guard.mts +248 -39
- package/src/array/impl/array-utils-length-bounded-array-guard.test.mts +135 -24
- package/src/array/impl/array-utils-length-bounded-tuple-cast.mts +364 -0
- package/src/array/impl/array-utils-length-bounded-tuple-cast.test.mts +207 -0
- package/src/array/impl/array-utils-length-bounded-tuple-guard.mts +266 -0
- package/src/array/impl/array-utils-length-bounded-tuple-guard.test.mts +441 -0
- package/src/array/impl/array-utils-length-constraint-preservation.test.mts +287 -0
- package/src/array/impl/array-utils-modification.mts +72 -57
- package/src/array/impl/array-utils-modification.test.mts +93 -6
- package/src/array/impl/array-utils-reducing-value.mts +5 -5
- package/src/array/impl/array-utils-slicing.mts +65 -43
- package/src/array/impl/array-utils-slicing.test.mts +55 -0
- package/src/array/impl/array-utils-transformation.mts +66 -39
- package/src/array/impl/array-utils-transformation.test.mts +62 -16
- package/src/array/impl/array-utils-validation.mts +2 -158
- package/src/array/impl/array-utils-validation.test.mts +0 -371
- package/src/array/impl/index.mts +2 -0
- package/src/collections/queue.test.mts +1 -1
- package/src/string/str.mts +69 -69
- package/src/string/str.test.mts +40 -40
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type BoolOr,
|
|
3
|
-
type BoundedLengthTuple,
|
|
4
|
-
type FixedLengthTuple,
|
|
5
|
-
type MaxLengthTuple,
|
|
6
|
-
type MinLengthTuple,
|
|
7
|
-
type NonEmptyArray,
|
|
8
|
-
type TypeEq,
|
|
9
|
-
} from 'ts-type-forge';
|
|
1
|
+
import { type BoolOr, type IsAny, type IsUnknown } from 'ts-type-forge';
|
|
10
2
|
import { asUint32, Num } from '../../number/index.mjs';
|
|
11
3
|
import { type ArrayIndex, type SizeType } from '../../types.mjs';
|
|
12
4
|
|
|
@@ -33,8 +25,7 @@ export const isArray = <E,>(value: E): value is FilterArray<E> =>
|
|
|
33
25
|
Array.isArray(value);
|
|
34
26
|
|
|
35
27
|
type FilterArray<T> = T extends T
|
|
36
|
-
?
|
|
37
|
-
BoolOr<TypeEq<T, unknown>, TypeEq<T, any>> extends true
|
|
28
|
+
? BoolOr<IsUnknown<T>, IsAny<T>> extends true
|
|
38
29
|
? Cast<readonly unknown[], T>
|
|
39
30
|
: T extends readonly unknown[]
|
|
40
31
|
? T
|
|
@@ -45,153 +36,6 @@ type Cast<A, B> = A extends B ? A : never;
|
|
|
45
36
|
|
|
46
37
|
// validation
|
|
47
38
|
|
|
48
|
-
/**
|
|
49
|
-
* Type guard that checks if an array is empty.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
*
|
|
53
|
-
* ```ts
|
|
54
|
-
* const emptyNumbers: readonly number[] = [] as const;
|
|
55
|
-
*
|
|
56
|
-
* const words = ['Ada', 'Lovelace'] as const;
|
|
57
|
-
*
|
|
58
|
-
* assert.isTrue(Arr.isEmpty(emptyNumbers));
|
|
59
|
-
*
|
|
60
|
-
* assert.isFalse(Arr.isEmpty(words));
|
|
61
|
-
*
|
|
62
|
-
* if (Arr.isEmpty(emptyNumbers)) {
|
|
63
|
-
* assert.deepStrictEqual(emptyNumbers, []);
|
|
64
|
-
* }
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export const isEmpty = <E,>(array: readonly E[]): array is readonly [] =>
|
|
68
|
-
array.length === 0;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Type guard that checks if an array is non-empty.
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
*
|
|
75
|
-
* ```ts
|
|
76
|
-
* const users: readonly Readonly<{ id: number }>[] = [{ id: 1 }] as const;
|
|
77
|
-
*
|
|
78
|
-
* const emptyUsers: readonly Readonly<{ id: number }>[] = [] as const;
|
|
79
|
-
*
|
|
80
|
-
* assert.isTrue(Arr.isNonEmpty(users));
|
|
81
|
-
*
|
|
82
|
-
* assert.isFalse(Arr.isNonEmpty(emptyUsers));
|
|
83
|
-
*
|
|
84
|
-
* if (Arr.isNonEmpty(users)) {
|
|
85
|
-
* assert.deepStrictEqual(users[0], { id: 1 });
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
export const isNonEmpty = <E,>(
|
|
90
|
-
array: readonly E[],
|
|
91
|
-
): array is NonEmptyArray<E> => array.length > 0;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Checks if an array has a specific length.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
*
|
|
98
|
-
* ```ts
|
|
99
|
-
* const pair: readonly number[] = [1, 2] as const;
|
|
100
|
-
*
|
|
101
|
-
* const triple: readonly number[] = [1, 2, 3] as const;
|
|
102
|
-
*
|
|
103
|
-
* assert.isTrue(Arr.isFixedLengthTuple(pair, 2));
|
|
104
|
-
*
|
|
105
|
-
* assert.isFalse(Arr.isFixedLengthTuple(triple, 2));
|
|
106
|
-
*
|
|
107
|
-
* if (Arr.isFixedLengthTuple(pair, 2)) {
|
|
108
|
-
* assert.deepStrictEqual(pair, [1, 2]);
|
|
109
|
-
* }
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
export const isFixedLengthTuple = <E, N extends SizeType.ArgArr>(
|
|
113
|
-
array: readonly E[],
|
|
114
|
-
len: N,
|
|
115
|
-
): array is FixedLengthTuple<N, E> => array.length === len;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Checks if an array has at least a specific length.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
*
|
|
122
|
-
* ```ts
|
|
123
|
-
* const queue: readonly string[] = ['task-1', 'task-2'] as const;
|
|
124
|
-
*
|
|
125
|
-
* const emptyQueue: readonly string[] = [] as const;
|
|
126
|
-
*
|
|
127
|
-
* assert.isTrue(Arr.isMinLengthTuple(queue, 1));
|
|
128
|
-
*
|
|
129
|
-
* assert.isFalse(Arr.isMinLengthTuple(emptyQueue, 1));
|
|
130
|
-
*
|
|
131
|
-
* if (Arr.isMinLengthTuple(queue, 1)) {
|
|
132
|
-
* assert.isTrue(queue[0] === 'task-1');
|
|
133
|
-
* }
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
export const isMinLengthTuple = <E, N extends SizeType.ArgArr>(
|
|
137
|
-
array: readonly E[],
|
|
138
|
-
len: N,
|
|
139
|
-
): array is MinLengthTuple<N, E> => array.length >= len;
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Checks if an array has at most a specific length.
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
*
|
|
146
|
-
* ```ts
|
|
147
|
-
* const pair: readonly number[] = [1, 2] as const;
|
|
148
|
-
*
|
|
149
|
-
* const triple: readonly number[] = [1, 2, 3] as const;
|
|
150
|
-
*
|
|
151
|
-
* assert.isTrue(Arr.isMaxLengthTuple(pair, 2));
|
|
152
|
-
*
|
|
153
|
-
* assert.isFalse(Arr.isMaxLengthTuple(triple, 2));
|
|
154
|
-
*
|
|
155
|
-
* if (Arr.isMaxLengthTuple(pair, 2)) {
|
|
156
|
-
* assert.isTrue(pair.length <= 2);
|
|
157
|
-
* }
|
|
158
|
-
* ```
|
|
159
|
-
*/
|
|
160
|
-
export const isMaxLengthTuple = <E, N extends SizeType.ArgArr>(
|
|
161
|
-
array: readonly E[],
|
|
162
|
-
len: N,
|
|
163
|
-
): array is MaxLengthTuple<N, E> => array.length <= len;
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Checks if an array's length is within a specific inclusive range.
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
*
|
|
170
|
-
* ```ts
|
|
171
|
-
* const pair: readonly number[] = [1, 2] as const;
|
|
172
|
-
*
|
|
173
|
-
* const quad: readonly number[] = [1, 2, 3, 4] as const;
|
|
174
|
-
*
|
|
175
|
-
* assert.isTrue(Arr.isBoundedLengthTuple(pair, 1, 3));
|
|
176
|
-
*
|
|
177
|
-
* assert.isFalse(Arr.isBoundedLengthTuple(quad, 1, 3));
|
|
178
|
-
*
|
|
179
|
-
* if (Arr.isBoundedLengthTuple(pair, 1, 3)) {
|
|
180
|
-
* assert.isTrue(pair.length >= 1 && pair.length <= 3);
|
|
181
|
-
* }
|
|
182
|
-
* ```
|
|
183
|
-
*/
|
|
184
|
-
export const isBoundedLengthTuple = <
|
|
185
|
-
E,
|
|
186
|
-
Min extends SizeType.ArgArr,
|
|
187
|
-
Max extends SizeType.ArgArr,
|
|
188
|
-
>(
|
|
189
|
-
array: readonly E[],
|
|
190
|
-
min: Min,
|
|
191
|
-
max: Max,
|
|
192
|
-
): array is BoundedLengthTuple<Min, Max, E> =>
|
|
193
|
-
array.length >= min && array.length <= max;
|
|
194
|
-
|
|
195
39
|
/**
|
|
196
40
|
* Tests whether all elements in an array pass a test implemented by a predicate.
|
|
197
41
|
*
|
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type BoundedLengthTuple,
|
|
3
|
-
type FixedLengthTuple,
|
|
4
|
-
type MaxLengthTuple,
|
|
5
|
-
type MinLengthTuple,
|
|
6
|
-
} from 'ts-type-forge';
|
|
7
1
|
import { expectType } from '../../expect-type.mjs';
|
|
8
2
|
import {
|
|
9
3
|
every,
|
|
10
4
|
indexIsInRange,
|
|
11
5
|
isArray,
|
|
12
|
-
isBoundedLengthTuple,
|
|
13
|
-
isEmpty,
|
|
14
|
-
isFixedLengthTuple,
|
|
15
|
-
isMaxLengthTuple,
|
|
16
|
-
isMinLengthTuple,
|
|
17
|
-
isNonEmpty,
|
|
18
6
|
some,
|
|
19
7
|
} from './array-utils-validation.mjs';
|
|
20
8
|
|
|
@@ -384,365 +372,6 @@ describe('Arr validations', () => {
|
|
|
384
372
|
});
|
|
385
373
|
});
|
|
386
374
|
|
|
387
|
-
describe(isEmpty, () => {
|
|
388
|
-
const xs = [1, 2, 3] as const;
|
|
389
|
-
|
|
390
|
-
const result = isEmpty(xs);
|
|
391
|
-
|
|
392
|
-
expectType<typeof result, boolean>('=');
|
|
393
|
-
|
|
394
|
-
test('case 1', () => {
|
|
395
|
-
assert.isFalse(result);
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
test('case 2', () => {
|
|
399
|
-
assert.isTrue(isEmpty([]));
|
|
400
|
-
});
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
describe(isNonEmpty, () => {
|
|
404
|
-
const xs = [1, 2, 3] as const;
|
|
405
|
-
|
|
406
|
-
const result = isNonEmpty(xs);
|
|
407
|
-
|
|
408
|
-
expectType<typeof result, boolean>('=');
|
|
409
|
-
|
|
410
|
-
test('case 1', () => {
|
|
411
|
-
assert.isTrue(result);
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
test('case 2', () => {
|
|
415
|
-
assert.isFalse(isNonEmpty([]));
|
|
416
|
-
});
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
describe(isFixedLengthTuple, () => {
|
|
420
|
-
test('should return true if array has specified length', () => {
|
|
421
|
-
const arr = [1, 2, 3] as const;
|
|
422
|
-
|
|
423
|
-
assert.isTrue(isFixedLengthTuple(arr, 3));
|
|
424
|
-
|
|
425
|
-
if (isFixedLengthTuple(arr, 3)) {
|
|
426
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
test('should return false if array does not have specified length', () => {
|
|
431
|
-
const arr = [1, 2, 3] as const;
|
|
432
|
-
|
|
433
|
-
assert.isFalse(isFixedLengthTuple(arr, 2));
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
test('should return true for empty array and length 0', () => {
|
|
437
|
-
const arr = [] as const;
|
|
438
|
-
|
|
439
|
-
assert.isTrue(isFixedLengthTuple(arr, 0));
|
|
440
|
-
|
|
441
|
-
if (isFixedLengthTuple(arr, 0)) {
|
|
442
|
-
expectType<typeof arr, readonly []>('=');
|
|
443
|
-
}
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
test('should return false for non-empty array and length 0', () => {
|
|
447
|
-
const arr = [1] as const;
|
|
448
|
-
|
|
449
|
-
assert.isFalse(isFixedLengthTuple(arr, 0));
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
test('should work with unknown array type', () => {
|
|
453
|
-
const mut_arr: number[] = [1, 2];
|
|
454
|
-
|
|
455
|
-
assert.isTrue(isFixedLengthTuple(mut_arr, 2));
|
|
456
|
-
|
|
457
|
-
if (isFixedLengthTuple(mut_arr, 2)) {
|
|
458
|
-
// transformer-ignore-next-line
|
|
459
|
-
expectType<typeof mut_arr, number[] & FixedLengthTuple<2, number>>('=');
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
assert.isFalse(isFixedLengthTuple(mut_arr, 3));
|
|
463
|
-
});
|
|
464
|
-
|
|
465
|
-
test('should work with unknown readonly array type', () => {
|
|
466
|
-
const arr: readonly number[] = [1, 2] as const;
|
|
467
|
-
|
|
468
|
-
assert.isTrue(isFixedLengthTuple(arr, 2));
|
|
469
|
-
|
|
470
|
-
if (isFixedLengthTuple(arr, 2)) {
|
|
471
|
-
expectType<typeof arr, FixedLengthTuple<2, number>>('=');
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
assert.isFalse(isFixedLengthTuple(arr, 3));
|
|
475
|
-
});
|
|
476
|
-
|
|
477
|
-
test('should return true for arrays of exact length (additional)', () => {
|
|
478
|
-
assert.isTrue(isFixedLengthTuple([1, 2, 3], 3));
|
|
479
|
-
|
|
480
|
-
assert.isTrue(isFixedLengthTuple([], 0));
|
|
481
|
-
|
|
482
|
-
assert.isTrue(isFixedLengthTuple(['a'], 1));
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
test('should return false for arrays of different length (additional)', () => {
|
|
486
|
-
assert.isFalse(isFixedLengthTuple([1, 2, 3], 2));
|
|
487
|
-
|
|
488
|
-
assert.isFalse(isFixedLengthTuple([1, 2, 3], 4));
|
|
489
|
-
|
|
490
|
-
assert.isFalse(isFixedLengthTuple([], 1));
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
test('should work as type guard with exact length (additional)', () => {
|
|
494
|
-
const array: readonly number[] = [1, 2, 3] as const;
|
|
495
|
-
|
|
496
|
-
if (isFixedLengthTuple(array, 3)) {
|
|
497
|
-
expectType<typeof array, FixedLengthTuple<3, number>>('=');
|
|
498
|
-
|
|
499
|
-
expect(array).toHaveLength(3);
|
|
500
|
-
}
|
|
501
|
-
});
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
describe(isMinLengthTuple, () => {
|
|
505
|
-
test('should return true if array length is greater than or equal to specified length', () => {
|
|
506
|
-
const arr = [1, 2, 3] as const;
|
|
507
|
-
|
|
508
|
-
assert.isTrue(isMinLengthTuple(arr, 3));
|
|
509
|
-
|
|
510
|
-
if (isMinLengthTuple(arr, 3)) {
|
|
511
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
assert.isTrue(isMinLengthTuple(arr, 2));
|
|
515
|
-
|
|
516
|
-
if (isMinLengthTuple(arr, 2)) {
|
|
517
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
518
|
-
}
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
test('should return false if array length is less than specified length', () => {
|
|
522
|
-
const arr = [1, 2, 3] as const;
|
|
523
|
-
|
|
524
|
-
assert.isFalse(isMinLengthTuple(arr, 4));
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
test('should return true for empty array and length 0', () => {
|
|
528
|
-
const arr = [] as const;
|
|
529
|
-
|
|
530
|
-
assert.isTrue(isMinLengthTuple(arr, 0));
|
|
531
|
-
|
|
532
|
-
if (isMinLengthTuple(arr, 0)) {
|
|
533
|
-
expectType<typeof arr, readonly []>('=');
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
|
|
537
|
-
test('should return false for empty array and positive length', () => {
|
|
538
|
-
const arr = [] as const;
|
|
539
|
-
|
|
540
|
-
assert.isFalse(isMinLengthTuple(arr, 1));
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
test('should work with unknown array type', () => {
|
|
544
|
-
const mut_arr: number[] = [1, 2];
|
|
545
|
-
|
|
546
|
-
assert.isTrue(isMinLengthTuple(mut_arr, 2));
|
|
547
|
-
|
|
548
|
-
// transformer-ignore-next-line
|
|
549
|
-
expectType<typeof mut_arr, number[] & MinLengthTuple<2, number>>('=');
|
|
550
|
-
|
|
551
|
-
assert.isFalse(isMinLengthTuple(mut_arr, 3));
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
test('should work with unknown array type 2', () => {
|
|
555
|
-
const mut_arr: number[] = [1, 2];
|
|
556
|
-
|
|
557
|
-
assert.isTrue(isMinLengthTuple(mut_arr, 1));
|
|
558
|
-
|
|
559
|
-
// transformer-ignore-next-line
|
|
560
|
-
expectType<typeof mut_arr, number[] & MinLengthTuple<1, number>>('=');
|
|
561
|
-
|
|
562
|
-
assert.isFalse(isMinLengthTuple(mut_arr, 3));
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
test('should return true for arrays of at least specified length (additional)', () => {
|
|
566
|
-
assert.isTrue(isMinLengthTuple([1, 2, 3], 3));
|
|
567
|
-
|
|
568
|
-
assert.isTrue(isMinLengthTuple([1, 2, 3], 2));
|
|
569
|
-
|
|
570
|
-
assert.isTrue(isMinLengthTuple([1, 2, 3], 1));
|
|
571
|
-
|
|
572
|
-
assert.isTrue(isMinLengthTuple([1, 2, 3], 0));
|
|
573
|
-
});
|
|
574
|
-
|
|
575
|
-
test('should return false for arrays shorter than specified length (additional)', () => {
|
|
576
|
-
assert.isFalse(isMinLengthTuple([1, 2, 3], 4));
|
|
577
|
-
|
|
578
|
-
assert.isFalse(isMinLengthTuple([], 1));
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
test('should work as type guard for at least length (additional)', () => {
|
|
582
|
-
const array: readonly number[] = [1, 2, 3] as const;
|
|
583
|
-
|
|
584
|
-
if (isMinLengthTuple(array, 2)) {
|
|
585
|
-
expectType<typeof array, MinLengthTuple<2, number>>('=');
|
|
586
|
-
|
|
587
|
-
expect(array.length).toBeGreaterThanOrEqual(2);
|
|
588
|
-
}
|
|
589
|
-
});
|
|
590
|
-
});
|
|
591
|
-
|
|
592
|
-
describe(isMaxLengthTuple, () => {
|
|
593
|
-
test('should return true if array length is less than or equal to specified length', () => {
|
|
594
|
-
const arr = [1, 2, 3] as const;
|
|
595
|
-
|
|
596
|
-
assert.isTrue(isMaxLengthTuple(arr, 3));
|
|
597
|
-
|
|
598
|
-
if (isMaxLengthTuple(arr, 3)) {
|
|
599
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
assert.isTrue(isMaxLengthTuple(arr, 4));
|
|
603
|
-
|
|
604
|
-
if (isMaxLengthTuple(arr, 4)) {
|
|
605
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
606
|
-
}
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
test('should return false if array length is greater than specified length', () => {
|
|
610
|
-
const arr = [1, 2, 3] as const;
|
|
611
|
-
|
|
612
|
-
assert.isFalse(isMaxLengthTuple(arr, 2));
|
|
613
|
-
});
|
|
614
|
-
|
|
615
|
-
test('should return true for empty array and length 0', () => {
|
|
616
|
-
const arr = [] as const;
|
|
617
|
-
|
|
618
|
-
assert.isTrue(isMaxLengthTuple(arr, 0));
|
|
619
|
-
|
|
620
|
-
if (isMaxLengthTuple(arr, 0)) {
|
|
621
|
-
expectType<typeof arr, readonly []>('=');
|
|
622
|
-
}
|
|
623
|
-
});
|
|
624
|
-
|
|
625
|
-
test('should return false for non-empty array and length 0', () => {
|
|
626
|
-
const arr = [1] as const;
|
|
627
|
-
|
|
628
|
-
assert.isFalse(isMaxLengthTuple(arr, 0));
|
|
629
|
-
});
|
|
630
|
-
|
|
631
|
-
test('should work with unknown array type', () => {
|
|
632
|
-
const mut_arr: number[] = [1, 2];
|
|
633
|
-
|
|
634
|
-
assert.isTrue(isMaxLengthTuple(mut_arr, 2));
|
|
635
|
-
|
|
636
|
-
// transformer-ignore-next-line
|
|
637
|
-
expectType<typeof mut_arr, number[] & MaxLengthTuple<2, number>>('=');
|
|
638
|
-
|
|
639
|
-
assert.isFalse(isMaxLengthTuple(mut_arr, 1));
|
|
640
|
-
});
|
|
641
|
-
|
|
642
|
-
test('should return true for arrays of at most specified length (additional)', () => {
|
|
643
|
-
assert.isTrue(isMaxLengthTuple([1, 2, 3], 3));
|
|
644
|
-
|
|
645
|
-
assert.isTrue(isMaxLengthTuple([1, 2, 3], 4));
|
|
646
|
-
|
|
647
|
-
assert.isTrue(isMaxLengthTuple([], 0));
|
|
648
|
-
});
|
|
649
|
-
|
|
650
|
-
test('should return false for arrays longer than specified length (additional)', () => {
|
|
651
|
-
assert.isFalse(isMaxLengthTuple([1, 2, 3], 2));
|
|
652
|
-
|
|
653
|
-
assert.isFalse(isMaxLengthTuple([1, 2, 3], 0));
|
|
654
|
-
});
|
|
655
|
-
|
|
656
|
-
test('should work as type guard for at most length (additional)', () => {
|
|
657
|
-
const array: readonly number[] = [1, 2] as const;
|
|
658
|
-
|
|
659
|
-
if (isMaxLengthTuple(array, 3)) {
|
|
660
|
-
expectType<typeof array, MaxLengthTuple<3, number>>('=');
|
|
661
|
-
|
|
662
|
-
expect(array.length).toBeLessThanOrEqual(3);
|
|
663
|
-
}
|
|
664
|
-
});
|
|
665
|
-
});
|
|
666
|
-
|
|
667
|
-
describe(isBoundedLengthTuple, () => {
|
|
668
|
-
test('should return true if array length is within the specified range', () => {
|
|
669
|
-
const arr = [1, 2, 3] as const;
|
|
670
|
-
|
|
671
|
-
assert.isTrue(isBoundedLengthTuple(arr, 1, 3));
|
|
672
|
-
|
|
673
|
-
if (isBoundedLengthTuple(arr, 1, 3)) {
|
|
674
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
assert.isTrue(isBoundedLengthTuple(arr, 3, 5));
|
|
678
|
-
|
|
679
|
-
if (isBoundedLengthTuple(arr, 3, 5)) {
|
|
680
|
-
expectType<typeof arr, readonly [1, 2, 3]>('=');
|
|
681
|
-
}
|
|
682
|
-
});
|
|
683
|
-
|
|
684
|
-
test('should return false if array length is below the specified range', () => {
|
|
685
|
-
const arr = [1, 2, 3] as const;
|
|
686
|
-
|
|
687
|
-
assert.isFalse(isBoundedLengthTuple(arr, 4, 5));
|
|
688
|
-
});
|
|
689
|
-
|
|
690
|
-
test('should return false if array length is above the specified range', () => {
|
|
691
|
-
const arr = [1, 2, 3] as const;
|
|
692
|
-
|
|
693
|
-
assert.isFalse(isBoundedLengthTuple(arr, 0, 2));
|
|
694
|
-
});
|
|
695
|
-
|
|
696
|
-
test('should return true for empty array and range including 0', () => {
|
|
697
|
-
const arr = [] as const;
|
|
698
|
-
|
|
699
|
-
assert.isTrue(isBoundedLengthTuple(arr, 0, 2));
|
|
700
|
-
|
|
701
|
-
if (isBoundedLengthTuple(arr, 0, 2)) {
|
|
702
|
-
expectType<typeof arr, readonly []>('=');
|
|
703
|
-
}
|
|
704
|
-
});
|
|
705
|
-
|
|
706
|
-
test('should work with unknown array type', () => {
|
|
707
|
-
const mut_arr: number[] = [1, 2];
|
|
708
|
-
|
|
709
|
-
assert.isTrue(isBoundedLengthTuple(mut_arr, 1, 3));
|
|
710
|
-
|
|
711
|
-
// transformer-ignore-next-line
|
|
712
|
-
expectType<typeof mut_arr, number[] & BoundedLengthTuple<1, 3, number>>(
|
|
713
|
-
'=',
|
|
714
|
-
);
|
|
715
|
-
|
|
716
|
-
assert.isFalse(isBoundedLengthTuple(mut_arr, 3, 5));
|
|
717
|
-
});
|
|
718
|
-
|
|
719
|
-
test('should return true for arrays within specified range (additional)', () => {
|
|
720
|
-
assert.isTrue(isBoundedLengthTuple([1, 2, 3], 1, 3));
|
|
721
|
-
|
|
722
|
-
assert.isTrue(isBoundedLengthTuple([1, 2, 3], 3, 3));
|
|
723
|
-
|
|
724
|
-
assert.isTrue(isBoundedLengthTuple([], 0, 0));
|
|
725
|
-
});
|
|
726
|
-
|
|
727
|
-
test('should return false for arrays outside specified range (additional)', () => {
|
|
728
|
-
assert.isFalse(isBoundedLengthTuple([1, 2, 3], 4, 5));
|
|
729
|
-
|
|
730
|
-
assert.isFalse(isBoundedLengthTuple([1, 2, 3], 0, 2));
|
|
731
|
-
});
|
|
732
|
-
|
|
733
|
-
test('should work as type guard for bounded length (additional)', () => {
|
|
734
|
-
const array: readonly number[] = [1, 2] as const;
|
|
735
|
-
|
|
736
|
-
if (isBoundedLengthTuple(array, 1, 3)) {
|
|
737
|
-
expectType<typeof array, BoundedLengthTuple<1, 3, number>>('=');
|
|
738
|
-
|
|
739
|
-
expect(array.length).toBeGreaterThanOrEqual(1);
|
|
740
|
-
|
|
741
|
-
expect(array.length).toBeLessThanOrEqual(3);
|
|
742
|
-
}
|
|
743
|
-
});
|
|
744
|
-
});
|
|
745
|
-
|
|
746
375
|
describe(every, () => {
|
|
747
376
|
test('should return true when all elements satisfy predicate', () => {
|
|
748
377
|
const evens = [2, 4, 6, 8] as const;
|
package/src/array/impl/index.mts
CHANGED
|
@@ -3,6 +3,8 @@ export * from './array-utils-element-access.mjs';
|
|
|
3
3
|
export * from './array-utils-iterators.mjs';
|
|
4
4
|
export * from './array-utils-length-bounded-array-cast.mjs';
|
|
5
5
|
export * from './array-utils-length-bounded-array-guard.mjs';
|
|
6
|
+
export * from './array-utils-length-bounded-tuple-cast.mjs';
|
|
7
|
+
export * from './array-utils-length-bounded-tuple-guard.mjs';
|
|
6
8
|
export * from './array-utils-modification.mjs';
|
|
7
9
|
export * from './array-utils-reducing-value.mjs';
|
|
8
10
|
export * from './array-utils-search.mjs';
|
|
@@ -586,7 +586,7 @@ describe('Queue test', () => {
|
|
|
586
586
|
for (const _i of range(asUint32(1000))) {
|
|
587
587
|
// Randomly decide to enqueue or dequeue
|
|
588
588
|
const shouldEnqueue =
|
|
589
|
-
Arr.
|
|
589
|
+
Arr.isEmptyTuple(mut_expected) || Math.random() < 0.6;
|
|
590
590
|
|
|
591
591
|
if (shouldEnqueue) {
|
|
592
592
|
mut_counter += 1;
|