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
|
@@ -13,30 +13,44 @@ import { type BoundedLengthArray, type FixedLengthArray, type MaxLengthArray, ty
|
|
|
13
13
|
* example, a value narrowed to `MinLengthArray<3, E>` is assignable to
|
|
14
14
|
* `MinLengthArray<1, E>`.
|
|
15
15
|
*
|
|
16
|
+
* The length argument comes first, matching the type-parameter order of
|
|
17
|
+
* `MinLengthArray<MinLength, Elm>`. Omitting the array returns a reusable type
|
|
18
|
+
* guard, which is convenient for `filter`/`pipe`.
|
|
19
|
+
*
|
|
16
20
|
* @example
|
|
17
21
|
*
|
|
18
22
|
* ```ts
|
|
19
|
-
* const input: readonly number[] = [0, 1, 2, 3];
|
|
23
|
+
* const input: readonly number[] = [0, 1, 2, 3] as const;
|
|
20
24
|
*
|
|
21
|
-
* assert.isTrue(Arr.isMinLengthArray(
|
|
25
|
+
* assert.isTrue(Arr.isMinLengthArray(3, input));
|
|
22
26
|
*
|
|
23
|
-
* assert.isFalse(Arr.isMinLengthArray([0]
|
|
27
|
+
* assert.isFalse(Arr.isMinLengthArray(3, [0]));
|
|
24
28
|
*
|
|
25
|
-
* if (Arr.isMinLengthArray(
|
|
29
|
+
* if (Arr.isMinLengthArray(3, input)) {
|
|
26
30
|
* const history: MinLengthArray<3, number> = input;
|
|
27
31
|
*
|
|
28
32
|
* const nonEmpty: MinLengthArray<1, number> = input; // OK (3 >= 1)
|
|
33
|
+
*
|
|
34
|
+
* assert.deepStrictEqual(history[0], 0);
|
|
35
|
+
*
|
|
36
|
+
* assert.deepStrictEqual(nonEmpty[0], 0);
|
|
29
37
|
* }
|
|
38
|
+
*
|
|
39
|
+
* // curried version
|
|
40
|
+
* const hasThree = Arr.isMinLengthArray(3);
|
|
41
|
+
*
|
|
42
|
+
* assert.isTrue(hasThree(input));
|
|
30
43
|
* ```
|
|
31
44
|
*
|
|
32
|
-
* @template Xs - The input array type (tuple types are preserved).
|
|
33
45
|
* @template MinLength - The minimum number of elements (inclusive).
|
|
34
|
-
* @
|
|
46
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
35
47
|
* @param minLength - The minimum number of elements (inclusive).
|
|
48
|
+
* @param xs - The array to check.
|
|
36
49
|
* @returns `true` if `xs.length >= minLength`, `false` otherwise. When `true`,
|
|
37
50
|
* TypeScript narrows `xs` to `MinLengthArray<MinLength, Xs[number]> & Xs`.
|
|
38
51
|
*/
|
|
39
|
-
export declare
|
|
52
|
+
export declare function isMinLengthArray<MinLength extends SupportedLength, Xs extends readonly unknown[]>(minLength: MinLength, xs: Xs): xs is MinLengthArray<MinLength, Xs[number]> & Xs;
|
|
53
|
+
export declare function isMinLengthArray<MinLength extends SupportedLength>(minLength: MinLength): <Xs extends readonly unknown[]>(xs: Xs) => xs is MinLengthArray<MinLength, Xs[number]> & Xs;
|
|
40
54
|
/**
|
|
41
55
|
* Type guard that checks if an array has at most `maxLength` elements.
|
|
42
56
|
*
|
|
@@ -51,30 +65,44 @@ export declare const isMinLengthArray: <Xs extends readonly unknown[], MinLength
|
|
|
51
65
|
* example, a value narrowed to `MaxLengthArray<8, E>` is assignable to
|
|
52
66
|
* `MaxLengthArray<16, E>`.
|
|
53
67
|
*
|
|
68
|
+
* The length argument comes first, matching the type-parameter order of
|
|
69
|
+
* `MaxLengthArray<MaxLength, Elm>`. Omitting the array returns a reusable type
|
|
70
|
+
* guard, which is convenient for `filter`/`pipe`.
|
|
71
|
+
*
|
|
54
72
|
* @example
|
|
55
73
|
*
|
|
56
74
|
* ```ts
|
|
57
|
-
* const input: readonly string[] = ['a', 'b', 'c'];
|
|
75
|
+
* const input: readonly string[] = ['a', 'b', 'c'] as const;
|
|
58
76
|
*
|
|
59
|
-
* assert.isTrue(Arr.isMaxLengthArray(
|
|
77
|
+
* assert.isTrue(Arr.isMaxLengthArray(8, input));
|
|
60
78
|
*
|
|
61
|
-
* assert.isFalse(Arr.isMaxLengthArray(
|
|
79
|
+
* assert.isFalse(Arr.isMaxLengthArray(2, input));
|
|
62
80
|
*
|
|
63
|
-
* if (Arr.isMaxLengthArray(
|
|
81
|
+
* if (Arr.isMaxLengthArray(8, input)) {
|
|
64
82
|
* const tags: MaxLengthArray<8, string> = input;
|
|
65
83
|
*
|
|
66
84
|
* const relaxed: MaxLengthArray<16, string> = input; // OK (8 <= 16)
|
|
85
|
+
*
|
|
86
|
+
* assert.deepStrictEqual(tags.length, 3);
|
|
87
|
+
*
|
|
88
|
+
* assert.deepStrictEqual(relaxed.length, 3);
|
|
67
89
|
* }
|
|
90
|
+
*
|
|
91
|
+
* // curried version
|
|
92
|
+
* const fitsInEight = Arr.isMaxLengthArray(8);
|
|
93
|
+
*
|
|
94
|
+
* assert.isTrue(fitsInEight(input));
|
|
68
95
|
* ```
|
|
69
96
|
*
|
|
70
|
-
* @template Xs - The input array type (tuple types are preserved).
|
|
71
97
|
* @template MaxLength - The maximum number of elements (inclusive).
|
|
72
|
-
* @
|
|
98
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
73
99
|
* @param maxLength - The maximum number of elements (inclusive).
|
|
100
|
+
* @param xs - The array to check.
|
|
74
101
|
* @returns `true` if `xs.length <= maxLength`, `false` otherwise. When `true`,
|
|
75
102
|
* TypeScript narrows `xs` to `MaxLengthArray<MaxLength, Xs[number]> & Xs`.
|
|
76
103
|
*/
|
|
77
|
-
export declare
|
|
104
|
+
export declare function isMaxLengthArray<MaxLength extends SupportedLength, Xs extends readonly unknown[]>(maxLength: MaxLength, xs: Xs): xs is MaxLengthArray<MaxLength, Xs[number]> & Xs;
|
|
105
|
+
export declare function isMaxLengthArray<MaxLength extends SupportedLength>(maxLength: MaxLength): <Xs extends readonly unknown[]>(xs: Xs) => xs is MaxLengthArray<MaxLength, Xs[number]> & Xs;
|
|
78
106
|
/**
|
|
79
107
|
* Type guard that checks if an array's length is within the inclusive range
|
|
80
108
|
* `[minLength, maxLength]`.
|
|
@@ -90,33 +118,47 @@ export declare const isMaxLengthArray: <Xs extends readonly unknown[], MaxLength
|
|
|
90
118
|
* type checker even when the element type is large — unlike
|
|
91
119
|
* `isBoundedLengthTuple`, which narrows to a union of tuple types.
|
|
92
120
|
*
|
|
121
|
+
* The length arguments come first, matching the type-parameter order of
|
|
122
|
+
* `BoundedLengthArray<MinLength, MaxLength, Elm>`. Omitting the array returns a
|
|
123
|
+
* reusable type guard, which is convenient for `filter`/`pipe`.
|
|
124
|
+
*
|
|
93
125
|
* @example
|
|
94
126
|
*
|
|
95
127
|
* ```ts
|
|
96
|
-
* const input: readonly number[] = [1, 2, 3];
|
|
128
|
+
* const input: readonly number[] = [1, 2, 3] as const;
|
|
97
129
|
*
|
|
98
|
-
* assert.isTrue(Arr.isBoundedLengthArray(
|
|
130
|
+
* assert.isTrue(Arr.isBoundedLengthArray(1, 5, input));
|
|
99
131
|
*
|
|
100
|
-
* assert.isFalse(Arr.isBoundedLengthArray(
|
|
132
|
+
* assert.isFalse(Arr.isBoundedLengthArray(1, 5, []));
|
|
101
133
|
*
|
|
102
|
-
* if (Arr.isBoundedLengthArray(
|
|
134
|
+
* if (Arr.isBoundedLengthArray(1, 5, input)) {
|
|
103
135
|
* const selection: BoundedLengthArray<1, 5, number> = input;
|
|
104
136
|
*
|
|
105
137
|
* const relaxed: BoundedLengthArray<0, 100, number> = input; // OK ([1, 5] ⊆ [0, 100])
|
|
138
|
+
*
|
|
139
|
+
* assert.deepStrictEqual(selection[0], 1);
|
|
140
|
+
*
|
|
141
|
+
* assert.deepStrictEqual(relaxed.length, 3);
|
|
106
142
|
* }
|
|
143
|
+
*
|
|
144
|
+
* // curried version
|
|
145
|
+
* const isSmallSelection = Arr.isBoundedLengthArray(1, 5);
|
|
146
|
+
*
|
|
147
|
+
* assert.isTrue(isSmallSelection(input));
|
|
107
148
|
* ```
|
|
108
149
|
*
|
|
109
|
-
* @template Xs - The input array type (tuple types are preserved).
|
|
110
150
|
* @template MinLength - The minimum number of elements (inclusive).
|
|
111
151
|
* @template MaxLength - The maximum number of elements (inclusive).
|
|
112
|
-
* @
|
|
152
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
113
153
|
* @param minLength - The minimum number of elements (inclusive).
|
|
114
154
|
* @param maxLength - The maximum number of elements (inclusive).
|
|
155
|
+
* @param xs - The array to check.
|
|
115
156
|
* @returns `true` if `minLength <= xs.length && xs.length <= maxLength`,
|
|
116
157
|
* `false` otherwise. When `true`, TypeScript narrows `xs` to
|
|
117
158
|
* `BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs`.
|
|
118
159
|
*/
|
|
119
|
-
export declare
|
|
160
|
+
export declare function isBoundedLengthArray<MinLength extends SupportedLength, MaxLength extends SupportedLength, Xs extends readonly unknown[]>(minLength: MinLength, maxLength: MaxLength, xs: Xs): xs is BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs;
|
|
161
|
+
export declare function isBoundedLengthArray<MinLength extends SupportedLength, MaxLength extends SupportedLength>(minLength: MinLength, maxLength: MaxLength): <Xs extends readonly unknown[]>(xs: Xs) => xs is BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs;
|
|
120
162
|
/**
|
|
121
163
|
* Type guard that checks if an array has exactly `length` elements.
|
|
122
164
|
*
|
|
@@ -132,28 +174,84 @@ export declare const isBoundedLengthArray: <Xs extends readonly unknown[], MinLe
|
|
|
132
174
|
* type checker even when the element type is large — unlike
|
|
133
175
|
* `isFixedLengthTuple`, which narrows to a structural tuple type.
|
|
134
176
|
*
|
|
177
|
+
* The length argument comes first, matching the type-parameter order of
|
|
178
|
+
* `FixedLengthArray<Length, Elm>`. Omitting the array returns a reusable type
|
|
179
|
+
* guard, which is convenient for `filter`/`pipe`.
|
|
180
|
+
*
|
|
135
181
|
* @example
|
|
136
182
|
*
|
|
137
183
|
* ```ts
|
|
138
|
-
* const input: readonly number[] = [255, 128, 0];
|
|
184
|
+
* const input: readonly number[] = [255, 128, 0] as const;
|
|
139
185
|
*
|
|
140
|
-
* assert.isTrue(Arr.isFixedLengthArray(
|
|
186
|
+
* assert.isTrue(Arr.isFixedLengthArray(3, input));
|
|
141
187
|
*
|
|
142
|
-
* assert.isFalse(Arr.isFixedLengthArray(
|
|
188
|
+
* assert.isFalse(Arr.isFixedLengthArray(4, input));
|
|
143
189
|
*
|
|
144
|
-
* if (Arr.isFixedLengthArray(
|
|
190
|
+
* if (Arr.isFixedLengthArray(3, input)) {
|
|
145
191
|
* const rgb: FixedLengthArray<3, number> = input;
|
|
146
192
|
*
|
|
147
193
|
* const atMost5: MaxLengthArray<5, number> = input; // OK (3 <= 5)
|
|
194
|
+
*
|
|
195
|
+
* assert.deepStrictEqual(rgb[0], 255);
|
|
196
|
+
*
|
|
197
|
+
* assert.deepStrictEqual(atMost5.length, 3);
|
|
148
198
|
* }
|
|
199
|
+
*
|
|
200
|
+
* // curried version
|
|
201
|
+
* const isRgb = Arr.isFixedLengthArray(3);
|
|
202
|
+
*
|
|
203
|
+
* assert.isTrue(isRgb(input));
|
|
149
204
|
* ```
|
|
150
205
|
*
|
|
151
|
-
* @template Xs - The input array type (tuple types are preserved).
|
|
152
206
|
* @template Length - The exact number of elements.
|
|
153
|
-
* @
|
|
207
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
154
208
|
* @param length - The exact number of elements.
|
|
209
|
+
* @param xs - The array to check.
|
|
155
210
|
* @returns `true` if `xs.length === length`, `false` otherwise. When `true`,
|
|
156
211
|
* TypeScript narrows `xs` to `FixedLengthArray<Length, Xs[number]> & Xs`.
|
|
157
212
|
*/
|
|
158
|
-
export declare
|
|
213
|
+
export declare function isFixedLengthArray<Length extends SupportedLength, Xs extends readonly unknown[]>(length: Length, xs: Xs): xs is FixedLengthArray<Length, Xs[number]> & Xs;
|
|
214
|
+
export declare function isFixedLengthArray<Length extends SupportedLength>(length: Length): <Xs extends readonly unknown[]>(xs: Xs) => xs is FixedLengthArray<Length, Xs[number]> & Xs;
|
|
215
|
+
/**
|
|
216
|
+
* Type guard that checks if an array is empty.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
*
|
|
220
|
+
* ```ts
|
|
221
|
+
* const emptyNumbers: readonly number[] = [] as const;
|
|
222
|
+
*
|
|
223
|
+
* const words = ['Ada', 'Lovelace'] as const;
|
|
224
|
+
*
|
|
225
|
+
* assert.isTrue(Arr.isEmpty(emptyNumbers));
|
|
226
|
+
*
|
|
227
|
+
* assert.isFalse(Arr.isEmpty(words));
|
|
228
|
+
*
|
|
229
|
+
* if (Arr.isEmpty(emptyNumbers)) {
|
|
230
|
+
* // `isEmpty` narrows to the branded `FixedLengthArray<0, number>`, so the
|
|
231
|
+
* // expected value is annotated with the unbranded type.
|
|
232
|
+
* assert.deepStrictEqual<readonly number[]>(emptyNumbers, []);
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export declare const isEmpty: <Xs extends readonly unknown[]>(array: Xs) => array is FixedLengthArray<0, Xs[number]> & Xs;
|
|
237
|
+
/**
|
|
238
|
+
* Type guard that checks if an array is non-empty.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
*
|
|
242
|
+
* ```ts
|
|
243
|
+
* const users: readonly Readonly<{ id: number }>[] = [{ id: 1 }] as const;
|
|
244
|
+
*
|
|
245
|
+
* const emptyUsers: readonly Readonly<{ id: number }>[] = [] as const;
|
|
246
|
+
*
|
|
247
|
+
* assert.isTrue(Arr.isNonEmpty(users));
|
|
248
|
+
*
|
|
249
|
+
* assert.isFalse(Arr.isNonEmpty(emptyUsers));
|
|
250
|
+
*
|
|
251
|
+
* if (Arr.isNonEmpty(users)) {
|
|
252
|
+
* assert.deepStrictEqual(users[0], { id: 1 });
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
export declare const isNonEmpty: <Xs extends readonly unknown[]>(array: Xs) => array is MinLengthArray<1, Xs[number]> & Xs;
|
|
159
257
|
//# sourceMappingURL=array-utils-length-bounded-array-guard.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-utils-length-bounded-array-guard.d.mts","sourceRoot":"","sources":["../../../src/array/impl/array-utils-length-bounded-array-guard.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAEvB
|
|
1
|
+
{"version":3,"file":"array-utils-length-bounded-array-guard.d.mts","sourceRoot":"","sources":["../../../src/array/impl/array-utils-length-bounded-array-guard.mts"],"names":[],"mappings":"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"}
|
|
@@ -1,7 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
function isMinLengthArray(...args) {
|
|
2
|
+
switch (args.length) {
|
|
3
|
+
case 2:
|
|
4
|
+
return hasMinLength(...args);
|
|
5
|
+
case 1:
|
|
6
|
+
return (xs) => hasMinLength(args[0], xs);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
const hasMinLength = (minLength, xs) => xs.length >= minLength;
|
|
10
|
+
function isMaxLengthArray(...args) {
|
|
11
|
+
switch (args.length) {
|
|
12
|
+
case 2:
|
|
13
|
+
return hasMaxLength(...args);
|
|
14
|
+
case 1:
|
|
15
|
+
return (xs) => hasMaxLength(args[0], xs);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const hasMaxLength = (maxLength, xs) => xs.length <= maxLength;
|
|
19
|
+
function isBoundedLengthArray(...args) {
|
|
20
|
+
switch (args.length) {
|
|
21
|
+
case 3:
|
|
22
|
+
return hasBoundedLength(...args);
|
|
23
|
+
case 2:
|
|
24
|
+
return (xs) => hasBoundedLength(args[0], args[1], xs);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const hasBoundedLength = (minLength, maxLength, xs) => xs.length >= minLength && xs.length <= maxLength;
|
|
28
|
+
function isFixedLengthArray(...args) {
|
|
29
|
+
switch (args.length) {
|
|
30
|
+
case 2:
|
|
31
|
+
return hasFixedLength(...args);
|
|
32
|
+
case 1:
|
|
33
|
+
return (xs) => hasFixedLength(args[0], xs);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const hasFixedLength = (length, xs) => xs.length === length;
|
|
37
|
+
const isEmpty = (array) => array.length === 0;
|
|
38
|
+
const isNonEmpty = (array) => array.length > 0;
|
|
5
39
|
|
|
6
|
-
export { isBoundedLengthArray, isFixedLengthArray, isMaxLengthArray, isMinLengthArray };
|
|
40
|
+
export { isBoundedLengthArray, isEmpty, isFixedLengthArray, isMaxLengthArray, isMinLengthArray, isNonEmpty };
|
|
7
41
|
//# sourceMappingURL=array-utils-length-bounded-array-guard.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-utils-length-bounded-array-guard.mjs","sources":["../../../src/array/impl/array-utils-length-bounded-array-guard.mts"],"sourcesContent":["import {\n type BoundedLengthArray,\n type FixedLengthArray,\n type MaxLengthArray,\n type MinLengthArray,\n type SupportedLength,\n} from 'ts-type-forge';\n\n/**\n * Type guard that checks if an array has at least `minLength` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MinLengthArray<MinLength, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isMinLengthTuple`, which narrows to a structural tuple type.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MinLengthArray<3, E>` is assignable to\n * `MinLengthArray<1, E>`.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [0, 1, 2, 3];\n *\n * assert.isTrue(Arr.isMinLengthArray(input, 3));\n *\n * assert.isFalse(Arr.isMinLengthArray([0], 3));\n *\n * if (Arr.isMinLengthArray(input, 3)) {\n * const history: MinLengthArray<3, number> = input;\n *\n * const nonEmpty: MinLengthArray<1, number> = input; // OK (3 >= 1)\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MinLength - The minimum number of elements (inclusive).\n * @param xs - The array to check.\n * @param minLength - The minimum number of elements (inclusive).\n * @returns `true` if `xs.length >= minLength`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `MinLengthArray<MinLength, Xs[number]> & Xs`.\n */\nexport const isMinLengthArray = <\n Xs extends readonly unknown[],\n MinLength extends SupportedLength,\n>(\n xs: Xs,\n minLength: MinLength,\n): xs is MinLengthArray<MinLength, Xs[number]> & Xs => xs.length >= minLength;\n\n/**\n * Type guard that checks if an array has at most `maxLength` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MaxLengthArray<MaxLength, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isMaxLengthTuple`, which narrows to a union of tuple types.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MaxLengthArray<8, E>` is assignable to\n * `MaxLengthArray<16, E>`.\n *\n * @example\n *\n * ```ts\n * const input: readonly string[] = ['a', 'b', 'c'];\n *\n * assert.isTrue(Arr.isMaxLengthArray(input, 8));\n *\n * assert.isFalse(Arr.isMaxLengthArray(input, 2));\n *\n * if (Arr.isMaxLengthArray(input, 8)) {\n * const tags: MaxLengthArray<8, string> = input;\n *\n * const relaxed: MaxLengthArray<16, string> = input; // OK (8 <= 16)\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @param xs - The array to check.\n * @param maxLength - The maximum number of elements (inclusive).\n * @returns `true` if `xs.length <= maxLength`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `MaxLengthArray<MaxLength, Xs[number]> & Xs`.\n */\nexport const isMaxLengthArray = <\n Xs extends readonly unknown[],\n MaxLength extends SupportedLength,\n>(\n xs: Xs,\n maxLength: MaxLength,\n): xs is MaxLengthArray<MaxLength, Xs[number]> & Xs => xs.length <= maxLength;\n\n/**\n * Type guard that checks if an array's length is within the inclusive range\n * `[minLength, maxLength]`.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `BoundedLengthArray<MinLength, MaxLength, E>` while\n * preserving the original array type (e.g. tuple types) via intersection.\n * - Since `BoundedLengthArray` is the intersection of `MinLengthArray` and\n * `MaxLengthArray`, the narrowed value is assignable to both, and both\n * bounds can be weakened independently.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isBoundedLengthTuple`, which narrows to a union of tuple types.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [1, 2, 3];\n *\n * assert.isTrue(Arr.isBoundedLengthArray(input, 1, 5));\n *\n * assert.isFalse(Arr.isBoundedLengthArray([], 1, 5));\n *\n * if (Arr.isBoundedLengthArray(input, 1, 5)) {\n * const selection: BoundedLengthArray<1, 5, number> = input;\n *\n * const relaxed: BoundedLengthArray<0, 100, number> = input; // OK ([1, 5] ⊆ [0, 100])\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MinLength - The minimum number of elements (inclusive).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @param xs - The array to check.\n * @param minLength - The minimum number of elements (inclusive).\n * @param maxLength - The maximum number of elements (inclusive).\n * @returns `true` if `minLength <= xs.length && xs.length <= maxLength`,\n * `false` otherwise. When `true`, TypeScript narrows `xs` to\n * `BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs`.\n */\nexport const isBoundedLengthArray = <\n Xs extends readonly unknown[],\n MinLength extends SupportedLength,\n MaxLength extends SupportedLength,\n>(\n xs: Xs,\n minLength: MinLength,\n maxLength: MaxLength,\n): xs is BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs =>\n xs.length >= minLength && xs.length <= maxLength;\n\n/**\n * Type guard that checks if an array has exactly `length` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `FixedLengthArray<Length, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - Since `FixedLengthArray<Length, E>` is defined as\n * `BoundedLengthArray<Length, Length, E>`, the narrowed value is assignable\n * to any `MinLengthArray<N, E>` with `N <= Length` and any\n * `MaxLengthArray<N, E>` with `N >= Length`.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isFixedLengthTuple`, which narrows to a structural tuple type.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [255, 128, 0];\n *\n * assert.isTrue(Arr.isFixedLengthArray(input, 3));\n *\n * assert.isFalse(Arr.isFixedLengthArray(input, 4));\n *\n * if (Arr.isFixedLengthArray(input, 3)) {\n * const rgb: FixedLengthArray<3, number> = input;\n *\n * const atMost5: MaxLengthArray<5, number> = input; // OK (3 <= 5)\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template Length - The exact number of elements.\n * @param xs - The array to check.\n * @param length - The exact number of elements.\n * @returns `true` if `xs.length === length`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `FixedLengthArray<Length, Xs[number]> & Xs`.\n */\nexport const isFixedLengthArray = <\n Xs extends readonly unknown[],\n Length extends SupportedLength,\n>(\n xs: Xs,\n length: Length,\n): xs is FixedLengthArray<Length, Xs[number]> & Xs => xs.length === length;\n"],"names":[],"mappings":"AA6CO,MAAM,gBAAA,GAAmB,CAI9B,EAAA,EACA,SAAA,KACqD,GAAG,MAAA,IAAU;AAuC7D,MAAM,gBAAA,GAAmB,CAI9B,EAAA,EACA,SAAA,KACqD,GAAG,MAAA,IAAU;AA2C7D,MAAM,oBAAA,GAAuB,CAKlC,EAAA,EACA,SAAA,EACA,cAEA,EAAA,CAAG,MAAA,IAAU,SAAA,IAAa,EAAA,CAAG,MAAA,IAAU;AAwClC,MAAM,kBAAA,GAAqB,CAIhC,EAAA,EACA,MAAA,KACoD,GAAG,MAAA,KAAW;;;;"}
|
|
1
|
+
{"version":3,"file":"array-utils-length-bounded-array-guard.mjs","sources":["../../../src/array/impl/array-utils-length-bounded-array-guard.mts"],"sourcesContent":["import {\n type BoundedLengthArray,\n type FixedLengthArray,\n type MaxLengthArray,\n type MinLengthArray,\n type SupportedLength,\n} from 'ts-type-forge';\n\n/**\n * Type guard that checks if an array has at least `minLength` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MinLengthArray<MinLength, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isMinLengthTuple`, which narrows to a structural tuple type.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MinLengthArray<3, E>` is assignable to\n * `MinLengthArray<1, E>`.\n *\n * The length argument comes first, matching the type-parameter order of\n * `MinLengthArray<MinLength, Elm>`. Omitting the array returns a reusable type\n * guard, which is convenient for `filter`/`pipe`.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [0, 1, 2, 3] as const;\n *\n * assert.isTrue(Arr.isMinLengthArray(3, input));\n *\n * assert.isFalse(Arr.isMinLengthArray(3, [0]));\n *\n * if (Arr.isMinLengthArray(3, input)) {\n * const history: MinLengthArray<3, number> = input;\n *\n * const nonEmpty: MinLengthArray<1, number> = input; // OK (3 >= 1)\n *\n * assert.deepStrictEqual(history[0], 0);\n *\n * assert.deepStrictEqual(nonEmpty[0], 0);\n * }\n *\n * // curried version\n * const hasThree = Arr.isMinLengthArray(3);\n *\n * assert.isTrue(hasThree(input));\n * ```\n *\n * @template MinLength - The minimum number of elements (inclusive).\n * @template Xs - The input array type (tuple types are preserved).\n * @param minLength - The minimum number of elements (inclusive).\n * @param xs - The array to check.\n * @returns `true` if `xs.length >= minLength`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `MinLengthArray<MinLength, Xs[number]> & Xs`.\n */\nexport function isMinLengthArray<\n MinLength extends SupportedLength,\n Xs extends readonly unknown[],\n>(\n minLength: MinLength,\n xs: Xs,\n): xs is MinLengthArray<MinLength, Xs[number]> & Xs;\n\n// Curried version\nexport function isMinLengthArray<MinLength extends SupportedLength>(\n minLength: MinLength,\n): <Xs extends readonly unknown[]>(\n xs: Xs,\n) => xs is MinLengthArray<MinLength, Xs[number]> & Xs;\n\nexport function isMinLengthArray(\n ...args:\n | readonly [minLength: SupportedLength, xs: readonly unknown[]]\n | readonly [minLength: SupportedLength]\n): boolean | ((xs: readonly unknown[]) => boolean) {\n switch (args.length) {\n case 2:\n return hasMinLength(...args);\n\n case 1:\n return (xs) => hasMinLength(args[0], xs);\n }\n}\n\nconst hasMinLength = (\n minLength: SupportedLength,\n xs: readonly unknown[],\n): boolean => xs.length >= minLength;\n\n/**\n * Type guard that checks if an array has at most `maxLength` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MaxLengthArray<MaxLength, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isMaxLengthTuple`, which narrows to a union of tuple types.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MaxLengthArray<8, E>` is assignable to\n * `MaxLengthArray<16, E>`.\n *\n * The length argument comes first, matching the type-parameter order of\n * `MaxLengthArray<MaxLength, Elm>`. Omitting the array returns a reusable type\n * guard, which is convenient for `filter`/`pipe`.\n *\n * @example\n *\n * ```ts\n * const input: readonly string[] = ['a', 'b', 'c'] as const;\n *\n * assert.isTrue(Arr.isMaxLengthArray(8, input));\n *\n * assert.isFalse(Arr.isMaxLengthArray(2, input));\n *\n * if (Arr.isMaxLengthArray(8, input)) {\n * const tags: MaxLengthArray<8, string> = input;\n *\n * const relaxed: MaxLengthArray<16, string> = input; // OK (8 <= 16)\n *\n * assert.deepStrictEqual(tags.length, 3);\n *\n * assert.deepStrictEqual(relaxed.length, 3);\n * }\n *\n * // curried version\n * const fitsInEight = Arr.isMaxLengthArray(8);\n *\n * assert.isTrue(fitsInEight(input));\n * ```\n *\n * @template MaxLength - The maximum number of elements (inclusive).\n * @template Xs - The input array type (tuple types are preserved).\n * @param maxLength - The maximum number of elements (inclusive).\n * @param xs - The array to check.\n * @returns `true` if `xs.length <= maxLength`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `MaxLengthArray<MaxLength, Xs[number]> & Xs`.\n */\nexport function isMaxLengthArray<\n MaxLength extends SupportedLength,\n Xs extends readonly unknown[],\n>(\n maxLength: MaxLength,\n xs: Xs,\n): xs is MaxLengthArray<MaxLength, Xs[number]> & Xs;\n\n// Curried version\nexport function isMaxLengthArray<MaxLength extends SupportedLength>(\n maxLength: MaxLength,\n): <Xs extends readonly unknown[]>(\n xs: Xs,\n) => xs is MaxLengthArray<MaxLength, Xs[number]> & Xs;\n\nexport function isMaxLengthArray(\n ...args:\n | readonly [maxLength: SupportedLength, xs: readonly unknown[]]\n | readonly [maxLength: SupportedLength]\n): boolean | ((xs: readonly unknown[]) => boolean) {\n switch (args.length) {\n case 2:\n return hasMaxLength(...args);\n\n case 1:\n return (xs) => hasMaxLength(args[0], xs);\n }\n}\n\nconst hasMaxLength = (\n maxLength: SupportedLength,\n xs: readonly unknown[],\n): boolean => xs.length <= maxLength;\n\n/**\n * Type guard that checks if an array's length is within the inclusive range\n * `[minLength, maxLength]`.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `BoundedLengthArray<MinLength, MaxLength, E>` while\n * preserving the original array type (e.g. tuple types) via intersection.\n * - Since `BoundedLengthArray` is the intersection of `MinLengthArray` and\n * `MaxLengthArray`, the narrowed value is assignable to both, and both\n * bounds can be weakened independently.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isBoundedLengthTuple`, which narrows to a union of tuple types.\n *\n * The length arguments come first, matching the type-parameter order of\n * `BoundedLengthArray<MinLength, MaxLength, Elm>`. Omitting the array returns a\n * reusable type guard, which is convenient for `filter`/`pipe`.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [1, 2, 3] as const;\n *\n * assert.isTrue(Arr.isBoundedLengthArray(1, 5, input));\n *\n * assert.isFalse(Arr.isBoundedLengthArray(1, 5, []));\n *\n * if (Arr.isBoundedLengthArray(1, 5, input)) {\n * const selection: BoundedLengthArray<1, 5, number> = input;\n *\n * const relaxed: BoundedLengthArray<0, 100, number> = input; // OK ([1, 5] ⊆ [0, 100])\n *\n * assert.deepStrictEqual(selection[0], 1);\n *\n * assert.deepStrictEqual(relaxed.length, 3);\n * }\n *\n * // curried version\n * const isSmallSelection = Arr.isBoundedLengthArray(1, 5);\n *\n * assert.isTrue(isSmallSelection(input));\n * ```\n *\n * @template MinLength - The minimum number of elements (inclusive).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @template Xs - The input array type (tuple types are preserved).\n * @param minLength - The minimum number of elements (inclusive).\n * @param maxLength - The maximum number of elements (inclusive).\n * @param xs - The array to check.\n * @returns `true` if `minLength <= xs.length && xs.length <= maxLength`,\n * `false` otherwise. When `true`, TypeScript narrows `xs` to\n * `BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs`.\n */\nexport function isBoundedLengthArray<\n MinLength extends SupportedLength,\n MaxLength extends SupportedLength,\n Xs extends readonly unknown[],\n>(\n minLength: MinLength,\n maxLength: MaxLength,\n xs: Xs,\n): xs is BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs;\n\n// Curried version\nexport function isBoundedLengthArray<\n MinLength extends SupportedLength,\n MaxLength extends SupportedLength,\n>(\n minLength: MinLength,\n maxLength: MaxLength,\n): <Xs extends readonly unknown[]>(\n xs: Xs,\n) => xs is BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs;\n\nexport function isBoundedLengthArray(\n ...args:\n | readonly [\n minLength: SupportedLength,\n maxLength: SupportedLength,\n xs: readonly unknown[],\n ]\n | readonly [minLength: SupportedLength, maxLength: SupportedLength]\n): boolean | ((xs: readonly unknown[]) => boolean) {\n switch (args.length) {\n case 3:\n return hasBoundedLength(...args);\n\n case 2:\n return (xs) => hasBoundedLength(args[0], args[1], xs);\n }\n}\n\nconst hasBoundedLength = (\n minLength: SupportedLength,\n maxLength: SupportedLength,\n xs: readonly unknown[],\n): boolean => xs.length >= minLength && xs.length <= maxLength;\n\n/**\n * Type guard that checks if an array has exactly `length` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `FixedLengthArray<Length, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - Since `FixedLengthArray<Length, E>` is defined as\n * `BoundedLengthArray<Length, Length, E>`, the narrowed value is assignable\n * to any `MinLengthArray<N, E>` with `N <= Length` and any\n * `MaxLengthArray<N, E>` with `N >= Length`.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isFixedLengthTuple`, which narrows to a structural tuple type.\n *\n * The length argument comes first, matching the type-parameter order of\n * `FixedLengthArray<Length, Elm>`. Omitting the array returns a reusable type\n * guard, which is convenient for `filter`/`pipe`.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [255, 128, 0] as const;\n *\n * assert.isTrue(Arr.isFixedLengthArray(3, input));\n *\n * assert.isFalse(Arr.isFixedLengthArray(4, input));\n *\n * if (Arr.isFixedLengthArray(3, input)) {\n * const rgb: FixedLengthArray<3, number> = input;\n *\n * const atMost5: MaxLengthArray<5, number> = input; // OK (3 <= 5)\n *\n * assert.deepStrictEqual(rgb[0], 255);\n *\n * assert.deepStrictEqual(atMost5.length, 3);\n * }\n *\n * // curried version\n * const isRgb = Arr.isFixedLengthArray(3);\n *\n * assert.isTrue(isRgb(input));\n * ```\n *\n * @template Length - The exact number of elements.\n * @template Xs - The input array type (tuple types are preserved).\n * @param length - The exact number of elements.\n * @param xs - The array to check.\n * @returns `true` if `xs.length === length`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `FixedLengthArray<Length, Xs[number]> & Xs`.\n */\nexport function isFixedLengthArray<\n Length extends SupportedLength,\n Xs extends readonly unknown[],\n>(length: Length, xs: Xs): xs is FixedLengthArray<Length, Xs[number]> & Xs;\n\n// Curried version\nexport function isFixedLengthArray<Length extends SupportedLength>(\n length: Length,\n): <Xs extends readonly unknown[]>(\n xs: Xs,\n) => xs is FixedLengthArray<Length, Xs[number]> & Xs;\n\nexport function isFixedLengthArray(\n ...args:\n | readonly [length: SupportedLength, xs: readonly unknown[]]\n | readonly [length: SupportedLength]\n): boolean | ((xs: readonly unknown[]) => boolean) {\n switch (args.length) {\n case 2:\n return hasFixedLength(...args);\n\n case 1:\n return (xs) => hasFixedLength(args[0], xs);\n }\n}\n\nconst hasFixedLength = (\n length: SupportedLength,\n xs: readonly unknown[],\n): boolean => xs.length === length;\n\n/**\n * Type guard that checks if an array is empty.\n *\n * @example\n *\n * ```ts\n * const emptyNumbers: readonly number[] = [] as const;\n *\n * const words = ['Ada', 'Lovelace'] as const;\n *\n * assert.isTrue(Arr.isEmpty(emptyNumbers));\n *\n * assert.isFalse(Arr.isEmpty(words));\n *\n * if (Arr.isEmpty(emptyNumbers)) {\n * // `isEmpty` narrows to the branded `FixedLengthArray<0, number>`, so the\n * // expected value is annotated with the unbranded type.\n * assert.deepStrictEqual<readonly number[]>(emptyNumbers, []);\n * }\n * ```\n */\nexport const isEmpty = <Xs extends readonly unknown[]>(\n array: Xs,\n): array is FixedLengthArray<0, Xs[number]> & Xs => array.length === 0;\n\n/**\n * Type guard that checks if an array is non-empty.\n *\n * @example\n *\n * ```ts\n * const users: readonly Readonly<{ id: number }>[] = [{ id: 1 }] as const;\n *\n * const emptyUsers: readonly Readonly<{ id: number }>[] = [] as const;\n *\n * assert.isTrue(Arr.isNonEmpty(users));\n *\n * assert.isFalse(Arr.isNonEmpty(emptyUsers));\n *\n * if (Arr.isNonEmpty(users)) {\n * assert.deepStrictEqual(users[0], { id: 1 });\n * }\n * ```\n */\nexport const isNonEmpty = <Xs extends readonly unknown[]>(\n array: Xs,\n): array is MinLengthArray<1, Xs[number]> & Xs => array.length > 0;\n"],"names":[],"mappings":"AAyEO,SAAS,oBACX,IAAA,EAG8C;AACjD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA;AACH,MAAA,OAAO,YAAA,CAAa,GAAG,IAAI,CAAA;AAAA,IAE7B,KAAK,CAAA;AACH,MAAA,OAAO,CAAC,EAAA,KAAO,YAAA,CAAa,IAAA,CAAK,CAAC,GAAG,EAAE,CAAA;AAAA;AAE7C;AAEA,MAAM,YAAA,GAAe,CACnB,SAAA,EACA,EAAA,KACY,GAAG,MAAA,IAAU,SAAA;AAmEpB,SAAS,oBACX,IAAA,EAG8C;AACjD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA;AACH,MAAA,OAAO,YAAA,CAAa,GAAG,IAAI,CAAA;AAAA,IAE7B,KAAK,CAAA;AACH,MAAA,OAAO,CAAC,EAAA,KAAO,YAAA,CAAa,IAAA,CAAK,CAAC,GAAG,EAAE,CAAA;AAAA;AAE7C;AAEA,MAAM,YAAA,GAAe,CACnB,SAAA,EACA,EAAA,KACY,GAAG,MAAA,IAAU,SAAA;AA6EpB,SAAS,wBACX,IAAA,EAO8C;AACjD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA;AACH,MAAA,OAAO,gBAAA,CAAiB,GAAG,IAAI,CAAA;AAAA,IAEjC,KAAK,CAAA;AACH,MAAA,OAAO,CAAC,OAAO,gBAAA,CAAiB,IAAA,CAAK,CAAC,CAAA,EAAG,IAAA,CAAK,CAAC,CAAA,EAAG,EAAE,CAAA;AAAA;AAE1D;AAEA,MAAM,gBAAA,GAAmB,CACvB,SAAA,EACA,SAAA,EACA,OACY,EAAA,CAAG,MAAA,IAAU,SAAA,IAAa,EAAA,CAAG,MAAA,IAAU,SAAA;AAiE9C,SAAS,sBACX,IAAA,EAG8C;AACjD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA;AACH,MAAA,OAAO,cAAA,CAAe,GAAG,IAAI,CAAA;AAAA,IAE/B,KAAK,CAAA;AACH,MAAA,OAAO,CAAC,EAAA,KAAO,cAAA,CAAe,IAAA,CAAK,CAAC,GAAG,EAAE,CAAA;AAAA;AAE/C;AAEA,MAAM,cAAA,GAAiB,CACrB,MAAA,EACA,EAAA,KACY,GAAG,MAAA,KAAW,MAAA;AAuBrB,MAAM,OAAA,GAAU,CACrB,KAAA,KACkD,KAAA,CAAM,MAAA,KAAW;AAqB9D,MAAM,UAAA,GAAa,CACxB,KAAA,KACgD,KAAA,CAAM,MAAA,GAAS;;;;"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { type BoundedLengthTuple, type FixedLengthTuple, type MaxLengthTuple, type MinLengthTuple } from 'ts-type-forge';
|
|
2
|
+
import { type SizeType } from '../../types.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Casts an array to `FixedLengthTuple<Length, E>` after checking that it has
|
|
5
|
+
* exactly `length` elements.
|
|
6
|
+
*
|
|
7
|
+
* This is the structural counterpart of `asFixedLengthArray`: it produces a
|
|
8
|
+
* tuple type rather than the branded `FixedLengthArray`. Prefer the branded
|
|
9
|
+
* one unless you specifically need the structural type — the brand keeps
|
|
10
|
+
* type-checking cost independent of the element type and of the bound.
|
|
11
|
+
*
|
|
12
|
+
* The length argument comes first, matching the type-parameter order of
|
|
13
|
+
* `FixedLengthTuple<Length, Elm>`. Omitting the array returns a function that
|
|
14
|
+
* casts one.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const rgb = Arr.asFixedLengthTuple(3, [255, 128, 0]);
|
|
20
|
+
*
|
|
21
|
+
* const red: number = rgb[0]; // OK — no `undefined`
|
|
22
|
+
*
|
|
23
|
+
* // curried version
|
|
24
|
+
* const asRgb = Arr.asFixedLengthTuple(3);
|
|
25
|
+
*
|
|
26
|
+
* // Arr.asFixedLengthTuple(3, [255, 128]); // throws TypeError
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @template Length - The exact number of elements.
|
|
30
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
31
|
+
* @param length - The exact number of elements.
|
|
32
|
+
* @param xs - The array to cast.
|
|
33
|
+
* @returns `xs` typed as `FixedLengthTuple<Length, Xs[number]> & Xs`.
|
|
34
|
+
* @throws {TypeError} If `xs.length !== length`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function asFixedLengthTuple<Length extends SizeType.ArgArr, Xs extends readonly unknown[]>(length: Length, xs: Xs): FixedLengthTuple<Length, Xs[number]> & Xs;
|
|
37
|
+
export declare function asFixedLengthTuple<Length extends SizeType.ArgArr>(length: Length): <Xs extends readonly unknown[]>(xs: Xs) => FixedLengthTuple<Length, Xs[number]> & Xs;
|
|
38
|
+
/**
|
|
39
|
+
* Casts an array to `MinLengthTuple<MinLength, E>` after checking that it has
|
|
40
|
+
* at least `minLength` elements.
|
|
41
|
+
*
|
|
42
|
+
* This is the structural counterpart of `asMinLengthArray`; see
|
|
43
|
+
* {@link asFixedLengthTuple} for when to prefer which.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* const history = Arr.asMinLengthTuple(3, [0, 1, 2, 3]);
|
|
49
|
+
*
|
|
50
|
+
* const first: number = history[0]; // OK — no `undefined`
|
|
51
|
+
*
|
|
52
|
+
* // curried version
|
|
53
|
+
* const asHistory = Arr.asMinLengthTuple(3);
|
|
54
|
+
*
|
|
55
|
+
* // Arr.asMinLengthTuple(3, [0, 1]); // throws TypeError
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @template MinLength - The minimum number of elements (inclusive).
|
|
59
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
60
|
+
* @param minLength - The minimum number of elements (inclusive).
|
|
61
|
+
* @param xs - The array to cast.
|
|
62
|
+
* @returns `xs` typed as `MinLengthTuple<MinLength, Xs[number]> & Xs`.
|
|
63
|
+
* @throws {TypeError} If `xs.length < minLength`.
|
|
64
|
+
*/
|
|
65
|
+
export declare function asMinLengthTuple<MinLength extends SizeType.ArgArr, Xs extends readonly unknown[]>(minLength: MinLength, xs: Xs): MinLengthTuple<MinLength, Xs[number]> & Xs;
|
|
66
|
+
export declare function asMinLengthTuple<MinLength extends SizeType.ArgArr>(minLength: MinLength): <Xs extends readonly unknown[]>(xs: Xs) => MinLengthTuple<MinLength, Xs[number]> & Xs;
|
|
67
|
+
/**
|
|
68
|
+
* Casts an array to `MaxLengthTuple<MaxLength, E>` after checking that it has
|
|
69
|
+
* at most `maxLength` elements.
|
|
70
|
+
*
|
|
71
|
+
* This is the structural counterpart of `asMaxLengthArray`; see
|
|
72
|
+
* {@link asFixedLengthTuple} for when to prefer which.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* const tags = Arr.asMaxLengthTuple(8, ['a', 'b', 'c']);
|
|
78
|
+
*
|
|
79
|
+
* // curried version
|
|
80
|
+
* const asTags = Arr.asMaxLengthTuple(8);
|
|
81
|
+
*
|
|
82
|
+
* // Arr.asMaxLengthTuple(2, ['a', 'b', 'c']); // throws TypeError
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @template MaxLength - The maximum number of elements (inclusive).
|
|
86
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
87
|
+
* @param maxLength - The maximum number of elements (inclusive).
|
|
88
|
+
* @param xs - The array to cast.
|
|
89
|
+
* @returns `xs` typed as `MaxLengthTuple<MaxLength, Xs[number]> & Xs`.
|
|
90
|
+
* @throws {TypeError} If `xs.length > maxLength`.
|
|
91
|
+
*/
|
|
92
|
+
export declare function asMaxLengthTuple<MaxLength extends SizeType.ArgArr, Xs extends readonly unknown[]>(maxLength: MaxLength, xs: Xs): MaxLengthTuple<MaxLength, Xs[number]> & Xs;
|
|
93
|
+
export declare function asMaxLengthTuple<MaxLength extends SizeType.ArgArr>(maxLength: MaxLength): <Xs extends readonly unknown[]>(xs: Xs) => MaxLengthTuple<MaxLength, Xs[number]> & Xs;
|
|
94
|
+
/**
|
|
95
|
+
* Casts an array to `BoundedLengthTuple<Min, Max, E>` after checking that its
|
|
96
|
+
* length is within the inclusive range `[min, max]`.
|
|
97
|
+
*
|
|
98
|
+
* This is the structural counterpart of `asBoundedLengthArray`; see
|
|
99
|
+
* {@link asFixedLengthTuple} for when to prefer which.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
*
|
|
103
|
+
* ```ts
|
|
104
|
+
* const selection = Arr.asBoundedLengthTuple(1, 5, [1, 2, 3]);
|
|
105
|
+
*
|
|
106
|
+
* // curried version
|
|
107
|
+
* const asSelection = Arr.asBoundedLengthTuple(1, 5);
|
|
108
|
+
*
|
|
109
|
+
* // Arr.asBoundedLengthTuple(1, 2, [1, 2, 3]); // throws TypeError
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @template Min - The minimum number of elements (inclusive).
|
|
113
|
+
* @template Max - The maximum number of elements (inclusive).
|
|
114
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
115
|
+
* @param min - The minimum number of elements (inclusive).
|
|
116
|
+
* @param max - The maximum number of elements (inclusive).
|
|
117
|
+
* @param xs - The array to cast.
|
|
118
|
+
* @returns `xs` typed as `BoundedLengthTuple<Min, Max, Xs[number]> & Xs`.
|
|
119
|
+
* @throws {TypeError} If `xs.length` is outside `[min, max]`.
|
|
120
|
+
*/
|
|
121
|
+
export declare function asBoundedLengthTuple<Min extends SizeType.ArgArr, Max extends SizeType.ArgArr, Xs extends readonly unknown[]>(min: Min, max: Max, xs: Xs): BoundedLengthTuple<Min, Max, Xs[number]> & Xs;
|
|
122
|
+
export declare function asBoundedLengthTuple<Min extends SizeType.ArgArr, Max extends SizeType.ArgArr>(min: Min, max: Max): <Xs extends readonly unknown[]>(xs: Xs) => BoundedLengthTuple<Min, Max, Xs[number]> & Xs;
|
|
123
|
+
/**
|
|
124
|
+
* Casts an array to the structural empty tuple after checking that it is
|
|
125
|
+
* empty.
|
|
126
|
+
*
|
|
127
|
+
* This is the structural counterpart of `asEmptyArray`, and the length-0
|
|
128
|
+
* specialization of {@link asFixedLengthTuple}.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* const nothing = Arr.asEmptyTuple([]);
|
|
134
|
+
*
|
|
135
|
+
* // Arr.asEmptyTuple([1]); // throws TypeError
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
139
|
+
* @param xs - The array to cast.
|
|
140
|
+
* @returns `xs` typed as `readonly [] & Xs`.
|
|
141
|
+
* @throws {TypeError} If `xs` is not empty.
|
|
142
|
+
*/
|
|
143
|
+
export declare const asEmptyTuple: <Xs extends readonly unknown[]>(xs: Xs) => readonly [] & Xs;
|
|
144
|
+
/**
|
|
145
|
+
* Casts an array to `MinLengthTuple<1, E>` after checking that it has at least
|
|
146
|
+
* one element.
|
|
147
|
+
*
|
|
148
|
+
* This is the structural counterpart of `asNonEmptyArray`, and the length-1
|
|
149
|
+
* specialization of {@link asMinLengthTuple}.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
*
|
|
153
|
+
* ```ts
|
|
154
|
+
* const values = Arr.asNonEmptyTuple([1, 2, 3]);
|
|
155
|
+
*
|
|
156
|
+
* const first: number = values[0]; // OK — no `undefined`
|
|
157
|
+
*
|
|
158
|
+
* // Arr.asNonEmptyTuple([]); // throws TypeError
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @template Xs - The input array type (tuple types are preserved).
|
|
162
|
+
* @param xs - The array to cast.
|
|
163
|
+
* @returns `xs` typed as `MinLengthTuple<1, Xs[number]> & Xs`.
|
|
164
|
+
* @throws {TypeError} If `xs` is empty.
|
|
165
|
+
*/
|
|
166
|
+
export declare const asNonEmptyTuple: <Xs extends readonly unknown[]>(xs: Xs) => MinLengthTuple<1, Xs[number]> & Xs;
|
|
167
|
+
//# sourceMappingURL=array-utils-length-bounded-tuple-cast.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array-utils-length-bounded-tuple-cast.d.mts","sourceRoot":"","sources":["../../../src/array/impl/array-utils-length-bounded-tuple-cast.mts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { isEmptyTuple, isMinLengthTuple, isBoundedLengthTuple, isFixedLengthTuple, isMaxLengthTuple } from './array-utils-length-bounded-tuple-guard.mjs';
|
|
2
|
+
|
|
3
|
+
function asFixedLengthTuple(...args) {
|
|
4
|
+
switch (args.length) {
|
|
5
|
+
case 2:
|
|
6
|
+
return asFixedLengthTupleImpl(...args);
|
|
7
|
+
case 1:
|
|
8
|
+
return (xs) => asFixedLengthTupleImpl(args[0], xs);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const asFixedLengthTupleImpl = (length, xs) => {
|
|
12
|
+
const actualLength = xs.length;
|
|
13
|
+
if (!isFixedLengthTuple(length, xs)) {
|
|
14
|
+
throw new TypeError(
|
|
15
|
+
`Expected an array of length ${length}, got an array of length ${actualLength}`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
return xs;
|
|
19
|
+
};
|
|
20
|
+
function asMinLengthTuple(...args) {
|
|
21
|
+
switch (args.length) {
|
|
22
|
+
case 2:
|
|
23
|
+
return asMinLengthTupleImpl(...args);
|
|
24
|
+
case 1:
|
|
25
|
+
return (xs) => asMinLengthTupleImpl(args[0], xs);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const asMinLengthTupleImpl = (minLength, xs) => {
|
|
29
|
+
const actualLength = xs.length;
|
|
30
|
+
if (!isMinLengthTuple(minLength, xs)) {
|
|
31
|
+
throw new TypeError(
|
|
32
|
+
`Expected an array of length >= ${minLength}, got an array of length ${actualLength}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return xs;
|
|
36
|
+
};
|
|
37
|
+
function asMaxLengthTuple(...args) {
|
|
38
|
+
switch (args.length) {
|
|
39
|
+
case 2:
|
|
40
|
+
return asMaxLengthTupleImpl(...args);
|
|
41
|
+
case 1:
|
|
42
|
+
return (xs) => asMaxLengthTupleImpl(args[0], xs);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const asMaxLengthTupleImpl = (maxLength, xs) => {
|
|
46
|
+
const actualLength = xs.length;
|
|
47
|
+
if (!isMaxLengthTuple(maxLength, xs)) {
|
|
48
|
+
throw new TypeError(
|
|
49
|
+
`Expected an array of length <= ${maxLength}, got an array of length ${actualLength}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return xs;
|
|
53
|
+
};
|
|
54
|
+
function asBoundedLengthTuple(...args) {
|
|
55
|
+
switch (args.length) {
|
|
56
|
+
case 3:
|
|
57
|
+
return asBoundedLengthTupleImpl(...args);
|
|
58
|
+
case 2:
|
|
59
|
+
return (xs) => asBoundedLengthTupleImpl(...args, xs);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const asBoundedLengthTupleImpl = (min, max, xs) => {
|
|
63
|
+
const actualLength = xs.length;
|
|
64
|
+
if (!isBoundedLengthTuple(min, max, xs)) {
|
|
65
|
+
throw new TypeError(
|
|
66
|
+
`Expected an array of length in [${min}, ${max}], got an array of length ${actualLength}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return xs;
|
|
70
|
+
};
|
|
71
|
+
const asEmptyTuple = (xs) => {
|
|
72
|
+
if (!isEmptyTuple(xs)) {
|
|
73
|
+
throw new TypeError(
|
|
74
|
+
`Expected an empty array, got an array of length ${xs.length}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return xs;
|
|
78
|
+
};
|
|
79
|
+
const asNonEmptyTuple = (xs) => {
|
|
80
|
+
if (!isMinLengthTuple(1, xs)) {
|
|
81
|
+
throw new TypeError("Expected a non-empty array, got an empty array");
|
|
82
|
+
}
|
|
83
|
+
return xs;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { asBoundedLengthTuple, asEmptyTuple, asFixedLengthTuple, asMaxLengthTuple, asMinLengthTuple, asNonEmptyTuple };
|
|
87
|
+
//# sourceMappingURL=array-utils-length-bounded-tuple-cast.mjs.map
|