@xylabs/assert
Advanced tools
@@ -40,26 +40,3 @@ import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'; | ||
| declare function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T; | ||
| /** | ||
| * Asserts that a value is defined (not undefined) and returns the value. | ||
| * Throws an error if the value is undefined. | ||
| * | ||
| * @deprecated Use overload with message function instead - passing a message will soon be required | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for being defined | ||
| * @throws Error with a generic message | ||
| * @returns The value of the expression (guaranteed to be defined) | ||
| */ | ||
| declare function assertDefinedEx<T>(expr: T | undefined): T; | ||
| /** | ||
| * Asserts that a value is defined (not undefined) and returns the value. | ||
| * Throws an error with the provided message if the value is undefined. | ||
| * | ||
| * @deprecated Replace string with () => string for consistency | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for being defined | ||
| * @param message - Error message if expression is undefined | ||
| * @throws Error with the provided message | ||
| * @returns The value of the expression (guaranteed to be defined) | ||
| */ | ||
| declare function assertDefinedEx<T>(expr: T | undefined, message?: string): T; | ||
| export { assertDefinedEx }; | ||
| //# sourceMappingURL=assertDefinedEx.d.ts.map |
@@ -1,1 +0,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;AAEzG;;;;;;;;;GASG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,GAAG,CAAC,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;AAmBrE,OAAO,EAAE,eAAe,EAAE,CAAA"} | ||
| {"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"} |
@@ -40,26 +40,3 @@ import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'; | ||
| declare function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T; | ||
| /** | ||
| * Asserts that an expression is truthy and returns the value. | ||
| * Throws an error if the expression is falsy. | ||
| * | ||
| * @deprecated Use overload with message function instead - passing a message will soon be required | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for truthiness | ||
| * @throws Error with a generic message | ||
| * @returns The value of the expression (guaranteed to be truthy) | ||
| */ | ||
| declare function assertEx<T>(expr: T | null | undefined): T; | ||
| /** | ||
| * Asserts that an expression is truthy and returns the value. | ||
| * Throws an error with the provided message if the expression is falsy. | ||
| * | ||
| * @deprecated Replace string with () => string for consistency | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for truthiness | ||
| * @param message - Error message if expression is falsy | ||
| * @throws Error with the provided message | ||
| * @returns The value of the expression (guaranteed to be truthy) | ||
| */ | ||
| declare function assertEx<T>(expr: T | null | undefined, message?: string): T; | ||
| export { assertEx }; | ||
| //# sourceMappingURL=assertEx.d.ts.map |
@@ -1,1 +0,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;AAEzG;;;;;;;;;GASG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,CAAC,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;AAmBrE,OAAO,EAAE,QAAQ,EAAE,CAAA"} | ||
| {"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"} |
| // src/assertDefinedEx.ts | ||
| function assertDefinedEx(expr, messageOrFunc) { | ||
| function assertDefinedEx(expr, func) { | ||
| if (expr !== void 0) return expr; | ||
| if (typeof messageOrFunc === "function") { | ||
| const errorOrMessage = messageOrFunc(expr); | ||
| if (typeof func === "function") { | ||
| const errorOrMessage = func(expr); | ||
| throw typeof errorOrMessage === "string" ? new Error(errorOrMessage) : errorOrMessage; | ||
| } | ||
| throw new Error(messageOrFunc); | ||
| if (func !== void 0) { | ||
| throw new Error("Invalid assertEx usage: second argument must be a function or undefined"); | ||
| } | ||
| throw new Error("Assertion failed: value is undefined"); | ||
| } | ||
| // src/assertEx.ts | ||
| function assertEx(expr, messageOrFunc) { | ||
| function assertEx(expr, func) { | ||
| if (expr) return expr; | ||
| if (typeof messageOrFunc === "function") { | ||
| const errorOrMessage = messageOrFunc(expr); | ||
| if (typeof func === "function") { | ||
| const errorOrMessage = func(expr); | ||
| throw typeof errorOrMessage === "string" ? new Error(errorOrMessage) : errorOrMessage; | ||
| } | ||
| throw new Error(messageOrFunc); | ||
| if (func !== void 0) { | ||
| throw new Error("Invalid assertEx usage: second argument must be a function or undefined"); | ||
| } | ||
| throw new Error("Assertion failed"); | ||
| } | ||
@@ -20,0 +26,0 @@ export { |
@@ -1,1 +0,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 * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error if the value is undefined.\n *\n * @deprecated Use overload with message function instead - passing a message will soon be required\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @throws Error with a generic message\n * @returns The value of the expression (guaranteed to be defined)\n */\nfunction assertDefinedEx<T>(expr: T | undefined): T\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error with the provided message if the value is undefined.\n *\n * @deprecated Replace string with () => string for consistency\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @param message - Error message if expression is undefined\n * @throws Error with the provided message\n * @returns The value of the expression (guaranteed to be defined)\n */\nfunction assertDefinedEx<T>(expr: T | undefined, message?: string): T\n\n/**\n * Implementation of assertDefinedEx that handles all overloads.\n *\n */\nfunction assertDefinedEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(\n expr: T | undefined,\n messageOrFunc?: P,\n): T {\n if (expr !== undefined) return expr\n if (typeof messageOrFunc === 'function') {\n const errorOrMessage = messageOrFunc(expr)\n throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage\n }\n // a string was sent\n throw new Error(messageOrFunc)\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 * Asserts that an expression is truthy and returns the value.\n * Throws an error if the expression is falsy.\n *\n * @deprecated Use overload with message function instead - passing a message will soon be required\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for truthiness\n * @throws Error with a generic message\n * @returns The value of the expression (guaranteed to be truthy)\n */\nfunction assertEx<T>(expr: T | null | undefined): T\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws an error with the provided message if the expression is falsy.\n *\n * @deprecated Replace string with () => string for consistency\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for truthiness\n * @param message - Error message if expression is falsy\n * @throws Error with the provided message\n * @returns The value of the expression (guaranteed to be truthy)\n */\nfunction assertEx<T>(expr: T | null | undefined, message?: string): T\n\n/**\n * Implementation of assertEx that handles all overloads.\n */\nfunction assertEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(\n expr: T | null | undefined,\n messageOrFunc?: P,\n): T {\n // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions\n if (expr) return expr\n if (typeof messageOrFunc === 'function') {\n const errorOrMessage = messageOrFunc(expr)\n throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage\n }\n // a string was sent\n throw new Error(messageOrFunc)\n}\n\nexport { assertEx }\n"],"mappings":";AAuEA,SAAS,gBACP,MACA,eACG;AACH,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,OAAO,kBAAkB,YAAY;AACvC,UAAM,iBAAiB,cAAc,IAAI;AACzC,UAAM,OAAO,mBAAmB,WAAW,IAAI,MAAM,cAAc,IAAI;AAAA,EACzE;AAEA,QAAM,IAAI,MAAM,aAAa;AAC/B;;;ACZA,SAAS,SACP,MACA,eACG;AAEH,MAAI,KAAM,QAAO;AACjB,MAAI,OAAO,kBAAkB,YAAY;AACvC,UAAM,iBAAiB,cAAc,IAAI;AACzC,UAAM,OAAO,mBAAmB,WAAW,IAAI,MAAM,cAAc,IAAI;AAAA,EACzE;AAEA,QAAM,IAAI,MAAM,aAAa;AAC/B;","names":[]} | ||
| {"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":[]} |
+3
-3
| { | ||
| "name": "@xylabs/assert", | ||
| "version": "5.0.64", | ||
| "version": "5.0.65", | ||
| "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries", | ||
@@ -47,4 +47,4 @@ "keywords": [ | ||
| "devDependencies": { | ||
| "@xylabs/ts-scripts-yarn3": "~7.3.1", | ||
| "@xylabs/tsconfig": "~7.3.1", | ||
| "@xylabs/ts-scripts-yarn3": "~7.3.2", | ||
| "@xylabs/tsconfig": "~7.3.2", | ||
| "typescript": "~5.9.3" | ||
@@ -51,0 +51,0 @@ }, |
@@ -44,43 +44,20 @@ import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts' | ||
| /** | ||
| * Asserts that a value is defined (not undefined) and returns the value. | ||
| * Throws an error if the value is undefined. | ||
| * | ||
| * @deprecated Use overload with message function instead - passing a message will soon be required | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for being defined | ||
| * @throws Error with a generic message | ||
| * @returns The value of the expression (guaranteed to be defined) | ||
| */ | ||
| function assertDefinedEx<T>(expr: T | undefined): T | ||
| /** | ||
| * Asserts that a value is defined (not undefined) and returns the value. | ||
| * Throws an error with the provided message if the value is undefined. | ||
| * | ||
| * @deprecated Replace string with () => string for consistency | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for being defined | ||
| * @param message - Error message if expression is undefined | ||
| * @throws Error with the provided message | ||
| * @returns The value of the expression (guaranteed to be defined) | ||
| */ | ||
| function assertDefinedEx<T>(expr: T | undefined, message?: string): T | ||
| /** | ||
| * Implementation of assertDefinedEx that handles all overloads. | ||
| * | ||
| */ | ||
| function assertDefinedEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>( | ||
| function assertDefinedEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>( | ||
| expr: T | undefined, | ||
| messageOrFunc?: P, | ||
| func?: P, | ||
| ): T { | ||
| if (expr !== undefined) return expr | ||
| if (typeof messageOrFunc === 'function') { | ||
| const errorOrMessage = messageOrFunc(expr) | ||
| if (typeof func === 'function') { | ||
| const errorOrMessage = func(expr) | ||
| throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage | ||
| } | ||
| // a string was sent | ||
| throw new Error(messageOrFunc) | ||
| if (func !== undefined) { | ||
| throw new Error('Invalid assertEx usage: second argument must be a function or undefined') | ||
| } | ||
| throw new Error('Assertion failed: value is undefined') | ||
| } | ||
| export { assertDefinedEx } |
+8
-31
@@ -44,43 +44,20 @@ import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts' | ||
| /** | ||
| * Asserts that an expression is truthy and returns the value. | ||
| * Throws an error if the expression is falsy. | ||
| * | ||
| * @deprecated Use overload with message function instead - passing a message will soon be required | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for truthiness | ||
| * @throws Error with a generic message | ||
| * @returns The value of the expression (guaranteed to be truthy) | ||
| */ | ||
| function assertEx<T>(expr: T | null | undefined): T | ||
| /** | ||
| * Asserts that an expression is truthy and returns the value. | ||
| * Throws an error with the provided message if the expression is falsy. | ||
| * | ||
| * @deprecated Replace string with () => string for consistency | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for truthiness | ||
| * @param message - Error message if expression is falsy | ||
| * @throws Error with the provided message | ||
| * @returns The value of the expression (guaranteed to be truthy) | ||
| */ | ||
| function assertEx<T>(expr: T | null | undefined, message?: string): T | ||
| /** | ||
| * Implementation of assertEx that handles all overloads. | ||
| */ | ||
| function assertEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>( | ||
| function assertEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>( | ||
| expr: T | null | undefined, | ||
| messageOrFunc?: P, | ||
| func?: P, | ||
| ): T { | ||
| // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
| if (expr) return expr | ||
| if (typeof messageOrFunc === 'function') { | ||
| const errorOrMessage = messageOrFunc(expr) | ||
| if (typeof func === 'function') { | ||
| const errorOrMessage = func(expr) | ||
| throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage | ||
| } | ||
| // a string was sent | ||
| throw new Error(messageOrFunc) | ||
| if (func !== undefined) { | ||
| throw new Error('Invalid assertEx usage: second argument must be a function or undefined') | ||
| } | ||
| throw new Error('Assertion failed') | ||
| } | ||
| export { assertEx } |
37031
-14.21%300
-21.47%