@rzl-zone/utils-js 3.3.0 → 3.4.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.
@@ -1,343 +0,0 @@
1
- import{I as If}from'./if-ChM35c_q.js';import{I as IsNever}from'./never-D89PbPh5.js';
2
- /** -------------------------------------------------------
3
- * * ***Utility Type: `Arrayable`.***
4
- * -------------------------------------------------------
5
- * **Useful when a function or API accepts **either one item or multiple items**.**
6
- * - **Represents a type that can be either:**
7
- * - a single value of type `T`, or an array of values of type `T`.
8
- * @template T - The element type.
9
- * @example
10
- * ```ts
11
- * function toArray<T>(input: Arrayable<T>): T[] {
12
- * return Array.isArray(input) ? input : [input];
13
- * }
14
- *
15
- * type A = Arrayable<string>;
16
- * // ➔ string | string[]
17
- *
18
- * const a: A = "foo";
19
- * const b: A = ["foo", "bar"];
20
- * ```
21
- */
22
- type Arrayable<T>=T|Array<T>;
23
- /** -------------------------------------------------------
24
- * * ***Utility Type: `MutableArray`.***
25
- * -------------------------------------------------------
26
- * **Recursively creates a **mutable version** of a readonly array, tuple, or object type.**
27
- * @description
28
- * By default, TypeScript infers tuple/array literals as `readonly` (especially with `as const`).
29
- * This utility removes the `readonly` modifier from all elements recursively,
30
- * turning a readonly tuple, array, or object into a mutable one.
31
- * - **Behavior:**
32
- * - Optionally, if `Widen` is `true`, literal types (`1`, `'foo'`, `true`) are widened to
33
- * their primitive equivalents (`number`, `string`, `boolean`) for easier assignment.
34
- * @template T - The readonly array, tuple, or object type to make mutable.
35
- * @template Widen - Whether to widen literal primitive types to their base types (default: `false`).
36
- * @example
37
- * ```ts
38
- * type A = readonly [1, 2, 3];
39
- * type B = MutableArray<A>;
40
- * // ➔ [1, 2, 3]
41
- *
42
- * const x: A = [1, 2, 3] as const;
43
- * // x[0] = 9; // ❌ Error: read-only
44
- *
45
- * const y: MutableArray<B,true> = [1, 2, 3];
46
- * y[0] = 9; // ✅ Allowed
47
- *
48
- * // Recursive example with objects
49
- * type Obj = readonly [{ a: 1, b: readonly [2] }];
50
- * type MutableObj = MutableArray<Obj, true>;
51
- * // ➔ [{ a: number; b: [number]; }]
52
- * ```
53
- */
54
- type MutableArray<T,Widen extends boolean=false>=T extends(...args:any)=>any?T:T extends readonly any[]?{-readonly [K in keyof T]:MutableArray<T[K],Widen>;}:T extends object?{-readonly [K in keyof T]:MutableArray<T[K],Widen>;}:Widen extends true?T extends number?number:T extends string?string:T extends boolean?boolean:T extends bigint?bigint:T extends symbol?symbol:T:T;
55
- /** --------------------------------------------------
56
- * * ***Utility Type: `GetArrayElementType`.***
57
- * --------------------------------------------------
58
- * **Gets the element type from a readonly array or tuple.**
59
- * - ✅ Useful when working with `as const` arrays to extract the union of literal types.
60
- * @template T - A readonly array or tuple type.
61
- * @example
62
- * ```ts
63
- * const roles = ['admin', 'user'] as const;
64
- * type Role = GetArrayElementType<typeof roles>;
65
- * // ➔ "admin" | "user"
66
- * ```
67
- */
68
- type GetArrayElementType<T extends readonly any[]>=T extends readonly(infer U)[]?U:never;
69
- /** -------------------------------------------------------
70
- * * ***Utility Type: `EmptyArray`.***
71
- * -------------------------------------------------------
72
- * **A type-level utility that returns `T` if it is an ***empty array***,
73
- * otherwise returns `never`.**
74
- * @template T - The array type to check.
75
- * @example
76
- * ```ts
77
- * type A = EmptyArray<[]>;
78
- * // ➔ []
79
- * type B = EmptyArray<[1]>;
80
- * // ➔ never
81
- * type C = EmptyArray<string[]>;
82
- * // ➔ string[]
83
- * type D = EmptyArray<number[]>;
84
- * // ➔ number[]
85
- * type E = EmptyArray<readonly []>;
86
- * // ➔ readonly []
87
- * ```
88
- */
89
- type EmptyArray<T extends readonly unknown[]>=T extends readonly [ unknown,...unknown[]]?never:T;
90
- /** -------------------------------------------------------
91
- * * ***Utility Type: `NonEmptyArray`.***
92
- * -------------------------------------------------------
93
- * **A type-level utility that returns `T` if it is a ***non-empty array***,
94
- * otherwise returns `never`.**
95
- * @template T - The array type to check.
96
- * @example
97
- * ```ts
98
- * type A = NonEmptyArray<[]>;
99
- * // ➔ never
100
- * type B = NonEmptyArray<[1]>;
101
- * // ➔ [1]
102
- * type C = NonEmptyArray<string[]>;
103
- * // ➔ never
104
- * type D = NonEmptyArray<number[]>;
105
- * // ➔ never
106
- * type E = NonEmptyArray<readonly []>;
107
- * // ➔ never
108
- * ```
109
- */
110
- type NonEmptyArray<T extends readonly unknown[]>=If<IsNever<EmptyArray<T>>,T,never>;
111
- /** -------------------------------------------------------
112
- * * ***Utility Type: `IsEmptyArray`.***
113
- * -------------------------------------------------------
114
- * **A type-level utility that evaluates to `true` if `T` is an ***empty array.***
115
- * (or can be empty per this definition), otherwise `false`.**
116
- * @template T - The array type to check.
117
- * @example
118
- * ```ts
119
- * type A = IsEmptyArray<[]>;
120
- * // ➔ true
121
- * type B = IsEmptyArray<[1]>;
122
- * // ➔ false
123
- * type C = IsEmptyArray<string[]>;
124
- * // ➔ true
125
- * type D = IsEmptyArray<number[]>;
126
- * // ➔ true
127
- * type E = IsEmptyArray<readonly []>;
128
- * // ➔ true
129
- * ```
130
- */
131
- type IsEmptyArray<T extends readonly unknown[]>=If<IsNever<EmptyArray<T>>,false,true>;
132
- /** -------------------------------------------------------
133
- * * ***Utility Type: `IsNonEmptyArray`.***
134
- * -------------------------------------------------------
135
- * **A type-level utility that evaluates to `true` if `T` is a ***non-empty array.***
136
- * (strictly a non-empty tuple), otherwise `false`.**
137
- * @template T - The array type to check.
138
- * @example
139
- * ```ts
140
- * type A = IsNonEmptyArray<[]>;
141
- * // ➔ false
142
- * type B = IsNonEmptyArray<[1]>;
143
- * // ➔ true
144
- * type C = IsNonEmptyArray<string[]>;
145
- * // ➔ false
146
- * type D = IsNonEmptyArray<number[]>;
147
- * // ➔ false
148
- * type E = IsNonEmptyArray<readonly []>;
149
- * // ➔ false
150
- * ```
151
- */
152
- type IsNonEmptyArray<T extends readonly unknown[]>=If<IsNever<EmptyArray<T>>,true,false>;
153
- /** -------------------------------------------------------
154
- * * ***Utility Type: `IfEmptyArray`.***
155
- * -------------------------------------------------------
156
- * **Returns the second argument if `T` is an ***empty array*** (per this utility),
157
- * otherwise returns the third argument.**
158
- * - Defaults: `IfTrue = true`, `IfFalse = false`.
159
- * @template T - The array type to check.
160
- * @template IfTrue - Returned type if `T` is empty by this definition.
161
- * @template IfFalse - Returned type if `T` is not empty by this definition.
162
- * @example
163
- * ```ts
164
- * type A = IfEmptyArray<[]>;
165
- * // ➔ true
166
- * type B = IfEmptyArray<[1]>;
167
- * // ➔ false
168
- * type C = IfEmptyArray<string[]>;
169
- * // ➔ true
170
- * type D = IfEmptyArray<readonly []>;
171
- * // ➔ true
172
- * type E = IfEmptyArray<[], "yes", "no">;
173
- * // ➔ "yes"
174
- * type F = IfEmptyArray<[1], "yes", "no">;
175
- * // ➔ "no"
176
- * type G = IfEmptyArray<string[], "yes", "no">;
177
- * // ➔ "yes"
178
- * ```
179
- */
180
- type IfEmptyArray<T extends readonly unknown[],IfTrue=true,IfFalse=false>=If<IsEmptyArray<T>,IfTrue,IfFalse>;
181
- /** -------------------------------------------------------
182
- * * ***Utility Type: `IfNonEmptyArray`.***
183
- * -------------------------------------------------------
184
- * **Returns the second argument if `T` is a ***non-empty array*** (strict tuple),
185
- * otherwise returns the third argument.**
186
- * - Defaults: `IfTrue = true`, `IfFalse = false`.
187
- * @template T - The array type to check.
188
- * @template IfTrue - Returned type if `T` is non-empty by this definition.
189
- * @template IfFalse - Returned type if `T` is not non-empty by this definition.
190
- * @example
191
- * ```ts
192
- * type A = IfNonEmptyArray<[]>;
193
- * // ➔ false
194
- * type B = IfNonEmptyArray<[1]>;
195
- * // ➔ true
196
- * type C = IfNonEmptyArray<string[]>;
197
- * // ➔ false
198
- * type D = IfNonEmptyArray<readonly []>;
199
- * // ➔ false
200
- * type E = IfNonEmptyArray<[1], "yes", "no">;
201
- * // ➔ "yes"
202
- * type F = IfNonEmptyArray<[], "yes", "no">;
203
- * // ➔ "no"
204
- * type G = IfNonEmptyArray<string[], "yes", "no">;
205
- * // ➔ "no"
206
- * ```
207
- */
208
- type IfNonEmptyArray<T extends readonly unknown[],IfTrue=true,IfFalse=false>=If<IsNonEmptyArray<T>,IfTrue,IfFalse>;
209
- /** -------------------------------------------------------
210
- * * ***Utility Type: `Not`.***
211
- * -------------------------------------------------------
212
- * **Accepts a boolean type `T` and returns its negation.**
213
- * @template T - Boolean type to negate.
214
- * @example
215
- * ```ts
216
- * type A = Not<true>; // ➔ false
217
- * type B = Not<false>; // ➔ true
218
- * ```
219
- */
220
- type Not<T extends boolean>=T extends true?false:true;
221
- /** ---------------------------------------------------------------------------
222
- * * ***Options for {@link Pop|`Pop`}.***
223
- * ---------------------------------------------------------------------------
224
- * **Configuration options for the {@link Pop | **`Pop`**} type utility.**
225
- */
226
- type PopOptions={
227
- /**
228
- * If `true`, {@link Pop | **`Pop`**} will return a tuple `[Rest, Removed]`
229
- * instead of just the remaining array, default: `false`.
230
- *
231
- * @example
232
- * ```ts
233
- * type Options = { includeRemoved: true };
234
- * type Result = Pop<[1, 2, 3], Options>; // ➔ [[1, 2], 3]
235
- * ```
236
- */
237
- includeRemoved:boolean;};
238
- /** -------------------------------------------------------
239
- * * ***Utility Type: `Pop`.***
240
- * -------------------------------------------------------
241
- * **Removes the last element from a tuple/array type.**
242
- * - If the `includeRemoved` option is `true`, it returns a tuple `[Rest, Removed]`
243
- * where `Rest` is the array without the last element, and `Removed` is the last
244
- * element.
245
- * @template T - The tuple or array to pop from.
246
- * @template Options - Configuration object. Default `{ includeRemoved: false }`.
247
- * @example
248
- * ```ts
249
- * // Removes last element
250
- * type Case1 = Pop<[1, 2, 3]>
251
- * // ➔ [1, 2]
252
- *
253
- * // Removes last element and includes the removed value
254
- * type Case2 = Pop<[1, 2, 3], { includeRemoved: true }>
255
- * // ➔ [[1, 2], 3]
256
- *
257
- * // Edge case: empty array
258
- * type Case3 = Pop<[]>
259
- * // ➔ never
260
- * ```
261
- */
262
- type Pop<T extends readonly unknown[],Options extends PopOptions={includeRemoved:false;}>=IsEmptyArray<T>extends true?never:T extends readonly [...infer Rest extends readonly unknown[],infer Removed]?If<Options["includeRemoved"],[Rest,Removed],Rest>:never;
263
- /** -------------------------------------------------------
264
- * * ***Utility Type: `Extends`.***
265
- * -------------------------------------------------------
266
- * **Returns a boolean indicating whether the first argument ***extends*** the second argument.**
267
- * @template T - The type to check.
268
- * @template Base - The type to compare against.
269
- * @example
270
- * ```ts
271
- * type A = Extends<1, number>; // ➔ true
272
- * type B = Extends<number, 1>; // ➔ false
273
- * ```
274
- */
275
- type Extends<T,Base>=[T] extends [Base]?true:false;
276
- /** -------------------------------------------------------
277
- * * ***Utility Type: `NotExtends`.***
278
- * -------------------------------------------------------
279
- * **Returns a boolean indicating whether the first argument does ***not extend*** the second argument.**
280
- * @template T - The type to check.
281
- * @template Base - The type to compare against.
282
- * @example
283
- * ```ts
284
- * type A = NotExtends<1, number>; // ➔ false
285
- * type B = NotExtends<number, 1>; // ➔ true
286
- * ```
287
- */
288
- type NotExtends<T,Base>=Not<Extends<T,Base>>;
289
- /** -------------------------------------------------------
290
- * * ***Utility Type: `IfExtends`.***
291
- * -------------------------------------------------------
292
- * - **Conditional:**
293
- * - Returns the third argument if the first argument ***extends*** the secon
294
- * argument, otherwise returns the fourth argument.
295
- * - Defaults: `IfTrue = true`, `IfFalse = false`.
296
- * @template T - The type to check.
297
- * @template Base - The type to compare against.
298
- * @template IfTrue - The branch type if condition is met, (default: `true`).
299
- * @template IfFalse - The branch type if condition is not met, (default: `false`).
300
- * @example
301
- * ```ts
302
- * type A = IfExtends<1, number, "valid">;
303
- * // ➔ "valid"
304
- * type B = IfExtends<1, string, "valid", "invalid">;
305
- * // ➔ "invalid"
306
- * ```
307
- */
308
- type IfExtends<T,Base,IfTrue=true,IfFalse=false>=If<Extends<T,Base>,IfTrue,IfFalse>;
309
- /** -------------------------------------------------------
310
- * * ***Utility Type: `IfNotExtends`.***
311
- * -------------------------------------------------------
312
- * - **Conditional:**
313
- * - Returns the third argument if the first argument does ***not extend*** the
314
- * second argument, otherwise returns the fourth argument.
315
- * - Defaults: `IfTrue = true`, `IfFalse = false`.
316
- * @template T - The type to check.
317
- * @template Base - The type to compare against.
318
- * @template IfTrue - The branch type if condition is met, (default: `true`).
319
- * @template IfFalse - The branch type if condition is not met, (default: `false`).
320
- * @example
321
- * ```ts
322
- * type A = IfNotExtends<1, string, "valid">;
323
- * // ➔ "valid"
324
- * type B = IfNotExtends<1, number, "valid", "invalid">;
325
- * // ➔ "invalid"
326
- * ```
327
- */
328
- type IfNotExtends<T,Base,IfTrue=true,IfFalse=false>=If<NotExtends<T,Base>,IfTrue,IfFalse>;
329
- /** -------------------------------------------------------
330
- * * ***Utility Type: `ExtendsArr`.***
331
- * -------------------------------------------------------
332
- * **Returns a boolean indicating whether every element of the first array argument ***extends*** the second argument.**
333
- * @template T - The array to check.
334
- * @template Base - The type to compare each element against.
335
- * @example
336
- * ```ts
337
- * type A = ExtendsArr<[1, 2, 3], number>;
338
- * // ➔ true
339
- * type B = ExtendsArr<[1, "2", 3], number>;
340
- * // ➔ false
341
- * ```
342
- */
343
- type ExtendsArr<T extends readonly unknown[],Base>=IsEmptyArray<T>extends true?true:Pop<T,{includeRemoved:true;}>extends readonly [infer Rest extends readonly unknown[],infer Removed]?Extends<Removed,Base>extends true?ExtendsArr<Rest,Base>:false:false;export type{Arrayable as A,Extends as E,GetArrayElementType as G,IfExtends as I,MutableArray as M,Not as N,Pop as P,NotExtends as a,IsEmptyArray as b,ExtendsArr as c,IfEmptyArray as d,EmptyArray as e,IfNonEmptyArray as f,IsNonEmptyArray as g,NonEmptyArray as h,IfNotExtends as i,PopOptions as j};
@@ -1,19 +0,0 @@
1
- /** -------------------------------------------------------
2
- * * ***Utility Type: `If`.***
3
- * -------------------------------------------------------
4
- * - **Conditional:**
5
- * - Returns the second argument if the first argument is `true`, otherwise
6
- * returns the third argument.
7
- * - Defaults: `IfTrue = true`, `IfFalse = false`.
8
- * @template Condition - The boolean condition to check.
9
- * @template IfTrue - The branch type if condition is `true`. (default: `true`).
10
- * @template IfFalse - The branch type if condition is `false`. (default: `false`).
11
- * @example
12
- * ```ts
13
- * type A = If<true, "valid">;
14
- * // ➔ "valid"
15
- * type B = If<false, "valid", "invalid">;
16
- * // ➔ "invalid"
17
- * ```
18
- */
19
- type If<Condition,IfTrue=true,IfFalse=false>=Condition extends true?IfTrue:IfFalse;export type{If as I};