@rzl-zone/utils-js 3.10.0 → 3.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +131 -129
- package/dist/assertions/index.cjs +11 -11
- package/dist/assertions/index.d.ts +487 -215
- package/dist/assertions/index.js +5 -5
- package/dist/{chunk-SN5HAK3Y.js → chunk-22V4WP3H.js} +4 -4
- package/dist/{chunk-EJV5AF4L.js → chunk-2XSZ2ANI.js} +2 -2
- package/dist/{chunk-OSSFLQDD.js → chunk-3T6VSWYX.js} +2 -2
- package/dist/{chunk-3LE6NX57.js → chunk-5WIEDF2J.js} +4 -4
- package/dist/{chunk-WLEZ2KSG.cjs → chunk-6EDFZJZ5.cjs} +126 -126
- package/dist/{chunk-GHU356XQ.js → chunk-6LXWT2I5.js} +3 -3
- package/dist/{chunk-GKDSBOYE.js → chunk-6YGBRENU.js} +3 -3
- package/dist/{chunk-NAOVH4OH.cjs → chunk-7FGNVDEV.cjs} +7 -7
- package/dist/{chunk-X6ULJZ3X.js → chunk-AXDYWO67.js} +2 -2
- package/dist/{chunk-UV5BKAYW.cjs → chunk-B6PMNZ4M.cjs} +7 -7
- package/dist/chunk-BAV5T2E3.cjs +1 -1
- package/dist/{chunk-YHFRCVTN.js → chunk-BLF7SD66.js} +3 -3
- package/dist/{chunk-QZI5PVCI.cjs → chunk-BMZZXZJ2.cjs} +4 -4
- package/dist/{chunk-DXPM4NOU.js → chunk-BOYP3ARU.js} +4 -4
- package/dist/{chunk-UXT4XSUK.js → chunk-BPYW5YL7.js} +2 -2
- package/dist/{chunk-QGTFQ7RO.cjs → chunk-C2PV3VWC.cjs} +6 -6
- package/dist/{chunk-MZ3T6L7Z.js → chunk-CCJ2MSN7.js} +2 -2
- package/dist/{chunk-H44QVAZL.cjs → chunk-DAPAK2W3.cjs} +31 -31
- package/dist/{chunk-OYFUBKEG.cjs → chunk-DLS3G6WQ.cjs} +4 -4
- package/dist/{chunk-MS2KSKD7.js → chunk-DNMCR5JH.js} +9 -9
- package/dist/chunk-DVMHRLKP.cjs +1 -1
- package/dist/{chunk-IX6PF5ZP.cjs → chunk-EBYFYZW4.cjs} +4 -4
- package/dist/{chunk-5Y6JL47L.js → chunk-ENMRZ4BE.js} +3 -3
- package/dist/{chunk-7EIFPHV3.js → chunk-FJ4BQFVO.js} +8 -8
- package/dist/{chunk-TJ5OY6MC.cjs → chunk-GIKL4PUF.cjs} +10 -10
- package/dist/{chunk-M5QB2GM5.js → chunk-GOFINGT6.js} +2 -2
- package/dist/{chunk-J6VLFVIL.js → chunk-GXKQ3LHF.js} +3 -3
- package/dist/{chunk-5KDVIEVO.js → chunk-HNBRGN4R.js} +2 -2
- package/dist/{chunk-OCTHWEZK.cjs → chunk-HYN6FC5A.cjs} +34 -34
- package/dist/{chunk-MBFVTGYS.js → chunk-IZGVBYFN.js} +4 -4
- package/dist/{chunk-QXTJVDWE.js → chunk-JIE447J5.js} +3 -3
- package/dist/{chunk-UFYMRRJH.cjs → chunk-JXEIJM5M.cjs} +61 -61
- package/dist/{chunk-WB3FT62A.js → chunk-K47GZDBH.js} +3 -3
- package/dist/{chunk-KUVRZ2JW.cjs → chunk-K5536YHG.cjs} +50 -50
- package/dist/chunk-KHO2SBNA.cjs +1 -1
- package/dist/{chunk-NJ24M6ZH.cjs → chunk-KXJ7X325.cjs} +37 -37
- package/dist/chunk-L5RDAVVH.js +1 -1
- package/dist/{chunk-SRWL4YCP.js → chunk-LJHPTLWB.js} +7 -7
- package/dist/{chunk-6AMDHVS2.cjs → chunk-M7ELWZXM.cjs} +9 -9
- package/dist/chunk-MSUW5VHZ.js +1590 -0
- package/dist/{chunk-2XGQQZ6A.cjs → chunk-MUHRPRR7.cjs} +3 -3
- package/dist/{chunk-XYWC4EQ3.cjs → chunk-MV3TSQSH.cjs} +71 -71
- package/dist/{chunk-VCYXNIZ2.cjs → chunk-NLZLXWAU.cjs} +9 -9
- package/dist/{chunk-76ATVDCR.cjs → chunk-NVRZPF5M.cjs} +3 -3
- package/dist/chunk-ONZFBJVW.js +1 -1
- package/dist/{chunk-62FS7WMB.cjs → chunk-PGNL7JXO.cjs} +31 -31
- package/dist/{chunk-JI57K7D4.cjs → chunk-Q4GEQS7X.cjs} +161 -161
- package/dist/chunk-QNKGP5DY.js +1 -1
- package/dist/{chunk-ZTHJQJ5F.cjs → chunk-QYI2VJLS.cjs} +9 -9
- package/dist/{chunk-C7GC2PFX.js → chunk-RBWZII5I.js} +3 -3
- package/dist/{chunk-MMRHKYT6.cjs → chunk-RMP7VMPB.cjs} +12 -12
- package/dist/{chunk-TXOVQZPU.js → chunk-RZOGBYIS.js} +2 -2
- package/dist/{chunk-ALRISPTL.cjs → chunk-SU3UELUB.cjs} +3 -3
- package/dist/chunk-SYHPSOUU.cjs +1626 -0
- package/dist/{chunk-R2DR7SPJ.js → chunk-SZUNAEMR.js} +6 -6
- package/dist/{chunk-YVRUY4EW.cjs → chunk-TER22LO4.cjs} +8 -8
- package/dist/{chunk-WRGN6UBK.js → chunk-TJHGRQ4P.js} +2 -2
- package/dist/{chunk-I33PB44Q.cjs → chunk-U23I7JPB.cjs} +15 -15
- package/dist/{chunk-VBSLIIDB.js → chunk-U7HHN47R.js} +10 -10
- package/dist/chunk-UDA26MCU.cjs +1 -1
- package/dist/{chunk-44X74C26.js → chunk-UIAWUZ4H.js} +4 -4
- package/dist/{chunk-5O66AUEC.js → chunk-ULQPCIA2.js} +2 -2
- package/dist/chunk-VJDDGRIK.cjs +1 -1
- package/dist/{chunk-6WVOUVWD.js → chunk-WETQI6HM.js} +9 -9
- package/dist/{chunk-FWWPEL7J.cjs → chunk-WLOQQFDS.cjs} +3 -3
- package/dist/chunk-WVSPXFTY.js +1 -1
- package/dist/{chunk-WKM6UVMG.cjs → chunk-WXFTVXBF.cjs} +4 -4
- package/dist/{chunk-YQHJB7KR.cjs → chunk-XPVTIGU2.cjs} +26 -26
- package/dist/{chunk-PWKOFPAH.cjs → chunk-XX6RUGTM.cjs} +3 -3
- package/dist/chunk-YWHHVDT4.js +1 -1
- package/dist/{chunk-7NXFGJJE.cjs → chunk-ZPDMWDGZ.cjs} +14 -14
- package/dist/{chunk-JYOCB6OV.js → chunk-ZVWZEGQP.js} +2 -2
- package/dist/conversions/index.cjs +34 -34
- package/dist/conversions/index.d.ts +489 -333
- package/dist/conversions/index.js +15 -15
- package/dist/events/index.cjs +8 -8
- package/dist/events/index.d.ts +35 -29
- package/dist/events/index.js +4 -4
- package/dist/formatters/index.cjs +19 -19
- package/dist/formatters/index.d.ts +917 -873
- package/dist/formatters/index.js +9 -9
- package/dist/generators/index.cjs +9 -9
- package/dist/generators/index.d.ts +104 -86
- package/dist/generators/index.js +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/isPlainObject-0p3VveWr.d.ts +534 -0
- package/dist/next/index.cjs +37 -37
- package/dist/next/index.d.ts +36 -21
- package/dist/next/index.js +9 -9
- package/dist/next/server/index.cjs +4 -4
- package/dist/next/server/index.d.ts +6 -3
- package/dist/next/server/index.js +2 -2
- package/dist/operations/index.cjs +11 -11
- package/dist/operations/index.d.ts +14 -5
- package/dist/operations/index.js +8 -8
- package/dist/parsers/index.cjs +6 -6
- package/dist/parsers/index.d.ts +68 -65
- package/dist/parsers/index.js +5 -5
- package/dist/predicates/index.cjs +77 -77
- package/dist/predicates/index.d.ts +442 -258
- package/dist/predicates/index.js +14 -14
- package/dist/promises/index.cjs +6 -6
- package/dist/promises/index.d.ts +28 -16
- package/dist/promises/index.js +4 -4
- package/dist/rzl-utils.global.js +2 -2
- package/dist/strings/index.cjs +23 -23
- package/dist/strings/index.d.ts +82 -54
- package/dist/strings/index.js +7 -7
- package/dist/tailwind/index.cjs +13 -13
- package/dist/tailwind/index.d.ts +186 -169
- package/dist/tailwind/index.js +5 -5
- package/dist/urls/index.cjs +23 -23
- package/dist/urls/index.d.ts +252 -229
- package/dist/urls/index.js +16 -16
- package/package.json +6 -4
- package/dist/chunk-7C7TQC5J.cjs +0 -620
- package/dist/chunk-J4TT33ZX.js +0 -584
- package/dist/isPlainObject-BTPjv6zB.d.ts +0 -178
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
* ====================================================
|
|
3
3
|
* Rzl Utils-JS.
|
|
4
4
|
* ----------------------------------------------------
|
|
5
|
-
* Version: 3.
|
|
5
|
+
* Version: 3.11.1.
|
|
6
6
|
* Author: Rizalvin Dwiky.
|
|
7
7
|
* Repository: https://github.com/rzl-zone/utils-js.
|
|
8
8
|
* ====================================================
|
|
9
9
|
*/
|
|
10
|
-
import{Prettify,PickStrict}from'@rzl-zone/ts-types-plus';
|
|
10
|
+
import { Prettify, PickStrict } from '@rzl-zone/ts-types-plus';
|
|
11
|
+
import { G as GetPreciseTypeOptions, I as IsNumberOptions, a as IsPlainObjectResult } from '../isPlainObject-0p3VveWr.js';
|
|
12
|
+
|
|
11
13
|
/** -------------------------------------------------------
|
|
12
14
|
* * ***Shape of the object passed to custom error message functions.***
|
|
13
15
|
* -------------------------------------------------------
|
|
@@ -25,21 +27,22 @@ import{Prettify,PickStrict}from'@rzl-zone/ts-types-plus';import{G as GetPreciseT
|
|
|
25
27
|
* };
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
type OptionsMessageFunctionAssertIs={
|
|
29
|
-
/** ---------------------------------------------------------------------------
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
currentType:string;
|
|
36
|
-
/** ---------------------------------------------------------------------------
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
validType:string;
|
|
30
|
+
type OptionsMessageFunctionAssertIs = {
|
|
31
|
+
/** ---------------------------------------------------------------------------
|
|
32
|
+
* * ***The actual runtime type of the value being checked.***
|
|
33
|
+
* ---------------------------------------------------------------------------
|
|
34
|
+
* - ***Example:***
|
|
35
|
+
* - `"number"`, `"big-int"`, `"plain-object"`, (depends `formatCase` options).
|
|
36
|
+
*/
|
|
37
|
+
currentType: string;
|
|
38
|
+
/** ---------------------------------------------------------------------------
|
|
39
|
+
* * ***The required/expected type that the value must conform to.***
|
|
40
|
+
* ---------------------------------------------------------------------------
|
|
41
|
+
* - ***Example:***
|
|
42
|
+
* - `"boolean"`, `"string"`, `"big-int"`, `"plain-object"`, (will force format to `kebab-case`).
|
|
43
|
+
*/
|
|
44
|
+
validType: string;
|
|
45
|
+
};
|
|
43
46
|
/** -------------------------------------------------------
|
|
44
47
|
* * ***Custom error-message type for assertions option {@link OptionsAssertIs | `OptionsAssertIs`}.***
|
|
45
48
|
* -------------------------------------------------------
|
|
@@ -47,66 +50,71 @@ validType:string;};
|
|
|
47
50
|
* - A static string message.
|
|
48
51
|
* - A function receiving `{ currentType, validType }` and returning a string.
|
|
49
52
|
*/
|
|
50
|
-
type OptionsMessageAssertIs=string|(({currentType,validType}:OptionsMessageFunctionAssertIs)=>string);
|
|
53
|
+
type OptionsMessageAssertIs = string | (({ currentType, validType }: OptionsMessageFunctionAssertIs) => string);
|
|
51
54
|
/** ---------------------------------------------------------------------------
|
|
52
55
|
* * ***Base options for `assertIs*` functions.***
|
|
53
56
|
* ---------------------------------------------------------------------------
|
|
54
57
|
*/
|
|
55
|
-
type OptionsAssertIs=Prettify<{
|
|
56
|
-
/** -------------------------------------------------------
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
message?:OptionsMessageAssertIs;
|
|
85
|
-
/** -------------------------------------------------------
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
errorType?:
|
|
58
|
+
type OptionsAssertIs = Prettify<{
|
|
59
|
+
/** -------------------------------------------------------
|
|
60
|
+
* * ***Custom error message for assertion failures.***
|
|
61
|
+
* -------------------------------------------------------
|
|
62
|
+
* **This option allows overriding the **default error message** when a value
|
|
63
|
+
* does not match the required type.**
|
|
64
|
+
* - If a **string** is provided:
|
|
65
|
+
* - Must be non-empty after trimming.
|
|
66
|
+
* - Will be used directly as the error message.
|
|
67
|
+
* - If a **function** is provided:
|
|
68
|
+
* - Receives an object containing:
|
|
69
|
+
* - `currentType` ➔ the detected runtime type of the value (depends `formatCase` options, e.g., `"number"`).
|
|
70
|
+
* - `validType` ➔ the expected type name (with format `kebab-case`, e.g., `"boolean"`, `"big-int"`, `"plain-object"`).
|
|
71
|
+
* - **Must** return a **string**:
|
|
72
|
+
* - **If** the **returned string is** `empty` or `whitespace`,
|
|
73
|
+
* the **default message** will be used instead.
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* // Static message
|
|
77
|
+
* { message: "Must be a boolean!" }
|
|
78
|
+
*
|
|
79
|
+
* // Dynamic message
|
|
80
|
+
* {
|
|
81
|
+
* message: ({ currentType, validType }) => {
|
|
82
|
+
* return `Expected ${validType} but got ${currentType}`;
|
|
83
|
+
* };
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
message?: OptionsMessageAssertIs;
|
|
88
|
+
/** -------------------------------------------------------
|
|
89
|
+
* * ***Custom error type for assertion failures.***
|
|
90
|
+
* -------------------------------------------------------
|
|
91
|
+
* **This option allows overriding the default error type** that will be thrown
|
|
92
|
+
* when a value does not match the required type.
|
|
93
|
+
*
|
|
94
|
+
* - **Behavior:**
|
|
95
|
+
* - Must be one of the standard JavaScript built-in error types:
|
|
96
|
+
* `"Error" | "EvalError" | "RangeError" | "ReferenceError" | "SyntaxError" | "TypeError" | "URIError"`
|
|
97
|
+
* - **Default:** `"TypeError"` if not provided or if an invalid value is passed.
|
|
98
|
+
* - The assertion function will **always throw a valid built-in error**, ensuring
|
|
99
|
+
* fallback to `TypeError` in case of an unknown or incorrect type.
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* // Valid: Throw a RangeError instead of TypeError
|
|
103
|
+
* { errorType: "RangeError" }
|
|
104
|
+
*
|
|
105
|
+
* // Valid: Throw a ReferenceError
|
|
106
|
+
* { errorType: "ReferenceError" }
|
|
107
|
+
*
|
|
108
|
+
* // Invalid value ➔ fallback to TypeError
|
|
109
|
+
* { errorType: "SomeUnknownError" as ErrorType }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
errorType?: ErrorType;
|
|
113
|
+
} & PickStrict<GetPreciseTypeOptions, "formatCase" | "useAcronyms">, {
|
|
114
|
+
recursive: true;
|
|
115
|
+
}>;
|
|
116
|
+
type ErrorType = "Error" | "EvalError" | "RangeError" | "ReferenceError" | "SyntaxError" | "TypeError" | "URIError";
|
|
117
|
+
|
|
110
118
|
/** -------------------------------------------------------
|
|
111
119
|
* * ***Type guard assertion: `assertIsBoolean`.***
|
|
112
120
|
* -------------------------------------------------------
|
|
@@ -134,7 +142,8 @@ errorType?:ErrorType;}& PickStrict<GetPreciseTypeOptions,"formatCase">,{recursiv
|
|
|
134
142
|
* ***Optional configuration:***
|
|
135
143
|
* - `message`: A custom error message (`string` or `function`).
|
|
136
144
|
* - `errorType`: A custom built-in JavaScript error type to throw.
|
|
137
|
-
* - `formatCase`: Controls type
|
|
145
|
+
* - `formatCase`: Controls how detected type names are formatted case in error messages.
|
|
146
|
+
* - `useAcronyms`: Control uppercase preservation of recognized acronyms during formatting.
|
|
138
147
|
* @returns {boolean} Narrows `value` to `boolean` if no error is thrown.
|
|
139
148
|
* @throws [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) | [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) | [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) | [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) | [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) if the value is not a boolean.
|
|
140
149
|
* @example
|
|
@@ -143,40 +152,82 @@ errorType?:ErrorType;}& PickStrict<GetPreciseTypeOptions,"formatCase">,{recursiv
|
|
|
143
152
|
* assertIsBoolean(true);
|
|
144
153
|
* // No error, value is boolean
|
|
145
154
|
*
|
|
146
|
-
* // ❌ Throws TypeError
|
|
147
|
-
*
|
|
155
|
+
* // ❌ Throws TypeError (default behavior)
|
|
156
|
+
* // Case 1: Invalid input type — received a string instead of a boolean
|
|
157
|
+
* assertIsBoolean("hello");
|
|
148
158
|
* // ➔ TypeError: "Parameter input (`value`) must be of type `boolean`, but received: `number`."
|
|
149
159
|
*
|
|
150
|
-
* //
|
|
160
|
+
* // Case 2: The new Boolean() is a Boolean object (constructor), not a primitive boolean
|
|
161
|
+
* assertIsBoolean(new Boolean(true));
|
|
162
|
+
* // ➔ TypeError: "Parameter input (`value`) must be of type `boolean`, but received: `boolean-constructor`."
|
|
163
|
+
*
|
|
164
|
+
* // ❌ Throws a TypeError with a custom string static message
|
|
151
165
|
* assertIsBoolean(42, { message: "Must be boolean!" });
|
|
152
166
|
* // ➔ TypeError: "Must be boolean!"
|
|
153
167
|
*
|
|
154
|
-
* // ❌ Throws RangeError
|
|
168
|
+
* // ❌ Throws RangeError (custom error type)
|
|
155
169
|
* assertIsBoolean(42, { errorType: "RangeError" });
|
|
156
170
|
* // ➔ RangeError: "Parameter input (`value`) must be of type `boolean`, but received: `number`."
|
|
157
171
|
*
|
|
158
|
-
* // ❌ Throws with custom
|
|
159
|
-
* assertIsBoolean(
|
|
160
|
-
* message: ({ currentType, validType }) =>
|
|
161
|
-
*
|
|
172
|
+
* // ❌ Throws a TypeError with a custom message function and formatCase
|
|
173
|
+
* assertIsBoolean(/regex/, {
|
|
174
|
+
* message: ({ currentType, validType }) => {
|
|
175
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
176
|
+
* },
|
|
177
|
+
* formatCase: "toPascalCaseSpace"
|
|
178
|
+
* });
|
|
179
|
+
* // ➔ TypeError: "Expected boolean but got (Reg Exp)."
|
|
180
|
+
*
|
|
181
|
+
* // ❌ Throws a TypeError with a custom useAcronyms option
|
|
182
|
+
* // Case 1:
|
|
183
|
+
* assertIsBoolean(new URL("https://example.com"),{
|
|
184
|
+
* message: ({ currentType, validType }) => {
|
|
185
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
186
|
+
* },
|
|
162
187
|
* });
|
|
163
|
-
* // ➔ TypeError: "Expected boolean but got (
|
|
188
|
+
* // ➔ TypeError: "Expected boolean but got (url)."
|
|
189
|
+
* assertIsBoolean(new URL("https://example.com"), {
|
|
190
|
+
* useAcronyms: true,
|
|
191
|
+
* message: ({ currentType, validType }) => {
|
|
192
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
193
|
+
* },
|
|
194
|
+
* });
|
|
195
|
+
* // ➔ TypeError: "Expected boolean but got (URL)."
|
|
196
|
+
*
|
|
197
|
+
* // Case 2:
|
|
198
|
+
* assertIsBoolean(new URLSearchParams, {
|
|
199
|
+
* formatCase: "toPascalCase",
|
|
200
|
+
* message: ({ currentType, validType }) => {
|
|
201
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
202
|
+
* },
|
|
203
|
+
* });
|
|
204
|
+
* // ➔ TypeError: "Expected boolean but got (UrlSearchParams)."
|
|
205
|
+
* assertIsBoolean(new URLSearchParams, {
|
|
206
|
+
* useAcronyms: true,
|
|
207
|
+
* formatCase: "toPascalCase",
|
|
208
|
+
* message: ({ currentType, validType }) => {
|
|
209
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
210
|
+
* },
|
|
211
|
+
* });
|
|
212
|
+
* // ➔ TypeError: "Expected boolean but got (URLSearchParams)."
|
|
164
213
|
* ```
|
|
165
214
|
* -------------------------------------------------------
|
|
166
215
|
* ✅ ***Real-world usage example***:
|
|
167
216
|
* ```ts
|
|
168
|
-
*
|
|
169
|
-
* const mixedValue: string | User | boolean | number | undefined = getUserInput();
|
|
217
|
+
* const mixedValue: string | boolean | number | undefined = getUserInput();
|
|
170
218
|
*
|
|
171
|
-
* //
|
|
172
|
-
*
|
|
173
|
-
*
|
|
219
|
+
* // Runtime assertion: throws if `mixedValue` is not a `boolean`
|
|
220
|
+
* assertIsBoolean(mixedValue, {
|
|
221
|
+
* errorType: "RangeError",
|
|
222
|
+
* message: "Must be boolean!"
|
|
223
|
+
* });
|
|
174
224
|
*
|
|
175
|
-
* // ✅
|
|
176
|
-
* const result: boolean = mixedValue; // ➔ Safe
|
|
225
|
+
* // ✅ If no error thrown, TypeScript narrows `mixedValue` to `boolean` here
|
|
226
|
+
* const result: boolean = mixedValue; // ➔ Safe type assignment
|
|
177
227
|
* ```
|
|
178
228
|
*/
|
|
179
|
-
declare const assertIsBoolean:(value:unknown,options?:OptionsAssertIs)=>asserts value is boolean;
|
|
229
|
+
declare const assertIsBoolean: (value: unknown, options?: OptionsAssertIs) => asserts value is boolean;
|
|
230
|
+
|
|
180
231
|
/** -------------------------------------------------------
|
|
181
232
|
* * ***Type guard assertion: `assertIsBigInt`.***
|
|
182
233
|
* -------------------------------------------------------
|
|
@@ -205,9 +256,10 @@ declare const assertIsBoolean:(value:unknown,options?:OptionsAssertIs)=>asserts
|
|
|
205
256
|
* @param {*} value - ***The value to validate.***
|
|
206
257
|
* @param {OptionsAssertIs} [options]
|
|
207
258
|
* ***Optional configuration:***
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
259
|
+
* - `message`: A custom error message (`string` or `function`).
|
|
260
|
+
* - `errorType`: Built-in JavaScript error type to throw on failure (default `"TypeError"`).
|
|
261
|
+
* - `formatCase`: Controls how detected type names are formatted case in error messages.
|
|
262
|
+
* - `useAcronyms`: Control uppercase preservation of recognized acronyms during formatting.
|
|
211
263
|
* @returns {boolean} Narrows `value` to `bigint` if no error is thrown.
|
|
212
264
|
* @throws [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) | [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) | [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) | [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) | [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) if the value is not a bigint.
|
|
213
265
|
* @example
|
|
@@ -216,46 +268,79 @@ declare const assertIsBoolean:(value:unknown,options?:OptionsAssertIs)=>asserts
|
|
|
216
268
|
* assertIsBigInt(123n);
|
|
217
269
|
* // No error, value is bigint
|
|
218
270
|
*
|
|
219
|
-
* // ❌ Throws TypeError
|
|
271
|
+
* // ❌ Throws TypeError (default behavior)
|
|
220
272
|
* assertIsBigInt(42);
|
|
221
273
|
* // ➔ TypeError: "Parameter input (`value`) must be of type `bigint`, but received: `number`."
|
|
222
274
|
*
|
|
223
|
-
* // ❌ Throws with custom string message
|
|
275
|
+
* // ❌ Throws a TypeError with a custom string static message
|
|
224
276
|
* assertIsBigInt("123", { message: "Must be a bigint!" });
|
|
225
277
|
* // ➔ TypeError: "Must be a bigint!"
|
|
226
278
|
*
|
|
227
|
-
* // ❌ Throws
|
|
228
|
-
* assertIsBigInt(
|
|
229
|
-
*
|
|
230
|
-
*
|
|
279
|
+
* // ❌ Throws custom error type (e.g., RangeError)
|
|
280
|
+
* assertIsBigInt(async function () {}, { errorType: "RangeError" });
|
|
281
|
+
* // ➔ RangeError: "Parameter input (`value`) must be of type `bigint`, but received: `async-function`."
|
|
282
|
+
*
|
|
283
|
+
* // ❌ Throws a TypeError with a custom message function and formatCase
|
|
284
|
+
* assertIsBigInt(/regex/, {
|
|
285
|
+
* message: ({ currentType, validType }) => {
|
|
286
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
287
|
+
* },
|
|
288
|
+
* formatCase: "toPascalCaseSpace"
|
|
231
289
|
* });
|
|
232
|
-
* // ➔ TypeError: "Expected bigint but got (
|
|
290
|
+
* // ➔ TypeError: "Expected bigint but got (Reg Exp)."
|
|
233
291
|
*
|
|
234
|
-
* // ❌ Throws
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
292
|
+
* // ❌ Throws a TypeError with a custom useAcronyms option
|
|
293
|
+
* // Case 1:
|
|
294
|
+
* assertIsBigInt(new URL("https://example.com"),{
|
|
295
|
+
* message: ({ currentType, validType }) => {
|
|
296
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
297
|
+
* },
|
|
238
298
|
* });
|
|
239
|
-
* // ➔ TypeError: "Expected bigint but got (
|
|
299
|
+
* // ➔ TypeError: "Expected bigint but got (url)."
|
|
300
|
+
* assertIsBigInt(new URL("https://example.com"), {
|
|
301
|
+
* useAcronyms: true,
|
|
302
|
+
* message: ({ currentType, validType }) => {
|
|
303
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
304
|
+
* },
|
|
305
|
+
* });
|
|
306
|
+
* // ➔ TypeError: "Expected bigint but got (URL)."
|
|
240
307
|
*
|
|
241
|
-
* //
|
|
242
|
-
* assertIsBigInt(
|
|
243
|
-
*
|
|
308
|
+
* // Case 2:
|
|
309
|
+
* assertIsBigInt(new URLSearchParams, {
|
|
310
|
+
* formatCase: "toPascalCase",
|
|
311
|
+
* message: ({ currentType, validType }) => {
|
|
312
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
313
|
+
* },
|
|
314
|
+
* });
|
|
315
|
+
* // ➔ TypeError: "Expected bigint but got (UrlSearchParams)."
|
|
316
|
+
* assertIsBigInt(new URLSearchParams, {
|
|
317
|
+
* useAcronyms: true,
|
|
318
|
+
* formatCase: "toPascalCase",
|
|
319
|
+
* message: ({ currentType, validType }) => {
|
|
320
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
321
|
+
* },
|
|
322
|
+
* });
|
|
323
|
+
* // ➔ TypeError: "Expected bigint but got (URLSearchParams)."
|
|
244
324
|
* ```
|
|
245
325
|
* -------------------------------------------------------
|
|
246
326
|
* ✅ ***Real-world usage example***:
|
|
247
327
|
* ```ts
|
|
248
328
|
* const mixedValue: string | bigint | undefined = getUserInput();
|
|
249
329
|
*
|
|
250
|
-
* //
|
|
251
|
-
* assertIsBigInt(mixedValue, {
|
|
330
|
+
* // Runtime assertion: throws if `mixedValue` is not a `bigint`
|
|
331
|
+
* assertIsBigInt(mixedValue, {
|
|
332
|
+
* errorType: "RangeError",
|
|
333
|
+
* message: "Must be bigint!"
|
|
334
|
+
* });
|
|
252
335
|
*
|
|
253
|
-
* // ✅
|
|
254
|
-
* const result: bigint = mixedValue;
|
|
255
|
-
* console.log(result + 100n);
|
|
336
|
+
* // ✅ If no error thrown, TypeScript narrows `mixedValue` to `bigint` here
|
|
337
|
+
* const result: bigint = mixedValue; // ➔ Safe type assignment
|
|
338
|
+
* console.log(result + 100n); // ➔ Safe to operation
|
|
256
339
|
* ```
|
|
257
340
|
*/
|
|
258
|
-
declare const assertIsBigInt:(value:unknown,options?:OptionsAssertIs)=>asserts value is bigint;
|
|
341
|
+
declare const assertIsBigInt: (value: unknown, options?: OptionsAssertIs) => asserts value is bigint;
|
|
342
|
+
|
|
343
|
+
type OptionsAssertIsNumber = OptionsAssertIs & IsNumberOptions;
|
|
259
344
|
/** -------------------------------------------------------
|
|
260
345
|
* * ***Type guard assertion: `assertIsNumber`.***
|
|
261
346
|
* -------------------------------------------------------
|
|
@@ -285,59 +370,101 @@ declare const assertIsBigInt:(value:unknown,options?:OptionsAssertIs)=>asserts v
|
|
|
285
370
|
* @param {*} value - ***The value to validate.***
|
|
286
371
|
* @param {OptionsAssertIsNumber} [options]
|
|
287
372
|
* ***Optional configuration:***
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
373
|
+
* - `message`: A custom error message (`string` or `function`).
|
|
374
|
+
* - `errorType`: Built-in JavaScript error type to throw on failure (default `"TypeError"`).
|
|
375
|
+
* - `formatCase`: Controls how detected type names are formatted case in error messages.
|
|
376
|
+
* - `useAcronyms`: Control uppercase preservation of recognized acronyms during formatting.
|
|
377
|
+
* - `includeNaN`: Whether to treat `NaN` as valid.
|
|
292
378
|
* @returns {boolean} Narrows `value` to `number` if no error is thrown.
|
|
293
379
|
* @throws [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) | [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) | [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) | [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) | [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) if the value is not a number (or is `NaN` when `includeNaN` is `false`).
|
|
294
380
|
* @example
|
|
295
381
|
* ```ts
|
|
296
382
|
* // ✅ Simple usage
|
|
297
383
|
* assertIsNumber(123);
|
|
384
|
+
* assertIsNumber(NaN, { includeNaN: true });
|
|
298
385
|
* // No error, value is number
|
|
299
386
|
*
|
|
300
|
-
* // ❌ Throws TypeError
|
|
387
|
+
* // ❌ Throws TypeError (default behavior)
|
|
388
|
+
* // Case 1: Invalid input type — received a string instead of a number
|
|
301
389
|
* assertIsNumber("42");
|
|
302
390
|
* // ➔ TypeError: "Parameter input (`value`) must be of type `number`, but received: `string`."
|
|
303
391
|
*
|
|
304
|
-
* //
|
|
305
|
-
* assertIsNumber(
|
|
306
|
-
* // ➔ TypeError: "
|
|
392
|
+
* // Case 2: Default option includeNaN is false
|
|
393
|
+
* assertIsNumber(NaN);
|
|
394
|
+
* // ➔ TypeError: "Parameter input (`value`) must be of type `number`, but received: `NaN`."
|
|
395
|
+
*
|
|
396
|
+
* // Case 3: The new Number() is a Number object (constructor), not a primitive number
|
|
397
|
+
* assertIsNumber(new Number("123"));
|
|
398
|
+
* // ➔ TypeError: "Parameter input (`value`) must be of type `number`, but received: `number-constructor`."
|
|
399
|
+
*
|
|
400
|
+
* // ❌ Throws custom error type (e.g., RangeError)
|
|
401
|
+
* assertIsNumber(async function () {}, { errorType: "RangeError" });
|
|
402
|
+
* // ➔ RangeError: "Parameter input (`value`) must be of type `number`, but received: `async-function`."
|
|
307
403
|
*
|
|
308
|
-
* // ❌ Throws
|
|
309
|
-
* assertIsNumber("
|
|
310
|
-
* // ➔
|
|
404
|
+
* // ❌ Throws a TypeError with a custom string static message
|
|
405
|
+
* assertIsNumber("123", { message: "Must be a number!" });
|
|
406
|
+
* // ➔ TypeError: "Must be a number!"
|
|
311
407
|
*
|
|
312
|
-
* // ❌ Throws with custom
|
|
313
|
-
* assertIsNumber(
|
|
314
|
-
* message: ({ currentType, validType }) =>
|
|
315
|
-
*
|
|
408
|
+
* // ❌ Throws a TypeError with a custom message function and formatCase
|
|
409
|
+
* assertIsNumber(/regex/, {
|
|
410
|
+
* message: ({ currentType, validType }) => {
|
|
411
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
412
|
+
* },
|
|
413
|
+
* formatCase: "toPascalCaseSpace"
|
|
316
414
|
* });
|
|
317
|
-
* // ➔ TypeError: "Expected number but got (
|
|
415
|
+
* // ➔ TypeError: "Expected number but got (Reg Exp)."
|
|
318
416
|
*
|
|
319
|
-
* //
|
|
320
|
-
*
|
|
321
|
-
*
|
|
417
|
+
* // ❌ Throws a TypeError with a custom useAcronyms option
|
|
418
|
+
* // Case 1:
|
|
419
|
+
* assertIsNumber(new URL("https://example.com"),{
|
|
420
|
+
* message: ({ currentType, validType }) => {
|
|
421
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
422
|
+
* },
|
|
423
|
+
* });
|
|
424
|
+
* // ➔ TypeError: "Expected number but got (url)."
|
|
425
|
+
* assertIsNumber(new URL("https://example.com"), {
|
|
426
|
+
* useAcronyms: true,
|
|
427
|
+
* message: ({ currentType, validType }) => {
|
|
428
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
429
|
+
* },
|
|
430
|
+
* });
|
|
431
|
+
* // ➔ TypeError: "Expected number but got (URL)."
|
|
322
432
|
*
|
|
323
|
-
* //
|
|
324
|
-
* assertIsNumber(
|
|
325
|
-
*
|
|
433
|
+
* // Case 2:
|
|
434
|
+
* assertIsNumber(new URLSearchParams, {
|
|
435
|
+
* formatCase: "toPascalCase",
|
|
436
|
+
* message: ({ currentType, validType }) => {
|
|
437
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
438
|
+
* },
|
|
439
|
+
* });
|
|
440
|
+
* // ➔ TypeError: "Expected number but got (UrlSearchParams)."
|
|
441
|
+
* assertIsNumber(new URLSearchParams, {
|
|
442
|
+
* useAcronyms: true,
|
|
443
|
+
* formatCase: "toPascalCase",
|
|
444
|
+
* message: ({ currentType, validType }) => {
|
|
445
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
446
|
+
* },
|
|
447
|
+
* });
|
|
448
|
+
* // ➔ TypeError: "Expected number but got (URLSearchParams)."
|
|
326
449
|
* ```
|
|
327
450
|
* -------------------------------------------------------
|
|
328
451
|
* ✅ ***Real-world usage example***:
|
|
329
452
|
* ```ts
|
|
330
453
|
* const mixedValue: string | number | undefined = getUserInput();
|
|
331
454
|
*
|
|
332
|
-
* //
|
|
333
|
-
* assertIsNumber(mixedValue, {
|
|
455
|
+
* // Runtime assertion: throws if `mixedValue` is not a `number`
|
|
456
|
+
* assertIsNumber(mixedValue, {
|
|
457
|
+
* errorType: "RangeError",
|
|
458
|
+
* message: "Must be number!"
|
|
459
|
+
* });
|
|
334
460
|
*
|
|
335
|
-
* // ✅
|
|
336
|
-
* const result: number = mixedValue; // ➔ Safe
|
|
337
|
-
* console.log(result + 100);
|
|
461
|
+
* // ✅ If no error thrown, TypeScript narrows `mixedValue` to `number` here
|
|
462
|
+
* const result: number = mixedValue; // ➔ Safe type assignment
|
|
463
|
+
* console.log(result + 100); // ➔ Safe to operation
|
|
338
464
|
* ```
|
|
339
465
|
*/
|
|
340
|
-
declare const assertIsNumber:(value:unknown,options?:OptionsAssertIsNumber)=>asserts value is number;
|
|
466
|
+
declare const assertIsNumber: (value: unknown, options?: OptionsAssertIsNumber) => asserts value is number;
|
|
467
|
+
|
|
341
468
|
/** -------------------------------------------------------
|
|
342
469
|
* * ***Type guard assertion: `assertIsArray`.***
|
|
343
470
|
* -------------------------------------------------------
|
|
@@ -364,50 +491,95 @@ declare const assertIsNumber:(value:unknown,options?:OptionsAssertIsNumber)=>ass
|
|
|
364
491
|
* @param {*} value - ***The value to validate.***
|
|
365
492
|
* @param {OptionsAssertIs} [options]
|
|
366
493
|
* ***Optional configuration:***
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
494
|
+
* - `message`: A custom error message (`string` or `function`).
|
|
495
|
+
* - `errorType`: Built-in JavaScript error type to throw on failure (default `"TypeError"`).
|
|
496
|
+
* - `formatCase`: Controls how detected type names are formatted case in error messages.
|
|
497
|
+
* - `useAcronyms`: Control uppercase preservation of recognized acronyms during formatting.
|
|
370
498
|
* @returns {boolean} Narrows `value` to an `array` **(generic support)** if no error is thrown.
|
|
371
499
|
* @throws [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) | [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) | [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) | [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) | [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) if the value is not an array.
|
|
372
500
|
* @example
|
|
373
501
|
* ```ts
|
|
374
502
|
* // ✅ Simple usage
|
|
375
|
-
* assertIsArray([
|
|
503
|
+
* assertIsArray([]);
|
|
504
|
+
* assertIsArray(["123", 456]);
|
|
505
|
+
* assertIsArray(Array.from(["abc"]));
|
|
376
506
|
* // No error, value is array
|
|
377
507
|
*
|
|
378
|
-
* // ❌ Throws TypeError
|
|
379
|
-
*
|
|
380
|
-
*
|
|
508
|
+
* // ❌ Throws TypeError (default behavior)
|
|
509
|
+
* // Case 1: Invalid input type — received a string instead of a array
|
|
510
|
+
* assertIsArray("42");
|
|
511
|
+
* // ➔ TypeError: "Parameter input (`value`) must be of type `array`, but received: `string`."
|
|
381
512
|
*
|
|
382
|
-
* // ❌ Throws
|
|
383
|
-
* assertIsArray(
|
|
384
|
-
* // ➔
|
|
513
|
+
* // ❌ Throws custom error type (e.g., RangeError)
|
|
514
|
+
* assertIsArray(async function () {}, { errorType: "RangeError" });
|
|
515
|
+
* // ➔ RangeError: "Parameter input (`value`) must be of type `array`, but received: `async-function`."
|
|
516
|
+
*
|
|
517
|
+
* // ❌ Throws a TypeError with a custom string static message
|
|
518
|
+
* assertIsArray("123", { message: "Must be a array!" });
|
|
519
|
+
* // ➔ TypeError: "Must be a array!"
|
|
385
520
|
*
|
|
386
|
-
* // ❌ Throws
|
|
387
|
-
* assertIsArray(
|
|
388
|
-
*
|
|
521
|
+
* // ❌ Throws a TypeError with a custom message function and formatCase
|
|
522
|
+
* assertIsArray(/regex/, {
|
|
523
|
+
* message: ({ currentType, validType }) => {
|
|
524
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
525
|
+
* },
|
|
526
|
+
* formatCase: "toPascalCaseSpace"
|
|
527
|
+
* });
|
|
528
|
+
* // ➔ TypeError: "Expected array but got (Reg Exp)."
|
|
389
529
|
*
|
|
390
|
-
* // ❌ Throws
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
530
|
+
* // ❌ Throws a TypeError with a custom useAcronyms option
|
|
531
|
+
* // Case 1:
|
|
532
|
+
* assertIsArray(new URL("https://example.com"),{
|
|
533
|
+
* message: ({ currentType, validType }) => {
|
|
534
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
535
|
+
* },
|
|
536
|
+
* });
|
|
537
|
+
* // ➔ TypeError: "Expected array but got (url)."
|
|
538
|
+
* assertIsArray(new URL("https://example.com"), {
|
|
539
|
+
* useAcronyms: true,
|
|
540
|
+
* message: ({ currentType, validType }) => {
|
|
541
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
542
|
+
* },
|
|
394
543
|
* });
|
|
395
|
-
* // ➔ TypeError: "Expected array but got (
|
|
544
|
+
* // ➔ TypeError: "Expected array but got (URL)."
|
|
545
|
+
*
|
|
546
|
+
* // Case 2:
|
|
547
|
+
* assertIsArray(new URLSearchParams, {
|
|
548
|
+
* formatCase: "toPascalCase",
|
|
549
|
+
* message: ({ currentType, validType }) => {
|
|
550
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
551
|
+
* },
|
|
552
|
+
* });
|
|
553
|
+
* // ➔ TypeError: "Expected array but got (UrlSearchParams)."
|
|
554
|
+
* assertIsArray(new URLSearchParams, {
|
|
555
|
+
* useAcronyms: true,
|
|
556
|
+
* formatCase: "toPascalCase",
|
|
557
|
+
* message: ({ currentType, validType }) => {
|
|
558
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
559
|
+
* },
|
|
560
|
+
* });
|
|
561
|
+
* // ➔ TypeError: "Expected array but got (URLSearchParams)."
|
|
396
562
|
* ```
|
|
397
563
|
* -------------------------------------------------------
|
|
398
|
-
* ✅ ***Real-world usage
|
|
564
|
+
* ✅ ***Real-world usage example***:
|
|
399
565
|
* ```ts
|
|
400
566
|
* const mixedValue: string | number[] | undefined = getUserInput();
|
|
401
567
|
*
|
|
402
|
-
* //
|
|
403
|
-
* assertIsArray(mixedValue, {
|
|
568
|
+
* // Runtime assertion: throws if `mixedValue` is not a `number[]`
|
|
569
|
+
* assertIsArray(mixedValue, {
|
|
570
|
+
* errorType: "RangeError",
|
|
571
|
+
* message: "Must be array!"
|
|
572
|
+
* });
|
|
404
573
|
*
|
|
405
|
-
* // ✅
|
|
406
|
-
* const result: number[] = mixedValue; // ➔ Safe
|
|
407
|
-
* console.log(result.
|
|
574
|
+
* // ✅ If no error thrown, TypeScript narrows `mixedValue` to `number[]` here
|
|
575
|
+
* const result: number[] = mixedValue; // ➔ Safe type assignment
|
|
576
|
+
* console.log(result.push(1, 2, 3)); // ➔ Safe to use array methods
|
|
408
577
|
* ```
|
|
409
578
|
*/
|
|
410
|
-
declare function assertIsArray<T extends unknown[]>(value:T,options?:OptionsAssertIs):
|
|
579
|
+
declare function assertIsArray<T extends unknown[]>(value: T, options?: OptionsAssertIs): asserts value is Extract<T, unknown[]>;
|
|
580
|
+
declare function assertIsArray<T extends readonly unknown[]>(value: T, options?: OptionsAssertIs): asserts value is Extract<T, readonly unknown[]>;
|
|
581
|
+
declare function assertIsArray(value: unknown, options?: OptionsAssertIs): asserts value is unknown[];
|
|
582
|
+
|
|
411
583
|
/** -------------------------------------------------------
|
|
412
584
|
* * ***Type guard assertion: `assertIsPlainObject`.***
|
|
413
585
|
* -------------------------------------------------------
|
|
@@ -419,6 +591,20 @@ declare function assertIsArray<T extends unknown[]>(value:T,options?:OptionsAsse
|
|
|
419
591
|
* - ❌ If `value` is not a `plain-object` ➔ throws a built-in error with either:
|
|
420
592
|
* - A custom error message (`options.message`), or
|
|
421
593
|
* - A default message including the actual type.
|
|
594
|
+
* - **A valid `plain object` is:**
|
|
595
|
+
* - Created by the `Object` constructor, or
|
|
596
|
+
* - Has a `[[Prototype]]` of `null` (e.g. `Object.create(null)`).
|
|
597
|
+
* - **✅ Returns `undefined` (valid) for:**
|
|
598
|
+
* - Empty object literals: `{}`
|
|
599
|
+
* - Objects with null prototype: `Object.create(null)`
|
|
600
|
+
* - **❌ Returns `throws` for:**
|
|
601
|
+
* - Arrays (`[]`, `new Array()`)
|
|
602
|
+
* - Functions (regular, arrow, or class constructors)
|
|
603
|
+
* - Built-in objects: `Date`, `RegExp`, `Error`, `Map`, `Set`, `WeakMap`, `WeakSet`
|
|
604
|
+
* - Boxed primitives: `new String()`, `new Number()`, `new Boolean()`
|
|
605
|
+
* - `null` or `undefined`
|
|
606
|
+
* - Symbols
|
|
607
|
+
* - Class instances
|
|
422
608
|
* - **⚠️ Error type selection (`options.errorType`):**
|
|
423
609
|
* - You can override the type of error thrown when validation fails.
|
|
424
610
|
* - Must be one of the standard JavaScript built-in errors:
|
|
@@ -434,35 +620,72 @@ declare function assertIsArray<T extends unknown[]>(value:T,options?:OptionsAsse
|
|
|
434
620
|
* @param {*} value - ***The value to validate.***
|
|
435
621
|
* @param {OptionsAssertIs} [options]
|
|
436
622
|
* ***Optional configuration:***
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
623
|
+
* - `message`: A custom error message (`string` or `function`).
|
|
624
|
+
* - `errorType`: Built-in JavaScript error type to throw on failure (default `"TypeError"`).
|
|
625
|
+
* - `formatCase`: Controls how detected type names are formatted case in error messages.
|
|
626
|
+
* - `useAcronyms`: Control uppercase preservation of recognized acronyms during formatting.
|
|
440
627
|
* @returns {boolean} Narrows `value` to a `plain-object` **(generic support)** if no error is thrown.
|
|
441
628
|
* @throws [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) | [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) | [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) | [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) | [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) if `value` is not a plain-object.
|
|
442
629
|
* @example
|
|
443
630
|
* ```ts
|
|
444
631
|
* // ✅ Simple usage
|
|
445
632
|
* assertIsPlainObject({ a: 1, b: 2 });
|
|
446
|
-
* //
|
|
633
|
+
* // No error, value is plain-object
|
|
634
|
+
*
|
|
635
|
+
* // ❌ Throws TypeError (default behavior)
|
|
636
|
+
* // Case 1: Invalid input type — received a string instead of a plain-object
|
|
637
|
+
* assertIsPlainObject("42");
|
|
638
|
+
* // ➔ TypeError: "Parameter input (`value`) must be of type `plain-object`, but received: `string`."
|
|
447
639
|
*
|
|
448
|
-
* // ❌ Throws
|
|
449
|
-
* assertIsPlainObject(
|
|
450
|
-
* // ➔
|
|
640
|
+
* // ❌ Throws custom error type (e.g., RangeError)
|
|
641
|
+
* assertIsPlainObject(async function () {}, { errorType: "RangeError" });
|
|
642
|
+
* // ➔ RangeError: "Parameter input (`value`) must be of type `plain-object`, but received: `async-function`."
|
|
451
643
|
*
|
|
452
|
-
* // ❌ Throws with custom string message
|
|
453
|
-
* assertIsPlainObject("
|
|
454
|
-
* // ➔ TypeError: "Must be plain
|
|
644
|
+
* // ❌ Throws a TypeError with a custom string static message
|
|
645
|
+
* assertIsPlainObject("123", { message: "Must be a plain-object!" });
|
|
646
|
+
* // ➔ TypeError: "Must be a plain-object!"
|
|
455
647
|
*
|
|
456
|
-
* // ❌ Throws
|
|
457
|
-
* assertIsPlainObject(
|
|
458
|
-
*
|
|
648
|
+
* // ❌ Throws a TypeError with a custom message function and formatCase
|
|
649
|
+
* assertIsPlainObject(/regex/, {
|
|
650
|
+
* message: ({ currentType, validType }) => {
|
|
651
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
652
|
+
* },
|
|
653
|
+
* formatCase: "toPascalCaseSpace"
|
|
654
|
+
* });
|
|
655
|
+
* // ➔ TypeError: "Expected plain-object but got (Reg Exp)."
|
|
459
656
|
*
|
|
460
|
-
* // ❌ Throws
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
657
|
+
* // ❌ Throws a TypeError with a custom useAcronyms option
|
|
658
|
+
* // Case 1:
|
|
659
|
+
* assertIsPlainObject(new URL("https://example.com"),{
|
|
660
|
+
* message: ({ currentType, validType }) => {
|
|
661
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
662
|
+
* },
|
|
663
|
+
* });
|
|
664
|
+
* // ➔ TypeError: "Expected plain-object but got (url)."
|
|
665
|
+
* assertIsPlainObject(new URL("https://example.com"), {
|
|
666
|
+
* useAcronyms: true,
|
|
667
|
+
* message: ({ currentType, validType }) => {
|
|
668
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
669
|
+
* },
|
|
464
670
|
* });
|
|
465
|
-
* // ➔ TypeError: "Expected plain-object but got (
|
|
671
|
+
* // ➔ TypeError: "Expected plain-object but got (URL)."
|
|
672
|
+
*
|
|
673
|
+
* // Case 2:
|
|
674
|
+
* assertIsPlainObject(new URLSearchParams, {
|
|
675
|
+
* formatCase: "toPascalCase",
|
|
676
|
+
* message: ({ currentType, validType }) => {
|
|
677
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
678
|
+
* },
|
|
679
|
+
* });
|
|
680
|
+
* // ➔ TypeError: "Expected plain-object but got (UrlSearchParams)."
|
|
681
|
+
* assertIsPlainObject(new URLSearchParams, {
|
|
682
|
+
* useAcronyms: true,
|
|
683
|
+
* formatCase: "toPascalCase",
|
|
684
|
+
* message: ({ currentType, validType }) => {
|
|
685
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
686
|
+
* },
|
|
687
|
+
* });
|
|
688
|
+
* // ➔ TypeError: "Expected plain-object but got (URLSearchParams)."
|
|
466
689
|
* ```
|
|
467
690
|
* -------------------------------------------------------
|
|
468
691
|
* ✅ ***Real-world usage with generic narrowing***:
|
|
@@ -470,15 +693,19 @@ declare function assertIsArray<T extends unknown[]>(value:T,options?:OptionsAsse
|
|
|
470
693
|
* type User = { name: string; email: string };
|
|
471
694
|
* const mixedValue: string | User | boolean | number | undefined = getUserInput();
|
|
472
695
|
*
|
|
473
|
-
* //
|
|
474
|
-
* assertIsPlainObject(mixedValue, {
|
|
696
|
+
* // Runtime assertion: throws if `mixedValue` is not a `plain-object`
|
|
697
|
+
* assertIsPlainObject(mixedValue, {
|
|
698
|
+
* errorType: "RangeError",
|
|
699
|
+
* message: "Must be plain object!"
|
|
700
|
+
* });
|
|
475
701
|
*
|
|
476
|
-
* // ✅
|
|
477
|
-
* const user: User = mixedValue; // ➔
|
|
478
|
-
* console.log(user.email); // ➔
|
|
702
|
+
* // ✅ If no error thrown, TypeScript narrows `mixedValue` to `User` here
|
|
703
|
+
* const user: User = mixedValue; // ➔ Safe type assignment
|
|
704
|
+
* console.log(user.email); // ➔ Safe to access object properties
|
|
479
705
|
* ```
|
|
480
706
|
*/
|
|
481
|
-
declare function assertIsPlainObject<T>(value:T,options?:OptionsAssertIs):asserts value is IsPlainObjectResult<T>;
|
|
707
|
+
declare function assertIsPlainObject<T>(value: T, options?: OptionsAssertIs): asserts value is IsPlainObjectResult<T>;
|
|
708
|
+
|
|
482
709
|
/** -------------------------------------------------------
|
|
483
710
|
* * ***Type guard assertion: `assertIsString`.***
|
|
484
711
|
* -------------------------------------------------------
|
|
@@ -507,48 +734,93 @@ declare function assertIsPlainObject<T>(value:T,options?:OptionsAssertIs):assert
|
|
|
507
734
|
* @param {*} value - ***The value to validate.***
|
|
508
735
|
* @param {OptionsAssertIs} [options]
|
|
509
736
|
* ***Optional configuration:***
|
|
510
|
-
*
|
|
511
|
-
*
|
|
512
|
-
*
|
|
737
|
+
* - `message`: A custom error message (`string` or `function`).
|
|
738
|
+
* - `errorType`: Built-in JavaScript error type to throw on failure (default `"TypeError"`).
|
|
739
|
+
* - `formatCase`: Controls how detected type names are formatted case in error messages.
|
|
740
|
+
* - `useAcronyms`: Control uppercase preservation of recognized acronyms during formatting.
|
|
513
741
|
* @returns {boolean} Narrows `value` to `string` if no error is thrown.
|
|
514
742
|
* @throws [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) | [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) | [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) | [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) | [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) if the value is not a primitive string.
|
|
515
743
|
* @example
|
|
516
744
|
* ```ts
|
|
517
745
|
* // ✅ Simple usage
|
|
518
746
|
* assertIsString("hello");
|
|
519
|
-
* //
|
|
747
|
+
* // No error, value is string
|
|
520
748
|
*
|
|
521
|
-
* // ❌ Throws TypeError
|
|
749
|
+
* // ❌ Throws TypeError (default behavior)
|
|
750
|
+
* // Case 1: Invalid input type — received a string instead of a string
|
|
522
751
|
* assertIsString(42);
|
|
523
752
|
* // ➔ TypeError: "Parameter input (`value`) must be of type `string`, but received: `number`."
|
|
524
753
|
*
|
|
525
|
-
* //
|
|
526
|
-
* assertIsString(
|
|
754
|
+
* // Case 3: The new String() is a String object (constructor), not a primitive string
|
|
755
|
+
* assertIsString(new String("abc"));
|
|
756
|
+
* // ➔ TypeError: "Parameter input (`value`) must be of type `string`, but received: `string-constructor`."
|
|
757
|
+
*
|
|
758
|
+
* // ❌ Throws custom error type (e.g., RangeError)
|
|
759
|
+
* assertIsString(async function () {}, { errorType: "RangeError" });
|
|
760
|
+
* // ➔ RangeError: "Parameter input (`value`) must be of type `string`, but received: `async-function`."
|
|
761
|
+
*
|
|
762
|
+
* // ❌ Throws a TypeError with a custom string static message
|
|
763
|
+
* assertIsString(123, { message: "Must be a string!" });
|
|
527
764
|
* // ➔ TypeError: "Must be a string!"
|
|
528
765
|
*
|
|
529
|
-
* // ❌ Throws
|
|
530
|
-
* assertIsString(
|
|
531
|
-
*
|
|
766
|
+
* // ❌ Throws a TypeError with a custom message function and formatCase
|
|
767
|
+
* assertIsString(/regex/, {
|
|
768
|
+
* message: ({ currentType, validType }) => {
|
|
769
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
770
|
+
* },
|
|
771
|
+
* formatCase: "toPascalCaseSpace"
|
|
772
|
+
* });
|
|
773
|
+
* // ➔ TypeError: "Expected string but got (Reg Exp)."
|
|
532
774
|
*
|
|
533
|
-
* // ❌ Throws
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
775
|
+
* // ❌ Throws a TypeError with a custom useAcronyms option
|
|
776
|
+
* // Case 1:
|
|
777
|
+
* assertIsString(new URL("https://example.com"),{
|
|
778
|
+
* message: ({ currentType, validType }) => {
|
|
779
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
780
|
+
* },
|
|
781
|
+
* });
|
|
782
|
+
* // ➔ TypeError: "Expected string but got (url)."
|
|
783
|
+
* assertIsString(new URL("https://example.com"), {
|
|
784
|
+
* useAcronyms: true,
|
|
785
|
+
* message: ({ currentType, validType }) => {
|
|
786
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
787
|
+
* },
|
|
537
788
|
* });
|
|
538
|
-
* // ➔ TypeError: "Expected string but got (
|
|
789
|
+
* // ➔ TypeError: "Expected string but got (URL)."
|
|
790
|
+
*
|
|
791
|
+
* // Case 2:
|
|
792
|
+
* assertIsString(new URLSearchParams, {
|
|
793
|
+
* formatCase: "toPascalCase",
|
|
794
|
+
* message: ({ currentType, validType }) => {
|
|
795
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
796
|
+
* },
|
|
797
|
+
* });
|
|
798
|
+
* // ➔ TypeError: "Expected string but got (UrlSearchParams)."
|
|
799
|
+
* assertIsString(new URLSearchParams, {
|
|
800
|
+
* useAcronyms: true,
|
|
801
|
+
* formatCase: "toPascalCase",
|
|
802
|
+
* message: ({ currentType, validType }) => {
|
|
803
|
+
* return `Expected ${validType} but got (${currentType}).`
|
|
804
|
+
* },
|
|
805
|
+
* });
|
|
806
|
+
* // ➔ TypeError: "Expected string but got (URLSearchParams)."
|
|
539
807
|
* ```
|
|
540
808
|
* -------------------------------------------------------
|
|
541
809
|
* ✅ ***Real-world usage with generic narrowing***:
|
|
542
810
|
* ```ts
|
|
543
|
-
*
|
|
544
|
-
* const mixedValue: string | User | undefined = getUserInput();
|
|
811
|
+
* const mixedValue: string | boolean | undefined = getUserInput();
|
|
545
812
|
*
|
|
546
|
-
* //
|
|
547
|
-
* assertIsString(mixedValue, {
|
|
813
|
+
* // Runtime assertion: throws if `mixedValue` is not a `string`
|
|
814
|
+
* assertIsString(mixedValue, {
|
|
815
|
+
* errorType: "RangeError",
|
|
816
|
+
* message: "Must be a string!"
|
|
817
|
+
* });
|
|
548
818
|
*
|
|
549
|
-
* // ✅
|
|
550
|
-
* const result: string = mixedValue; // ➔
|
|
551
|
-
* console.log(result.toUpperCase()); // ➔
|
|
819
|
+
* // ✅ If no error thrown, TypeScript narrows `mixedValue` to `string` here
|
|
820
|
+
* const result: string = mixedValue; // ➔ Safe type assignment
|
|
821
|
+
* console.log(result.toUpperCase()); // ➔ Safe to call String.prototype methods
|
|
552
822
|
* ```
|
|
553
823
|
*/
|
|
554
|
-
declare const assertIsString:(value:unknown,options?:OptionsAssertIs)=>asserts value is string;
|
|
824
|
+
declare const assertIsString: (value: unknown, options?: OptionsAssertIs) => asserts value is string;
|
|
825
|
+
|
|
826
|
+
export { assertIsArray, assertIsBigInt, assertIsBoolean, assertIsNumber, assertIsPlainObject, assertIsString };
|