yummies 7.13.0 → 7.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assert.cjs +146 -0
- package/assert.cjs.map +1 -0
- package/assert.d.ts +134 -0
- package/assert.js +140 -0
- package/assert.js.map +1 -0
- package/package.json +7 -1
package/assert.cjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_chunk = require("./chunk-CVq3Gv4J.cjs");
|
|
3
|
+
let yummies_common = require("yummies/common");
|
|
4
|
+
//#region src/assert/_exports.ts
|
|
5
|
+
/**
|
|
6
|
+
* ---header-docs-section---
|
|
7
|
+
* # yummies/assert
|
|
8
|
+
*
|
|
9
|
+
* ## Description
|
|
10
|
+
*
|
|
11
|
+
* Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw
|
|
12
|
+
* {@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.defined`, `assert.fail`, and
|
|
14
|
+
* the rest on the namespace export from the package entry.
|
|
15
|
+
*
|
|
16
|
+
* ## Usage
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { assert } from "yummies/assert";
|
|
20
|
+
*
|
|
21
|
+
* assert.ok(user.id, "user id is required");
|
|
22
|
+
* assert.defined(maybeName);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
var _exports_exports = /* @__PURE__ */ require_chunk.__exportAll({
|
|
26
|
+
AssertionError: () => AssertionError,
|
|
27
|
+
defined: () => defined,
|
|
28
|
+
fail: () => fail,
|
|
29
|
+
instanceOf: () => instanceOf,
|
|
30
|
+
invariant: () => invariant,
|
|
31
|
+
never: () => unreachable,
|
|
32
|
+
notNull: () => notNull,
|
|
33
|
+
notUndefined: () => notUndefined,
|
|
34
|
+
ok: () => ok,
|
|
35
|
+
unreachable: () => unreachable
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* Error thrown by assertion helpers in this module.
|
|
39
|
+
*/
|
|
40
|
+
var AssertionError = class extends Error {
|
|
41
|
+
name = "AssertionError";
|
|
42
|
+
};
|
|
43
|
+
function resolveAssertMessage(message, fallback) {
|
|
44
|
+
if (message === void 0) return fallback;
|
|
45
|
+
return (0, yummies_common.callFunction)(message);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Throws when `condition` is falsy; narrows the type when it is truthy.
|
|
49
|
+
*
|
|
50
|
+
* @param condition Value treated as a boolean guard.
|
|
51
|
+
* @param message Optional message or lazy message factory.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* assert.ok(user.id, "user id is required");
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function ok(condition, message) {
|
|
59
|
+
if (!condition) throw new AssertionError(resolveAssertMessage(message, "Assertion failed"));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).
|
|
63
|
+
*/
|
|
64
|
+
var invariant = ok;
|
|
65
|
+
/**
|
|
66
|
+
* Throws when the value is `null` or `undefined`; otherwise narrows away nullish.
|
|
67
|
+
*
|
|
68
|
+
* @param value Possibly nullish value.
|
|
69
|
+
* @param message Optional message or lazy message factory.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* assert.defined(maybeName, "name must be present");
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function defined(value, message) {
|
|
77
|
+
if (value == null) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Throws when the value is `null`; allows `undefined` unless combined with other checks.
|
|
81
|
+
*
|
|
82
|
+
* @param value Value that may be `null`.
|
|
83
|
+
* @param message Optional message or lazy message factory.
|
|
84
|
+
*/
|
|
85
|
+
function notNull(value, message) {
|
|
86
|
+
if (value === null) throw new AssertionError(resolveAssertMessage(message, "Expected a non-null value"));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Throws when the value is `undefined`.
|
|
90
|
+
*
|
|
91
|
+
* @param value Value that may be `undefined`.
|
|
92
|
+
* @param message Optional message or lazy message factory.
|
|
93
|
+
*/
|
|
94
|
+
function notUndefined(value, message) {
|
|
95
|
+
if (value === void 0) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Always throws; use for branches that must be impossible or as a typed “abort”.
|
|
99
|
+
*
|
|
100
|
+
* @param message Optional message or lazy message factory.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* function parse(kind: "a" | "b") {
|
|
105
|
+
* if (kind === "a") return 1;
|
|
106
|
+
* if (kind === "b") return 2;
|
|
107
|
+
* assert.fail("unreachable");
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
function fail(message) {
|
|
112
|
+
throw new AssertionError(resolveAssertMessage(message, "Unreachable"));
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Exhaustiveness helper: call with a `never` value when all union cases are handled.
|
|
116
|
+
*
|
|
117
|
+
* @param value Should be `never` when the type checker is satisfied.
|
|
118
|
+
* @param message Optional message (this path always throws, so a lazy factory is unnecessary).
|
|
119
|
+
*/
|
|
120
|
+
function unreachable(value, message) {
|
|
121
|
+
throw new AssertionError(resolveAssertMessage(message, `Unexpected value: ${String(value)}`));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Throws when `value` is not an instance of `ctor`.
|
|
125
|
+
*
|
|
126
|
+
* @param value Value to check.
|
|
127
|
+
* @param ctor Constructor used with `instanceof`.
|
|
128
|
+
* @param message Optional message or lazy message factory.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* assert.instanceOf(el, HTMLElement, "expected a DOM element");
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
function instanceOf(value, ctor, message) {
|
|
136
|
+
if (!(value instanceof ctor)) throw new AssertionError(resolveAssertMessage(message, `Expected instance of ${ctor.name}`));
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
Object.defineProperty(exports, "assert", {
|
|
140
|
+
enumerable: true,
|
|
141
|
+
get: function() {
|
|
142
|
+
return _exports_exports;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
//# sourceMappingURL=assert.cjs.map
|
package/assert.cjs.map
ADDED
|
@@ -0,0 +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.defined`, `assert.fail`, and\n * 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 * ```\n */\n\nimport { callFunction } from 'yummies/common';\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 (!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 (value == null) {\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 (value === null) {\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 (value === undefined) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a defined value'),\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,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,CAAC,UACH,OAAM,IAAI,eAAe,qBAAqB,SAAS,mBAAmB,CAAC;;;;;AAO/E,IAAa,YAAuB;;;;;;;;;;;;AAapC,SAAgB,QACd,OACA,SACiC;AACjC,KAAI,SAAS,KACX,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,QACd,OACA,SACoB;AACpB,KAAI,UAAU,KACZ,OAAM,IAAI,eACR,qBAAqB,SAAS,4BAA4B,CAC3D;;;;;;;;AAUL,SAAgB,aACd,OACA,SACoB;AACpB,KAAI,UAAU,KAAA,EACZ,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;;;;;;;;;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
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Maybe, MaybeFn } from 'yummies/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ---header-docs-section---
|
|
5
|
+
* # yummies/assert
|
|
6
|
+
*
|
|
7
|
+
* ## Description
|
|
8
|
+
*
|
|
9
|
+
* Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw
|
|
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.defined`, `assert.fail`, and
|
|
12
|
+
* the rest on the namespace export from the package entry.
|
|
13
|
+
*
|
|
14
|
+
* ## Usage
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { assert } from "yummies/assert";
|
|
18
|
+
*
|
|
19
|
+
* assert.ok(user.id, "user id is required");
|
|
20
|
+
* assert.defined(maybeName);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown by assertion helpers in this module.
|
|
26
|
+
*/
|
|
27
|
+
declare class AssertionError extends Error {
|
|
28
|
+
name: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Throws when `condition` is falsy; narrows the type when it is truthy.
|
|
32
|
+
*
|
|
33
|
+
* @param condition Value treated as a boolean guard.
|
|
34
|
+
* @param message Optional message or lazy message factory.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* assert.ok(user.id, "user id is required");
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function ok(condition: unknown, message?: MaybeFn<string>): asserts condition;
|
|
42
|
+
/**
|
|
43
|
+
* Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).
|
|
44
|
+
*/
|
|
45
|
+
declare const invariant: typeof ok;
|
|
46
|
+
/**
|
|
47
|
+
* Throws when the value is `null` or `undefined`; otherwise narrows away nullish.
|
|
48
|
+
*
|
|
49
|
+
* @param value Possibly nullish value.
|
|
50
|
+
* @param message Optional message or lazy message factory.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* assert.defined(maybeName, "name must be present");
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function defined<T>(value: Maybe<T>, message?: MaybeFn<string>): asserts value is NonNullable<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Throws when the value is `null`; allows `undefined` unless combined with other checks.
|
|
60
|
+
*
|
|
61
|
+
* @param value Value that may be `null`.
|
|
62
|
+
* @param message Optional message or lazy message factory.
|
|
63
|
+
*/
|
|
64
|
+
declare function notNull<T>(value: T | null, message?: MaybeFn<string>): asserts value is T;
|
|
65
|
+
/**
|
|
66
|
+
* Throws when the value is `undefined`.
|
|
67
|
+
*
|
|
68
|
+
* @param value Value that may be `undefined`.
|
|
69
|
+
* @param message Optional message or lazy message factory.
|
|
70
|
+
*/
|
|
71
|
+
declare function notUndefined<T>(value: T | undefined, message?: MaybeFn<string>): asserts value is T;
|
|
72
|
+
/**
|
|
73
|
+
* Always throws; use for branches that must be impossible or as a typed “abort”.
|
|
74
|
+
*
|
|
75
|
+
* @param message Optional message or lazy message factory.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* function parse(kind: "a" | "b") {
|
|
80
|
+
* if (kind === "a") return 1;
|
|
81
|
+
* if (kind === "b") return 2;
|
|
82
|
+
* assert.fail("unreachable");
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function fail(message?: MaybeFn<string>): never;
|
|
87
|
+
/**
|
|
88
|
+
* Exhaustiveness helper: call with a `never` value when all union cases are handled.
|
|
89
|
+
*
|
|
90
|
+
* @param value Should be `never` when the type checker is satisfied.
|
|
91
|
+
* @param message Optional message (this path always throws, so a lazy factory is unnecessary).
|
|
92
|
+
*/
|
|
93
|
+
declare function unreachable(value: never, message?: string): never;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Throws when `value` is not an instance of `ctor`.
|
|
97
|
+
*
|
|
98
|
+
* @param value Value to check.
|
|
99
|
+
* @param ctor Constructor used with `instanceof`.
|
|
100
|
+
* @param message Optional message or lazy message factory.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* assert.instanceOf(el, HTMLElement, "expected a DOM element");
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
declare function instanceOf<T>(value: unknown, ctor: abstract new (...args: never[]) => T, message?: MaybeFn<string>): asserts value is T;
|
|
108
|
+
|
|
109
|
+
type _exports_AssertionError = AssertionError;
|
|
110
|
+
declare const _exports_AssertionError: typeof AssertionError;
|
|
111
|
+
declare const _exports_defined: typeof defined;
|
|
112
|
+
declare const _exports_fail: typeof fail;
|
|
113
|
+
declare const _exports_instanceOf: typeof instanceOf;
|
|
114
|
+
declare const _exports_invariant: typeof invariant;
|
|
115
|
+
declare const _exports_notNull: typeof notNull;
|
|
116
|
+
declare const _exports_notUndefined: typeof notUndefined;
|
|
117
|
+
declare const _exports_ok: typeof ok;
|
|
118
|
+
declare const _exports_unreachable: typeof unreachable;
|
|
119
|
+
declare namespace _exports {
|
|
120
|
+
export {
|
|
121
|
+
_exports_AssertionError as AssertionError,
|
|
122
|
+
_exports_defined as defined,
|
|
123
|
+
_exports_fail as fail,
|
|
124
|
+
_exports_instanceOf as instanceOf,
|
|
125
|
+
_exports_invariant as invariant,
|
|
126
|
+
unreachable as never,
|
|
127
|
+
_exports_notNull as notNull,
|
|
128
|
+
_exports_notUndefined as notUndefined,
|
|
129
|
+
_exports_ok as ok,
|
|
130
|
+
_exports_unreachable as unreachable,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { _exports as assert };
|
package/assert.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { n as __exportAll } from "./chunk-YKewjYmz.js";
|
|
2
|
+
import { callFunction } from "yummies/common";
|
|
3
|
+
//#region src/assert/_exports.ts
|
|
4
|
+
/**
|
|
5
|
+
* ---header-docs-section---
|
|
6
|
+
* # yummies/assert
|
|
7
|
+
*
|
|
8
|
+
* ## Description
|
|
9
|
+
*
|
|
10
|
+
* Runtime **assertions** for invariants, narrowing, and exhaustiveness. Helpers throw
|
|
11
|
+
* {@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.defined`, `assert.fail`, and
|
|
13
|
+
* the rest on the namespace export from the package entry.
|
|
14
|
+
*
|
|
15
|
+
* ## Usage
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { assert } from "yummies/assert";
|
|
19
|
+
*
|
|
20
|
+
* assert.ok(user.id, "user id is required");
|
|
21
|
+
* assert.defined(maybeName);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
25
|
+
AssertionError: () => AssertionError,
|
|
26
|
+
defined: () => defined,
|
|
27
|
+
fail: () => fail,
|
|
28
|
+
instanceOf: () => instanceOf,
|
|
29
|
+
invariant: () => invariant,
|
|
30
|
+
never: () => unreachable,
|
|
31
|
+
notNull: () => notNull,
|
|
32
|
+
notUndefined: () => notUndefined,
|
|
33
|
+
ok: () => ok,
|
|
34
|
+
unreachable: () => unreachable
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Error thrown by assertion helpers in this module.
|
|
38
|
+
*/
|
|
39
|
+
var AssertionError = class extends Error {
|
|
40
|
+
name = "AssertionError";
|
|
41
|
+
};
|
|
42
|
+
function resolveAssertMessage(message, fallback) {
|
|
43
|
+
if (message === void 0) return fallback;
|
|
44
|
+
return callFunction(message);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Throws when `condition` is falsy; narrows the type when it is truthy.
|
|
48
|
+
*
|
|
49
|
+
* @param condition Value treated as a boolean guard.
|
|
50
|
+
* @param message Optional message or lazy message factory.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* assert.ok(user.id, "user id is required");
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
function ok(condition, message) {
|
|
58
|
+
if (!condition) throw new AssertionError(resolveAssertMessage(message, "Assertion failed"));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Same as {@link ok}; common alias for internal invariants (e.g. after optional checks).
|
|
62
|
+
*/
|
|
63
|
+
var invariant = ok;
|
|
64
|
+
/**
|
|
65
|
+
* Throws when the value is `null` or `undefined`; otherwise narrows away nullish.
|
|
66
|
+
*
|
|
67
|
+
* @param value Possibly nullish value.
|
|
68
|
+
* @param message Optional message or lazy message factory.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* assert.defined(maybeName, "name must be present");
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function defined(value, message) {
|
|
76
|
+
if (value == null) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Throws when the value is `null`; allows `undefined` unless combined with other checks.
|
|
80
|
+
*
|
|
81
|
+
* @param value Value that may be `null`.
|
|
82
|
+
* @param message Optional message or lazy message factory.
|
|
83
|
+
*/
|
|
84
|
+
function notNull(value, message) {
|
|
85
|
+
if (value === null) throw new AssertionError(resolveAssertMessage(message, "Expected a non-null value"));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Throws when the value is `undefined`.
|
|
89
|
+
*
|
|
90
|
+
* @param value Value that may be `undefined`.
|
|
91
|
+
* @param message Optional message or lazy message factory.
|
|
92
|
+
*/
|
|
93
|
+
function notUndefined(value, message) {
|
|
94
|
+
if (value === void 0) throw new AssertionError(resolveAssertMessage(message, "Expected a defined value"));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Always throws; use for branches that must be impossible or as a typed “abort”.
|
|
98
|
+
*
|
|
99
|
+
* @param message Optional message or lazy message factory.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* function parse(kind: "a" | "b") {
|
|
104
|
+
* if (kind === "a") return 1;
|
|
105
|
+
* if (kind === "b") return 2;
|
|
106
|
+
* assert.fail("unreachable");
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
function fail(message) {
|
|
111
|
+
throw new AssertionError(resolveAssertMessage(message, "Unreachable"));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Exhaustiveness helper: call with a `never` value when all union cases are handled.
|
|
115
|
+
*
|
|
116
|
+
* @param value Should be `never` when the type checker is satisfied.
|
|
117
|
+
* @param message Optional message (this path always throws, so a lazy factory is unnecessary).
|
|
118
|
+
*/
|
|
119
|
+
function unreachable(value, message) {
|
|
120
|
+
throw new AssertionError(resolveAssertMessage(message, `Unexpected value: ${String(value)}`));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Throws when `value` is not an instance of `ctor`.
|
|
124
|
+
*
|
|
125
|
+
* @param value Value to check.
|
|
126
|
+
* @param ctor Constructor used with `instanceof`.
|
|
127
|
+
* @param message Optional message or lazy message factory.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* assert.instanceOf(el, HTMLElement, "expected a DOM element");
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function instanceOf(value, ctor, message) {
|
|
135
|
+
if (!(value instanceof ctor)) throw new AssertionError(resolveAssertMessage(message, `Expected instance of ${ctor.name}`));
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
export { _exports_exports as assert };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=assert.js.map
|
package/assert.js.map
ADDED
|
@@ -0,0 +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.defined`, `assert.fail`, and\n * 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 * ```\n */\n\nimport { callFunction } from 'yummies/common';\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 (!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 (value == null) {\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 (value === null) {\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 (value === undefined) {\n throw new AssertionError(\n resolveAssertMessage(message, 'Expected a defined value'),\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,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,CAAC,UACH,OAAM,IAAI,eAAe,qBAAqB,SAAS,mBAAmB,CAAC;;;;;AAO/E,IAAa,YAAuB;;;;;;;;;;;;AAapC,SAAgB,QACd,OACA,SACiC;AACjC,KAAI,SAAS,KACX,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;AAUL,SAAgB,QACd,OACA,SACoB;AACpB,KAAI,UAAU,KACZ,OAAM,IAAI,eACR,qBAAqB,SAAS,4BAA4B,CAC3D;;;;;;;;AAUL,SAAgB,aACd,OACA,SACoB;AACpB,KAAI,UAAU,KAAA,EACZ,OAAM,IAAI,eACR,qBAAqB,SAAS,2BAA2B,CAC1D;;;;;;;;;;;;;;;;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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yummies",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.14.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"javascript",
|
|
6
6
|
"typescript",
|
|
@@ -40,6 +40,12 @@
|
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"exports": {
|
|
43
|
+
"./assert": {
|
|
44
|
+
"types": "./assert.d.ts",
|
|
45
|
+
"import": "./assert.js",
|
|
46
|
+
"require": "./assert.cjs",
|
|
47
|
+
"default": "./assert.js"
|
|
48
|
+
},
|
|
43
49
|
"./async": {
|
|
44
50
|
"types": "./async.d.ts",
|
|
45
51
|
"import": "./async.js",
|