yummies 7.14.0 → 7.14.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/assert.cjs +48 -6
- package/assert.cjs.map +1 -1
- package/assert.d.ts +40 -3
- package/assert.js +48 -6
- package/assert.js.map +1 -1
- package/package.json +1 -1
- package/type-guard.cjs +38 -0
- package/type-guard.cjs.map +1 -1
- package/type-guard.d.ts +40 -0
- package/type-guard.js +38 -0
- package/type-guard.js.map +1 -1
package/assert.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_chunk = require("./chunk-CVq3Gv4J.cjs");
|
|
3
|
+
let yummies_type_guard = require("yummies/type-guard");
|
|
3
4
|
let yummies_common = require("yummies/common");
|
|
4
5
|
//#region src/assert/_exports.ts
|
|
5
6
|
/**
|
|
@@ -10,8 +11,8 @@ let yummies_common = require("yummies/common");
|
|
|
10
11
|
*
|
|
11
12
|
* Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw
|
|
12
13
|
* {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays
|
|
13
|
-
* explicit without a heavy assertion library. Use `assert.ok`, `assert.
|
|
14
|
-
* the rest on the namespace export from the package entry.
|
|
14
|
+
* explicit without a heavy assertion library. Use `assert.ok`, `assert.string`, `assert.defined`,
|
|
15
|
+
* `assert.fail`, and the rest on the namespace export from the package entry.
|
|
15
16
|
*
|
|
16
17
|
* ## Usage
|
|
17
18
|
*
|
|
@@ -20,10 +21,12 @@ let yummies_common = require("yummies/common");
|
|
|
20
21
|
*
|
|
21
22
|
* assert.ok(user.id, "user id is required");
|
|
22
23
|
* assert.defined(maybeName);
|
|
24
|
+
* assert.string(raw);
|
|
23
25
|
* ```
|
|
24
26
|
*/
|
|
25
27
|
var _exports_exports = /* @__PURE__ */ require_chunk.__exportAll({
|
|
26
28
|
AssertionError: () => AssertionError,
|
|
29
|
+
boolean: () => boolean,
|
|
27
30
|
defined: () => defined,
|
|
28
31
|
fail: () => fail,
|
|
29
32
|
instanceOf: () => instanceOf,
|
|
@@ -31,7 +34,10 @@ var _exports_exports = /* @__PURE__ */ require_chunk.__exportAll({
|
|
|
31
34
|
never: () => unreachable,
|
|
32
35
|
notNull: () => notNull,
|
|
33
36
|
notUndefined: () => notUndefined,
|
|
37
|
+
number: () => number,
|
|
34
38
|
ok: () => ok,
|
|
39
|
+
string: () => string,
|
|
40
|
+
symbol: () => symbol,
|
|
35
41
|
unreachable: () => unreachable
|
|
36
42
|
});
|
|
37
43
|
/**
|
|
@@ -56,7 +62,7 @@ function resolveAssertMessage(message, fallback) {
|
|
|
56
62
|
* ```
|
|
57
63
|
*/
|
|
58
64
|
function ok(condition, message) {
|
|
59
|
-
if (
|
|
65
|
+
if (yummies_type_guard.typeGuard.isFalsy(condition)) throw new AssertionError(resolveAssertMessage(message, "Assertion failed"));
|
|
60
66
|
}
|
|
61
67
|
/**
|
|
62
68
|
* Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).
|
|
@@ -74,7 +80,7 @@ var invariant = ok;
|
|
|
74
80
|
* ```
|
|
75
81
|
*/
|
|
76
82
|
function defined(value, message) {
|
|
77
|
-
if (value
|
|
83
|
+
if (!yummies_type_guard.typeGuard.isDefined(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
78
84
|
}
|
|
79
85
|
/**
|
|
80
86
|
* Throws when the value is `null`; allows `undefined` unless combined with other checks.
|
|
@@ -83,7 +89,7 @@ function defined(value, message) {
|
|
|
83
89
|
* @param message Optional message or lazy message factory.
|
|
84
90
|
*/
|
|
85
91
|
function notNull(value, message) {
|
|
86
|
-
if (value
|
|
92
|
+
if (yummies_type_guard.typeGuard.isNull(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a non-null value"));
|
|
87
93
|
}
|
|
88
94
|
/**
|
|
89
95
|
* Throws when the value is `undefined`.
|
|
@@ -92,7 +98,43 @@ function notNull(value, message) {
|
|
|
92
98
|
* @param message Optional message or lazy message factory.
|
|
93
99
|
*/
|
|
94
100
|
function notUndefined(value, message) {
|
|
95
|
-
if (value
|
|
101
|
+
if (yummies_type_guard.typeGuard.isUndefined(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Throws when `value` is not a string (primitive or `String` object); uses `typeGuard.isString`.
|
|
105
|
+
*
|
|
106
|
+
* @param value Value to narrow.
|
|
107
|
+
* @param message Optional message or lazy message factory.
|
|
108
|
+
*/
|
|
109
|
+
function string(value, message) {
|
|
110
|
+
if (!yummies_type_guard.typeGuard.isString(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a string"));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Throws when `value` is not a finite non-NaN number; uses `typeGuard.isNumber`.
|
|
114
|
+
*
|
|
115
|
+
* @param value Value to narrow.
|
|
116
|
+
* @param message Optional message or lazy message factory.
|
|
117
|
+
*/
|
|
118
|
+
function number(value, message) {
|
|
119
|
+
if (!yummies_type_guard.typeGuard.isNumber(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a finite number"));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Throws when `value` is not a boolean; uses `typeGuard.isBoolean`.
|
|
123
|
+
*
|
|
124
|
+
* @param value Value to narrow.
|
|
125
|
+
* @param message Optional message or lazy message factory.
|
|
126
|
+
*/
|
|
127
|
+
function boolean(value, message) {
|
|
128
|
+
if (!yummies_type_guard.typeGuard.isBoolean(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a boolean"));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Throws when `value` is not a symbol; uses `typeGuard.isSymbol`.
|
|
132
|
+
*
|
|
133
|
+
* @param value Value to narrow.
|
|
134
|
+
* @param message Optional message or lazy message factory.
|
|
135
|
+
*/
|
|
136
|
+
function symbol(value, message) {
|
|
137
|
+
if (!yummies_type_guard.typeGuard.isSymbol(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a symbol"));
|
|
96
138
|
}
|
|
97
139
|
/**
|
|
98
140
|
* Always throws; use for branches that must be impossible or as a typed “abort”.
|
package/assert.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.cjs","names":[],"sources":["../src/assert/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/assert\n *\n * ## Description\n *\n * Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw\n * {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays\n * explicit without a heavy assertion library. Use `assert.ok`, `assert.
|
|
1
|
+
{"version":3,"file":"assert.cjs","names":[],"sources":["../src/assert/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/assert\n *\n * ## Description\n *\n * Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw\n * {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays\n * explicit without a heavy assertion library. Use `assert.ok`, `assert.string`, `assert.defined`,\n * `assert.fail`, and the rest on the namespace export from the package entry.\n *\n * ## Usage\n *\n * ```ts\n * import { assert } from \"yummies/assert\";\n *\n * assert.ok(user.id, \"user id is required\");\n * assert.defined(maybeName);\n * assert.string(raw);\n * ```\n */\n\nimport { callFunction } from 'yummies/common';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe, MaybeFn } from 'yummies/types';\n\n/**\n * Error thrown by assertion helpers in this module.\n */\nexport class AssertionError extends Error {\n override name = 'AssertionError';\n}\n\nfunction resolveAssertMessage(\n message: MaybeFn<string> | undefined,\n fallback: string,\n): string {\n if (message === undefined) {\n return fallback;\n }\n return callFunction(message);\n}\n\n/**\n * Throws when `condition` is falsy; narrows the type when it is truthy.\n *\n * @param condition Value treated as a boolean guard.\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * assert.ok(user.id, \"user id is required\");\n * ```\n */\nexport function ok(\n condition: unknown,\n message?: MaybeFn<string>,\n): asserts condition {\n if (typeGuard.isFalsy(condition)) {\n throw new AssertionError(resolveAssertMessage(message, 'Assertion failed'));\n }\n}\n\n/**\n * Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).\n */\nexport const invariant: typeof ok = ok;\n\n/**\n * Throws when the value is `null` or `undefined`; otherwise narrows away nullish.\n *\n * @param value Possibly nullish value.\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * assert.defined(maybeName, \"name must be present\");\n * ```\n */\nexport function defined<T>(\n value: Maybe<T>,\n message?: MaybeFn<string>,\n): asserts value is NonNullable<T> {\n if (!typeGuard.isDefined(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a defined value'),\n );\n }\n}\n\n/**\n * Throws when the value is `null`; allows `undefined` unless combined with other checks.\n *\n * @param value Value that may be `null`.\n * @param message Optional message or lazy message factory.\n */\nexport function notNull<T>(\n value: T | null,\n message?: MaybeFn<string>,\n): asserts value is T {\n if (typeGuard.isNull(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a non-null value'),\n );\n }\n}\n\n/**\n * Throws when the value is `undefined`.\n *\n * @param value Value that may be `undefined`.\n * @param message Optional message or lazy message factory.\n */\nexport function notUndefined<T>(\n value: T | undefined,\n message?: MaybeFn<string>,\n): asserts value is T {\n if (typeGuard.isUndefined(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a defined value'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a string (primitive or `String` object); uses `typeGuard.isString`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function string(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is string {\n if (!typeGuard.isString(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a string'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a finite non-NaN number; uses `typeGuard.isNumber`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function number(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is number {\n if (!typeGuard.isNumber(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a finite number'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a boolean; uses `typeGuard.isBoolean`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function boolean(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is boolean {\n if (!typeGuard.isBoolean(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a boolean'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a symbol; uses `typeGuard.isSymbol`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function symbol(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is symbol {\n if (!typeGuard.isSymbol(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a symbol'),\n );\n }\n}\n\n/**\n * Always throws; use for branches that must be impossible or as a typed “abort”.\n *\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * function parse(kind: \"a\" | \"b\") {\n * if (kind === \"a\") return 1;\n * if (kind === \"b\") return 2;\n * assert.fail(\"unreachable\");\n * }\n * ```\n */\nexport function fail(message?: MaybeFn<string>): never {\n throw new AssertionError(resolveAssertMessage(message, 'Unreachable'));\n}\n\n/**\n * Exhaustiveness helper: call with a `never` value when all union cases are handled.\n *\n * @param value Should be `never` when the type checker is satisfied.\n * @param message Optional message (this path always throws, so a lazy factory is unnecessary).\n */\nexport function unreachable(value: never, message?: string): never {\n throw new AssertionError(\n resolveAssertMessage(message, `Unexpected value: ${String(value)}`),\n );\n}\n\n/**\n * Alias for {@link unreachable} (common name in switch exhaustiveness snippets).\n */\nexport { unreachable as never };\n\n/**\n * Throws when `value` is not an instance of `ctor`.\n *\n * @param value Value to check.\n * @param ctor Constructor used with `instanceof`.\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * assert.instanceOf(el, HTMLElement, \"expected a DOM element\");\n * ```\n */\nexport function instanceOf<T>(\n value: unknown,\n ctor: abstract new (...args: never[]) => T,\n message?: MaybeFn<string>,\n): asserts value is T {\n if (!(value instanceof ctor)) {\n throw new AssertionError(\n resolveAssertMessage(message, `Expected instance of ${ctor.name}`),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,IAAa,iBAAb,cAAoC,MAAM;CACxC,OAAgB;;AAGlB,SAAS,qBACP,SACA,UACQ;AACR,KAAI,YAAY,KAAA,EACd,QAAO;AAET,SAAA,GAAA,eAAA,cAAoB,QAAQ;;;;;;;;;;;;;AAc9B,SAAgB,GACd,WACA,SACmB;AACnB,KAAI,mBAAA,UAAU,QAAQ,UAAU,CAC9B,OAAM,IAAI,eAAe,qBAAqB,SAAS,mBAAmB,CAAC;;;;;AAO/E,IAAa,YAAuB;;;;;;;;;;;;AAapC,SAAgB,QACd,OACA,SACiC;AACjC,KAAI,CAAC,mBAAA,UAAU,UAAU,MAAM,CAC7B,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,QACd,OACA,SACoB;AACpB,KAAI,mBAAA,UAAU,OAAO,MAAM,CACzB,OAAM,IAAI,eACR,qBAAqB,SAAS,4BAA4B,CAC3D;;;;;;;;AAUL,SAAgB,aACd,OACA,SACoB;AACpB,KAAI,mBAAA,UAAU,YAAY,MAAM,CAC9B,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,OACd,OACA,SACyB;AACzB,KAAI,CAAC,mBAAA,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,eACR,qBAAqB,SAAS,oBAAoB,CACnD;;;;;;;;AAUL,SAAgB,OACd,OACA,SACyB;AACzB,KAAI,CAAC,mBAAA,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,QACd,OACA,SAC0B;AAC1B,KAAI,CAAC,mBAAA,UAAU,UAAU,MAAM,CAC7B,OAAM,IAAI,eACR,qBAAqB,SAAS,qBAAqB,CACpD;;;;;;;;AAUL,SAAgB,OACd,OACA,SACyB;AACzB,KAAI,CAAC,mBAAA,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,eACR,qBAAqB,SAAS,oBAAoB,CACnD;;;;;;;;;;;;;;;;AAkBL,SAAgB,KAAK,SAAkC;AACrD,OAAM,IAAI,eAAe,qBAAqB,SAAS,cAAc,CAAC;;;;;;;;AASxE,SAAgB,YAAY,OAAc,SAAyB;AACjE,OAAM,IAAI,eACR,qBAAqB,SAAS,qBAAqB,OAAO,MAAM,GAAG,CACpE;;;;;;;;;;;;;;AAoBH,SAAgB,WACd,OACA,MACA,SACoB;AACpB,KAAI,EAAE,iBAAiB,MACrB,OAAM,IAAI,eACR,qBAAqB,SAAS,wBAAwB,KAAK,OAAO,CACnE"}
|
package/assert.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MaybeFn, Maybe } from 'yummies/types';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* ---header-docs-section---
|
|
@@ -8,8 +8,8 @@ import { Maybe, MaybeFn } from 'yummies/types';
|
|
|
8
8
|
*
|
|
9
9
|
* Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw
|
|
10
10
|
* {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays
|
|
11
|
-
* explicit without a heavy assertion library. Use `assert.ok`, `assert.
|
|
12
|
-
* the rest on the namespace export from the package entry.
|
|
11
|
+
* explicit without a heavy assertion library. Use `assert.ok`, `assert.string`, `assert.defined`,
|
|
12
|
+
* `assert.fail`, and the rest on the namespace export from the package entry.
|
|
13
13
|
*
|
|
14
14
|
* ## Usage
|
|
15
15
|
*
|
|
@@ -18,6 +18,7 @@ import { Maybe, MaybeFn } from 'yummies/types';
|
|
|
18
18
|
*
|
|
19
19
|
* assert.ok(user.id, "user id is required");
|
|
20
20
|
* assert.defined(maybeName);
|
|
21
|
+
* assert.string(raw);
|
|
21
22
|
* ```
|
|
22
23
|
*/
|
|
23
24
|
|
|
@@ -69,6 +70,34 @@ declare function notNull<T>(value: T | null, message?: MaybeFn<string>): asserts
|
|
|
69
70
|
* @param message Optional message or lazy message factory.
|
|
70
71
|
*/
|
|
71
72
|
declare function notUndefined<T>(value: T | undefined, message?: MaybeFn<string>): asserts value is T;
|
|
73
|
+
/**
|
|
74
|
+
* Throws when `value` is not a string (primitive or `String` object); uses `typeGuard.isString`.
|
|
75
|
+
*
|
|
76
|
+
* @param value Value to narrow.
|
|
77
|
+
* @param message Optional message or lazy message factory.
|
|
78
|
+
*/
|
|
79
|
+
declare function string(value: unknown, message?: MaybeFn<string>): asserts value is string;
|
|
80
|
+
/**
|
|
81
|
+
* Throws when `value` is not a finite non-NaN number; uses `typeGuard.isNumber`.
|
|
82
|
+
*
|
|
83
|
+
* @param value Value to narrow.
|
|
84
|
+
* @param message Optional message or lazy message factory.
|
|
85
|
+
*/
|
|
86
|
+
declare function number(value: unknown, message?: MaybeFn<string>): asserts value is number;
|
|
87
|
+
/**
|
|
88
|
+
* Throws when `value` is not a boolean; uses `typeGuard.isBoolean`.
|
|
89
|
+
*
|
|
90
|
+
* @param value Value to narrow.
|
|
91
|
+
* @param message Optional message or lazy message factory.
|
|
92
|
+
*/
|
|
93
|
+
declare function boolean(value: unknown, message?: MaybeFn<string>): asserts value is boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Throws when `value` is not a symbol; uses `typeGuard.isSymbol`.
|
|
96
|
+
*
|
|
97
|
+
* @param value Value to narrow.
|
|
98
|
+
* @param message Optional message or lazy message factory.
|
|
99
|
+
*/
|
|
100
|
+
declare function symbol(value: unknown, message?: MaybeFn<string>): asserts value is symbol;
|
|
72
101
|
/**
|
|
73
102
|
* Always throws; use for branches that must be impossible or as a typed “abort”.
|
|
74
103
|
*
|
|
@@ -108,17 +137,22 @@ declare function instanceOf<T>(value: unknown, ctor: abstract new (...args: neve
|
|
|
108
137
|
|
|
109
138
|
type _exports_AssertionError = AssertionError;
|
|
110
139
|
declare const _exports_AssertionError: typeof AssertionError;
|
|
140
|
+
declare const _exports_boolean: typeof boolean;
|
|
111
141
|
declare const _exports_defined: typeof defined;
|
|
112
142
|
declare const _exports_fail: typeof fail;
|
|
113
143
|
declare const _exports_instanceOf: typeof instanceOf;
|
|
114
144
|
declare const _exports_invariant: typeof invariant;
|
|
115
145
|
declare const _exports_notNull: typeof notNull;
|
|
116
146
|
declare const _exports_notUndefined: typeof notUndefined;
|
|
147
|
+
declare const _exports_number: typeof number;
|
|
117
148
|
declare const _exports_ok: typeof ok;
|
|
149
|
+
declare const _exports_string: typeof string;
|
|
150
|
+
declare const _exports_symbol: typeof symbol;
|
|
118
151
|
declare const _exports_unreachable: typeof unreachable;
|
|
119
152
|
declare namespace _exports {
|
|
120
153
|
export {
|
|
121
154
|
_exports_AssertionError as AssertionError,
|
|
155
|
+
_exports_boolean as boolean,
|
|
122
156
|
_exports_defined as defined,
|
|
123
157
|
_exports_fail as fail,
|
|
124
158
|
_exports_instanceOf as instanceOf,
|
|
@@ -126,7 +160,10 @@ declare namespace _exports {
|
|
|
126
160
|
unreachable as never,
|
|
127
161
|
_exports_notNull as notNull,
|
|
128
162
|
_exports_notUndefined as notUndefined,
|
|
163
|
+
_exports_number as number,
|
|
129
164
|
_exports_ok as ok,
|
|
165
|
+
_exports_string as string,
|
|
166
|
+
_exports_symbol as symbol,
|
|
130
167
|
_exports_unreachable as unreachable,
|
|
131
168
|
};
|
|
132
169
|
}
|
package/assert.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { n as __exportAll } from "./chunk-YKewjYmz.js";
|
|
2
|
+
import { typeGuard } from "yummies/type-guard";
|
|
2
3
|
import { callFunction } from "yummies/common";
|
|
3
4
|
//#region src/assert/_exports.ts
|
|
4
5
|
/**
|
|
@@ -9,8 +10,8 @@ import { callFunction } from "yummies/common";
|
|
|
9
10
|
*
|
|
10
11
|
* Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw
|
|
11
12
|
* {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays
|
|
12
|
-
* explicit without a heavy assertion library. Use `assert.ok`, `assert.
|
|
13
|
-
* the rest on the namespace export from the package entry.
|
|
13
|
+
* explicit without a heavy assertion library. Use `assert.ok`, `assert.string`, `assert.defined`,
|
|
14
|
+
* `assert.fail`, and the rest on the namespace export from the package entry.
|
|
14
15
|
*
|
|
15
16
|
* ## Usage
|
|
16
17
|
*
|
|
@@ -19,10 +20,12 @@ import { callFunction } from "yummies/common";
|
|
|
19
20
|
*
|
|
20
21
|
* assert.ok(user.id, "user id is required");
|
|
21
22
|
* assert.defined(maybeName);
|
|
23
|
+
* assert.string(raw);
|
|
22
24
|
* ```
|
|
23
25
|
*/
|
|
24
26
|
var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
25
27
|
AssertionError: () => AssertionError,
|
|
28
|
+
boolean: () => boolean,
|
|
26
29
|
defined: () => defined,
|
|
27
30
|
fail: () => fail,
|
|
28
31
|
instanceOf: () => instanceOf,
|
|
@@ -30,7 +33,10 @@ var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
30
33
|
never: () => unreachable,
|
|
31
34
|
notNull: () => notNull,
|
|
32
35
|
notUndefined: () => notUndefined,
|
|
36
|
+
number: () => number,
|
|
33
37
|
ok: () => ok,
|
|
38
|
+
string: () => string,
|
|
39
|
+
symbol: () => symbol,
|
|
34
40
|
unreachable: () => unreachable
|
|
35
41
|
});
|
|
36
42
|
/**
|
|
@@ -55,7 +61,7 @@ function resolveAssertMessage(message, fallback) {
|
|
|
55
61
|
* ```
|
|
56
62
|
*/
|
|
57
63
|
function ok(condition, message) {
|
|
58
|
-
if (
|
|
64
|
+
if (typeGuard.isFalsy(condition)) throw new AssertionError(resolveAssertMessage(message, "Assertion failed"));
|
|
59
65
|
}
|
|
60
66
|
/**
|
|
61
67
|
* Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).
|
|
@@ -73,7 +79,7 @@ var invariant = ok;
|
|
|
73
79
|
* ```
|
|
74
80
|
*/
|
|
75
81
|
function defined(value, message) {
|
|
76
|
-
if (value
|
|
82
|
+
if (!typeGuard.isDefined(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
77
83
|
}
|
|
78
84
|
/**
|
|
79
85
|
* Throws when the value is `null`; allows `undefined` unless combined with other checks.
|
|
@@ -82,7 +88,7 @@ function defined(value, message) {
|
|
|
82
88
|
* @param message Optional message or lazy message factory.
|
|
83
89
|
*/
|
|
84
90
|
function notNull(value, message) {
|
|
85
|
-
if (value
|
|
91
|
+
if (typeGuard.isNull(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a non-null value"));
|
|
86
92
|
}
|
|
87
93
|
/**
|
|
88
94
|
* Throws when the value is `undefined`.
|
|
@@ -91,7 +97,43 @@ function notNull(value, message) {
|
|
|
91
97
|
* @param message Optional message or lazy message factory.
|
|
92
98
|
*/
|
|
93
99
|
function notUndefined(value, message) {
|
|
94
|
-
if (value
|
|
100
|
+
if (typeGuard.isUndefined(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Throws when `value` is not a string (primitive or `String` object); uses `typeGuard.isString`.
|
|
104
|
+
*
|
|
105
|
+
* @param value Value to narrow.
|
|
106
|
+
* @param message Optional message or lazy message factory.
|
|
107
|
+
*/
|
|
108
|
+
function string(value, message) {
|
|
109
|
+
if (!typeGuard.isString(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a string"));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Throws when `value` is not a finite non-NaN number; uses `typeGuard.isNumber`.
|
|
113
|
+
*
|
|
114
|
+
* @param value Value to narrow.
|
|
115
|
+
* @param message Optional message or lazy message factory.
|
|
116
|
+
*/
|
|
117
|
+
function number(value, message) {
|
|
118
|
+
if (!typeGuard.isNumber(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a finite number"));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Throws when `value` is not a boolean; uses `typeGuard.isBoolean`.
|
|
122
|
+
*
|
|
123
|
+
* @param value Value to narrow.
|
|
124
|
+
* @param message Optional message or lazy message factory.
|
|
125
|
+
*/
|
|
126
|
+
function boolean(value, message) {
|
|
127
|
+
if (!typeGuard.isBoolean(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a boolean"));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Throws when `value` is not a symbol; uses `typeGuard.isSymbol`.
|
|
131
|
+
*
|
|
132
|
+
* @param value Value to narrow.
|
|
133
|
+
* @param message Optional message or lazy message factory.
|
|
134
|
+
*/
|
|
135
|
+
function symbol(value, message) {
|
|
136
|
+
if (!typeGuard.isSymbol(value)) throw new AssertionError(resolveAssertMessage(message, "Expected a symbol"));
|
|
95
137
|
}
|
|
96
138
|
/**
|
|
97
139
|
* Always throws; use for branches that must be impossible or as a typed “abort”.
|
package/assert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.js","names":[],"sources":["../src/assert/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/assert\n *\n * ## Description\n *\n * Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw\n * {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays\n * explicit without a heavy assertion library. Use `assert.ok`, `assert.
|
|
1
|
+
{"version":3,"file":"assert.js","names":[],"sources":["../src/assert/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/assert\n *\n * ## Description\n *\n * Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw\n * {@link AssertionError} with an optional message (`string` or {@link MaybeFn}) so failing fast stays\n * explicit without a heavy assertion library. Use `assert.ok`, `assert.string`, `assert.defined`,\n * `assert.fail`, and the rest on the namespace export from the package entry.\n *\n * ## Usage\n *\n * ```ts\n * import { assert } from \"yummies/assert\";\n *\n * assert.ok(user.id, \"user id is required\");\n * assert.defined(maybeName);\n * assert.string(raw);\n * ```\n */\n\nimport { callFunction } from 'yummies/common';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe, MaybeFn } from 'yummies/types';\n\n/**\n * Error thrown by assertion helpers in this module.\n */\nexport class AssertionError extends Error {\n override name = 'AssertionError';\n}\n\nfunction resolveAssertMessage(\n message: MaybeFn<string> | undefined,\n fallback: string,\n): string {\n if (message === undefined) {\n return fallback;\n }\n return callFunction(message);\n}\n\n/**\n * Throws when `condition` is falsy; narrows the type when it is truthy.\n *\n * @param condition Value treated as a boolean guard.\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * assert.ok(user.id, \"user id is required\");\n * ```\n */\nexport function ok(\n condition: unknown,\n message?: MaybeFn<string>,\n): asserts condition {\n if (typeGuard.isFalsy(condition)) {\n throw new AssertionError(resolveAssertMessage(message, 'Assertion failed'));\n }\n}\n\n/**\n * Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).\n */\nexport const invariant: typeof ok = ok;\n\n/**\n * Throws when the value is `null` or `undefined`; otherwise narrows away nullish.\n *\n * @param value Possibly nullish value.\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * assert.defined(maybeName, \"name must be present\");\n * ```\n */\nexport function defined<T>(\n value: Maybe<T>,\n message?: MaybeFn<string>,\n): asserts value is NonNullable<T> {\n if (!typeGuard.isDefined(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a defined value'),\n );\n }\n}\n\n/**\n * Throws when the value is `null`; allows `undefined` unless combined with other checks.\n *\n * @param value Value that may be `null`.\n * @param message Optional message or lazy message factory.\n */\nexport function notNull<T>(\n value: T | null,\n message?: MaybeFn<string>,\n): asserts value is T {\n if (typeGuard.isNull(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a non-null value'),\n );\n }\n}\n\n/**\n * Throws when the value is `undefined`.\n *\n * @param value Value that may be `undefined`.\n * @param message Optional message or lazy message factory.\n */\nexport function notUndefined<T>(\n value: T | undefined,\n message?: MaybeFn<string>,\n): asserts value is T {\n if (typeGuard.isUndefined(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a defined value'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a string (primitive or `String` object); uses `typeGuard.isString`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function string(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is string {\n if (!typeGuard.isString(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a string'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a finite non-NaN number; uses `typeGuard.isNumber`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function number(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is number {\n if (!typeGuard.isNumber(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a finite number'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a boolean; uses `typeGuard.isBoolean`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function boolean(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is boolean {\n if (!typeGuard.isBoolean(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a boolean'),\n );\n }\n}\n\n/**\n * Throws when `value` is not a symbol; uses `typeGuard.isSymbol`.\n *\n * @param value Value to narrow.\n * @param message Optional message or lazy message factory.\n */\nexport function symbol(\n value: unknown,\n message?: MaybeFn<string>,\n): asserts value is symbol {\n if (!typeGuard.isSymbol(value)) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a symbol'),\n );\n }\n}\n\n/**\n * Always throws; use for branches that must be impossible or as a typed “abort”.\n *\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * function parse(kind: \"a\" | \"b\") {\n * if (kind === \"a\") return 1;\n * if (kind === \"b\") return 2;\n * assert.fail(\"unreachable\");\n * }\n * ```\n */\nexport function fail(message?: MaybeFn<string>): never {\n throw new AssertionError(resolveAssertMessage(message, 'Unreachable'));\n}\n\n/**\n * Exhaustiveness helper: call with a `never` value when all union cases are handled.\n *\n * @param value Should be `never` when the type checker is satisfied.\n * @param message Optional message (this path always throws, so a lazy factory is unnecessary).\n */\nexport function unreachable(value: never, message?: string): never {\n throw new AssertionError(\n resolveAssertMessage(message, `Unexpected value: ${String(value)}`),\n );\n}\n\n/**\n * Alias for {@link unreachable} (common name in switch exhaustiveness snippets).\n */\nexport { unreachable as never };\n\n/**\n * Throws when `value` is not an instance of `ctor`.\n *\n * @param value Value to check.\n * @param ctor Constructor used with `instanceof`.\n * @param message Optional message or lazy message factory.\n *\n * @example\n * ```ts\n * assert.instanceOf(el, HTMLElement, \"expected a DOM element\");\n * ```\n */\nexport function instanceOf<T>(\n value: unknown,\n ctor: abstract new (...args: never[]) => T,\n message?: MaybeFn<string>,\n): asserts value is T {\n if (!(value instanceof ctor)) {\n throw new AssertionError(\n resolveAssertMessage(message, `Expected instance of ${ctor.name}`),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,IAAa,iBAAb,cAAoC,MAAM;CACxC,OAAgB;;AAGlB,SAAS,qBACP,SACA,UACQ;AACR,KAAI,YAAY,KAAA,EACd,QAAO;AAET,QAAO,aAAa,QAAQ;;;;;;;;;;;;;AAc9B,SAAgB,GACd,WACA,SACmB;AACnB,KAAI,UAAU,QAAQ,UAAU,CAC9B,OAAM,IAAI,eAAe,qBAAqB,SAAS,mBAAmB,CAAC;;;;;AAO/E,IAAa,YAAuB;;;;;;;;;;;;AAapC,SAAgB,QACd,OACA,SACiC;AACjC,KAAI,CAAC,UAAU,UAAU,MAAM,CAC7B,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,QACd,OACA,SACoB;AACpB,KAAI,UAAU,OAAO,MAAM,CACzB,OAAM,IAAI,eACR,qBAAqB,SAAS,4BAA4B,CAC3D;;;;;;;;AAUL,SAAgB,aACd,OACA,SACoB;AACpB,KAAI,UAAU,YAAY,MAAM,CAC9B,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,OACd,OACA,SACyB;AACzB,KAAI,CAAC,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,eACR,qBAAqB,SAAS,oBAAoB,CACnD;;;;;;;;AAUL,SAAgB,OACd,OACA,SACyB;AACzB,KAAI,CAAC,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,QACd,OACA,SAC0B;AAC1B,KAAI,CAAC,UAAU,UAAU,MAAM,CAC7B,OAAM,IAAI,eACR,qBAAqB,SAAS,qBAAqB,CACpD;;;;;;;;AAUL,SAAgB,OACd,OACA,SACyB;AACzB,KAAI,CAAC,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,eACR,qBAAqB,SAAS,oBAAoB,CACnD;;;;;;;;;;;;;;;;AAkBL,SAAgB,KAAK,SAAkC;AACrD,OAAM,IAAI,eAAe,qBAAqB,SAAS,cAAc,CAAC;;;;;;;;AASxE,SAAgB,YAAY,OAAc,SAAyB;AACjE,OAAM,IAAI,eACR,qBAAqB,SAAS,qBAAqB,OAAO,MAAM,GAAG,CACpE;;;;;;;;;;;;;;AAoBH,SAAgB,WACd,OACA,MACA,SACoB;AACpB,KAAI,EAAE,iBAAiB,MACrB,OAAM,IAAI,eACR,qBAAqB,SAAS,wBAAwB,KAAK,OAAO,CACnE"}
|
package/package.json
CHANGED
package/type-guard.cjs
CHANGED
|
@@ -5,6 +5,7 @@ var _exports_exports = /* @__PURE__ */ require("./chunk-CVq3Gv4J.cjs").__exportA
|
|
|
5
5
|
isBoolean: () => isBoolean,
|
|
6
6
|
isDefined: () => isDefined,
|
|
7
7
|
isElement: () => isElement,
|
|
8
|
+
isFalsy: () => isFalsy,
|
|
8
9
|
isFunction: () => isFunction,
|
|
9
10
|
isInfinite: () => isInfinite,
|
|
10
11
|
isNaN: () => isNaN,
|
|
@@ -14,6 +15,7 @@ var _exports_exports = /* @__PURE__ */ require("./chunk-CVq3Gv4J.cjs").__exportA
|
|
|
14
15
|
isRegExp: () => isRegExp,
|
|
15
16
|
isString: () => isString,
|
|
16
17
|
isSymbol: () => isSymbol,
|
|
18
|
+
isTruthy: () => isTruthy,
|
|
17
19
|
isUndefined: () => isUndefined
|
|
18
20
|
});
|
|
19
21
|
var TYPE = {
|
|
@@ -63,6 +65,42 @@ var createTypeGuard = (...types) => (value) => types.includes(getType(value));
|
|
|
63
65
|
*/
|
|
64
66
|
var isDefined = (value) => value != null;
|
|
65
67
|
/**
|
|
68
|
+
* Checks whether a value is truthy (same as `Boolean(value)` in `if (...)`).
|
|
69
|
+
*
|
|
70
|
+
* @param value Value to test.
|
|
71
|
+
* @returns `true` when coercion to boolean is true.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* isTruthy(1); // true
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* isTruthy(0); // false
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
var isTruthy = (value) => Boolean(value);
|
|
84
|
+
/**
|
|
85
|
+
* Checks whether a value is falsy (same as `!value` / the `else` branch of `if (value)`).
|
|
86
|
+
*
|
|
87
|
+
* Includes `false`, `0`, `-0`, `NaN`, `""`, `null`, `undefined`, and `0n`.
|
|
88
|
+
*
|
|
89
|
+
* @param value Value to test.
|
|
90
|
+
* @returns `true` when coercion to boolean is false.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* isFalsy(null); // true
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* isFalsy("x"); // false
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
var isFalsy = (value) => !value;
|
|
103
|
+
/**
|
|
66
104
|
* Checks whether a value is exactly `null`.
|
|
67
105
|
*
|
|
68
106
|
* @param value Value to test.
|
package/type-guard.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-guard.cjs","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"type-guard.cjs","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is truthy (same as `Boolean(value)` in `if (...)`).\n *\n * @param value Value to test.\n * @returns `true` when coercion to boolean is true.\n *\n * @example\n * ```ts\n * isTruthy(1); // true\n * ```\n *\n * @example\n * ```ts\n * isTruthy(0); // false\n * ```\n */\nexport const isTruthy = (value: unknown): boolean => Boolean(value);\n\n/**\n * Checks whether a value is falsy (same as `!value` / the `else` branch of `if (value)`).\n *\n * Includes `false`, `0`, `-0`, `NaN`, `\"\"`, `null`, `undefined`, and `0n`.\n *\n * @param value Value to test.\n * @returns `true` when coercion to boolean is false.\n *\n * @example\n * ```ts\n * isFalsy(null); // true\n * ```\n *\n * @example\n * ```ts\n * isFalsy(\"x\"); // false\n * ```\n */\nexport const isFalsy = (value: unknown): boolean => !value;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,IAAM,OAAO;CACX,MAAM;CACN,WAAW;CACX,KAAK;CACL,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,UAAU;CACV,eAAe;CACf,QAAQ;CACR,QAAQ;CACR,UAAU;CACV,SAAS;CACV;AAID,SAAS,QAAQ,OAAsB;AACrC,KAAI,UAAU,KAAA,EACZ,QAAO,KAAK;AAEd,KAAI,UAAU,KACZ,QAAO,KAAK;AAKd,KAAI,UAAU,MAAM,aAAa,KAAK,MAAM,aAAa,GACvD,QAAO,KAAK;CAGd,MAAM,mBAAmB,OAAO,UAAU,SAAS,KAAK,MAAM;AAG9D,KAAI,qBAAqB,KAAK,QAAQ;AACpC,MAAI,OAAO,MAAM,MAAgB,CAC/B,QAAO,KAAK;AAEd,MAAI,CAAC,OAAO,SAAS,MAAgB,CACnC,QAAO,KAAK;;AAIhB,QAAO;;AAGT,IAAM,mBACA,GAAG,WACN,UACC,MAAM,SAAS,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;AAmBlC,IAAa,aAAgB,UAC3B,SAAS;;;;;;;;;;;;;;;;;AAkBX,IAAa,YAAY,UAA4B,QAAQ,MAAM;;;;;;;;;;;;;;;;;;;AAoBnE,IAAa,WAAW,UAA4B,CAAC;;;;;;;;;;;;;;;;;AAkBrD,IAAa,SAAS,gBAAsB,KAAK,KAAK;;;;;;;;;;;;;;;;;AAkBtD,IAAa,cAAc,gBAA2B,KAAK,UAAU;;;;;;;;;;;;;;;;;AAkBrE,IAAa,WAAW,gBAA2B,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB/D,IAAa,UAAU,gBAA2B,KAAK,MAAM;;;;;;;;;;;;;;;;;AAkB7D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;;;AAoB5D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAAyB,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkB/D,IAAa,aAAa,gBACxB,KAAK,UACL,KAAK,cACN;;;;;;;;;;;;;;;;;AAkBD,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAA6B,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkBnE,IAAa,QAAQ,gBAAwB,KAAK,IAAI;;;;;;;;;;;;;;;;;AAoBtD,IAAa,aAAa,gBAAwB,KAAK,SAAS;;;;;;;;;;;;;;;;;AAoBhE,IAAa,WAAW,gBAAwB,KAAK,OAAO"}
|
package/type-guard.d.ts
CHANGED
|
@@ -36,6 +36,42 @@ import { AnyFunction, AnyObject } from 'yummies/types';
|
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
38
|
declare const isDefined: <T>(value: T | undefined | null) => value is T;
|
|
39
|
+
/**
|
|
40
|
+
* Checks whether a value is truthy (same as `Boolean(value)` in `if (...)`).
|
|
41
|
+
*
|
|
42
|
+
* @param value Value to test.
|
|
43
|
+
* @returns `true` when coercion to boolean is true.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* isTruthy(1); // true
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* isTruthy(0); // false
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare const isTruthy: (value: unknown) => boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Checks whether a value is falsy (same as `!value` / the `else` branch of `if (value)`).
|
|
58
|
+
*
|
|
59
|
+
* Includes `false`, `0`, `-0`, `NaN`, `""`, `null`, `undefined`, and `0n`.
|
|
60
|
+
*
|
|
61
|
+
* @param value Value to test.
|
|
62
|
+
* @returns `true` when coercion to boolean is false.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* isFalsy(null); // true
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* isFalsy("x"); // false
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare const isFalsy: (value: unknown) => boolean;
|
|
39
75
|
/**
|
|
40
76
|
* Checks whether a value is exactly `null`.
|
|
41
77
|
*
|
|
@@ -264,6 +300,7 @@ declare const _exports_isArray: typeof isArray;
|
|
|
264
300
|
declare const _exports_isBoolean: typeof isBoolean;
|
|
265
301
|
declare const _exports_isDefined: typeof isDefined;
|
|
266
302
|
declare const _exports_isElement: typeof isElement;
|
|
303
|
+
declare const _exports_isFalsy: typeof isFalsy;
|
|
267
304
|
declare const _exports_isFunction: typeof isFunction;
|
|
268
305
|
declare const _exports_isInfinite: typeof isInfinite;
|
|
269
306
|
declare const _exports_isNaN: typeof isNaN;
|
|
@@ -273,6 +310,7 @@ declare const _exports_isObject: typeof isObject;
|
|
|
273
310
|
declare const _exports_isRegExp: typeof isRegExp;
|
|
274
311
|
declare const _exports_isString: typeof isString;
|
|
275
312
|
declare const _exports_isSymbol: typeof isSymbol;
|
|
313
|
+
declare const _exports_isTruthy: typeof isTruthy;
|
|
276
314
|
declare const _exports_isUndefined: typeof isUndefined;
|
|
277
315
|
declare namespace _exports {
|
|
278
316
|
export {
|
|
@@ -280,6 +318,7 @@ declare namespace _exports {
|
|
|
280
318
|
_exports_isBoolean as isBoolean,
|
|
281
319
|
_exports_isDefined as isDefined,
|
|
282
320
|
_exports_isElement as isElement,
|
|
321
|
+
_exports_isFalsy as isFalsy,
|
|
283
322
|
_exports_isFunction as isFunction,
|
|
284
323
|
_exports_isInfinite as isInfinite,
|
|
285
324
|
_exports_isNaN as isNaN,
|
|
@@ -289,6 +328,7 @@ declare namespace _exports {
|
|
|
289
328
|
_exports_isRegExp as isRegExp,
|
|
290
329
|
_exports_isString as isString,
|
|
291
330
|
_exports_isSymbol as isSymbol,
|
|
331
|
+
_exports_isTruthy as isTruthy,
|
|
292
332
|
_exports_isUndefined as isUndefined,
|
|
293
333
|
};
|
|
294
334
|
}
|
package/type-guard.js
CHANGED
|
@@ -5,6 +5,7 @@ var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
5
5
|
isBoolean: () => isBoolean,
|
|
6
6
|
isDefined: () => isDefined,
|
|
7
7
|
isElement: () => isElement,
|
|
8
|
+
isFalsy: () => isFalsy,
|
|
8
9
|
isFunction: () => isFunction,
|
|
9
10
|
isInfinite: () => isInfinite,
|
|
10
11
|
isNaN: () => isNaN,
|
|
@@ -14,6 +15,7 @@ var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
14
15
|
isRegExp: () => isRegExp,
|
|
15
16
|
isString: () => isString,
|
|
16
17
|
isSymbol: () => isSymbol,
|
|
18
|
+
isTruthy: () => isTruthy,
|
|
17
19
|
isUndefined: () => isUndefined
|
|
18
20
|
});
|
|
19
21
|
var TYPE = {
|
|
@@ -63,6 +65,42 @@ var createTypeGuard = (...types) => (value) => types.includes(getType(value));
|
|
|
63
65
|
*/
|
|
64
66
|
var isDefined = (value) => value != null;
|
|
65
67
|
/**
|
|
68
|
+
* Checks whether a value is truthy (same as `Boolean(value)` in `if (...)`).
|
|
69
|
+
*
|
|
70
|
+
* @param value Value to test.
|
|
71
|
+
* @returns `true` when coercion to boolean is true.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* isTruthy(1); // true
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* isTruthy(0); // false
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
var isTruthy = (value) => Boolean(value);
|
|
84
|
+
/**
|
|
85
|
+
* Checks whether a value is falsy (same as `!value` / the `else` branch of `if (value)`).
|
|
86
|
+
*
|
|
87
|
+
* Includes `false`, `0`, `-0`, `NaN`, `""`, `null`, `undefined`, and `0n`.
|
|
88
|
+
*
|
|
89
|
+
* @param value Value to test.
|
|
90
|
+
* @returns `true` when coercion to boolean is false.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* isFalsy(null); // true
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* isFalsy("x"); // false
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
var isFalsy = (value) => !value;
|
|
103
|
+
/**
|
|
66
104
|
* Checks whether a value is exactly `null`.
|
|
67
105
|
*
|
|
68
106
|
* @param value Value to test.
|
package/type-guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-guard.js","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"type-guard.js","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is truthy (same as `Boolean(value)` in `if (...)`).\n *\n * @param value Value to test.\n * @returns `true` when coercion to boolean is true.\n *\n * @example\n * ```ts\n * isTruthy(1); // true\n * ```\n *\n * @example\n * ```ts\n * isTruthy(0); // false\n * ```\n */\nexport const isTruthy = (value: unknown): boolean => Boolean(value);\n\n/**\n * Checks whether a value is falsy (same as `!value` / the `else` branch of `if (value)`).\n *\n * Includes `false`, `0`, `-0`, `NaN`, `\"\"`, `null`, `undefined`, and `0n`.\n *\n * @param value Value to test.\n * @returns `true` when coercion to boolean is false.\n *\n * @example\n * ```ts\n * isFalsy(null); // true\n * ```\n *\n * @example\n * ```ts\n * isFalsy(\"x\"); // false\n * ```\n */\nexport const isFalsy = (value: unknown): boolean => !value;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,IAAM,OAAO;CACX,MAAM;CACN,WAAW;CACX,KAAK;CACL,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,UAAU;CACV,eAAe;CACf,QAAQ;CACR,QAAQ;CACR,UAAU;CACV,SAAS;CACV;AAID,SAAS,QAAQ,OAAsB;AACrC,KAAI,UAAU,KAAA,EACZ,QAAO,KAAK;AAEd,KAAI,UAAU,KACZ,QAAO,KAAK;AAKd,KAAI,UAAU,MAAM,aAAa,KAAK,MAAM,aAAa,GACvD,QAAO,KAAK;CAGd,MAAM,mBAAmB,OAAO,UAAU,SAAS,KAAK,MAAM;AAG9D,KAAI,qBAAqB,KAAK,QAAQ;AACpC,MAAI,OAAO,MAAM,MAAgB,CAC/B,QAAO,KAAK;AAEd,MAAI,CAAC,OAAO,SAAS,MAAgB,CACnC,QAAO,KAAK;;AAIhB,QAAO;;AAGT,IAAM,mBACA,GAAG,WACN,UACC,MAAM,SAAS,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;AAmBlC,IAAa,aAAgB,UAC3B,SAAS;;;;;;;;;;;;;;;;;AAkBX,IAAa,YAAY,UAA4B,QAAQ,MAAM;;;;;;;;;;;;;;;;;;;AAoBnE,IAAa,WAAW,UAA4B,CAAC;;;;;;;;;;;;;;;;;AAkBrD,IAAa,SAAS,gBAAsB,KAAK,KAAK;;;;;;;;;;;;;;;;;AAkBtD,IAAa,cAAc,gBAA2B,KAAK,UAAU;;;;;;;;;;;;;;;;;AAkBrE,IAAa,WAAW,gBAA2B,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB/D,IAAa,UAAU,gBAA2B,KAAK,MAAM;;;;;;;;;;;;;;;;;AAkB7D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;;;AAoB5D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAAyB,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkB/D,IAAa,aAAa,gBACxB,KAAK,UACL,KAAK,cACN;;;;;;;;;;;;;;;;;AAkBD,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAA6B,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkBnE,IAAa,QAAQ,gBAAwB,KAAK,IAAI;;;;;;;;;;;;;;;;;AAoBtD,IAAa,aAAa,gBAAwB,KAAK,SAAS;;;;;;;;;;;;;;;;;AAoBhE,IAAa,WAAW,gBAAwB,KAAK,OAAO"}
|