@rzl-zone/utils-js 2.1.0 → 3.0.0-beta.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/dist/NumberRangeUnion-DC-C3_Kq.d.ts +26 -0
- package/dist/any-BmdI8UbK.d.ts +67 -0
- package/dist/array-CIZRbqTF.d.ts +223 -0
- package/dist/arrays-normalize-recursive-CnjYJ9xg.d.ts +72 -0
- package/dist/assertions/index.cjs +1 -0
- package/dist/assertions/index.d.ts +428 -0
- package/dist/assertions/index.js +1 -0
- package/dist/chunk-2TRAPBZ7.cjs +1 -0
- package/dist/chunk-3D74QO5D.cjs +1 -0
- package/dist/chunk-4DK3RCC4.js +2 -0
- package/dist/chunk-4JOQ45HL.js +1 -0
- package/dist/chunk-55CZALRS.js +1 -0
- package/dist/chunk-56QBKKRE.js +1 -0
- package/dist/chunk-5JFV3GDE.cjs +4 -0
- package/dist/chunk-5SZUSNGZ.js +1 -0
- package/dist/chunk-62HX5Z45.cjs +1 -0
- package/dist/chunk-7NA6DUHR.cjs +2 -0
- package/dist/chunk-7YWAQOA4.cjs +1 -0
- package/dist/chunk-BVPMMWDL.cjs +1 -0
- package/dist/chunk-BYZAD3XN.cjs +1 -0
- package/dist/chunk-CEFYHEM4.cjs +1 -0
- package/dist/chunk-CN53M4QZ.cjs +1 -0
- package/dist/chunk-D47MHBSD.cjs +1 -0
- package/dist/chunk-DGH75GJD.js +1 -0
- package/dist/chunk-E5NUI7PN.js +1 -0
- package/dist/chunk-ELDDNERR.js +1 -0
- package/dist/chunk-EXZ47NOW.js +1 -0
- package/dist/chunk-FDITZ5C6.cjs +1 -0
- package/dist/chunk-FF76ISQ2.js +1 -0
- package/dist/chunk-FIEZKTAZ.cjs +1 -0
- package/dist/chunk-FSSV24W7.cjs +1 -0
- package/dist/chunk-G5LXIEFO.js +0 -0
- package/dist/chunk-GCGU2WB7.js +1 -0
- package/dist/chunk-GECI2YBP.js +1 -0
- package/dist/chunk-GQE4OVHC.cjs +1 -0
- package/dist/chunk-GRVZXQXL.cjs +1 -0
- package/dist/chunk-I4AVNHPA.cjs +1 -0
- package/dist/chunk-JFRL7NKU.js +4 -0
- package/dist/chunk-JWHM3WZQ.cjs +1 -0
- package/dist/chunk-KCQDDZJE.cjs +1 -0
- package/dist/chunk-L54ZPSYJ.js +1 -0
- package/dist/chunk-LVKAYEZ4.js +1 -0
- package/dist/chunk-MBDWTK54.cjs +1 -0
- package/dist/chunk-MMSYCIJ2.cjs +1 -0
- package/dist/chunk-MNGGDB2G.js +1 -0
- package/dist/chunk-MY7BA4GI.cjs +1 -0
- package/dist/chunk-N2IJPIND.cjs +1 -0
- package/dist/chunk-NIMNTEGV.js +1 -0
- package/dist/chunk-NNM7QCNB.js +1 -0
- package/dist/chunk-PUQXRLZH.cjs +1 -0
- package/dist/chunk-PVJF2JHM.js +1 -0
- package/dist/chunk-QCFXEUKL.js +1 -0
- package/dist/chunk-QFCGBBSY.js +1 -0
- package/dist/chunk-QQYAUPSK.cjs +1 -0
- package/dist/chunk-RN3TP3S3.js +1 -0
- package/dist/chunk-RV2VULM7.cjs +1 -0
- package/dist/chunk-SW6HYEW7.cjs +1 -0
- package/dist/chunk-SYJC7UAW.js +1 -0
- package/dist/chunk-TC4VBE4Y.cjs +1 -0
- package/dist/chunk-TUXDINHF.cjs +1 -0
- package/dist/chunk-U5Y2FXMN.cjs +1 -0
- package/dist/chunk-UUPQI6ND.cjs +1 -0
- package/dist/chunk-VYCGZ2S6.js +1 -0
- package/dist/chunk-W5EDKJK3.js +1 -0
- package/dist/chunk-WHAVUFEU.js +1 -0
- package/dist/chunk-XCFIOTCV.js +1 -0
- package/dist/chunk-XE27XPJR.js +1 -0
- package/dist/chunk-XH6MCRLP.js +1 -0
- package/dist/chunk-XVB3ZZEB.js +1 -0
- package/dist/chunk-ZBSIGJFO.cjs +1 -0
- package/dist/chunk-ZYAML74V.js +1 -0
- package/dist/conversions/index.cjs +1 -0
- package/dist/conversions/index.d.ts +1633 -0
- package/dist/conversions/index.js +1 -0
- package/dist/env/index.cjs +1 -0
- package/dist/env/index.d.ts +19 -0
- package/dist/env/index.js +1 -0
- package/dist/events/index.cjs +1 -0
- package/dist/events/index.d.ts +119 -0
- package/dist/events/index.js +1 -0
- package/dist/extends-Mp81Hq9-.d.ts +145 -0
- package/dist/formatting/index.cjs +1 -0
- package/dist/formatting/index.d.ts +912 -0
- package/dist/formatting/index.js +1 -0
- package/dist/generator/index.cjs +1 -0
- package/dist/generator/index.d.ts +163 -0
- package/dist/generator/index.js +1 -0
- package/dist/if-CvT4R7Kh.d.ts +18 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -4334
- package/dist/index.js +1 -1
- package/dist/is-array-Ckm_47hw.d.ts +1557 -0
- package/dist/isPlainObject-BKYaI6a8.d.ts +182 -0
- package/dist/never-BfayMBF9.d.ts +75 -0
- package/dist/next/index.cjs +8 -1
- package/dist/next/index.d.ts +169 -169
- package/dist/next/index.js +8 -1
- package/dist/next/server/index.cjs +2 -1
- package/dist/next/server/index.d.ts +37 -37
- package/dist/next/server/index.js +2 -1
- package/dist/nils-DMz3kU7M.d.ts +177 -0
- package/dist/omit-VvmIsZmX.d.ts +28 -0
- package/dist/operations/index.cjs +1 -0
- package/dist/operations/index.d.ts +182 -0
- package/dist/operations/index.js +1 -0
- package/dist/parsers/index.cjs +1 -0
- package/dist/parsers/index.d.ts +37 -0
- package/dist/parsers/index.js +1 -0
- package/dist/predicates/index.cjs +1 -0
- package/dist/predicates/index.d.ts +1839 -0
- package/dist/predicates/index.js +1 -0
- package/dist/prettify-C4xLcYOP.d.ts +353 -0
- package/dist/promise/index.cjs +1 -0
- package/dist/promise/index.d.ts +40 -0
- package/dist/promise/index.js +1 -0
- package/dist/rzl-utils.global.js +4 -1
- package/dist/strings/index.cjs +1 -0
- package/dist/strings/index.d.ts +816 -0
- package/dist/strings/index.js +1 -0
- package/dist/stylings/index.cjs +1 -0
- package/dist/stylings/index.d.ts +49 -0
- package/dist/stylings/index.js +1 -0
- package/dist/type-data-DDs-u2kq.d.ts +261 -0
- package/dist/types/index.d.ts +3589 -2090
- package/dist/urls/index.cjs +1 -0
- package/dist/urls/index.d.ts +418 -0
- package/dist/urls/index.js +1 -0
- package/package.json +147 -21
|
@@ -0,0 +1,1557 @@
|
|
|
1
|
+
import{I as If}from'./if-CvT4R7Kh.js';import{I as IsNever}from'./never-BfayMBF9.js';import{I as IfExtends,E as Extends,N as Not,a as NotExtends}from'./extends-Mp81Hq9-.js';import{I as IsAny}from'./any-BmdI8UbK.js';import{P as Prettify}from'./prettify-C4xLcYOP.js';
|
|
2
|
+
/** -------------------------------------------------------
|
|
3
|
+
* * ***And.***
|
|
4
|
+
* -------------------------------------------------------
|
|
5
|
+
* Performs a **logical AND** operation between two boolean types.
|
|
6
|
+
* - Returns `true` if **both** conditions extend `true`.
|
|
7
|
+
* - Returns `false` otherwise.
|
|
8
|
+
*
|
|
9
|
+
* @template Condition1 - The first condition.
|
|
10
|
+
* @template Condition2 - The second condition.
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type Case1 = And<true, true>;
|
|
14
|
+
* // ➔ true
|
|
15
|
+
* type Case2 = And<false, true>;
|
|
16
|
+
* // ➔ false
|
|
17
|
+
* type Case3 = And<true, false>;
|
|
18
|
+
* // ➔ false
|
|
19
|
+
* type Case4 = And<false, false>;
|
|
20
|
+
* // ➔ false
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
type And<Condition1,Condition2>=IfExtends<Condition1,true,Extends<Condition2,true>>;
|
|
24
|
+
/** -------------------------------------------------------
|
|
25
|
+
* * ***AndArr.***
|
|
26
|
+
* -------------------------------------------------------
|
|
27
|
+
* Performs a **logical AND** operation across all elements in an array of boolean types.
|
|
28
|
+
* - Returns `true` if **all elements** extend `true`.
|
|
29
|
+
* - Returns `false` if **any element** is not `true`.
|
|
30
|
+
*
|
|
31
|
+
* @template Conditions - A readonly array of boolean conditions.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* type Case1 = AndArr<[true, true, true]>;
|
|
35
|
+
* // ➔ true
|
|
36
|
+
* type Case2 = AndArr<[true, true, false]>;
|
|
37
|
+
* // ➔ false
|
|
38
|
+
* type Case3 = AndArr<[false, false, false]>;
|
|
39
|
+
* // ➔ false
|
|
40
|
+
* type Case4 = AndArr<[]>;
|
|
41
|
+
* // ➔ true (vacuous truth)
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
type AndArr<Conditions extends readonly unknown[]>=Extends<Conditions[number],true>;
|
|
45
|
+
/** -------------------------------------------------------
|
|
46
|
+
* * ***IsStringLiteral.***
|
|
47
|
+
* -------------------------------------------------------
|
|
48
|
+
* Returns a boolean whether the passed argument is literal string
|
|
49
|
+
*
|
|
50
|
+
* @template T - The type value to check.
|
|
51
|
+
* @example
|
|
52
|
+
* type Case1 = IsStringLiteral<'a'>; // ➔ true
|
|
53
|
+
* type Case2 = IsStringLiteral<1>; // ➔ false
|
|
54
|
+
* type Case3 = IsStringLiteral<string>; // ➔ false
|
|
55
|
+
*/
|
|
56
|
+
type IsStringLiteral<T>=If<Extends<T,string>,Extends<string,T>extends true?false:true,false>;
|
|
57
|
+
/** -------------------------------------------------------
|
|
58
|
+
* * ***IfNot.***
|
|
59
|
+
* -------------------------------------------------------
|
|
60
|
+
*
|
|
61
|
+
* Conditional: returns the second argument if the first argument is `false`, otherwise returns the third argument.
|
|
62
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
63
|
+
*
|
|
64
|
+
* @template Condition - The boolean condition to check.
|
|
65
|
+
* @template IfTrue - The branch type if condition is `false`. (default: `true`)
|
|
66
|
+
* @template IfFalse - The branch type if condition is `true`. (default: `false`)
|
|
67
|
+
*
|
|
68
|
+
* ### Examples
|
|
69
|
+
* ```ts
|
|
70
|
+
* type A = IfNot<false, "valid">;
|
|
71
|
+
* // ➔ "valid"
|
|
72
|
+
* type B = IfNot<false, "valid", "invalid">;
|
|
73
|
+
* // ➔ "invalid"
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
type IfNot<Condition,IfTrue=true,IfFalse=false>=If<Condition,IfFalse,IfTrue>;
|
|
77
|
+
/** -------------------------------------------------------
|
|
78
|
+
* * ***WordSeparator.***
|
|
79
|
+
* -------------------------------------------------------
|
|
80
|
+
* A type-level utility that defines all valid ***word separators***.
|
|
81
|
+
* - Can be a space `" "`, a dash `"-"`, or an underscore `"_"`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* type A = WordSeparator; // ➔ " " | "-" | "_"
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
type WordSeparator=" "|"-"|"_";
|
|
89
|
+
/** --------------------------------------------------
|
|
90
|
+
* * ***Whitespace.***
|
|
91
|
+
* --------------------------------------------------
|
|
92
|
+
* Represents common whitespace characters.
|
|
93
|
+
*
|
|
94
|
+
* ✅ Used as the default trimming characters in string utility types.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* type W = Whitespace;
|
|
98
|
+
* // ➔ " " | "\t" | "\r" | "\n"
|
|
99
|
+
*/
|
|
100
|
+
type Whitespace=" "|"\t"|"\r"|"\n";
|
|
101
|
+
/** **Helper Internal.** */
|
|
102
|
+
type SafeKeyTrimming<T>=Exclude<T,symbol>;
|
|
103
|
+
/** --------------------------------------------------
|
|
104
|
+
* * ***TrimLeft.***
|
|
105
|
+
* --------------------------------------------------
|
|
106
|
+
* Recursively trims specified characters (default: **{@link Whitespace}**)
|
|
107
|
+
* from the **start (left)** of a string.
|
|
108
|
+
*
|
|
109
|
+
* @template Text - The string to trim.
|
|
110
|
+
* @template Chars - The characters to remove (default: `Whitespace`).
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* type T1 = TrimLeft<"\n hello", " " | "\n">;
|
|
114
|
+
* // ➔ "hello"
|
|
115
|
+
*
|
|
116
|
+
* type T2 = TrimLeft<" world">;
|
|
117
|
+
* // ➔ "world"
|
|
118
|
+
*
|
|
119
|
+
* type T3 = TrimLeft<" world ">;
|
|
120
|
+
* // ➔ "world "
|
|
121
|
+
*/
|
|
122
|
+
type TrimLeft<Text extends PropertyKey,Chars extends PropertyKey=Whitespace>=Text extends`${SafeKeyTrimming<Chars>}${infer Rest}`?TrimLeft<Rest,Chars>:Text;
|
|
123
|
+
/** --------------------------------------------------
|
|
124
|
+
* * ***TrimRight.***
|
|
125
|
+
* --------------------------------------------------
|
|
126
|
+
* Recursively trims specified characters (default: **{@link Whitespace}**)
|
|
127
|
+
* from the **end (right)** of a string.
|
|
128
|
+
*
|
|
129
|
+
* @template Text - The string to trim.
|
|
130
|
+
* @template Chars - The characters to remove (default: `Whitespace`).
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* type T1 = TrimRight<"hello \t", " " | "\t">;
|
|
134
|
+
* // ➔ "hello"
|
|
135
|
+
*
|
|
136
|
+
* type T2 = TrimRight<"world ">;
|
|
137
|
+
* // ➔ "world"
|
|
138
|
+
*
|
|
139
|
+
* type T2 = TrimRight<" world ">;
|
|
140
|
+
* // ➔ " world"
|
|
141
|
+
*/
|
|
142
|
+
type TrimRight<Text extends PropertyKey,Chars extends PropertyKey=Whitespace>=Text extends`${infer Rest}${SafeKeyTrimming<Chars>}`?TrimRight<Rest,Chars>:Text;
|
|
143
|
+
/** --------------------------------------------------
|
|
144
|
+
* * ***Trim.***
|
|
145
|
+
* --------------------------------------------------
|
|
146
|
+
* Trims specified characters (default: **{@link Whitespace}**)
|
|
147
|
+
* from **both the start and end** of a string.
|
|
148
|
+
*
|
|
149
|
+
* @template Text - The string to trim.
|
|
150
|
+
* @template Chars - The characters to remove (default: `Whitespace`).
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* type T1 = Trim<" hello ", " ">;
|
|
154
|
+
* // ➔ "hello"
|
|
155
|
+
*
|
|
156
|
+
* type T2 = Trim<"\n world \t">;
|
|
157
|
+
* // ➔ "world"
|
|
158
|
+
*/
|
|
159
|
+
type Trim<Text extends PropertyKey,Chars extends PropertyKey=Whitespace>=TrimRight<TrimLeft<Text,Chars>,Chars>;
|
|
160
|
+
/** -------------------------------------------------------
|
|
161
|
+
* * ***TrimsLower***
|
|
162
|
+
* -------------------------------------------------------
|
|
163
|
+
* Trims leading & trailing whitespace from a string and
|
|
164
|
+
* converts it to **lowercase**.
|
|
165
|
+
*
|
|
166
|
+
* Utilizes **{@link Trim}** to remove whitespace and **{@link Lowercase}** to convert the string to lowercase.
|
|
167
|
+
*
|
|
168
|
+
* @template S - The input string to transform.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* type T1 = TrimsLower<" HeLLo \n">;
|
|
173
|
+
* // ➔ "hello"
|
|
174
|
+
*
|
|
175
|
+
* type T2 = TrimsLower<" WoRLD ">;
|
|
176
|
+
* // ➔ "world"
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
type TrimsLower<S extends string>=Lowercase<Trim<S>>;
|
|
180
|
+
/** -------------------------------------------------------
|
|
181
|
+
* * ***TrimsUpper***
|
|
182
|
+
* -------------------------------------------------------
|
|
183
|
+
* Trims leading & trailing whitespace from a string and
|
|
184
|
+
* converts it to **uppercase**.
|
|
185
|
+
*
|
|
186
|
+
* Utilizes **{@link Trim}** to remove whitespace and **{@link Uppercase}** to convert the string to uppercase.
|
|
187
|
+
*
|
|
188
|
+
* @template S - The input string to transform.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* type T1 = TrimsUpper<" HeLLo \n">;
|
|
193
|
+
* // ➔ "HELLO"
|
|
194
|
+
*
|
|
195
|
+
* type T2 = TrimsUpper<" WoRLD ">;
|
|
196
|
+
* // ➔ "WORLD"
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
type TrimsUpper<S extends string>=Uppercase<Trim<S>>;
|
|
200
|
+
/** -------------------------------------------------------
|
|
201
|
+
* * ***AnyString.***
|
|
202
|
+
* -------------------------------------------------------
|
|
203
|
+
*
|
|
204
|
+
* A utility type that represents **any string value** while
|
|
205
|
+
* preventing unwanted widening of string literals to `string`.
|
|
206
|
+
*
|
|
207
|
+
* 🔹 This is achieved by intersecting `string` with `{}`,
|
|
208
|
+
* ensuring that the type remains assignable to `string`
|
|
209
|
+
* but is treated as a unique type in generic constraints.
|
|
210
|
+
*
|
|
211
|
+
* 📌 Useful in scenarios where:
|
|
212
|
+
* - You want to accept **any string**, but still preserve
|
|
213
|
+
* literal types in inference.
|
|
214
|
+
* - You need stricter typing than just `string`.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* declare function acceptsAnyString<T extends AnyString>(value: T): T;
|
|
219
|
+
*
|
|
220
|
+
* // Preserves literal
|
|
221
|
+
* const a = acceptsAnyString("hello");
|
|
222
|
+
* // ➔ "hello"
|
|
223
|
+
*
|
|
224
|
+
* // Also allows generic string
|
|
225
|
+
* const b = acceptsAnyString(String("world"));
|
|
226
|
+
* // ➔ string
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
type AnyString={}& string;
|
|
230
|
+
/** -------------------------------------------------------
|
|
231
|
+
* * ***EmptyString.***
|
|
232
|
+
* -------------------------------------------------------
|
|
233
|
+
* Returns the type `T` only if it is the empty string `""`.
|
|
234
|
+
* Optionally trims whitespace before checking.
|
|
235
|
+
*
|
|
236
|
+
* Behavior:
|
|
237
|
+
* - If `WithTrim` is `true` (default), trims `T` before checking.
|
|
238
|
+
* - If `T` is the general `string` type, returns `never`.
|
|
239
|
+
* - If `T` is empty (after optional trimming), returns `T` or `Trim<T>`.
|
|
240
|
+
*
|
|
241
|
+
* @template T - The string type to check.
|
|
242
|
+
* @template WithTrim - Whether to trim whitespace before checking (default `true`).
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* // Basic empty string
|
|
247
|
+
* type Case1 = EmptyString<"">;
|
|
248
|
+
* // ➔ ""
|
|
249
|
+
*
|
|
250
|
+
* // Non-empty string
|
|
251
|
+
* type Case2 = EmptyString<"abc">;
|
|
252
|
+
* // ➔ never
|
|
253
|
+
*
|
|
254
|
+
* // General string type
|
|
255
|
+
* type Case3 = EmptyString<string>;
|
|
256
|
+
* // ➔ never
|
|
257
|
+
*
|
|
258
|
+
* // With leading/trailing whitespace
|
|
259
|
+
* type Case4 = EmptyString<" ", true>;
|
|
260
|
+
* // ➔ "" (trimmed)
|
|
261
|
+
* type Case5 = EmptyString<" ", false>;
|
|
262
|
+
* // ➔ never (not trimmed)
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
type EmptyString<T extends string,WithTrim extends boolean=true>=""extends(WithTrim extends true?Trim<T>:T)?string extends(WithTrim extends true?Trim<T>:T)?never:WithTrim extends true?Trim<T>:T:never;
|
|
266
|
+
/** -------------------------------------------------------
|
|
267
|
+
* * ***NonEmptyString.***
|
|
268
|
+
* -------------------------------------------------------
|
|
269
|
+
* Returns the type `T` only if it is a non-empty string.
|
|
270
|
+
* Optionally trims whitespace before checking.
|
|
271
|
+
*
|
|
272
|
+
* Behavior:
|
|
273
|
+
* - If `WithTrim` is `true` (default), trims `T` before checking.
|
|
274
|
+
* - If `T` is the general `string` type, returns `string`.
|
|
275
|
+
* - If `T` is empty (after optional trimming), returns `never`.
|
|
276
|
+
*
|
|
277
|
+
* @template T - The string type to check.
|
|
278
|
+
* @template WithTrim - Whether to trim whitespace before checking (default `true`).
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* // Non-empty string
|
|
283
|
+
* type Case1 = NonEmptyString<"abc">; // ➔ "abc"
|
|
284
|
+
*
|
|
285
|
+
* // Empty string
|
|
286
|
+
* type Case2 = NonEmptyString<"">; // ➔ never
|
|
287
|
+
*
|
|
288
|
+
* // General string type
|
|
289
|
+
* type Case3 = NonEmptyString<string>; // ➔ string
|
|
290
|
+
*
|
|
291
|
+
* // String with whitespace
|
|
292
|
+
* type Case4 = NonEmptyString<" ", true>; // ➔ never (trimmed to empty)
|
|
293
|
+
* type Case5 = NonEmptyString<" ", false>; // ➔ " " (not trimmed)
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
type NonEmptyString<T extends string,WithTrim extends boolean=true>=string extends T?string:If<IsNever<EmptyString<T,WithTrim>>,WithTrim extends true?Trim<T>:T,never>;
|
|
297
|
+
/** -------------------------------------------------------
|
|
298
|
+
* * ***IsEmptyString.***
|
|
299
|
+
* -------------------------------------------------------
|
|
300
|
+
* Returns `true` if `T` is exactly the empty string `""`.
|
|
301
|
+
* Optionally trims whitespace before checking.
|
|
302
|
+
*
|
|
303
|
+
* Behavior:
|
|
304
|
+
* - If `WithTrim` is `true` (default), trims `T` before checking.
|
|
305
|
+
* - If `T` is empty after optional trimming, returns `true`.
|
|
306
|
+
* - Otherwise, returns `false`.
|
|
307
|
+
*
|
|
308
|
+
* @template T - The string type to check.
|
|
309
|
+
* @template WithTrim - Whether to trim whitespace before checking (default `true`).
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* type Case1 = IsEmptyString<"">;
|
|
314
|
+
* // ➔ true
|
|
315
|
+
* type Case2 = IsEmptyString<"abc">;
|
|
316
|
+
* // ➔ false
|
|
317
|
+
* type Case3 = IsEmptyString<" ", true>;
|
|
318
|
+
* // ➔ true (trimmed)
|
|
319
|
+
* type Case4 = IsEmptyString<" ", false>;
|
|
320
|
+
* // ➔ false (not trimmed)
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
type IsEmptyString<T extends string,WithTrim extends boolean=true>=IfNot<IsNever<EmptyString<T,WithTrim>>>;
|
|
324
|
+
/** -------------------------------------------------------
|
|
325
|
+
* * ***IsNonEmptyString.***
|
|
326
|
+
* -------------------------------------------------------
|
|
327
|
+
* Returns `true` if `T` is a non-empty string.
|
|
328
|
+
* Optionally trims whitespace before checking.
|
|
329
|
+
*
|
|
330
|
+
* Behavior:
|
|
331
|
+
* - If `WithTrim` is `true` (default), trims `T` before checking.
|
|
332
|
+
* - Returns `true` if `T` is non-empty after optional trimming.
|
|
333
|
+
* - Returns `false` if `T` is empty (after trimming if `WithTrim=true`).
|
|
334
|
+
*
|
|
335
|
+
* @template T - The string type to check.
|
|
336
|
+
* @template WithTrim - Whether to trim whitespace before checking (default `true`).
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```ts
|
|
340
|
+
* type Case1 = IsNonEmptyString<"abc">;
|
|
341
|
+
* // ➔ true
|
|
342
|
+
* type Case2 = IsNonEmptyString<"">;
|
|
343
|
+
* // ➔ false
|
|
344
|
+
* type Case3 = IsNonEmptyString<" ", true>;
|
|
345
|
+
* // ➔ false (trimmed)
|
|
346
|
+
* type Case4 = IsNonEmptyString<" ", false>;
|
|
347
|
+
* // ➔ true (not trimmed)
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
type IsNonEmptyString<T extends string,WithTrim extends boolean=true>=IfNot<IsNever<NonEmptyString<T,WithTrim>>>;
|
|
351
|
+
/** -------------------------------------------------------
|
|
352
|
+
* * ***IfEmptyString.***
|
|
353
|
+
* -------------------------------------------------------
|
|
354
|
+
* Conditional type: returns `IfTrue` if `T` is an empty string `""`,
|
|
355
|
+
* otherwise returns `IfFalse`. Optionally trims whitespace before checking.
|
|
356
|
+
*
|
|
357
|
+
* @template T - The string type to check.
|
|
358
|
+
* @template IfTrue - Type returned if `T` is empty (default `true`).
|
|
359
|
+
* @template IfFalse - Type returned if `T` is not empty (default `false`).
|
|
360
|
+
* @template WithTrim - Whether to trim whitespace before checking (default `true`).
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* type Case1 = IfEmptyString<"">;
|
|
365
|
+
* // ➔ true
|
|
366
|
+
* type Case2 = IfEmptyString<"abc">;
|
|
367
|
+
* // ➔ false
|
|
368
|
+
* type Case3 = IfEmptyString<"", "yes", "no">;
|
|
369
|
+
* // ➔ "yes"
|
|
370
|
+
* type Case4 = IfEmptyString<"abc", "yes", "no">;
|
|
371
|
+
* // ➔ "no"
|
|
372
|
+
* type Case5 = IfEmptyString<" ", "yes", "no", true>;
|
|
373
|
+
* // ➔ "yes" (trimmed)
|
|
374
|
+
* type Case6 = IfEmptyString<" ", "yes", "no", false>;
|
|
375
|
+
* // ➔ "no" (not trimmed)
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
type IfEmptyString<T extends string,IfTrue=true,IfFalse=false,WithTrim extends boolean=true>=IfNot<IsNever<EmptyString<T,WithTrim>>,IfTrue,IfFalse>;
|
|
379
|
+
/** -------------------------------------------------------
|
|
380
|
+
* * ***IfNonEmptyString.***
|
|
381
|
+
* -------------------------------------------------------
|
|
382
|
+
* Conditional type: returns `IfTrue` if `T` is a non-empty string,
|
|
383
|
+
* otherwise returns `IfFalse`. Optionally trims whitespace before checking.
|
|
384
|
+
*
|
|
385
|
+
* @template T - The string type to check.
|
|
386
|
+
* @template IfTrue - Type returned if `T` is non-empty (default `true`).
|
|
387
|
+
* @template IfFalse - Type returned if `T` is empty (default `false`).
|
|
388
|
+
* @template WithTrim - Whether to trim whitespace before checking (default `true`).
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* type Case1 = IfNonEmptyString<"abc">;
|
|
393
|
+
* // ➔ true
|
|
394
|
+
* type Case2 = IfNonEmptyString<"">;
|
|
395
|
+
* // ➔ false
|
|
396
|
+
* type Case3 = IfNonEmptyString<"abc", "yes", "no">;
|
|
397
|
+
* // ➔ "yes"
|
|
398
|
+
* type Case4 = IfNonEmptyString<"", "yes", "no">;
|
|
399
|
+
* // ➔ "no"
|
|
400
|
+
* type Case5 = IfNonEmptyString<" ", "yes", "no", true>;
|
|
401
|
+
* // ➔ "no" (trimmed)
|
|
402
|
+
* type Case6 = IfNonEmptyString<" ", "yes", "no", false>;
|
|
403
|
+
* // ➔ "yes" (not trimmed)
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
type IfNonEmptyString<T extends string,IfTrue=true,IfFalse=false,WithTrim extends boolean=true>=IfNot<IsNever<NonEmptyString<T,WithTrim>>,IfTrue,IfFalse>;
|
|
407
|
+
/** -------------------------------------------------------
|
|
408
|
+
* * ***Or.***
|
|
409
|
+
* -------------------------------------------------------
|
|
410
|
+
*
|
|
411
|
+
* Computes the logical OR of two type-level boolean conditions.
|
|
412
|
+
*
|
|
413
|
+
* @template Condition1 - First boolean condition.
|
|
414
|
+
* @template Condition2 - Second boolean condition.
|
|
415
|
+
*
|
|
416
|
+
* @example
|
|
417
|
+
* ```ts
|
|
418
|
+
* type Case1 = Or<true, true>; // true
|
|
419
|
+
* type Case2 = Or<false, true>; // true
|
|
420
|
+
* type Case3 = Or<false, false>; // false
|
|
421
|
+
* type Case4 = Or<true, false>; // true
|
|
422
|
+
* ```
|
|
423
|
+
*
|
|
424
|
+
* @remarks
|
|
425
|
+
* - Uses {@link IfExtends} to determine if either condition is `true`.
|
|
426
|
+
* - Returns `true` if at least one of the two conditions is `true`.
|
|
427
|
+
* - Returns `false` only if both are `false`.
|
|
428
|
+
*/
|
|
429
|
+
type Or<Condition1,Condition2>=IfExtends<Condition1,true,true,IfExtends<Condition2,true>>;
|
|
430
|
+
/** -------------------------------------------------------
|
|
431
|
+
* * ***OrArr.***
|
|
432
|
+
* -------------------------------------------------------
|
|
433
|
+
*
|
|
434
|
+
* Computes the logical OR of all elements inside a tuple or array of boolean types.
|
|
435
|
+
*
|
|
436
|
+
* @template Conditions - An array of boolean type elements.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* type Case1 = OrArr<[true, true, true]>; // true
|
|
441
|
+
* type Case2 = OrArr<[true, true, false]>; // true
|
|
442
|
+
* type Case3 = OrArr<[false, false, false]>; // false
|
|
443
|
+
* type Case4 = OrArr<[]>; // false
|
|
444
|
+
* ```
|
|
445
|
+
*
|
|
446
|
+
* @remarks
|
|
447
|
+
* - Uses TypeScript's indexed access types and conditional type inference.
|
|
448
|
+
* - Returns `true` if at least one element in the array is `true`.
|
|
449
|
+
* - Returns `false` if all elements are `false` or array is empty.
|
|
450
|
+
*/
|
|
451
|
+
type OrArr<Conditions extends readonly unknown[]>=true extends Conditions[number]?true:false;
|
|
452
|
+
/** -------------------------------------------------------
|
|
453
|
+
* * ***Push.***
|
|
454
|
+
* -------------------------------------------------------
|
|
455
|
+
*
|
|
456
|
+
* Appends a type `U` to the end of a tuple or readonly array type `T`.
|
|
457
|
+
*
|
|
458
|
+
* @template T - The tuple or readonly array type to append to.
|
|
459
|
+
* @template U - The type of the element to push.
|
|
460
|
+
*
|
|
461
|
+
* @example
|
|
462
|
+
* ```ts
|
|
463
|
+
* type Case1 = Push<[1, 2, 3, 4], 5>;
|
|
464
|
+
* // ➔ [1, 2, 3, 4, 5]
|
|
465
|
+
*
|
|
466
|
+
* type Case2 = Push<["a", "b"], "c">;
|
|
467
|
+
* // ➔ ["a", "b", "c"]
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
type Push<T extends readonly unknown[],U>=[...T,U];type _Repeat<T extends string,Count extends number,Result extends string="",Iteration extends unknown[]=[]>=Iteration["length"] extends Count?Result:_Repeat<T,Count,`${T}${Result}`,Push<Iteration,unknown>>;
|
|
471
|
+
/** -------------------------------------------------------
|
|
472
|
+
* * ***Repeat.***
|
|
473
|
+
* -------------------------------------------------------
|
|
474
|
+
*
|
|
475
|
+
* Repeats a string literal type `T` a specified number of times `Count`.
|
|
476
|
+
* - Supports a range of `[0, 999]` due to TypeScript recursion limits.
|
|
477
|
+
* - If `Count > 999`, it is automatically error `Type instantiation is excessively deep and possibly infinite.ts(2589)`.
|
|
478
|
+
*
|
|
479
|
+
* @template T - The string literal to repeat.
|
|
480
|
+
* @template Count - Number of times to repeat.
|
|
481
|
+
*
|
|
482
|
+
* @example
|
|
483
|
+
* ```ts
|
|
484
|
+
* type Case0 = Repeat<'x', 0>; // ➔ ''
|
|
485
|
+
* type Case1 = Repeat<'x', 1>; // ➔ 'x'
|
|
486
|
+
* type Case2 = Repeat<'x', 5>; // ➔ 'xxxxx'
|
|
487
|
+
* type Case3 = Repeat<'ab', 3>; // ➔ 'ababab'
|
|
488
|
+
*
|
|
489
|
+
* // ❌ Invalid:
|
|
490
|
+
* type Case1000 = Repeat<'x', 1000>;
|
|
491
|
+
* // ➔ same as any (because: TypeScript recursion limits)
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
494
|
+
type Repeat<T extends string,Count extends number>=_Repeat<T,Count>;
|
|
495
|
+
/** -------------------------------------------------------
|
|
496
|
+
* * ***OddDigit.***
|
|
497
|
+
* -------------------------------------------------------
|
|
498
|
+
*
|
|
499
|
+
* A union of string literal digits considered ***odd***.
|
|
500
|
+
*
|
|
501
|
+
* - Includes: `"1" | "3" | "5" | "7" | "9"`.
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
*
|
|
505
|
+
* ```ts
|
|
506
|
+
* type A = OddDigit; // ➔ "1" | "3" | "5" | "7" | "9"
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
type OddDigit="1"|"3"|"5"|"7"|"9";
|
|
510
|
+
/** -------------------------------------------------------
|
|
511
|
+
* * ***EvenDigit.***
|
|
512
|
+
* -------------------------------------------------------
|
|
513
|
+
*
|
|
514
|
+
* A union of string literal digits considered ***even***.
|
|
515
|
+
*
|
|
516
|
+
* - Includes: `"0" | "2" | "4" | "6" | "8"`.
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
*
|
|
520
|
+
* ```ts
|
|
521
|
+
* type A = EvenDigit; // ➔ "0" | "2" | "4" | "6" | "8"
|
|
522
|
+
* ```
|
|
523
|
+
*/
|
|
524
|
+
type EvenDigit="0"|"2"|"4"|"6"|"8";
|
|
525
|
+
/** -------------------------------------------------------
|
|
526
|
+
* * ***Integer.***
|
|
527
|
+
* -------------------------------------------------------
|
|
528
|
+
*
|
|
529
|
+
* A type-level utility that validates if `T` is an ***integer***.
|
|
530
|
+
*
|
|
531
|
+
* - Returns `T` if it is an integer.
|
|
532
|
+
* - Returns `never` if `T` is a ***float*** (decimal).
|
|
533
|
+
*
|
|
534
|
+
* @template T - A number type to validate.
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
*
|
|
538
|
+
* ```ts
|
|
539
|
+
* type A = Integer<42>; // ➔ 42
|
|
540
|
+
* type B = Integer<-10>; // ➔ -10
|
|
541
|
+
* type C = Integer<3.14>; // ➔ never
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
type Integer<T extends number>=`${T}`extends`${string}.${string}`?never:T;
|
|
545
|
+
/** -------------------------------------------------------
|
|
546
|
+
* * ***Float.***
|
|
547
|
+
* -------------------------------------------------------
|
|
548
|
+
*
|
|
549
|
+
* A type-level utility that validates if `T` is a ***float***.
|
|
550
|
+
*
|
|
551
|
+
* - Returns `T` if it is a float.
|
|
552
|
+
* - Returns `never` if `T` is an ***integer***.
|
|
553
|
+
*
|
|
554
|
+
* @template T - A number type to validate.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
*
|
|
558
|
+
* ```ts
|
|
559
|
+
* type A = Float<3.14>; // ➔ 3.14
|
|
560
|
+
* type B = Float<42>; // ➔ never
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
type Float<T extends number>=If<IsNever<Integer<T>>,T,never>;
|
|
564
|
+
/** -------------------------------------------------------
|
|
565
|
+
* * ***Negative.***
|
|
566
|
+
* -------------------------------------------------------
|
|
567
|
+
*
|
|
568
|
+
* Extracts `T` if it is ***negative***, otherwise `never`.
|
|
569
|
+
*
|
|
570
|
+
* @template T - A number type to check.
|
|
571
|
+
*
|
|
572
|
+
* @example
|
|
573
|
+
*
|
|
574
|
+
* ```ts
|
|
575
|
+
* type A = Negative<-10>; // ➔ -10
|
|
576
|
+
* type B = Negative<5>; // ➔ never
|
|
577
|
+
* type C = Negative<0>; // ➔ never
|
|
578
|
+
* ```
|
|
579
|
+
*/
|
|
580
|
+
type Negative<T extends number>=`${T}`extends`-${string}`?T:never;
|
|
581
|
+
/** -------------------------------------------------------
|
|
582
|
+
* * ***Positive.***
|
|
583
|
+
* -------------------------------------------------------
|
|
584
|
+
*
|
|
585
|
+
* Extracts `T` if it is ***positive*** (or zero), otherwise `never`.
|
|
586
|
+
*
|
|
587
|
+
* @template T - A number type to check.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
*
|
|
591
|
+
* ```ts
|
|
592
|
+
* type A = Positive<10>; // ➔ 10
|
|
593
|
+
* type B = Positive<0>; // ➔ 0
|
|
594
|
+
* type C = Positive<-5>; // ➔ never
|
|
595
|
+
* ```
|
|
596
|
+
*/
|
|
597
|
+
type Positive<T extends number>=If<IsNever<Negative<T>>,T,never>;
|
|
598
|
+
/** -------------------------------------------------------
|
|
599
|
+
* * ***PositiveInteger.***
|
|
600
|
+
* -------------------------------------------------------
|
|
601
|
+
*
|
|
602
|
+
* Restricts `T` to ***positive integers*** only.
|
|
603
|
+
*
|
|
604
|
+
* @template T - A number type.
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
*
|
|
608
|
+
* ```ts
|
|
609
|
+
* type A = PositiveInteger<42>; // ➔ 42
|
|
610
|
+
* type B = PositiveInteger<0>; // ➔ 0
|
|
611
|
+
* type C = PositiveInteger<-5>; // ➔ never
|
|
612
|
+
* type D = PositiveInteger<3.14>; // ➔ never
|
|
613
|
+
* ```
|
|
614
|
+
*/
|
|
615
|
+
type PositiveInteger<T extends number>=Positive<Integer<T>>;
|
|
616
|
+
/** -------------------------------------------------------
|
|
617
|
+
* * ***NegativeInteger.***
|
|
618
|
+
* -------------------------------------------------------
|
|
619
|
+
*
|
|
620
|
+
* Restricts `T` to ***negative integers*** only.
|
|
621
|
+
*
|
|
622
|
+
* @template T - A number type.
|
|
623
|
+
*
|
|
624
|
+
* @example
|
|
625
|
+
*
|
|
626
|
+
* ```ts
|
|
627
|
+
* type A = NegativeInteger<-42>; // ➔ -42
|
|
628
|
+
* type B = NegativeInteger<5>; // ➔ never
|
|
629
|
+
* type C = NegativeInteger<-3.14>; // ➔ never
|
|
630
|
+
* ```
|
|
631
|
+
*/
|
|
632
|
+
type NegativeInteger<T extends number>=Negative<Integer<T>>;
|
|
633
|
+
/** -------------------------------------------------------
|
|
634
|
+
* * ***PositiveFloat.***
|
|
635
|
+
* -------------------------------------------------------
|
|
636
|
+
*
|
|
637
|
+
* Restricts `T` to ***positive floats*** only.
|
|
638
|
+
*
|
|
639
|
+
* @template T - A number type.
|
|
640
|
+
*
|
|
641
|
+
* @example
|
|
642
|
+
*
|
|
643
|
+
* ```ts
|
|
644
|
+
* type A = PositiveFloat<3.14>; // ➔ 3.14
|
|
645
|
+
* type B = PositiveFloat<-2.5>; // ➔ never
|
|
646
|
+
* type C = PositiveFloat<5>; // ➔ never
|
|
647
|
+
* ```
|
|
648
|
+
*/
|
|
649
|
+
type PositiveFloat<T extends number>=Positive<Float<T>>;
|
|
650
|
+
/** -------------------------------------------------------
|
|
651
|
+
* * ***NegativeFloat.***
|
|
652
|
+
* -------------------------------------------------------
|
|
653
|
+
*
|
|
654
|
+
* Restricts `T` to ***negative floats*** only.
|
|
655
|
+
*
|
|
656
|
+
* @template T - A number type.
|
|
657
|
+
*
|
|
658
|
+
* @example
|
|
659
|
+
*
|
|
660
|
+
* ```ts
|
|
661
|
+
* type A = NegativeFloat<-3.14>; // ➔ -3.14
|
|
662
|
+
* type B = NegativeFloat<2.5>; // ➔ never
|
|
663
|
+
* type C = NegativeFloat<-5>; // ➔ never
|
|
664
|
+
* ```
|
|
665
|
+
*/
|
|
666
|
+
type NegativeFloat<T extends number>=Negative<Float<T>>;
|
|
667
|
+
/** -------------------------------------------------------
|
|
668
|
+
* * ***Even.***
|
|
669
|
+
* -------------------------------------------------------
|
|
670
|
+
*
|
|
671
|
+
* A type-level utility that extracts `T` if it is an ***even integer***.
|
|
672
|
+
*
|
|
673
|
+
* @template T - A number type to check.
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
*
|
|
677
|
+
* ```ts
|
|
678
|
+
* type A = Even<0>; // ➔ 0
|
|
679
|
+
* type B = Even<4>; // ➔ 4
|
|
680
|
+
* type C = Even<5>; // ➔ never
|
|
681
|
+
* type D = Even<24>; // ➔ 24
|
|
682
|
+
* type E = Even<27>; // ➔ never
|
|
683
|
+
* type F = Even<3.14>; // ➔ never
|
|
684
|
+
* ```
|
|
685
|
+
*/
|
|
686
|
+
type Even<T extends number>=IfNot<IsNever<Integer<T>>,`${T}`extends`${string}${EvenDigit}`?T:never,never>;
|
|
687
|
+
/** -------------------------------------------------------
|
|
688
|
+
* * ***Odd.***
|
|
689
|
+
* -------------------------------------------------------
|
|
690
|
+
*
|
|
691
|
+
* A type-level utility that extracts `T` if it is an ***odd integer***.
|
|
692
|
+
*
|
|
693
|
+
* @template T - A number type to check.
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
*
|
|
697
|
+
* ```ts
|
|
698
|
+
* type A = Odd<0>; // ➔ never
|
|
699
|
+
* type B = Odd<5>; // ➔ 5
|
|
700
|
+
* type C = Odd<4>; // ➔ never
|
|
701
|
+
* type D = Odd<23>; // ➔ 23
|
|
702
|
+
* type E = Odd<26>; // ➔ never
|
|
703
|
+
* type F = Odd<4.2>; // ➔ never
|
|
704
|
+
* ```
|
|
705
|
+
*/
|
|
706
|
+
type Odd<T extends number>=IfNot<IsNever<Integer<T>>,If<IsNever<Even<T>>,T,never>,never>;
|
|
707
|
+
/** -------------------------------------------------------
|
|
708
|
+
* * ***IsInteger.***
|
|
709
|
+
* -------------------------------------------------------
|
|
710
|
+
*
|
|
711
|
+
* Whether `T` is an ***integer***.
|
|
712
|
+
*
|
|
713
|
+
* @example
|
|
714
|
+
*
|
|
715
|
+
* ```ts
|
|
716
|
+
* type A = IsInteger<-2>; // ➔ true
|
|
717
|
+
* type B = IsInteger<0>; // ➔ true
|
|
718
|
+
* type C = IsInteger<42>; // ➔ true
|
|
719
|
+
* type D = IsInteger<3.14>; // ➔ false
|
|
720
|
+
* ```
|
|
721
|
+
*/
|
|
722
|
+
type IsInteger<T extends number>=Not<IsNever<Integer<T>>>;
|
|
723
|
+
/** -------------------------------------------------------
|
|
724
|
+
* * ***IsFloat.***
|
|
725
|
+
* -------------------------------------------------------
|
|
726
|
+
*
|
|
727
|
+
* Whether `T` is a ***float***.
|
|
728
|
+
*
|
|
729
|
+
* @example
|
|
730
|
+
*
|
|
731
|
+
* ```ts
|
|
732
|
+
* type A = IsFloat<3.14>; // ➔ true
|
|
733
|
+
* type B = IsFloat<-3.14>; // ➔ true
|
|
734
|
+
* type C = IsFloat<0>; // ➔ false
|
|
735
|
+
* type D = IsFloat<42>; // ➔ false
|
|
736
|
+
* type E = IsFloat<-42>; // ➔ false
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
type IsFloat<T extends number>=Not<IsNever<Float<T>>>;
|
|
740
|
+
/** -------------------------------------------------------
|
|
741
|
+
* * ***IsEven.***
|
|
742
|
+
* -------------------------------------------------------
|
|
743
|
+
*
|
|
744
|
+
* Whether `T` is ***even***.
|
|
745
|
+
*
|
|
746
|
+
* @example
|
|
747
|
+
*
|
|
748
|
+
* ```ts
|
|
749
|
+
* type A = IsEven<0>; // ➔ true
|
|
750
|
+
* type B = IsEven<4>; // ➔ true
|
|
751
|
+
* type C = IsEven<5>; // ➔ false
|
|
752
|
+
* type D = IsEven<24>; // ➔ true
|
|
753
|
+
* type E = IsEven<27>; // ➔ false
|
|
754
|
+
* type F = IsEven<3.14>; // ➔ false
|
|
755
|
+
* ```
|
|
756
|
+
*/
|
|
757
|
+
type IsEven<T extends number>=If<IsInteger<T>,`${T}`extends`${string}${EvenDigit}`?true:false>;
|
|
758
|
+
/** -------------------------------------------------------
|
|
759
|
+
* * ***IsOdd.***
|
|
760
|
+
* -------------------------------------------------------
|
|
761
|
+
*
|
|
762
|
+
* Whether `T` is ***odd***.
|
|
763
|
+
*
|
|
764
|
+
* @example
|
|
765
|
+
*
|
|
766
|
+
* ```ts
|
|
767
|
+
* type A = IsEven<0>; // ➔ false
|
|
768
|
+
* type B = IsEven<4>; // ➔ false
|
|
769
|
+
* type C = IsEven<5>; // ➔ true
|
|
770
|
+
* type D = IsEven<24>; // ➔ false
|
|
771
|
+
* type E = IsEven<27>; // ➔ true
|
|
772
|
+
* type F = IsEven<3.14>; // ➔ true
|
|
773
|
+
* ```
|
|
774
|
+
*/
|
|
775
|
+
type IsOdd<T extends number>=If<IsInteger<T>,Not<IsEven<T>>>;
|
|
776
|
+
/** -------------------------------------------------------
|
|
777
|
+
* * ***IsPositive.***
|
|
778
|
+
* -------------------------------------------------------
|
|
779
|
+
*
|
|
780
|
+
* Whether `T` is ***positive***.
|
|
781
|
+
*
|
|
782
|
+
* @example
|
|
783
|
+
*
|
|
784
|
+
* ```ts
|
|
785
|
+
* type A = IsPositive<10>; // ➔ true
|
|
786
|
+
* type B = IsPositive<0>; // ➔ true
|
|
787
|
+
* type C = IsPositive<-5>; // ➔ false
|
|
788
|
+
* type D = IsPositive<3.5>; // ➔ true
|
|
789
|
+
* type E = IsPositive<-3.5>; // ➔ false
|
|
790
|
+
* ```
|
|
791
|
+
*/
|
|
792
|
+
type IsPositive<T extends number>=Not<IsNever<Positive<T>>>;
|
|
793
|
+
/** -------------------------------------------------------
|
|
794
|
+
* * ***IsNegative.***
|
|
795
|
+
* -------------------------------------------------------
|
|
796
|
+
*
|
|
797
|
+
* Whether `T` is ***negative***.
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
*
|
|
801
|
+
* ```ts
|
|
802
|
+
* type A = IsNegative<-10>; // ➔ true
|
|
803
|
+
* type B = IsNegative<5>; // ➔ false
|
|
804
|
+
* type C = IsNegative<0>; // ➔ false
|
|
805
|
+
* type D = IsPositive<3.5>; // ➔ false
|
|
806
|
+
* type E = IsPositive<-3.5>; // ➔ true
|
|
807
|
+
* ```
|
|
808
|
+
*/
|
|
809
|
+
type IsNegative<T extends number>=Not<IsNever<Negative<T>>>;
|
|
810
|
+
/** -------------------------------------------------------
|
|
811
|
+
* * ***IsPositiveInteger.***
|
|
812
|
+
* -------------------------------------------------------
|
|
813
|
+
*
|
|
814
|
+
* Whether `T` is a ***positive integer***.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
*
|
|
818
|
+
* ```ts
|
|
819
|
+
* type A = IsPositiveInteger<42>; // ➔ true
|
|
820
|
+
* type B = IsPositiveInteger<0>; // ➔ true
|
|
821
|
+
* type C = IsPositiveInteger<-5>; // ➔ false
|
|
822
|
+
* type D = IsPositiveInteger<3.14>; // ➔ false
|
|
823
|
+
* ```
|
|
824
|
+
*/
|
|
825
|
+
type IsPositiveInteger<T extends number>=Not<IsNever<PositiveInteger<T>>>;
|
|
826
|
+
/** -------------------------------------------------------
|
|
827
|
+
* * ***IsNegativeInteger.***
|
|
828
|
+
* -------------------------------------------------------
|
|
829
|
+
*
|
|
830
|
+
* Whether `T` is a ***negative integer***.
|
|
831
|
+
*
|
|
832
|
+
* @example
|
|
833
|
+
*
|
|
834
|
+
* ```ts
|
|
835
|
+
* type A = IsNegativeInteger<-42>; // ➔ true
|
|
836
|
+
* type B = IsNegativeInteger<5>; // ➔ false
|
|
837
|
+
* type C = IsNegativeInteger<-3.14>; // ➔ false
|
|
838
|
+
* ```
|
|
839
|
+
*/
|
|
840
|
+
type IsNegativeInteger<T extends number>=Not<IsNever<NegativeInteger<T>>>;
|
|
841
|
+
/** -------------------------------------------------------
|
|
842
|
+
* * ***IsPositiveFloat.***
|
|
843
|
+
* -------------------------------------------------------
|
|
844
|
+
*
|
|
845
|
+
* Whether `T` is a ***positive float***.
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
*
|
|
849
|
+
* ```ts
|
|
850
|
+
* type A = IsPositiveFloat<3.14>; // ➔ true
|
|
851
|
+
* type B = IsPositiveFloat<-2.5>; // ➔ false
|
|
852
|
+
* type C = IsPositiveFloat<5>; // ➔ false
|
|
853
|
+
* ```
|
|
854
|
+
*/
|
|
855
|
+
type IsPositiveFloat<T extends number>=Not<IsNever<PositiveFloat<T>>>;
|
|
856
|
+
/** -------------------------------------------------------
|
|
857
|
+
* * ***IsNegativeFloat.***
|
|
858
|
+
* -------------------------------------------------------
|
|
859
|
+
*
|
|
860
|
+
* Whether `T` is a ***negative float***.
|
|
861
|
+
*
|
|
862
|
+
* @example
|
|
863
|
+
*
|
|
864
|
+
* ```ts
|
|
865
|
+
* type A = IsNegativeFloat<-3.14>; // ➔ true
|
|
866
|
+
* type B = IsNegativeFloat<2.5>; // ➔ false
|
|
867
|
+
* type C = IsNegativeFloat<-5>; // ➔ false
|
|
868
|
+
* ```
|
|
869
|
+
*/
|
|
870
|
+
type IsNegativeFloat<T extends number>=Not<IsNever<NegativeFloat<T>>>;
|
|
871
|
+
/** -------------------------------------------------------
|
|
872
|
+
* * ***IfInteger.***
|
|
873
|
+
* -------------------------------------------------------
|
|
874
|
+
*
|
|
875
|
+
* Conditional: `If` branch if `T` is an ***integer***.
|
|
876
|
+
*
|
|
877
|
+
* @example
|
|
878
|
+
*
|
|
879
|
+
* ```ts
|
|
880
|
+
* type A = IfInteger<42>; // ➔ true
|
|
881
|
+
* type B = IfInteger<3.14>; // ➔ false
|
|
882
|
+
* type C = IfInteger<42, "yes", "no">; // ➔ "yes"
|
|
883
|
+
* type D = IfInteger<3.14, "yes", "no">; // ➔ "no"
|
|
884
|
+
* ```
|
|
885
|
+
*/
|
|
886
|
+
type IfInteger<T extends number,IfTrue=true,IfFalse=false>=If<IsInteger<T>,IfTrue,IfFalse>;
|
|
887
|
+
/** -------------------------------------------------------
|
|
888
|
+
* * ***IfFloat.***
|
|
889
|
+
* -------------------------------------------------------
|
|
890
|
+
*
|
|
891
|
+
* Conditional: selects one of two branches depending on whether `T` is a ***float***.
|
|
892
|
+
*
|
|
893
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
894
|
+
*
|
|
895
|
+
* @template T - A number type.
|
|
896
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
897
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
898
|
+
*
|
|
899
|
+
* @example
|
|
900
|
+
*
|
|
901
|
+
* ```ts
|
|
902
|
+
* type A = IfFloat<3.14>; // ➔ true
|
|
903
|
+
* type B = IfFloat<42>; // ➔ false
|
|
904
|
+
* type C = IfFloat<3.14, "yes", "no">; // ➔ "yes"
|
|
905
|
+
* type D = IfFloat<42, "yes", "no">; // ➔ "no"
|
|
906
|
+
* ```
|
|
907
|
+
*/
|
|
908
|
+
type IfFloat<T extends number,IfTrue=true,IfFalse=false>=If<IsFloat<T>,IfTrue,IfFalse>;
|
|
909
|
+
/** -------------------------------------------------------
|
|
910
|
+
* * ***IfEven.***
|
|
911
|
+
* -------------------------------------------------------
|
|
912
|
+
*
|
|
913
|
+
* Conditional: selects one of two branches depending on whether `T` is ***even***.
|
|
914
|
+
*
|
|
915
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
916
|
+
*
|
|
917
|
+
* @template T - A number type.
|
|
918
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
919
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
920
|
+
*
|
|
921
|
+
* @example
|
|
922
|
+
*
|
|
923
|
+
* ```ts
|
|
924
|
+
* type A = IfEven<4>; // ➔ true
|
|
925
|
+
* type B = IfEven<5>; // ➔ false
|
|
926
|
+
* type C = IfEven<4, "even", "odd">; // ➔ "even"
|
|
927
|
+
* type D = IfEven<5, "even", "odd">; // ➔ "odd"
|
|
928
|
+
* ```
|
|
929
|
+
*/
|
|
930
|
+
type IfEven<T extends number,IfTrue=true,IfFalse=false>=If<IsEven<T>,IfTrue,IfFalse>;
|
|
931
|
+
/** -------------------------------------------------------
|
|
932
|
+
* * ***IfOdd.***
|
|
933
|
+
* -------------------------------------------------------
|
|
934
|
+
*
|
|
935
|
+
* Conditional: selects one of two branches depending on whether `T` is ***odd***.
|
|
936
|
+
*
|
|
937
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
938
|
+
*
|
|
939
|
+
* @template T - A number type.
|
|
940
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
941
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
942
|
+
*
|
|
943
|
+
* @example
|
|
944
|
+
*
|
|
945
|
+
* ```ts
|
|
946
|
+
* type A = IfOdd<5>; // ➔ true
|
|
947
|
+
* type B = IfOdd<4>; // ➔ false
|
|
948
|
+
* type C = IfOdd<5, "odd", "even">; // ➔ "odd"
|
|
949
|
+
* type D = IfOdd<4, "odd", "even">; // ➔ "even"
|
|
950
|
+
* ```
|
|
951
|
+
*/
|
|
952
|
+
type IfOdd<T extends number,IfTrue=true,IfFalse=false>=If<IsOdd<T>,IfTrue,IfFalse>;
|
|
953
|
+
/** -------------------------------------------------------
|
|
954
|
+
* * ***IfPositive.***
|
|
955
|
+
* -------------------------------------------------------
|
|
956
|
+
*
|
|
957
|
+
* Conditional: selects one of two branches depending on whether `T` is ***positive***.
|
|
958
|
+
*
|
|
959
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
960
|
+
*
|
|
961
|
+
* @template T - A number type.
|
|
962
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
963
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
964
|
+
*
|
|
965
|
+
* @example
|
|
966
|
+
*
|
|
967
|
+
* ```ts
|
|
968
|
+
* type A = IfPositive<10>; // ➔ true
|
|
969
|
+
* type B = IfPositive<-5>; // ➔ false
|
|
970
|
+
* type C = IfPositive<10, "yes", "no">; // ➔ "yes"
|
|
971
|
+
* type D = IfPositive<-5, "yes", "no">; // ➔ "no"
|
|
972
|
+
* ```
|
|
973
|
+
*/
|
|
974
|
+
type IfPositive<T extends number,IfTrue=true,IfFalse=false>=If<IsPositive<T>,IfTrue,IfFalse>;
|
|
975
|
+
/** -------------------------------------------------------
|
|
976
|
+
* * ***IfNegative.***
|
|
977
|
+
* -------------------------------------------------------
|
|
978
|
+
*
|
|
979
|
+
* Conditional: selects one of two branches depending on whether `T` is ***negative***.
|
|
980
|
+
*
|
|
981
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
982
|
+
*
|
|
983
|
+
* @template T - A number type.
|
|
984
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
985
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
986
|
+
*
|
|
987
|
+
* @example
|
|
988
|
+
*
|
|
989
|
+
* ```ts
|
|
990
|
+
* type A = IfNegative<-10>; // ➔ true
|
|
991
|
+
* type B = IfNegative<5>; // ➔ false
|
|
992
|
+
* type C = IfNegative<-10, "yes", "no">; // ➔ "yes"
|
|
993
|
+
* type D = IfNegative<5, "yes", "no">; // ➔ "no"
|
|
994
|
+
* ```
|
|
995
|
+
*/
|
|
996
|
+
type IfNegative<T extends number,IfTrue=true,IfFalse=false>=If<IsNegative<T>,IfTrue,IfFalse>;
|
|
997
|
+
/** -------------------------------------------------------
|
|
998
|
+
* * ***IfPositiveInteger.***
|
|
999
|
+
* -------------------------------------------------------
|
|
1000
|
+
*
|
|
1001
|
+
* Conditional: selects one of two branches depending on whether `T` is a ***positive integer***.
|
|
1002
|
+
*
|
|
1003
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
1004
|
+
*
|
|
1005
|
+
* @template T - A number type.
|
|
1006
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
1007
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
1008
|
+
*
|
|
1009
|
+
* @example
|
|
1010
|
+
*
|
|
1011
|
+
* ```ts
|
|
1012
|
+
* type A = IfPositiveInteger<42>; // ➔ true
|
|
1013
|
+
* type B = IfPositiveInteger<-5>; // ➔ false
|
|
1014
|
+
* type C = IfPositiveInteger<42, "yes", "no">; // ➔ "yes"
|
|
1015
|
+
* type D = IfPositiveInteger<-5, "yes", "no">; // ➔ "no"
|
|
1016
|
+
* ```
|
|
1017
|
+
*/
|
|
1018
|
+
type IfPositiveInteger<T extends number,IfTrue=true,IfFalse=false>=If<IsPositiveInteger<T>,IfTrue,IfFalse>;
|
|
1019
|
+
/** -------------------------------------------------------
|
|
1020
|
+
* * ***IfNegativeInteger.***
|
|
1021
|
+
* -------------------------------------------------------
|
|
1022
|
+
*
|
|
1023
|
+
* Conditional: selects one of two branches depending on whether `T` is a ***negative integer***.
|
|
1024
|
+
*
|
|
1025
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
1026
|
+
*
|
|
1027
|
+
* @template T - A number type.
|
|
1028
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
1029
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
1030
|
+
*
|
|
1031
|
+
* @example
|
|
1032
|
+
*
|
|
1033
|
+
* ```ts
|
|
1034
|
+
* type A = IfNegativeInteger<-42>; // ➔ true
|
|
1035
|
+
* type B = IfNegativeInteger<5>; // ➔ false
|
|
1036
|
+
* type C = IfNegativeInteger<-42, "yes", "no">; // ➔ "yes"
|
|
1037
|
+
* type D = IfNegativeInteger<5, "yes", "no">; // ➔ "no"
|
|
1038
|
+
* ```
|
|
1039
|
+
*/
|
|
1040
|
+
type IfNegativeInteger<T extends number,IfTrue=true,IfFalse=false>=If<IsNegativeInteger<T>,IfTrue,IfFalse>;
|
|
1041
|
+
/** -------------------------------------------------------
|
|
1042
|
+
* * ***IfPositiveFloat.***
|
|
1043
|
+
* -------------------------------------------------------
|
|
1044
|
+
*
|
|
1045
|
+
* Conditional: selects one of two branches depending on whether `T` is a ***positive float***.
|
|
1046
|
+
*
|
|
1047
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
1048
|
+
*
|
|
1049
|
+
* @template T - A number type.
|
|
1050
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
1051
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
1052
|
+
*
|
|
1053
|
+
* @example
|
|
1054
|
+
*
|
|
1055
|
+
* ```ts
|
|
1056
|
+
* type A = IfPositiveFloat<3.14>; // ➔ true
|
|
1057
|
+
* type B = IfPositiveFloat<-2.5>; // ➔ false
|
|
1058
|
+
* type C = IfPositiveFloat<3.14, "yes", "no">; // ➔ "yes"
|
|
1059
|
+
* type D = IfPositiveFloat<-2.5, "yes", "no">; // ➔ "no"
|
|
1060
|
+
* ```
|
|
1061
|
+
*/
|
|
1062
|
+
type IfPositiveFloat<T extends number,IfTrue=true,IfFalse=false>=If<IsPositiveFloat<T>,IfTrue,IfFalse>;
|
|
1063
|
+
/** -------------------------------------------------------
|
|
1064
|
+
* * ***IfNegativeFloat.***
|
|
1065
|
+
* -------------------------------------------------------
|
|
1066
|
+
* Conditional: selects one of two branches depending on whether `T` is a ***negative float***.
|
|
1067
|
+
* - Defaults: `IfTrue = true`, `IfFalse = false`.
|
|
1068
|
+
*
|
|
1069
|
+
* @template T - A number type.
|
|
1070
|
+
* @template IfTrue - The branch type if condition is met. (default: `true`)
|
|
1071
|
+
* @template IfFalse - The branch type if condition is not met. (default: `false`)
|
|
1072
|
+
* @example
|
|
1073
|
+
* ```ts
|
|
1074
|
+
* type A = IfNegativeFloat<-3.14>; // ➔ true
|
|
1075
|
+
* type B = IfNegativeFloat<2.5>; // ➔ false
|
|
1076
|
+
* type C = IfNegativeFloat<-3.14, "yes", "no">; // ➔ "yes"
|
|
1077
|
+
* type D = IfNegativeFloat<2.5, "yes", "no">; // ➔ "no"
|
|
1078
|
+
* ```
|
|
1079
|
+
*/
|
|
1080
|
+
type IfNegativeFloat<T extends number,IfTrue=true,IfFalse=false>=If<IsNegativeFloat<T>,IfTrue,IfFalse>;
|
|
1081
|
+
/** -------------------------------------------------------
|
|
1082
|
+
* * ***ParseNumber.***
|
|
1083
|
+
* --------------------------------------------------------
|
|
1084
|
+
* Converts a string or property key literal into a ***number literal***.
|
|
1085
|
+
* - Supports decimal numbers only.
|
|
1086
|
+
* - Automatically trims whitespace.
|
|
1087
|
+
* - Returns the number literal if valid.
|
|
1088
|
+
* - Supports scientific notation strings (e.g., `"2e-3"`, `"-5e2"`, `"2E-3"`, `"-5E2"`).
|
|
1089
|
+
* - **Note:** TypeScript cannot represent very small (<1e-6) or very large (>1e15)
|
|
1090
|
+
* numbers as literal types. In such cases, scientific notation strings return `0`.
|
|
1091
|
+
* - Returns `0` for strings representing hexadecimal (`0x...`), octal (`0o...`), or binary (`0b...`) if they are string literals.
|
|
1092
|
+
* - Returns `never` for non-numeric strings or unsupported formats.
|
|
1093
|
+
*
|
|
1094
|
+
* @template T - A string, number, or symbol (property key).
|
|
1095
|
+
* @example
|
|
1096
|
+
* ```ts
|
|
1097
|
+
* // Number:
|
|
1098
|
+
* type A = ParseNumber<0>; // ➔ 0
|
|
1099
|
+
* type B = ParseNumber<-0>; // ➔ 0
|
|
1100
|
+
* type C = ParseNumber<-0.>; // ➔ 0
|
|
1101
|
+
* type D = ParseNumber<42>; // ➔ 42
|
|
1102
|
+
* type E = ParseNumber<0.42>; // ➔ 0.42
|
|
1103
|
+
* type F = ParseNumber<-5>; // ➔ -5
|
|
1104
|
+
* type G = ParseNumber<-2.5>; // ➔ -2.5
|
|
1105
|
+
* type H = ParseNumber<2.5e3>; // ➔ 2500
|
|
1106
|
+
* type I = ParseNumber<-2.5e3>;// ➔ -2500
|
|
1107
|
+
* type J = ParseNumber<5e3>; // ➔ 5000
|
|
1108
|
+
* type K = ParseNumber<-5e3>; // ➔ -5000
|
|
1109
|
+
* type L = ParseNumber<5e21>; // ➔ 5e+21
|
|
1110
|
+
* type M = ParseNumber<5e-3>; // ➔ 0.005
|
|
1111
|
+
* type N = ParseNumber<5e-21>; // ➔ 5e-21
|
|
1112
|
+
* type O = ParseNumber<-5e-3>; // ➔ -0.005
|
|
1113
|
+
*
|
|
1114
|
+
* // Numeric String:
|
|
1115
|
+
* type A = ParseNumber<"0">; // ➔ 0
|
|
1116
|
+
* type B = ParseNumber<"-0">; // ➔ 0
|
|
1117
|
+
* type C = ParseNumber<"42">; // ➔ 42
|
|
1118
|
+
* type D = ParseNumber<"0.42">; // ➔ 0.42
|
|
1119
|
+
* type E = ParseNumber<"-42">; // ➔ -42
|
|
1120
|
+
* type F = ParseNumber<"-0.42">; // ➔ -0.42
|
|
1121
|
+
* type G = ParseNumber<" 42 ">; // ➔ 42
|
|
1122
|
+
* type H = ParseNumber<" -42 ">; // ➔ -1
|
|
1123
|
+
*
|
|
1124
|
+
* // Scientific notation string:
|
|
1125
|
+
* type S1 = ParseNumber<"2e3">; // ➔ 2000
|
|
1126
|
+
* type S2 = ParseNumber<"-2e3">; // ➔ -2000
|
|
1127
|
+
* type S3 = ParseNumber<"2e-3">; // ➔ 0.002
|
|
1128
|
+
* type S4 = ParseNumber<"-2e-3">; // ➔ -0.002
|
|
1129
|
+
* type S5 = ParseNumber<"2.5e3">; // ➔ 0
|
|
1130
|
+
* type S6 = ParseNumber<"2.5e-3">; // ➔ 0
|
|
1131
|
+
* type S7 = ParseNumber<"2e-7">; // ➔ 0 (too small include "-2e-7" for TypeScript literal)
|
|
1132
|
+
* type S8 = ParseNumber<"5e21">; // ➔ 0 (too large include "-5e21" for TypeScript literal)
|
|
1133
|
+
*
|
|
1134
|
+
* // Number representing hexadecimal, octal or binary:
|
|
1135
|
+
* type A = ParseNumber<"011">; // ➔ 9 (same as octal but deprecated)
|
|
1136
|
+
* type B = ParseNumber<"0o11">; // ➔ 9 (octal)
|
|
1137
|
+
* type C = ParseNumber<"-0o11">; // ➔ -9 (octal)
|
|
1138
|
+
* type D = ParseNumber<"0x12">; // ➔ 18 (hexadecimal)
|
|
1139
|
+
* type E = ParseNumber<"-0x12">; // ➔ -18 (hexadecimal)
|
|
1140
|
+
* type F = ParseNumber<"0b111">; // ➔ 7 (binary)
|
|
1141
|
+
* type G = ParseNumber<"-0b111">; // ➔ -7 (binary)
|
|
1142
|
+
*
|
|
1143
|
+
* // String representing hexadecimal, octal or binary:
|
|
1144
|
+
* type A = ParseNumber<"0x2A">; // ➔ 0 (hex on string not supported)
|
|
1145
|
+
* type B = ParseNumber<"0o52">; // ➔ 0 (octal on string not supported)
|
|
1146
|
+
* type C = ParseNumber<"0b101010">; // ➔ 0 (binary on string not supported)
|
|
1147
|
+
*
|
|
1148
|
+
* // Never Result
|
|
1149
|
+
* type A = ParseNumber<string>; // ➔ never
|
|
1150
|
+
* type B = ParseNumber<number>; // ➔ never
|
|
1151
|
+
* type C = ParseNumber<"abc">; // ➔ never
|
|
1152
|
+
* type D = ParseNumber<"a1">; // ➔ never
|
|
1153
|
+
* type E = ParseNumber<"3b">; // ➔ never
|
|
1154
|
+
* ```
|
|
1155
|
+
*/
|
|
1156
|
+
type ParseNumber<T extends PropertyKey|bigint>=T extends bigint?T:If<number extends T?false:true,IfExtends<OrArr<[ Extends<`-0`,Trim<Extract<T,PropertyKey>>>,Extends<-0,T>,Extends<T,`${"-" | ""}${"0"}.`>]>,true,0,T extends`${"-" | ""}0${"x" | "b" | "o"}${number}`?0:Trim<Extract<T,PropertyKey>>extends`${infer NumT extends number | string}`?T extends`${infer N extends number}.`?N:NumT extends string?ParseScientificNumber<NumT>:NumT:Trim<Extract<T,PropertyKey>>extends number?T:never>,never>;
|
|
1157
|
+
/** -------------------------------------------------------
|
|
1158
|
+
* * ***IsScientificNumber.***
|
|
1159
|
+
* -------------------------------------------------------
|
|
1160
|
+
*
|
|
1161
|
+
* Checks if a string literal `T` represents a **scientific number**.
|
|
1162
|
+
*
|
|
1163
|
+
* A scientific number is defined as a number in the form of:
|
|
1164
|
+
* - Optional negative sign (`-`)
|
|
1165
|
+
* - Mantissa (digits, can be integer or decimal)
|
|
1166
|
+
* - Exponent indicated by `e` or `E`
|
|
1167
|
+
* - Exponent value (digits, optional negative sign)
|
|
1168
|
+
*
|
|
1169
|
+
* **Important:** TypeScript cannot detect numeric literals in scientific notation
|
|
1170
|
+
* at type-level because number literals are normalized to decimals.
|
|
1171
|
+
* Only string literals like `"2.5E3"` or `"-1e-5"` can be detected.
|
|
1172
|
+
*
|
|
1173
|
+
* @template T - A string literal to check.
|
|
1174
|
+
*
|
|
1175
|
+
* @example
|
|
1176
|
+
* ```ts
|
|
1177
|
+
* type A = IsScientificNumber<"1e5">; // true
|
|
1178
|
+
* type B = IsScientificNumber<"-1e-5">; // true
|
|
1179
|
+
* type C = IsScientificNumber<"2.5E3">; // true
|
|
1180
|
+
* type D = IsScientificNumber<"42">; // false
|
|
1181
|
+
* type E = IsScientificNumber<"-0.42">; // false
|
|
1182
|
+
* type F = IsScientificNumber<string>; // false
|
|
1183
|
+
* ```
|
|
1184
|
+
*
|
|
1185
|
+
* @remarks
|
|
1186
|
+
* - Uses template literal types and conditional type {@link Extends}.
|
|
1187
|
+
* - Returns `true` if `T` is scientific number string literal, otherwise `false`.
|
|
1188
|
+
* - Returns `boolean` if `T` is generic `string`.
|
|
1189
|
+
*/
|
|
1190
|
+
type IsScientificNumber<T extends string>=Extends<T,`${"-" | ""}${number}${"e" | "E"}${number}`>;
|
|
1191
|
+
/**
|
|
1192
|
+
* Helper for {@link ParseScientificNumber}
|
|
1193
|
+
*/
|
|
1194
|
+
type BuildTuple<L extends number,T extends unknown[]=[]>=T["length"] extends L?T:BuildTuple<L,[...T,unknown]>;
|
|
1195
|
+
/**
|
|
1196
|
+
* Helper for {@link ParseScientificNumber}
|
|
1197
|
+
*/
|
|
1198
|
+
type _DecrementParseScientific<N extends number>=BuildTuple<N>extends [ infer _,...infer Rest]?Rest["length"]:never;
|
|
1199
|
+
/** -------------------------------------------------------
|
|
1200
|
+
* * ***ParseScientificNumber***
|
|
1201
|
+
* -------------------------------------------------------
|
|
1202
|
+
*
|
|
1203
|
+
* Converts a numeric string in scientific notation (e.g., `"2e-3"`, `"-5e2"`)
|
|
1204
|
+
* into a literal number type.
|
|
1205
|
+
*
|
|
1206
|
+
* **Important:** TypeScript cannot represent very small or very large numbers
|
|
1207
|
+
* as literal types. In such cases, this utility will return `0`.
|
|
1208
|
+
*
|
|
1209
|
+
* @template T - A numeric string to parse. Can be in:
|
|
1210
|
+
* - Positive or negative scientific notation (e.g., `"1e3"`, `"-2e-2"`)
|
|
1211
|
+
* - Regular number literal (e.g., `"42"`, `"-5"`)
|
|
1212
|
+
*
|
|
1213
|
+
* @example
|
|
1214
|
+
* ```ts
|
|
1215
|
+
* type A1 = ParseScientificNumber<"2e-3">; // 0.002
|
|
1216
|
+
* type A2 = ParseScientificNumber<"-2e-3">; // -0.002
|
|
1217
|
+
* type A3 = ParseScientificNumber<"5e2">; // 500
|
|
1218
|
+
* type A4 = ParseScientificNumber<"-5e2">; // -500
|
|
1219
|
+
* type A5 = ParseScientificNumber<"2e-7">; // 0 (TypeScript cannot represent literal)
|
|
1220
|
+
* type A6 = ParseScientificNumber<"5e21">; // 0 (TypeScript cannot represent literal)
|
|
1221
|
+
* type A7 = ParseScientificNumber<"42">; // 42
|
|
1222
|
+
* type A8 = ParseScientificNumber<"-42">; // -42
|
|
1223
|
+
* ```
|
|
1224
|
+
*
|
|
1225
|
+
* @remarks
|
|
1226
|
+
* - Uses type-level string manipulation to handle scientific notation.
|
|
1227
|
+
* - Negative exponents are adjusted with {@link _DecrementParseScientific} and {@link Repeat}.
|
|
1228
|
+
* - Returns `0` if TypeScript cannot infer the exact numeric literal.
|
|
1229
|
+
*/
|
|
1230
|
+
type ParseScientificNumber<T extends string>=T extends`-${infer Mantissa}${"e" | "E"}-${infer Exp extends number}`?`-${"0."}${Repeat<"0", _DecrementParseScientific<Exp>>}${Mantissa}`extends`${infer N extends number}`?number extends N?0:N:never:T extends`${infer Mantissa}${"e" | "E"}-${infer Exp extends number}`?`0.${Repeat<"0", _DecrementParseScientific<Exp>>}${Mantissa}`extends`${infer N extends number}`?number extends N?0:N:never:T extends`-${infer Mantissa}${"e" | "E"}${infer Exp extends number}`?`-${Mantissa}${Repeat<"0", Exp>}`extends`${infer N extends number}`?number extends N?0:N:never:T extends`${infer Mantissa}${"e" | "E"}${infer Exp extends number}`?`${Mantissa}${Repeat<"0", Exp>}`extends`${infer N extends number}`?number extends N?0:N:never:T extends`${infer N extends number}`?number extends N?0:N:never;
|
|
1231
|
+
/** -------------------------------------------------------
|
|
1232
|
+
* * ***Abs.***
|
|
1233
|
+
* -------------------------------------------------------
|
|
1234
|
+
*
|
|
1235
|
+
* Computes the ***absolute value*** of `T`.
|
|
1236
|
+
*
|
|
1237
|
+
* - Accepts `number` literals or numeric `string` literals.
|
|
1238
|
+
* - Returns the ***absolute value*** as a `number`.
|
|
1239
|
+
* - If `T` is not a valid number (e.g., hex, binary, octal, or non-numeric string), the result is `never`.
|
|
1240
|
+
*
|
|
1241
|
+
* @template T - A number type or string literal representing a number.
|
|
1242
|
+
*
|
|
1243
|
+
* @example
|
|
1244
|
+
*
|
|
1245
|
+
* ```ts
|
|
1246
|
+
* type A = Abs<-42>; // ➔ 42
|
|
1247
|
+
* type B = Abs<10>; // ➔ 10
|
|
1248
|
+
* type C = Abs<"11">; // ➔ 11
|
|
1249
|
+
* type D = Abs<"-11">; // ➔ 11
|
|
1250
|
+
*
|
|
1251
|
+
* // Not a number
|
|
1252
|
+
* type Invalid1 = Abs<"1a">; // ➔ never
|
|
1253
|
+
* type Invalid2 = Abs<"a1">; // ➔ never
|
|
1254
|
+
* type Invalid3 = Abs<"a1a">; // ➔ never
|
|
1255
|
+
* type Invalid4 = Abs<"abc">; // ➔ never
|
|
1256
|
+
* type Invalid5 = Abs<string>; // ➔ never
|
|
1257
|
+
* type Invalid6 = Abs<number>; // ➔ never
|
|
1258
|
+
* ```
|
|
1259
|
+
*/
|
|
1260
|
+
type Abs<T extends PropertyKey|bigint>=`${Exclude<T, symbol>}`extends`-${infer PositiveT extends number}`?ParseNumber<PositiveT>:ParseNumber<T>;
|
|
1261
|
+
/** -------------------------------------------------------
|
|
1262
|
+
* * ***Negate.***
|
|
1263
|
+
* -------------------------------------------------------
|
|
1264
|
+
*
|
|
1265
|
+
* Produces the ***negated value*** of `T` (multiplies by `-1`).
|
|
1266
|
+
*
|
|
1267
|
+
* - Only supports valid **number literals** or **numeric-strings**.
|
|
1268
|
+
* - Invalid numeric-strings (e.g. `"1a"`, `"abc"`, hex, binary, octal)
|
|
1269
|
+
* or non-numeric types (e.g. `string`, `number`, `symbol`) will return `0`.
|
|
1270
|
+
*
|
|
1271
|
+
* @template T - A number type or numeric-string.
|
|
1272
|
+
*
|
|
1273
|
+
* @example
|
|
1274
|
+
*
|
|
1275
|
+
* ```ts
|
|
1276
|
+
* type A = Negate<5>; // ➔ -5
|
|
1277
|
+
* type B = Negate<-10>; // ➔ -10
|
|
1278
|
+
* type C = Negate<0>; // ➔ 0
|
|
1279
|
+
* type D = Negate<-0>; // ➔ 0
|
|
1280
|
+
* type E = Negate<"123">; // ➔ -123
|
|
1281
|
+
*
|
|
1282
|
+
* // Not a number or numeric-string:
|
|
1283
|
+
* type Invalid1 = Negate<string>; // ➔ 0
|
|
1284
|
+
* type Invalid2 = Negate<number>; // ➔ 0
|
|
1285
|
+
* type Invalid3 = Negate<"abc">; // ➔ 0
|
|
1286
|
+
* type Invalid4 = Negate<"1a">; // ➔ 0
|
|
1287
|
+
* type Invalid5 = Negate<"2b">; // ➔ 0
|
|
1288
|
+
* type Invalid6 = Negate<"0x1f">; // ➔ 0
|
|
1289
|
+
* type Invalid7 = Negate<"0b101">; // ➔ 0
|
|
1290
|
+
* type Invalid8 = Negate<"0o77">; // ➔ 0
|
|
1291
|
+
* ```
|
|
1292
|
+
*/
|
|
1293
|
+
type Negate<T extends PropertyKey>=ParseNumber<`-${Abs<ParseNumber<T>>}`>;
|
|
1294
|
+
/** -------------------------------------------------------
|
|
1295
|
+
* * ***Split***
|
|
1296
|
+
* -------------------------------------------------------
|
|
1297
|
+
*
|
|
1298
|
+
* A type-level utility that mimics `String.prototype.split()`.
|
|
1299
|
+
* Splits a string literal `Str` into a tuple of substrings,
|
|
1300
|
+
* using `Del` as the delimiter.
|
|
1301
|
+
*
|
|
1302
|
+
* - If `Del` is the empty string `""`, the result is a tuple of characters.
|
|
1303
|
+
* - If `Del` is not found in `Str`, the result is a tuple with the original string.
|
|
1304
|
+
* - Works only with string literals. If `Str` is just `string`, the result is `string[]`.
|
|
1305
|
+
*
|
|
1306
|
+
* @template Str - The input string literal to be split.
|
|
1307
|
+
* @template Del - The delimiter used to split the string.
|
|
1308
|
+
* Defaults to `""` (character-level split).
|
|
1309
|
+
*
|
|
1310
|
+
* #### Constraints
|
|
1311
|
+
* - `Str` must be a string literal to get precise results.
|
|
1312
|
+
* - `Del` can be a string or number (numbers are converted to strings).
|
|
1313
|
+
*
|
|
1314
|
+
* #### Examples
|
|
1315
|
+
* ```ts
|
|
1316
|
+
* // ✅ Split into characters
|
|
1317
|
+
* type A = Split<"abc">; // ["a", "b", "c"]
|
|
1318
|
+
*
|
|
1319
|
+
* // ✅ Split by a comma
|
|
1320
|
+
* type B = Split<"a,b,c", ",">; // ["a", "b", "c"]
|
|
1321
|
+
*
|
|
1322
|
+
* // ✅ Split by multi-char delimiter
|
|
1323
|
+
* type C = Split<"2025-08-22", "-">; // ["2025", "08", "22"]
|
|
1324
|
+
*
|
|
1325
|
+
* // ✅ Delimiter not found → returns whole string
|
|
1326
|
+
* type D = Split<"hello", "|">; // ["hello"]
|
|
1327
|
+
*
|
|
1328
|
+
* // ⚠️ Non-literal string
|
|
1329
|
+
* type E = Split<string, ",">; // string[]
|
|
1330
|
+
* ```
|
|
1331
|
+
*/
|
|
1332
|
+
type Split<Str extends string,Del extends string|number="">=string extends Str?string[]:""extends Str?[]:Str extends`${infer T}${Del}${infer U}`?[T,...Split<U,Del>]:[Str];type _CharAt<I extends string,N extends number|`${number}`,_S extends string[]=Split<I,"">>=IfExtends<And<Extends<IsPositive<ParseNumber<N>>,true>,Extends<And<Extends<N,keyof _S>,Extends<IsStringLiteral<I>,true>>,true>>,true,_S[Extract<N,keyof _S>],undefined>;
|
|
1333
|
+
/** -------------------------------------------------------
|
|
1334
|
+
* * ***CharAt.***
|
|
1335
|
+
* -------------------------------------------------------
|
|
1336
|
+
* A type-level utility that extracts the character at a given index `N`
|
|
1337
|
+
* from a string literal type `I`.
|
|
1338
|
+
* - If the index is out of range, the result is `undefined`.
|
|
1339
|
+
* - If `I` is not a literal string (just `string`), the result is `undefined`.
|
|
1340
|
+
* - Only **positive indices** are supported (`0` and above`).
|
|
1341
|
+
*
|
|
1342
|
+
* @template I - The input string literal to extract the character from.
|
|
1343
|
+
* @template N - The zero-based index of the character to retrieve.
|
|
1344
|
+
* Can be a `number` or a stringified number (e.g. `2` or "2").
|
|
1345
|
+
* @example
|
|
1346
|
+
* ```ts
|
|
1347
|
+
* // ✅ Basic usage
|
|
1348
|
+
* type A = CharAt<"hello", 0>; // ➔ "h"
|
|
1349
|
+
* type B = CharAt<"hello", 1>; // ➔ "e"
|
|
1350
|
+
* type C = CharAt<"hello", 4>; // ➔ "o"
|
|
1351
|
+
*
|
|
1352
|
+
* // ✅ Index out of range → undefined
|
|
1353
|
+
* type D = CharAt<"hello", 5>; // ➔ undefined
|
|
1354
|
+
* type E = CharAt<"abc", 99>; // ➔ undefined
|
|
1355
|
+
*
|
|
1356
|
+
* // ✅ Stringified index also works
|
|
1357
|
+
* type F = CharAt<"testing", "0">; // ➔ "t"
|
|
1358
|
+
* type G = CharAt<"testing", "2">; // ➔ "s"
|
|
1359
|
+
* type H = CharAt<"testing", "6">; // ➔ "g"
|
|
1360
|
+
* type I = CharAt<"testing", "7">; // ➔ undefined
|
|
1361
|
+
*
|
|
1362
|
+
* // ⚠️ Non-literal strings → undefined
|
|
1363
|
+
* type J = CharAt<string, 2>; // ➔ undefined
|
|
1364
|
+
*
|
|
1365
|
+
* // ⚠️ Negative indices are not supported
|
|
1366
|
+
* type K = CharAt<"abc", -1>; // ➔ undefined
|
|
1367
|
+
* ```
|
|
1368
|
+
*/
|
|
1369
|
+
type CharAt<I extends string,N extends number|`${number}`>=_CharAt<I,N>;
|
|
1370
|
+
/** -------------------------------------------------------
|
|
1371
|
+
* * ***IsUnknown.***
|
|
1372
|
+
* -------------------------------------------------------
|
|
1373
|
+
* Returns a boolean indicating whether the given type `T` is `unknown`.
|
|
1374
|
+
*
|
|
1375
|
+
* @template T - The type to check.
|
|
1376
|
+
*
|
|
1377
|
+
* @example
|
|
1378
|
+
* ```ts
|
|
1379
|
+
* type TrueResult = IsUnknown<unknown>; // ➔ true
|
|
1380
|
+
* type FalseResult1 = IsUnknown<any>; // ➔ false
|
|
1381
|
+
* type FalseResult2 = IsUnknown<string>; // ➔ false
|
|
1382
|
+
* ```
|
|
1383
|
+
*/
|
|
1384
|
+
type IsUnknown<T>=IsAny<T>extends true?false:[unknown] extends [T]?true:false;
|
|
1385
|
+
/** -------------------------------------------------------
|
|
1386
|
+
* * ***IfUnknown.***
|
|
1387
|
+
* -------------------------------------------------------
|
|
1388
|
+
* Conditional type: returns `IfTrue` if `T` is `unknown`, otherwise returns `IfFalse`.
|
|
1389
|
+
*
|
|
1390
|
+
* @template T - The type to check.
|
|
1391
|
+
* @template IfTrue - The type returned if `T` is `unknown` (default: `true`).
|
|
1392
|
+
* @template IfFalse - The type returned if `T` is not `unknown` (default: `false`).
|
|
1393
|
+
*
|
|
1394
|
+
* @example
|
|
1395
|
+
* ```ts
|
|
1396
|
+
* type Result1 = IfUnknown<unknown, "foo", "bar">; // ➔ "foo"
|
|
1397
|
+
* type Result2 = IfUnknown<string, "foo", "bar">; // ➔ "bar"
|
|
1398
|
+
* ```
|
|
1399
|
+
*/
|
|
1400
|
+
type IfUnknown<T,IfTrue=true,IfFalse=false>=If<IsUnknown<T>,IfTrue,IfFalse>;
|
|
1401
|
+
/** -------------------------------------------------------
|
|
1402
|
+
* * ***UnknownifyPropertiesOptions.***
|
|
1403
|
+
* -------------------------------------------------------
|
|
1404
|
+
* Options for `UnknownifyProperties`.
|
|
1405
|
+
*
|
|
1406
|
+
* @property makeOptional - If `true`, all properties become optional.
|
|
1407
|
+
*/
|
|
1408
|
+
type UnknownifyPropertiesOptions={
|
|
1409
|
+
/**
|
|
1410
|
+
* If `true`, all properties of the object become optional.
|
|
1411
|
+
*
|
|
1412
|
+
* DefaultValue: `false`.
|
|
1413
|
+
*
|
|
1414
|
+
* @default false
|
|
1415
|
+
* @example
|
|
1416
|
+
* ```ts
|
|
1417
|
+
* type A = { a: string; b: number };
|
|
1418
|
+
*
|
|
1419
|
+
* type B = UnknownifyProperties<A, { makeOptional: true }>;
|
|
1420
|
+
* // ➔ { a?: unknown; b?: unknown }
|
|
1421
|
+
* ```
|
|
1422
|
+
*/
|
|
1423
|
+
makeOptional:boolean;};
|
|
1424
|
+
/** -------------------------------------------------------
|
|
1425
|
+
* * ***UnknownifyProperties.***
|
|
1426
|
+
* -------------------------------------------------------
|
|
1427
|
+
* Transforms all properties of an object type `T` to `unknown`.
|
|
1428
|
+
* Optionally, makes all properties optional based on `Options`.
|
|
1429
|
+
*
|
|
1430
|
+
* @template T - The object type to transform.
|
|
1431
|
+
* @template Options - Configuration options (default: `{ makeOptional: false }`).
|
|
1432
|
+
*
|
|
1433
|
+
* @example
|
|
1434
|
+
* ```ts
|
|
1435
|
+
* type A = { a: string; b: number };
|
|
1436
|
+
*
|
|
1437
|
+
* type Result1 = UnknownifyProperties<A>;
|
|
1438
|
+
* // ➔ { a: unknown; b: unknown }
|
|
1439
|
+
*
|
|
1440
|
+
* type Result2 = UnknownifyProperties<A, { makeOptional: true }>;
|
|
1441
|
+
* // ➔ { a?: unknown; b?: unknown }
|
|
1442
|
+
* ```
|
|
1443
|
+
*/
|
|
1444
|
+
type UnknownifyProperties<T extends object,Options extends UnknownifyPropertiesOptions={makeOptional:false;}>={[K in keyof T]:unknown;}extends infer Result?If<Options["makeOptional"],Partial<Result>,Result>:never;
|
|
1445
|
+
/** -------------------------------------------------------
|
|
1446
|
+
* * ***MutableOptions***
|
|
1447
|
+
* -------------------------------------------------------
|
|
1448
|
+
*
|
|
1449
|
+
* Configuration options for the ***{@link Mutable}*** type utilities.
|
|
1450
|
+
*
|
|
1451
|
+
* @property recursive - Whether to make nested objects mutable recursively.
|
|
1452
|
+
* - If `true`, all nested objects will also have their `readonly` removed.
|
|
1453
|
+
* - Default: `false`.
|
|
1454
|
+
*
|
|
1455
|
+
* @example
|
|
1456
|
+
* ```ts
|
|
1457
|
+
* type Opt1 = MutableOptions;
|
|
1458
|
+
* // ➔ { recursive: boolean }
|
|
1459
|
+
* ```
|
|
1460
|
+
*/
|
|
1461
|
+
type MutableOptions={
|
|
1462
|
+
/** Whether to make nested objects mutable recursively.
|
|
1463
|
+
*
|
|
1464
|
+
* - If `true`, all nested objects will also have their `readonly` removed.
|
|
1465
|
+
* - Default value: `false`.
|
|
1466
|
+
*
|
|
1467
|
+
* @default false
|
|
1468
|
+
*/
|
|
1469
|
+
recursive:boolean;};
|
|
1470
|
+
/** -------------------------------------------------------
|
|
1471
|
+
* * ***Mutable.***
|
|
1472
|
+
* -------------------------------------------------------
|
|
1473
|
+
* Removes `readonly` from all properties of the passed type `T`.
|
|
1474
|
+
* - If `Options["recursive"]` is `true`, nested objects are also made mutable.
|
|
1475
|
+
*
|
|
1476
|
+
* @template T - The type to make mutable.
|
|
1477
|
+
* @template Options - Configuration options. Default: `{ recursive: false }`.
|
|
1478
|
+
* @example
|
|
1479
|
+
* ```ts
|
|
1480
|
+
* type Case1 = Mutable<{ readonly a: { readonly b: string } }>;
|
|
1481
|
+
* // ➔ { a: { b: string } } (non-recursive by default)
|
|
1482
|
+
*
|
|
1483
|
+
* type Case2 = Mutable<{ readonly a: { readonly b: string } }, { recursive: true }>;
|
|
1484
|
+
* // ➔ { a: { b: string } } (nested properties also mutable)
|
|
1485
|
+
* ```
|
|
1486
|
+
*/
|
|
1487
|
+
type Mutable<T,Options extends MutableOptions={recursive:false;}>=Prettify<{-readonly [K in keyof T]:And<Options["recursive"],Extends<T[K],object>>extends true?Mutable<T[K],Options>:T[K];}>;
|
|
1488
|
+
/** -------------------------------------------------------
|
|
1489
|
+
* * ***MutableOnly.***
|
|
1490
|
+
* -------------------------------------------------------
|
|
1491
|
+
* Removes `readonly` only from the specified keys `K` of type `T`.
|
|
1492
|
+
*
|
|
1493
|
+
* @template T - The type to modify.
|
|
1494
|
+
* @template K - Keys to make mutable.
|
|
1495
|
+
* @example
|
|
1496
|
+
* ```ts
|
|
1497
|
+
* type Case1 = MutableOnly<{ readonly a: string; readonly b: string }, "a">;
|
|
1498
|
+
* // ➔ { a: string; readonly b: string }
|
|
1499
|
+
*
|
|
1500
|
+
* type Case2 = MutableOnly<{ readonly a: string; readonly b: string }, "a" | "b">;
|
|
1501
|
+
* // ➔ { a: string; b: string }
|
|
1502
|
+
* ```
|
|
1503
|
+
*/
|
|
1504
|
+
type MutableOnly<T,K extends keyof T>=Prettify<Pick<T,Exclude<keyof T,K>>&{-readonly [P in K]:T[P];}>;
|
|
1505
|
+
/** -------------------------------------------------------
|
|
1506
|
+
* * ***MutableExcept.***
|
|
1507
|
+
* -------------------------------------------------------
|
|
1508
|
+
* Removes `readonly` from all properties of `T` **except** the specified keys `K`.
|
|
1509
|
+
*
|
|
1510
|
+
* @template T - The type to modify.
|
|
1511
|
+
* @template K - Keys to keep as readonly.
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```ts
|
|
1514
|
+
* type Case1 = MutableExcept<{ readonly a: string; readonly b: string }, "b">;
|
|
1515
|
+
* // ➔ { a: string; readonly b: string }
|
|
1516
|
+
*
|
|
1517
|
+
* type Case2 = MutableExcept<{ a: string; readonly b: string }, "a">;
|
|
1518
|
+
* // ➔ { a: string; b: string } (all except "a" made mutable)
|
|
1519
|
+
* ```
|
|
1520
|
+
*/
|
|
1521
|
+
type MutableExcept<T,K extends keyof T>=Prettify<Pick<T,K>&{-readonly [P in Exclude<keyof T,K>]:T[P];}>;
|
|
1522
|
+
/** -------------------------------------------------------
|
|
1523
|
+
* * ***IsArray.***
|
|
1524
|
+
* -------------------------------------------------------
|
|
1525
|
+
* Returns a boolean whether the passed argument is an array.
|
|
1526
|
+
*
|
|
1527
|
+
* @example
|
|
1528
|
+
* type Case1 = IsArray<[]>;
|
|
1529
|
+
* // ➔ true
|
|
1530
|
+
* type Case2 = IsArray<string>;
|
|
1531
|
+
* // ➔ false
|
|
1532
|
+
*/
|
|
1533
|
+
type IsArray<T>=AndArr<[ Not<IsAny<T>>,Not<IsNever<T>>,Extends<T,readonly unknown[]>]>;
|
|
1534
|
+
/** -------------------------------------------------------
|
|
1535
|
+
* * ***IsMutableArray.***
|
|
1536
|
+
* -------------------------------------------------------
|
|
1537
|
+
* Returns a boolean whether the passed argument is a mutable array.
|
|
1538
|
+
*
|
|
1539
|
+
* @example
|
|
1540
|
+
* type Case1 = IsMutableArray<[]>;
|
|
1541
|
+
* // ➔ true
|
|
1542
|
+
* type Case2 = IsMutableArray<readonly []>;
|
|
1543
|
+
* // ➔ false
|
|
1544
|
+
*/
|
|
1545
|
+
type IsMutableArray<T>=And<IsArray<T>,NotExtends<Readonly<T>,T>>;
|
|
1546
|
+
/** -------------------------------------------------------
|
|
1547
|
+
* * ***IsReadonlyArray.***
|
|
1548
|
+
* -------------------------------------------------------
|
|
1549
|
+
* Returns a boolean whether the passed argument is a read-only array.
|
|
1550
|
+
*
|
|
1551
|
+
* @example
|
|
1552
|
+
* type Case1 = IsReadonlyArray<readonly []>;
|
|
1553
|
+
* // ➔ true
|
|
1554
|
+
* type Case2 = IsReadonlyArray<[]>;
|
|
1555
|
+
* // ➔ false
|
|
1556
|
+
*/
|
|
1557
|
+
type IsReadonlyArray<T>=And<IsArray<T>,NotExtends<T,Mutable<T>>>;export type{NegativeFloat as $,And as A,IfFloat as B,CharAt as C,IfInteger as D,Even as E,Float as F,IfNegativeFloat as G,IfNegativeInteger as H,IsStringLiteral as I,IfOdd as J,IfPositiveFloat as K,IfPositiveInteger as L,Mutable as M,Negate as N,Or as O,ParseNumber as P,Integer as Q,Repeat as R,Split as S,Trim as T,IsNegativeFloat as U,IsOdd as V,Whitespace as W,IsPositiveFloat as X,IsPositiveInteger as Y,IsScientificNumber as Z,Negative as _,IsEmptyString as a,NegativeInteger as a0,Odd as a1,OddDigit as a2,ParseScientificNumber as a3,Positive as a4,PositiveFloat as a5,PositiveInteger as a6,EmptyString as a7,IfNonEmptyString as a8,IsNonEmptyString as a9,NonEmptyString as aa,TrimLeft as ab,TrimRight as ac,TrimsLower as ad,TrimsUpper as ae,IfUnknown as af,UnknownifyProperties as ag,UnknownifyPropertiesOptions as ah,IsNegative as b,Abs as c,IsFloat as d,IsUnknown as e,IsNegativeInteger as f,IfEmptyString as g,OrArr as h,AndArr as i,IsInteger as j,Push as k,IfNegative as l,IsPositive as m,IsEven as n,AnyString as o,IsReadonlyArray as p,IfPositive as q,WordSeparator as r,IfNot as s,IsArray as t,IsMutableArray as u,MutableExcept as v,MutableOnly as w,MutableOptions as x,EvenDigit as y,IfEven as z};
|