@xylabs/assert 5.0.64 → 5.0.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/neutral/assertDefinedEx.d.ts +0 -23
- package/dist/neutral/assertDefinedEx.d.ts.map +1 -1
- package/dist/neutral/assertEx.d.ts +0 -23
- package/dist/neutral/assertEx.d.ts.map +1 -1
- package/dist/neutral/index.mjs +14 -8
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/assertDefinedEx.ts +8 -31
- package/src/assertEx.ts +8 -31
|
@@ -37,28 +37,5 @@ declare function assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExM
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
declare function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T;
|
|
40
|
-
/**
|
|
41
|
-
* Asserts that a value is defined (not undefined) and returns the value.
|
|
42
|
-
* Throws an error if the value is undefined.
|
|
43
|
-
*
|
|
44
|
-
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
45
|
-
* @template T - The type of value to check
|
|
46
|
-
* @param expr - Expression to be evaluated for being defined
|
|
47
|
-
* @throws Error with a generic message
|
|
48
|
-
* @returns The value of the expression (guaranteed to be defined)
|
|
49
|
-
*/
|
|
50
|
-
declare function assertDefinedEx<T>(expr: T | undefined): T;
|
|
51
|
-
/**
|
|
52
|
-
* Asserts that a value is defined (not undefined) and returns the value.
|
|
53
|
-
* Throws an error with the provided message if the value is undefined.
|
|
54
|
-
*
|
|
55
|
-
* @deprecated Replace string with () => string for consistency
|
|
56
|
-
* @template T - The type of value to check
|
|
57
|
-
* @param expr - Expression to be evaluated for being defined
|
|
58
|
-
* @param message - Error message if expression is undefined
|
|
59
|
-
* @throws Error with the provided message
|
|
60
|
-
* @returns The value of the expression (guaranteed to be defined)
|
|
61
|
-
*/
|
|
62
|
-
declare function assertDefinedEx<T>(expr: T | undefined, message?: string): T;
|
|
63
40
|
export { assertDefinedEx };
|
|
64
41
|
//# sourceMappingURL=assertDefinedEx.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertDefinedEx.d.ts","sourceRoot":"","sources":["../../src/assertDefinedEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"assertDefinedEx.d.ts","sourceRoot":"","sources":["../../src/assertDefinedEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAA;AAqBzG,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
|
@@ -37,28 +37,5 @@ declare function assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExM
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
declare function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T;
|
|
40
|
-
/**
|
|
41
|
-
* Asserts that an expression is truthy and returns the value.
|
|
42
|
-
* Throws an error if the expression is falsy.
|
|
43
|
-
*
|
|
44
|
-
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
45
|
-
* @template T - The type of value to check
|
|
46
|
-
* @param expr - Expression to be evaluated for truthiness
|
|
47
|
-
* @throws Error with a generic message
|
|
48
|
-
* @returns The value of the expression (guaranteed to be truthy)
|
|
49
|
-
*/
|
|
50
|
-
declare function assertEx<T>(expr: T | null | undefined): T;
|
|
51
|
-
/**
|
|
52
|
-
* Asserts that an expression is truthy and returns the value.
|
|
53
|
-
* Throws an error with the provided message if the expression is falsy.
|
|
54
|
-
*
|
|
55
|
-
* @deprecated Replace string with () => string for consistency
|
|
56
|
-
* @template T - The type of value to check
|
|
57
|
-
* @param expr - Expression to be evaluated for truthiness
|
|
58
|
-
* @param message - Error message if expression is falsy
|
|
59
|
-
* @throws Error with the provided message
|
|
60
|
-
* @returns The value of the expression (guaranteed to be truthy)
|
|
61
|
-
*/
|
|
62
|
-
declare function assertEx<T>(expr: T | null | undefined, message?: string): T;
|
|
63
40
|
export { assertEx };
|
|
64
41
|
//# sourceMappingURL=assertEx.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertEx.d.ts","sourceRoot":"","sources":["../../src/assertEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"assertEx.d.ts","sourceRoot":"","sources":["../../src/assertEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAA;AAqBzG,OAAO,EAAE,QAAQ,EAAE,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
// src/assertDefinedEx.ts
|
|
2
|
-
function assertDefinedEx(expr,
|
|
2
|
+
function assertDefinedEx(expr, func) {
|
|
3
3
|
if (expr !== void 0) return expr;
|
|
4
|
-
if (typeof
|
|
5
|
-
const errorOrMessage =
|
|
4
|
+
if (typeof func === "function") {
|
|
5
|
+
const errorOrMessage = func(expr);
|
|
6
6
|
throw typeof errorOrMessage === "string" ? new Error(errorOrMessage) : errorOrMessage;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
if (func !== void 0) {
|
|
9
|
+
throw new Error("Invalid assertEx usage: second argument must be a function or undefined");
|
|
10
|
+
}
|
|
11
|
+
throw new Error("Assertion failed: value is undefined");
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
// src/assertEx.ts
|
|
12
|
-
function assertEx(expr,
|
|
15
|
+
function assertEx(expr, func) {
|
|
13
16
|
if (expr) return expr;
|
|
14
|
-
if (typeof
|
|
15
|
-
const errorOrMessage =
|
|
17
|
+
if (typeof func === "function") {
|
|
18
|
+
const errorOrMessage = func(expr);
|
|
16
19
|
throw typeof errorOrMessage === "string" ? new Error(errorOrMessage) : errorOrMessage;
|
|
17
20
|
}
|
|
18
|
-
|
|
21
|
+
if (func !== void 0) {
|
|
22
|
+
throw new Error("Invalid assertEx usage: second argument must be a function or undefined");
|
|
23
|
+
}
|
|
24
|
+
throw new Error("Assertion failed");
|
|
19
25
|
}
|
|
20
26
|
export {
|
|
21
27
|
assertDefinedEx,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assertDefinedEx.ts","../../src/assertEx.ts"],"sourcesContent":["import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error if the value is undefined.\n *\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @param messageFunc - Function that returns a message for the error if expression is undefined\n * @throws Error with the message returned by messageFunc\n * @returns The value of the expression (guaranteed to be defined)\n * @example\n * ```typescript\n * // Simple usage with a message function\n * const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')\n *\n * // Using with type narrowing\n * const config: Config | undefined = loadConfig()\n * const safeConfig = assertDefinedEx(config, () => 'Config failed to load')\n * // safeConfig is now type Config (not Config | undefined)\n * ```\n */\nfunction assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws a custom error if the value is undefined.\n *\n * @template T - The type of value to check\n * @template R - The type of error to throw\n * @param expr - Expression to be evaluated for being defined\n * @param errorFunc - Function that returns a custom error instance if expression is undefined\n * @throws Custom error returned by errorFunc\n * @returns The value of the expression (guaranteed to be defined)\n * @example\n * ```typescript\n * // Using with a custom error\n * const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))\n * ```\n */\nfunction assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n\n/**\n *
|
|
1
|
+
{"version":3,"sources":["../../src/assertDefinedEx.ts","../../src/assertEx.ts"],"sourcesContent":["import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error if the value is undefined.\n *\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @param messageFunc - Function that returns a message for the error if expression is undefined\n * @throws Error with the message returned by messageFunc\n * @returns The value of the expression (guaranteed to be defined)\n * @example\n * ```typescript\n * // Simple usage with a message function\n * const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')\n *\n * // Using with type narrowing\n * const config: Config | undefined = loadConfig()\n * const safeConfig = assertDefinedEx(config, () => 'Config failed to load')\n * // safeConfig is now type Config (not Config | undefined)\n * ```\n */\nfunction assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws a custom error if the value is undefined.\n *\n * @template T - The type of value to check\n * @template R - The type of error to throw\n * @param expr - Expression to be evaluated for being defined\n * @param errorFunc - Function that returns a custom error instance if expression is undefined\n * @throws Custom error returned by errorFunc\n * @returns The value of the expression (guaranteed to be defined)\n * @example\n * ```typescript\n * // Using with a custom error\n * const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))\n * ```\n */\nfunction assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n\n/**\n * Implementation of assertDefinedEx that handles all overloads.\n *\n */\nfunction assertDefinedEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(\n expr: T | undefined,\n func?: P,\n): T {\n if (expr !== undefined) return expr\n if (typeof func === 'function') {\n const errorOrMessage = func(expr)\n throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage\n }\n if (func !== undefined) {\n throw new Error('Invalid assertEx usage: second argument must be a function or undefined')\n }\n throw new Error('Assertion failed: value is undefined')\n}\n\nexport { assertDefinedEx }\n","import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws an error if the expression is falsy.\n *\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for truthiness\n * @param messageFunc - Function that returns a message for the error if expression is falsy\n * @throws Error with the message returned by messageFunc\n * @returns The value of the expression (guaranteed to be truthy)\n * @example\n * ```typescript\n * // Simple usage with a message function\n * const value = assertEx(possiblyFalsy, () => 'Value must be truthy')\n *\n * // Using with type narrowing\n * const config: Config | null = loadConfig()\n * const safeConfig = assertEx(config, () => 'Config failed to load')\n * // safeConfig is now type Config (not Config | null)\n * ```\n */\nfunction assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws a custom error if the expression is falsy.\n *\n * @template T - The type of value to check\n * @template R - The type of error to throw\n * @param expr - Expression to be evaluated for truthiness\n * @param errorFunc - Function that returns a custom error instance if expression is falsy\n * @throws Custom error returned by errorFunc\n * @returns The value of the expression (guaranteed to be truthy)\n * @example\n * ```typescript\n * // Using with a custom error\n * const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))\n * ```\n */\nfunction assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n\n/**\n * Implementation of assertEx that handles all overloads.\n */\nfunction assertEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(\n expr: T | null | undefined,\n func?: P,\n): T {\n // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions\n if (expr) return expr\n if (typeof func === 'function') {\n const errorOrMessage = func(expr)\n throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage\n }\n if (func !== undefined) {\n throw new Error('Invalid assertEx usage: second argument must be a function or undefined')\n }\n throw new Error('Assertion failed')\n}\n\nexport { assertEx }\n"],"mappings":";AA8CA,SAAS,gBACP,MACA,MACG;AACH,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,OAAO,SAAS,YAAY;AAC9B,UAAM,iBAAiB,KAAK,IAAI;AAChC,UAAM,OAAO,mBAAmB,WAAW,IAAI,MAAM,cAAc,IAAI;AAAA,EACzE;AACA,MAAI,SAAS,QAAW;AACtB,UAAM,IAAI,MAAM,yEAAyE;AAAA,EAC3F;AACA,QAAM,IAAI,MAAM,sCAAsC;AACxD;;;ACdA,SAAS,SACP,MACA,MACG;AAEH,MAAI,KAAM,QAAO;AACjB,MAAI,OAAO,SAAS,YAAY;AAC9B,UAAM,iBAAiB,KAAK,IAAI;AAChC,UAAM,OAAO,mBAAmB,WAAW,IAAI,MAAM,cAAc,IAAI;AAAA,EACzE;AACA,MAAI,SAAS,QAAW;AACtB,UAAM,IAAI,MAAM,yEAAyE;AAAA,EAC3F;AACA,QAAM,IAAI,MAAM,kBAAkB;AACpC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/assert",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.65",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"xylabs",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"!**/*.test.*"
|
|
45
45
|
],
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@xylabs/ts-scripts-yarn3": "~7.3.
|
|
48
|
-
"@xylabs/tsconfig": "~7.3.
|
|
47
|
+
"@xylabs/ts-scripts-yarn3": "~7.3.2",
|
|
48
|
+
"@xylabs/tsconfig": "~7.3.2",
|
|
49
49
|
"typescript": "~5.9.3"
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
package/src/assertDefinedEx.ts
CHANGED
|
@@ -40,46 +40,23 @@ function assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFu
|
|
|
40
40
|
*/
|
|
41
41
|
function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T
|
|
42
42
|
|
|
43
|
-
/**
|
|
44
|
-
* Asserts that a value is defined (not undefined) and returns the value.
|
|
45
|
-
* Throws an error if the value is undefined.
|
|
46
|
-
*
|
|
47
|
-
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
48
|
-
* @template T - The type of value to check
|
|
49
|
-
* @param expr - Expression to be evaluated for being defined
|
|
50
|
-
* @throws Error with a generic message
|
|
51
|
-
* @returns The value of the expression (guaranteed to be defined)
|
|
52
|
-
*/
|
|
53
|
-
function assertDefinedEx<T>(expr: T | undefined): T
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Asserts that a value is defined (not undefined) and returns the value.
|
|
57
|
-
* Throws an error with the provided message if the value is undefined.
|
|
58
|
-
*
|
|
59
|
-
* @deprecated Replace string with () => string for consistency
|
|
60
|
-
* @template T - The type of value to check
|
|
61
|
-
* @param expr - Expression to be evaluated for being defined
|
|
62
|
-
* @param message - Error message if expression is undefined
|
|
63
|
-
* @throws Error with the provided message
|
|
64
|
-
* @returns The value of the expression (guaranteed to be defined)
|
|
65
|
-
*/
|
|
66
|
-
function assertDefinedEx<T>(expr: T | undefined, message?: string): T
|
|
67
|
-
|
|
68
43
|
/**
|
|
69
44
|
* Implementation of assertDefinedEx that handles all overloads.
|
|
70
45
|
*
|
|
71
46
|
*/
|
|
72
|
-
function assertDefinedEx<T, R extends Error, P extends
|
|
47
|
+
function assertDefinedEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(
|
|
73
48
|
expr: T | undefined,
|
|
74
|
-
|
|
49
|
+
func?: P,
|
|
75
50
|
): T {
|
|
76
51
|
if (expr !== undefined) return expr
|
|
77
|
-
if (typeof
|
|
78
|
-
const errorOrMessage =
|
|
52
|
+
if (typeof func === 'function') {
|
|
53
|
+
const errorOrMessage = func(expr)
|
|
79
54
|
throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage
|
|
80
55
|
}
|
|
81
|
-
|
|
82
|
-
|
|
56
|
+
if (func !== undefined) {
|
|
57
|
+
throw new Error('Invalid assertEx usage: second argument must be a function or undefined')
|
|
58
|
+
}
|
|
59
|
+
throw new Error('Assertion failed: value is undefined')
|
|
83
60
|
}
|
|
84
61
|
|
|
85
62
|
export { assertDefinedEx }
|
package/src/assertEx.ts
CHANGED
|
@@ -40,46 +40,23 @@ function assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFu
|
|
|
40
40
|
*/
|
|
41
41
|
function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T
|
|
42
42
|
|
|
43
|
-
/**
|
|
44
|
-
* Asserts that an expression is truthy and returns the value.
|
|
45
|
-
* Throws an error if the expression is falsy.
|
|
46
|
-
*
|
|
47
|
-
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
48
|
-
* @template T - The type of value to check
|
|
49
|
-
* @param expr - Expression to be evaluated for truthiness
|
|
50
|
-
* @throws Error with a generic message
|
|
51
|
-
* @returns The value of the expression (guaranteed to be truthy)
|
|
52
|
-
*/
|
|
53
|
-
function assertEx<T>(expr: T | null | undefined): T
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Asserts that an expression is truthy and returns the value.
|
|
57
|
-
* Throws an error with the provided message if the expression is falsy.
|
|
58
|
-
*
|
|
59
|
-
* @deprecated Replace string with () => string for consistency
|
|
60
|
-
* @template T - The type of value to check
|
|
61
|
-
* @param expr - Expression to be evaluated for truthiness
|
|
62
|
-
* @param message - Error message if expression is falsy
|
|
63
|
-
* @throws Error with the provided message
|
|
64
|
-
* @returns The value of the expression (guaranteed to be truthy)
|
|
65
|
-
*/
|
|
66
|
-
function assertEx<T>(expr: T | null | undefined, message?: string): T
|
|
67
|
-
|
|
68
43
|
/**
|
|
69
44
|
* Implementation of assertEx that handles all overloads.
|
|
70
45
|
*/
|
|
71
|
-
function assertEx<T, R extends Error, P extends
|
|
46
|
+
function assertEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(
|
|
72
47
|
expr: T | null | undefined,
|
|
73
|
-
|
|
48
|
+
func?: P,
|
|
74
49
|
): T {
|
|
75
50
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
76
51
|
if (expr) return expr
|
|
77
|
-
if (typeof
|
|
78
|
-
const errorOrMessage =
|
|
52
|
+
if (typeof func === 'function') {
|
|
53
|
+
const errorOrMessage = func(expr)
|
|
79
54
|
throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage
|
|
80
55
|
}
|
|
81
|
-
|
|
82
|
-
|
|
56
|
+
if (func !== undefined) {
|
|
57
|
+
throw new Error('Invalid assertEx usage: second argument must be a function or undefined')
|
|
58
|
+
}
|
|
59
|
+
throw new Error('Assertion failed')
|
|
83
60
|
}
|
|
84
61
|
|
|
85
62
|
export { assertEx }
|