@rzl-zone/utils-js 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,312 +0,0 @@
1
- import{I as If}from'./if-ChM35c_q.js';import{I as IsNever}from'./never-D89PbPh5.js';
2
- /** -------------------------------------------------------
3
- * * ***Utility Type: `IfNot`.***
4
- * -------------------------------------------------------
5
- * - **Conditional:**
6
- * - Returns the second argument if the first argument is `false`, otherwise returns the third argument.
7
- * - Defaults: `IfTrue = true`, `IfFalse = false`.
8
- * @template Condition - The boolean condition to check.
9
- * @template IfTrue - The branch type if condition is `false`. (default: `true`).
10
- * @template IfFalse - The branch type if condition is `true`. (default: `false`).
11
- * @example
12
- * ```ts
13
- * type A = IfNot<false, "valid">;
14
- * // ➔ "valid"
15
- * type B = IfNot<false, "valid", "invalid">;
16
- * // ➔ "invalid"
17
- * ```
18
- */
19
- type IfNot<Condition,IfTrue=true,IfFalse=false>=If<Condition,IfFalse,IfTrue>;
20
- /** -------------------------------------------------------
21
- * * ***Utility Type: `WordSeparator`.***
22
- * -------------------------------------------------------
23
- * **A type-level utility that defines all valid ***word separators***.**
24
- * - Can be a space `" "`, a dash `"-"`, or an underscore `"_"`.
25
- * @example
26
- * type A = WordSeparator; // ➔ " " | "-" | "_"
27
- */
28
- type WordSeparator=" "|"-"|"_";
29
- /** --------------------------------------------------
30
- * * ***Utility Type: `Whitespace`.***
31
- * --------------------------------------------------
32
- * **Represents common whitespace characters.**
33
- * - ✅ Used as the default trimming characters in string utility types.
34
- * @example
35
- * type W = Whitespace; // ➔ " " | "\t" | "\r" | "\n"
36
- */
37
- type Whitespace=" "|"\t"|"\r"|"\n";
38
- /** **Helper Type Internal.** */
39
- type SafeKeyTrimming<T>=Exclude<T,symbol>;
40
- /** --------------------------------------------------
41
- * * ***Utility Type: `TrimLeft`.***
42
- * --------------------------------------------------
43
- * **Recursively trims specified characters (default: **{@link Whitespace | `Whitespace`}**) from the **start (left)** of a string.**
44
- * @template Text - The string to trim.
45
- * @template Chars - The characters to remove (default: `Whitespace`).
46
- * @example
47
- * type T1 = TrimLeft<"\n hello", " " | "\n">;
48
- * // ➔ "hello"
49
- * type T2 = TrimLeft<" world">;
50
- * // ➔ "world"
51
- * type T3 = TrimLeft<" world ">;
52
- * // ➔ "world "
53
- */
54
- type TrimLeft<Text extends PropertyKey,Chars extends PropertyKey=Whitespace>=Text extends`${SafeKeyTrimming<Chars>}${infer Rest}`?TrimLeft<Rest,Chars>:Text;
55
- /** --------------------------------------------------
56
- * * ***Utility Type: `TrimRight`.***
57
- * --------------------------------------------------
58
- * **Recursively trims specified characters (default: **{@link Whitespace | `Whitespace`}**) from the **end (right)** of a string.**
59
- * @template Text - The string to trim.
60
- * @template Chars - The characters to remove (default: `Whitespace`).
61
- * @example
62
- * type T1 = TrimRight<"hello \t", " " | "\t">;
63
- * // ➔ "hello"
64
- * type T2 = TrimRight<"world ">;
65
- * // ➔ "world"
66
- * type T2 = TrimRight<" world ">;
67
- * // ➔ " world"
68
- */
69
- type TrimRight<Text extends PropertyKey,Chars extends PropertyKey=Whitespace>=Text extends`${infer Rest}${SafeKeyTrimming<Chars>}`?TrimRight<Rest,Chars>:Text;
70
- /** --------------------------------------------------
71
- * * ***Utility Type: `Trim`.***
72
- * --------------------------------------------------
73
- * **Trims specified characters (default: **{@link Whitespace | `Whitespace`}**)
74
- * from **both the start and end** of a string.**
75
- * @template Chars - The characters to remove (default: `Whitespace`).
76
- * @example
77
- * type T1 = Trim<" hello ", " ">;
78
- * // ➔ "hello"
79
- * type T2 = Trim<"\n world \t">;
80
- * // ➔ "world"
81
- */
82
- type Trim<Text extends PropertyKey,Chars extends PropertyKey=Whitespace>=TrimRight<TrimLeft<Text,Chars>,Chars>;
83
- /** -------------------------------------------------------
84
- * * ***Utility Type: `TrimsLower`.***
85
- * -------------------------------------------------------
86
- * **Trims leading & trailing whitespace from a string and
87
- * converts it to **lowercase**.**
88
- * @description
89
- * Utilizes **{@link Trim | `Trim`}** to remove whitespace and
90
- * **{@link Lowercase | `Lowercase`}** to convert the string to lowercase.
91
- * @template S - The input string to transform.
92
- * @example
93
- * ```ts
94
- * type T1 = TrimsLower<" HeLLo \n">;
95
- * // ➔ "hello"
96
- * type T2 = TrimsLower<" WoRLD ">;
97
- * // ➔ "world"
98
- * ```
99
- */
100
- type TrimsLower<S extends string>=Lowercase<Trim<S>>;
101
- /** -------------------------------------------------------
102
- * * ***Utility Type: `TrimsUpper`.***
103
- * -------------------------------------------------------
104
- * **Trims leading & trailing whitespace from a string and
105
- * converts it to **uppercase**.**
106
- * @description
107
- * Utilizes **{@link Trim | `Trim`}** to remove whitespace and
108
- * **{@link Uppercase | `Uppercase`}** to convert the string to uppercase.
109
- * @template S - The input string to transform.
110
- * @example
111
- * ```ts
112
- * type T1 = TrimsUpper<" HeLLo \n">;
113
- * // ➔ "HELLO"
114
- * type T2 = TrimsUpper<" WoRLD ">;
115
- * // ➔ "WORLD"
116
- * ```
117
- */
118
- type TrimsUpper<S extends string>=Uppercase<Trim<S>>;
119
- /** -------------------------------------------------------
120
- * * ***Utility Type: `AnyString`.***
121
- * -------------------------------------------------------
122
- * **A utility type that represents **any string value** while
123
- * preventing unwanted widening of string literals to `string`.**
124
- * @description
125
- * This is achieved by intersecting `string` with `{}`,
126
- * ensuring that the type remains assignable to `string`
127
- * but is treated as a unique type in generic constraints.
128
- * - **Useful in scenarios where:**
129
- * - You want to accept **any string**, but still preserve
130
- * literal types in inference.
131
- * - You need stricter typing than just `string`.
132
- * @example
133
- * ```ts
134
- * declare function acceptsAnyString<T extends AnyString>(value: T): T;
135
- *
136
- * // Preserves literal
137
- * const a = acceptsAnyString("hello");
138
- * // ➔ "hello"
139
- *
140
- * // Also allows generic string
141
- * const b = acceptsAnyString(String("world"));
142
- * // ➔ string
143
- * ```
144
- */
145
- type AnyString={}& string;
146
- /** -------------------------------------------------------
147
- * * ***Utility Type: `EmptyString`.***
148
- * -------------------------------------------------------
149
- * - **Conditional type:**
150
- * - Returns the type `T` only if it is the empty string `""`.
151
- * - Optionally trims whitespace before checking.
152
- * - **Behavior:**
153
- * - If `WithTrim` is `true` (default), trims `T` before checking.
154
- * - If `T` is the general `string` type, returns `never`.
155
- * - If `T` is empty (after optional trimming), returns `T` or `Trim<T>`.
156
- * @template T - The string type to check.
157
- * @template WithTrim - Whether to trim whitespace before checking (default `true`).
158
- * @example
159
- * ```ts
160
- * // Basic empty string
161
- * type Case1 = EmptyString<"">;
162
- * // ➔ ""
163
- *
164
- * // Non-empty string
165
- * type Case2 = EmptyString<"abc">;
166
- * // ➔ never
167
- *
168
- * // General string type
169
- * type Case3 = EmptyString<string>;
170
- * // ➔ never
171
- *
172
- * // With leading/trailing whitespace
173
- * type Case4 = EmptyString<" ", true>;
174
- * // ➔ "" (trimmed)
175
- * type Case5 = EmptyString<" ", false>;
176
- * // ➔ never (not trimmed)
177
- * ```
178
- */
179
- 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;
180
- /** -------------------------------------------------------
181
- * * ***Utility Type: `NonEmptyString`.***
182
- * -------------------------------------------------------
183
- * - **Conditional type:**
184
- * - Returns the type `T` only if it is a non-empty string.
185
- * - Optionally trims whitespace before checking.
186
- * - **Behavior:**
187
- * - If `WithTrim` is `true` (default), trims `T` before checking.
188
- * - If `T` is the general `string` type, returns `string`.
189
- * - If `T` is empty (after optional trimming), returns `never`.
190
- * @template T - The string type to check.
191
- * @template WithTrim - Whether to trim whitespace before checking (default `true`).
192
- * @example
193
- * ```ts
194
- * // Non-empty string
195
- * type Case1 = NonEmptyString<"abc">; // ➔ "abc"
196
- *
197
- * // Empty string
198
- * type Case2 = NonEmptyString<"">; // ➔ never
199
- *
200
- * // General string type
201
- * type Case3 = NonEmptyString<string>; // ➔ string
202
- *
203
- * // String with whitespace
204
- * type Case4 = NonEmptyString<" ", true>; // ➔ never (trimmed to empty)
205
- * type Case5 = NonEmptyString<" ", false>; // ➔ " " (not trimmed)
206
- * ```
207
- */
208
- 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>;
209
- /** -------------------------------------------------------
210
- * * ***Utility Type: `IsEmptyString`.***
211
- * -------------------------------------------------------
212
- * - **Conditional type:**
213
- * - Returns `true` if `T` is exactly the empty string `""`.
214
- * - Optionally trims whitespace before checking.
215
- * - **Behavior:**
216
- * - If `WithTrim` is `true` (default), trims `T` before checking.
217
- * - If `T` is empty after optional trimming, returns `true`.
218
- * - Otherwise, returns `false`.
219
- * @template T - The string type to check.
220
- * @template WithTrim - Whether to trim whitespace before checking (default `true`).
221
- * @example
222
- * ```ts
223
- * type Case1 = IsEmptyString<"">;
224
- * // ➔ true
225
- * type Case2 = IsEmptyString<"abc">;
226
- * // ➔ false
227
- * type Case3 = IsEmptyString<" ", true>;
228
- * // ➔ true (trimmed)
229
- * type Case4 = IsEmptyString<" ", false>;
230
- * // ➔ false (not trimmed)
231
- * ```
232
- */
233
- type IsEmptyString<T extends string,WithTrim extends boolean=true>=IfNot<IsNever<EmptyString<T,WithTrim>>>;
234
- /** -------------------------------------------------------
235
- * * ***Utility Type: `IsNonEmptyString`.***
236
- * -------------------------------------------------------
237
- * - **Conditional type:**
238
- * - Returns `true` if `T` is a non-empty string.
239
- * - Optionally trims whitespace before checking.
240
- * - **Behavior:**
241
- * - If `WithTrim` is `true` (default), trims `T` before checking.
242
- * - Returns `true` if `T` is non-empty after optional trimming.
243
- * - Returns `false` if `T` is empty (after trimming if `WithTrim=true`).
244
- * @template T - The string type to check.
245
- * @template WithTrim - Whether to trim whitespace before checking (default `true`).
246
- * @example
247
- * ```ts
248
- * type Case1 = IsNonEmptyString<"abc">;
249
- * // ➔ true
250
- * type Case2 = IsNonEmptyString<"">;
251
- * // ➔ false
252
- * type Case3 = IsNonEmptyString<" ", true>;
253
- * // ➔ false (trimmed)
254
- * type Case4 = IsNonEmptyString<" ", false>;
255
- * // ➔ true (not trimmed)
256
- * ```
257
- */
258
- type IsNonEmptyString<T extends string,WithTrim extends boolean=true>=IfNot<IsNever<NonEmptyString<T,WithTrim>>>;
259
- /** -------------------------------------------------------
260
- * * ***Utility Type: `IfEmptyString`.***
261
- * -------------------------------------------------------
262
- * - **Conditional type:**
263
- * - Returns `IfTrue` if `T` is an empty string `""`, otherwise returns `IfFalse`.
264
- * - Optionally trims whitespace before checking.
265
- * @template T - The string type to check.
266
- * @template IfTrue - Type returned if `T` is empty (default `true`).
267
- * @template IfFalse - Type returned if `T` is not empty (default `false`).
268
- * @template WithTrim - Whether to trim whitespace before checking (default `true`).
269
- * @example
270
- * ```ts
271
- * type Case1 = IfEmptyString<"">;
272
- * // ➔ true
273
- * type Case2 = IfEmptyString<"abc">;
274
- * // ➔ false
275
- * type Case3 = IfEmptyString<"", "yes", "no">;
276
- * // ➔ "yes"
277
- * type Case4 = IfEmptyString<"abc", "yes", "no">;
278
- * // ➔ "no"
279
- * type Case5 = IfEmptyString<" ", "yes", "no", true>;
280
- * // ➔ "yes" (trimmed)
281
- * type Case6 = IfEmptyString<" ", "yes", "no", false>;
282
- * // ➔ "no" (not trimmed)
283
- * ```
284
- */
285
- type IfEmptyString<T extends string,IfTrue=true,IfFalse=false,WithTrim extends boolean=true>=IfNot<IsNever<EmptyString<T,WithTrim>>,IfTrue,IfFalse>;
286
- /** -------------------------------------------------------
287
- * * ***Utility Type: `IfNonEmptyString`.***
288
- * -------------------------------------------------------
289
- * - **Conditional type:**
290
- * - Returns `IfTrue` if `T` is a non-empty string, otherwise returns `IfFalse`.
291
- * - Optionally trims whitespace before checking.
292
- * @template T - The string type to check.
293
- * @template IfTrue - Type returned if `T` is non-empty (default `true`).
294
- * @template IfFalse - Type returned if `T` is empty (default `false`).
295
- * @template WithTrim - Whether to trim whitespace before checking (default `true`).
296
- * @example
297
- * ```ts
298
- * type Case1 = IfNonEmptyString<"abc">;
299
- * // ➔ true
300
- * type Case2 = IfNonEmptyString<"">;
301
- * // ➔ false
302
- * type Case3 = IfNonEmptyString<"abc", "yes", "no">;
303
- * // ➔ "yes"
304
- * type Case4 = IfNonEmptyString<"", "yes", "no">;
305
- * // ➔ "no"
306
- * type Case5 = IfNonEmptyString<" ", "yes", "no", true>;
307
- * // ➔ "no" (trimmed)
308
- * type Case6 = IfNonEmptyString<" ", "yes", "no", false>;
309
- * // ➔ "yes" (not trimmed)
310
- * ```
311
- */
312
- type IfNonEmptyString<T extends string,IfTrue=true,IfFalse=false,WithTrim extends boolean=true>=IfNot<IsNever<NonEmptyString<T,WithTrim>>,IfTrue,IfFalse>;export type{AnyString as A,EmptyString as E,IfNot as I,NonEmptyString as N,Trim as T,Whitespace as W,IsEmptyString as a,IfEmptyString as b,WordSeparator as c,IfNonEmptyString as d,IsNonEmptyString as e,TrimLeft as f,TrimRight as g,TrimsLower as h,TrimsUpper as i};