@xylabs/assert
Advanced tools
+5
-7
| { | ||
| "name": "@xylabs/assert", | ||
| "version": "5.0.80", | ||
| "version": "5.0.81", | ||
| "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries", | ||
@@ -31,3 +31,2 @@ "keywords": [ | ||
| "types": "./dist/neutral/index.d.ts", | ||
| "source": "./src/index.ts", | ||
| "default": "./dist/neutral/index.mjs" | ||
@@ -38,7 +37,5 @@ }, | ||
| "module": "./dist/neutral/index.mjs", | ||
| "source": "./src/index.ts", | ||
| "types": "./dist/neutral/index.d.ts", | ||
| "files": [ | ||
| "dist", | ||
| "src", | ||
| "!**/*.bench.*", | ||
@@ -49,5 +46,6 @@ "!**/*.spec.*", | ||
| "devDependencies": { | ||
| "@xylabs/ts-scripts-yarn3": "~7.3.2", | ||
| "@xylabs/tsconfig": "~7.3.2", | ||
| "typescript": "~5.9.3" | ||
| "@xylabs/ts-scripts-yarn3": "~7.4.11", | ||
| "@xylabs/tsconfig": "~7.4.11", | ||
| "typescript": "~5.9.3", | ||
| "vitest": "^4.0.18" | ||
| }, | ||
@@ -54,0 +52,0 @@ "engines": { |
+4
-172
@@ -62,3 +62,3 @@ # @xylabs/assert | ||
| `undefined` | `T` | ||
| `T` | `undefined` | ||
@@ -122,3 +122,3 @@ ### messageFunc? | ||
| `undefined` | `T` | ||
| `T` | `undefined` | ||
@@ -148,86 +148,2 @@ ### errorFunc? | ||
| ## Call Signature | ||
| ```ts | ||
| function assertDefinedEx<T>(expr): T; | ||
| ``` | ||
| Asserts that a value is defined (not undefined) and returns the value. | ||
| Throws an error if the value is undefined. | ||
| ### Type Parameters | ||
| ### T | ||
| `T` | ||
| The type of value to check | ||
| ### Parameters | ||
| ### expr | ||
| Expression to be evaluated for being defined | ||
| `undefined` | `T` | ||
| ### Returns | ||
| `T` | ||
| The value of the expression (guaranteed to be defined) | ||
| ### Deprecated | ||
| Use overload with message function instead - passing a message will soon be required | ||
| ### Throws | ||
| Error with a generic message | ||
| ## Call Signature | ||
| ```ts | ||
| function assertDefinedEx<T>(expr, message?): 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. | ||
| ### Type Parameters | ||
| ### T | ||
| `T` | ||
| The type of value to check | ||
| ### Parameters | ||
| ### expr | ||
| Expression to be evaluated for being defined | ||
| `undefined` | `T` | ||
| ### message? | ||
| `string` | ||
| Error message if expression is undefined | ||
| ### Returns | ||
| `T` | ||
| The value of the expression (guaranteed to be defined) | ||
| ### Deprecated | ||
| Replace string with () => string for consistency | ||
| ### Throws | ||
| Error with the provided message | ||
| ### <a id="assertEx"></a>assertEx | ||
@@ -264,3 +180,3 @@ | ||
| `undefined` | `null` | `T` | ||
| `T` | `null` | `undefined` | ||
@@ -324,3 +240,3 @@ ### messageFunc? | ||
| `undefined` | `null` | `T` | ||
| `T` | `null` | `undefined` | ||
@@ -350,87 +266,3 @@ ### errorFunc? | ||
| ## Call Signature | ||
| ```ts | ||
| function assertEx<T>(expr): T; | ||
| ``` | ||
| Asserts that an expression is truthy and returns the value. | ||
| Throws an error if the expression is falsy. | ||
| ### Type Parameters | ||
| ### T | ||
| `T` | ||
| The type of value to check | ||
| ### Parameters | ||
| ### expr | ||
| Expression to be evaluated for truthiness | ||
| `undefined` | `null` | `T` | ||
| ### Returns | ||
| `T` | ||
| The value of the expression (guaranteed to be truthy) | ||
| ### Deprecated | ||
| Use overload with message function instead - passing a message will soon be required | ||
| ### Throws | ||
| Error with a generic message | ||
| ## Call Signature | ||
| ```ts | ||
| function assertEx<T>(expr, message?): T; | ||
| ``` | ||
| Asserts that an expression is truthy and returns the value. | ||
| Throws an error with the provided message if the expression is falsy. | ||
| ### Type Parameters | ||
| ### T | ||
| `T` | ||
| The type of value to check | ||
| ### Parameters | ||
| ### expr | ||
| Expression to be evaluated for truthiness | ||
| `undefined` | `null` | `T` | ||
| ### message? | ||
| `string` | ||
| Error message if expression is falsy | ||
| ### Returns | ||
| `T` | ||
| The value of the expression (guaranteed to be truthy) | ||
| ### Deprecated | ||
| Replace string with () => string for consistency | ||
| ### Throws | ||
| Error with the provided message | ||
| Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js) | ||
@@ -437,0 +269,0 @@ |
| 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. | ||
| * | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for being defined | ||
| * @param messageFunc - Function that returns a message for the error if expression is undefined | ||
| * @throws Error with the message returned by messageFunc | ||
| * @returns The value of the expression (guaranteed to be defined) | ||
| * @example | ||
| * ```typescript | ||
| * // Simple usage with a message function | ||
| * const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined') | ||
| * | ||
| * // Using with type narrowing | ||
| * const config: Config | undefined = loadConfig() | ||
| * const safeConfig = assertDefinedEx(config, () => 'Config failed to load') | ||
| * // safeConfig is now type Config (not Config | undefined) | ||
| * ``` | ||
| */ | ||
| function assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T | ||
| /** | ||
| * Asserts that a value is defined (not undefined) and returns the value. | ||
| * Throws a custom error if the value is undefined. | ||
| * | ||
| * @template T - The type of value to check | ||
| * @template R - The type of error to throw | ||
| * @param expr - Expression to be evaluated for being defined | ||
| * @param errorFunc - Function that returns a custom error instance if expression is undefined | ||
| * @throws Custom error returned by errorFunc | ||
| * @returns The value of the expression (guaranteed to be defined) | ||
| * @example | ||
| * ```typescript | ||
| * // Using with a custom error | ||
| * const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found')) | ||
| * ``` | ||
| */ | ||
| function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T | ||
| /** | ||
| * Implementation of assertDefinedEx that handles all overloads. | ||
| * | ||
| */ | ||
| function assertDefinedEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>( | ||
| expr: T | undefined, | ||
| func?: P, | ||
| ): T { | ||
| if (expr !== undefined) return expr | ||
| if (typeof func === 'function') { | ||
| const errorOrMessage = func(expr) | ||
| throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage | ||
| } | ||
| 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 } |
| 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. | ||
| * | ||
| * @template T - The type of value to check | ||
| * @param expr - Expression to be evaluated for truthiness | ||
| * @param messageFunc - Function that returns a message for the error if expression is falsy | ||
| * @throws Error with the message returned by messageFunc | ||
| * @returns The value of the expression (guaranteed to be truthy) | ||
| * @example | ||
| * ```typescript | ||
| * // Simple usage with a message function | ||
| * const value = assertEx(possiblyFalsy, () => 'Value must be truthy') | ||
| * | ||
| * // Using with type narrowing | ||
| * const config: Config | null = loadConfig() | ||
| * const safeConfig = assertEx(config, () => 'Config failed to load') | ||
| * // safeConfig is now type Config (not Config | null) | ||
| * ``` | ||
| */ | ||
| function assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T | ||
| /** | ||
| * Asserts that an expression is truthy and returns the value. | ||
| * Throws a custom error if the expression is falsy. | ||
| * | ||
| * @template T - The type of value to check | ||
| * @template R - The type of error to throw | ||
| * @param expr - Expression to be evaluated for truthiness | ||
| * @param errorFunc - Function that returns a custom error instance if expression is falsy | ||
| * @throws Custom error returned by errorFunc | ||
| * @returns The value of the expression (guaranteed to be truthy) | ||
| * @example | ||
| * ```typescript | ||
| * // Using with a custom error | ||
| * const user = assertEx(getUser(), () => new UserNotFoundError('User not found')) | ||
| * ``` | ||
| */ | ||
| function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T | ||
| /** | ||
| * Implementation of assertEx that handles all overloads. | ||
| */ | ||
| function assertEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>( | ||
| expr: T | null | undefined, | ||
| func?: P, | ||
| ): T { | ||
| // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
| if (expr) return expr | ||
| if (typeof func === 'function') { | ||
| const errorOrMessage = func(expr) | ||
| throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage | ||
| } | ||
| if (func !== undefined) { | ||
| throw new Error('Invalid assertEx usage: second argument must be a function or undefined') | ||
| } | ||
| throw new Error('Assertion failed') | ||
| } | ||
| export { assertEx } |
| export * from './assertDefinedEx.ts' | ||
| export * from './assertEx.ts' | ||
| export type * from './types.ts' |
-36
| /** | ||
| * @internal | ||
| * A function that takes a possibly null or undefined value and returns an error message string. | ||
| * Used in assertion functions to provide custom error messages. | ||
| * | ||
| * @internal | ||
| * @template T - The type of value being asserted | ||
| * @param value - The value being asserted (may be null or undefined) | ||
| * @returns A string message to be used in the error thrown by the assertion | ||
| * @example | ||
| * ```typescript | ||
| * const messageFunc: AssertExMessageFunc<User> = (user) => | ||
| * `User ${user ? user.id : 'unknown'} is not valid or accessible` | ||
| * ``` | ||
| */ | ||
| export type AssertExMessageFunc<T> = (value?: T | null) => string | ||
| /** | ||
| * A function that takes a possibly null or undefined value and returns a custom error instance. | ||
| * Used in assertion functions to provide specific error types with custom properties. | ||
| * | ||
| * @internal | ||
| * @template T - The type of value being asserted | ||
| * @template R - The specific error type to be returned, must extend Error | ||
| * @param value - The value being asserted (may be null or undefined) | ||
| * @returns An instance of the custom error type | ||
| * @example | ||
| * ```typescript | ||
| * const errorFunc: AssertExErrorFunc<User, UserNotFoundError> = (user) => | ||
| * new UserNotFoundError(`User ${user?.id || 'unknown'} not found`, { | ||
| * userId: user?.id, | ||
| * timestamp: new Date() | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export type AssertExErrorFunc<T, R extends Error> = (value?: T | null) => R |
28378
-23.37%4
33.33%13
-23.53%146
-51.33%299
-35.97%