@rzl-zone/utils-js 3.3.1 → 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.
- package/README.md +3 -3
- package/dist/assertions/index.d.ts +1 -1
- package/dist/conversions/index.d.ts +1 -1
- package/dist/formatters/index.d.ts +1 -1
- package/dist/generators/index.d.ts +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/{isPlainObject-DGJkcFYw.d.ts → isPlainObject-CEPWPiXh.d.ts} +1 -1
- package/dist/next/index.d.ts +1 -1
- package/dist/operations/index.d.ts +1 -1
- package/dist/predicates/index.d.ts +1 -1
- package/dist/promises/index.d.ts +1 -1
- package/dist/strings/index.d.ts +1 -1
- package/package.json +2 -7
- package/dist/NumberRangeUnion-B6bhM2s7.d.ts +0 -33
- package/dist/any-v4TsK9ES.d.ts +0 -66
- package/dist/arrays-normalize-recursive-BqmVuFlD.d.ts +0 -72
- package/dist/extends-DtdRjDyU.d.ts +0 -343
- package/dist/if-ChM35c_q.d.ts +0 -19
- package/dist/is-array-BJeHxPM3.d.ts +0 -952
- package/dist/never-D89PbPh5.d.ts +0 -66
- package/dist/nils-CO8zLHSB.d.ts +0 -151
- package/dist/or-C6qzKt2I.d.ts +0 -82
- package/dist/override-CL2olHE5.d.ts +0 -59
- package/dist/pick-BSMX6Xe2.d.ts +0 -15
- package/dist/prettify-3o8_Kw6b.d.ts +0 -564
- package/dist/promises-LU7K00H0.d.ts +0 -72
- package/dist/string-B1jlOnws.d.ts +0 -312
- package/dist/types/index.d.ts +0 -3345
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
- 📦 Works in **Node.js** & modern browsers
|
|
102
102
|
- ❤️ Simple API, easy to extend
|
|
103
103
|
- 🧬 **Next.js support**: helpers for dynamic routes, building URLs, reading env, extracting client IP
|
|
104
|
-
- 🛠 Additional TypeScript types: `OmitStrict`, `PartialOnly`, etc.
|
|
104
|
+
- 🛠 Additional including TypeScript package **`@rzl-zone/ts-types-plus`** types: `OmitStrict`, `PartialOnly`, etc.
|
|
105
105
|
|
|
106
106
|
---
|
|
107
107
|
|
|
@@ -148,7 +148,7 @@
|
|
|
148
148
|
import { | } from "@rzl-zone/utils-js/tailwind";
|
|
149
149
|
import { | } from "@rzl-zone/utils-js/urls";
|
|
150
150
|
|
|
151
|
-
import type { | } from "@rzl-zone/
|
|
151
|
+
import type { | } from "@rzl-zone/ts-types-plus";
|
|
152
152
|
```
|
|
153
153
|
#### Place your cursor inside { } or after "@rzl-zone/utils-js/{{ | }}" then press Ctrl+Space to see all available functions/types with full TSDoc hints.
|
|
154
154
|
---
|
|
@@ -222,7 +222,7 @@ console.log(isServer());
|
|
|
222
222
|
#### *Example Types Helper Import:*
|
|
223
223
|
|
|
224
224
|
```ts
|
|
225
|
-
import type { OmitStrict } from "@rzl-zone/
|
|
225
|
+
import type { OmitStrict } from "@rzl-zone/ts-types-plus";
|
|
226
226
|
|
|
227
227
|
type MyType = OmitStrict<OtherType, "omittingProps">;
|
|
228
228
|
// Fully strict TS omit that requires all keys to exist in target
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{Prettify,PickStrict}from'@rzl-zone/ts-types-plus';import{G as GetPreciseTypeOptions,I as IsNumberOptions,a as IsPlainObjectResult}from'../isPlainObject-CEPWPiXh.js';
|
|
2
2
|
/** -------------------------------------------------------
|
|
3
3
|
* * ***Shape of the object passed to custom error message functions.***
|
|
4
4
|
* -------------------------------------------------------
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{Nullish,KeepNull,KeepUndef,AnyFunction,NormalizeEmptyArraysRecursive,RemoveEmptyArrayElements,FixNeverArrayRecursive,IfNotExtends,IfExtends,IsAny,NumberRangeUnion,Prettify,OrArr,Extends,AndArr,TypedArray,WebApiObjects,IntlObjects}from'@rzl-zone/ts-types-plus';type NormalizeInputToNumberArrayUnRecursive<T>=T extends string|bigint|boolean|number|Nullish?T:HasNonNumberLikeNonNullish<T>;
|
|
2
2
|
/** Detects whether `T` contains any number-like type (`string | number | bigint`).
|
|
3
3
|
*
|
|
4
4
|
* @template T Input type.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{NumberFormat,CountryCode}from'libphonenumber-js';import{
|
|
1
|
+
import{NumberFormat,CountryCode}from'libphonenumber-js';import{OverrideTypes,ExtractStrict,Prettify,OmitStrict,AnyString}from'@rzl-zone/ts-types-plus';import{FormatOptions,Locale}from'date-fns';type NegativeFormatOptionCustom={
|
|
2
2
|
/** Custom formatter function for the final formatted negative string.
|
|
3
3
|
* If provided, it ***OVERRIDES*** style & space entirely. */
|
|
4
4
|
custom:(formatted:string)=>string;style?:never;space?:never;};type NegativeFormatOptionUnCustom={custom?:never;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{NullToUndefined,FixNeverArrayRecursive,IfNonEmptyArray,Extends}from'@rzl-zone/ts-types-plus';
|
|
2
2
|
/** ---------------------------------
|
|
3
3
|
* * ***Utility: `getRandomItem`.***
|
|
4
4
|
* ---------------------------------
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{NonPlainObject}from'@rzl-zone/ts-types-plus';
|
|
2
2
|
/** ---------------------------------------------------------------------------
|
|
3
3
|
* * ***Type Options for {@link getPreciseType | `getPreciseType`}.***
|
|
4
4
|
* ---------------------------------------------------------------------------
|
package/dist/next/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{IsAny}from'@rzl-zone/ts-types-plus';
|
|
2
2
|
/** ---------------------------------------------------------
|
|
3
3
|
* * ***Extracts dynamic route parameters from a given route string.***
|
|
4
4
|
* ---------------------------------------------------------
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{NumberRangeUnion}from'@rzl-zone/ts-types-plus';
|
|
2
2
|
/** ----------------------------------------------------------------------
|
|
3
3
|
* * ***Utility: `findDuplicates`.***
|
|
4
4
|
* ----------------------------------------------------------------------
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{NumberRangeUnion,Prettify,IsPositive,ParseNumber,Extends,IsStringLiteral,CharAt,AnyString,AnyFunction,IsEmptyString,Trim,IsAny,IsUnknown,IsNever,IsReadonlyArray,IsArray,AnObjectNonArray,OrArr,TypedArray}from'@rzl-zone/ts-types-plus';import{a as IsPlainObjectResult}from'../isPlainObject-CEPWPiXh.js';export{G as GetPreciseTypeOptions,I as IsNumberOptions,g as getPreciseType,i as isNumber,b as isPlainObject}from'../isPlainObject-CEPWPiXh.js';
|
|
2
2
|
/** ----------------------------------------------------------
|
|
3
3
|
* * ***Predicate: `areArraysEqual`.***
|
|
4
4
|
* ----------------------------------------------------------
|
package/dist/promises/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{CustomPromiseType}from'@rzl-zone/ts-types-plus';
|
|
2
2
|
/** -------------------------------------------------------------
|
|
3
3
|
* * ***Utility Class: `CustomPromise`.***
|
|
4
4
|
* -------------------------------------------------------------
|
package/dist/strings/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{Nilable,Extends}from'@rzl-zone/ts-types-plus';type CapitalizeFirstOptions={
|
|
2
2
|
/** If true **(default)**, the rest of the string will be converted to lowercase after capitalizing the first letter.
|
|
3
3
|
*
|
|
4
4
|
* @default true
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rzl-zone/utils-js",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"engineStrict": true,
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"url": "https://github.com/rzl-zone/utils-js/issues"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@rzl-zone/ts-types-plus": "latest",
|
|
21
22
|
"date-fns": "^4.1.0",
|
|
22
23
|
"libphonenumber-js": "^1.12.17",
|
|
23
24
|
"server-only": "^0.0.1",
|
|
@@ -201,9 +202,6 @@
|
|
|
201
202
|
"types": "./dist/urls/index.d.ts",
|
|
202
203
|
"default": "./dist/urls/index.cjs"
|
|
203
204
|
}
|
|
204
|
-
},
|
|
205
|
-
"./types": {
|
|
206
|
-
"types": "./dist/types/index.d.ts"
|
|
207
205
|
}
|
|
208
206
|
},
|
|
209
207
|
"typesVersions": {
|
|
@@ -252,9 +250,6 @@
|
|
|
252
250
|
],
|
|
253
251
|
"urls": [
|
|
254
252
|
"dist/urls/index.d.ts"
|
|
255
|
-
],
|
|
256
|
-
"types": [
|
|
257
|
-
"dist/types/index.d.ts"
|
|
258
253
|
]
|
|
259
254
|
}
|
|
260
255
|
},
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/** --------------------------------------------------
|
|
2
|
-
* * ***Internal Utility Type for: {@link NumberRangeUnion | `NumberRangeUnion`}.***
|
|
3
|
-
* --------------------------------------------------
|
|
4
|
-
* @template N - Starting/Ending number of the range (inclusive).
|
|
5
|
-
* @template Acc - Internal accumulator for recursion (do not set manually).
|
|
6
|
-
*/
|
|
7
|
-
type Enumerate<N extends number,Acc extends number[]=[]>=Acc["length"] extends N?Acc[number]:Enumerate<N,[...Acc,Acc["length"]]>;
|
|
8
|
-
/** --------------------------------------------------
|
|
9
|
-
* * ***Utility Type: `NumberRangeUnion`.***
|
|
10
|
-
* --------------------------------------------------
|
|
11
|
-
* **Generate a union type of numbers from `From` to `To` using enumeration.**
|
|
12
|
-
* @description
|
|
13
|
-
* Produces a **numeric union type** from `From` to `To` (inclusive),
|
|
14
|
-
* using a simpler approach based on `Enumerate<N>` helper type.
|
|
15
|
-
* - ✅ Straightforward & easy to reason about.
|
|
16
|
-
* - ⚠️ Still limited by TypeScript recursion depth (safe up to `999`).
|
|
17
|
-
* - ⚙️ Best used for **smaller ranges** (`≤ 100`) or when readability matters.
|
|
18
|
-
* - ℹ️ For **larger ranges** (`≥ 101`) use {@link NumberRangeLimit | `NumberRangeLimit`} instead.
|
|
19
|
-
* @template From - Starting number of the range (inclusive).
|
|
20
|
-
* @template To - Ending number of the range (inclusive).
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* type RangeA = NumberRangeUnion<3, 6>;
|
|
24
|
-
* // ➔ 3 | 4 | 5 | 6
|
|
25
|
-
* type RangeB = NumberRangeUnion<0, 2>;
|
|
26
|
-
* // ➔ 0 | 1 | 2
|
|
27
|
-
* type RangeC = NumberRangeUnion<8, 8>;
|
|
28
|
-
* // ➔ 8
|
|
29
|
-
* type RangeD = NumberRangeUnion<20, 10>;
|
|
30
|
-
* // ➔ 10
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
type NumberRangeUnion<From extends number,To extends number>=From extends To?From:Exclude<Enumerate<To>,Enumerate<From>>extends never?To:Exclude<Enumerate<To>,Enumerate<From>>|To;export type{NumberRangeUnion as N};
|
package/dist/any-v4TsK9ES.d.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import{I as If}from'./if-ChM35c_q.js';
|
|
2
|
-
/** -------------------------------------------------------
|
|
3
|
-
* * ***Utility Type: `IsAny`.***
|
|
4
|
-
* -------------------------------------------------------
|
|
5
|
-
* **A type-level utility that checks whether a type is ***`any`***.**
|
|
6
|
-
* - **Behavior:**
|
|
7
|
-
* - Returns `true` if `T` is `any`.
|
|
8
|
-
* - Returns `false` for otherwise.
|
|
9
|
-
* @template T - The type to evaluate.
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* type A = IsAny<any>; // ➔ true
|
|
13
|
-
* type B = IsAny<string>; // ➔ false
|
|
14
|
-
* type C = IsAny<unknown>; // ➔ false
|
|
15
|
-
* type D = IsAny<never>; // ➔ false
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
type IsAny<T>=0 extends 1 & T?true:false;
|
|
19
|
-
/** -------------------------------------------------------
|
|
20
|
-
* * ***Utility Type: `IfAny`.***
|
|
21
|
-
* -------------------------------------------------------
|
|
22
|
-
* **A type-level conditional utility that returns one type if ***`T` is `any`***,
|
|
23
|
-
* and another type otherwise.**
|
|
24
|
-
* - **Behavior:**
|
|
25
|
-
* - Defaults to `true` when `T` is `any`.
|
|
26
|
-
* - Defaults to `false` for otherwise.
|
|
27
|
-
* @template T - The type to check.
|
|
28
|
-
* @template IfTrue - The type to return if `T` is `any`, *(default: `true`)*.
|
|
29
|
-
* @template IfFalse - The type to return if `T` is not `any`, *(default: `false`)*.
|
|
30
|
-
* @example
|
|
31
|
-
* ```ts
|
|
32
|
-
* type A = IfAny<any, string, number>;
|
|
33
|
-
* // ➔ string
|
|
34
|
-
* type B = IfAny<string, string, number>;
|
|
35
|
-
* // ➔ number
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
type IfAny<T,IfTrue=true,IfFalse=false>=If<IsAny<T>,IfTrue,IfFalse>;
|
|
39
|
-
/** * ***Configuration options for a type-level utility for
|
|
40
|
-
* {@link AnifyProperties | `AnifyProperties`}.***
|
|
41
|
-
*/
|
|
42
|
-
type AnifyPropertiesOptions={
|
|
43
|
-
/** If `makeOptional: true`, all properties become optional, otherwise, all properties are required and typed as `any`, defaultValue: `false`.
|
|
44
|
-
*
|
|
45
|
-
* @default false
|
|
46
|
-
*/
|
|
47
|
-
makeOptional:boolean;};
|
|
48
|
-
/** -------------------------------------------------------
|
|
49
|
-
* * ***Utility Type: `AnifyProperties`.***
|
|
50
|
-
* -------------------------------------------------------
|
|
51
|
-
* **A type-level utility that transforms all properties of an object
|
|
52
|
-
* into ***`any`***.**
|
|
53
|
-
* - **Behavior:**
|
|
54
|
-
* - If `makeOptional: true`, all properties become optional.
|
|
55
|
-
* - Otherwise, all properties are required and typed as `any`.
|
|
56
|
-
* @template T The object type to transform.
|
|
57
|
-
* @template Options Configuration options, defaults to `{ makeOptional: false }`.
|
|
58
|
-
* @example
|
|
59
|
-
* ```ts
|
|
60
|
-
* type A = AnifyProperties<{a: string; b: number}>;
|
|
61
|
-
* // ➔ { a: any; b: any }
|
|
62
|
-
* type B = AnifyProperties<{a: string; b: number}, { makeOptional: true }>;
|
|
63
|
-
* // ➔ { a?: any; b?: any }
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
type AnifyProperties<T extends object,Options extends AnifyPropertiesOptions={makeOptional:false;}>={[K in keyof T]:any;}extends infer Result?If<Options["makeOptional"],Partial<Result>,Result>:never;export type{AnifyProperties as A,IsAny as I,AnifyPropertiesOptions as a,IfAny as b};
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/** -------------------------------------------------------
|
|
2
|
-
* * ***Utility Type: `FixNeverArrayRecursive`.***
|
|
3
|
-
* -------------------------------------------------------
|
|
4
|
-
* **A type-level utility that **recursively transforms arrays of type `never[]` into empty arrays**.**
|
|
5
|
-
* - **Behavior:**
|
|
6
|
-
* - Preserves `readonly` modifiers.
|
|
7
|
-
* - Applies recursively for nested arrays.
|
|
8
|
-
* - Leaves other types unchanged.
|
|
9
|
-
* @template T - The input type to recursively fix.
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* type A = FixNeverArrayRecursive<never[]>;
|
|
13
|
-
* // ➔ []
|
|
14
|
-
* type B = FixNeverArrayRecursive<readonly never[]>;
|
|
15
|
-
* // ➔ readonly []
|
|
16
|
-
* type C = FixNeverArrayRecursive<string[]>;
|
|
17
|
-
* // ➔ string[]
|
|
18
|
-
* type D = FixNeverArrayRecursive<(never | string)[]>;
|
|
19
|
-
* // ➔ (never | string)[]
|
|
20
|
-
* type E = FixNeverArrayRecursive<(never[])[]>;
|
|
21
|
-
* // ➔ [][]
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
type FixNeverArrayRecursive<T>=T extends readonly never[]?T extends never[]?[]:readonly []:T extends(infer U)[]?FixNeverArrayRecursive<U>[]:T extends readonly(infer U)[]?readonly FixNeverArrayRecursive<U>[]:T;
|
|
25
|
-
/** -------------------------------------------------------
|
|
26
|
-
* * ***Utility Type: `NormalizeEmptyArraysRecursive`.***
|
|
27
|
-
* -------------------------------------------------------
|
|
28
|
-
* **A type-level utility that **recursively normalizes empty array types** by converting arrays whose element type is `never`, `null`, or `undefined` into empty tuple types (`[]`).**
|
|
29
|
-
* - **Behavior:**
|
|
30
|
-
* - Preserves `readonly` modifiers.
|
|
31
|
-
* - Recurses into nested arrays.
|
|
32
|
-
* - Leaves other array types unchanged.
|
|
33
|
-
* @template T - The input type to normalize.
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* type A = NormalizeEmptyArraysRecursive<never[]>;
|
|
37
|
-
* // ➔ []
|
|
38
|
-
* type B = NormalizeEmptyArraysRecursive<readonly never[]>;
|
|
39
|
-
* // ➔ readonly []
|
|
40
|
-
* type C = NormalizeEmptyArraysRecursive<null[]>;
|
|
41
|
-
* // ➔ []
|
|
42
|
-
* type D = NormalizeEmptyArraysRecursive<(null[] | string[])[]>;
|
|
43
|
-
* // ➔ ([] | string[])[]
|
|
44
|
-
* type E = NormalizeEmptyArraysRecursive<string[]>;
|
|
45
|
-
* // ➔ string[]
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
type NormalizeEmptyArraysRecursive<T>=T extends readonly(infer U)[]?U extends never|null|undefined?T extends readonly unknown[]?T extends(infer E)[]?[]:readonly []:never:T extends(infer E)[]?NormalizeEmptyArraysRecursive<U>[]:readonly NormalizeEmptyArraysRecursive<U>[]:T;
|
|
49
|
-
/** -------------------------------------------------------
|
|
50
|
-
* * ***Utility Type: `RemoveEmptyArrayElements`.***
|
|
51
|
-
* -------------------------------------------------------
|
|
52
|
-
* **A type-level utility that **recursively removes empty array elements (`[]`) from a tuple type**.**
|
|
53
|
-
* - **Behavior:**
|
|
54
|
-
* - If `T` is a tuple, checks the first element:
|
|
55
|
-
* - If `Head` is an empty array type (`[]`), it is removed.
|
|
56
|
-
* - Otherwise, `Head` is preserved.
|
|
57
|
-
* - Repeats recursively on the rest of the tuple.
|
|
58
|
-
* - Leaves non-tuple types unchanged.
|
|
59
|
-
* @template T - The tuple type to process.
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* type A = RemoveEmptyArrayElements<[[], 1, [], 2]>;
|
|
63
|
-
* // ➔ [1, 2]
|
|
64
|
-
* type B = RemoveEmptyArrayElements<[]>;
|
|
65
|
-
* // ➔ []
|
|
66
|
-
* type C = RemoveEmptyArrayElements<[[], [], []]>;
|
|
67
|
-
* // ➔ []
|
|
68
|
-
* type D = RemoveEmptyArrayElements<[1, 2, 3]>;
|
|
69
|
-
* // ➔ [1, 2, 3]
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
type RemoveEmptyArrayElements<T>=T extends [infer Head,...infer Tail]?Head extends []?RemoveEmptyArrayElements<Tail>:[Head,...RemoveEmptyArrayElements<Tail>]:T extends []?[]:T;export type{FixNeverArrayRecursive as F,NormalizeEmptyArraysRecursive as N,RemoveEmptyArrayElements as R};
|
|
@@ -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};
|