|
| 1 | +import { |
| 2 | + isArray, |
| 3 | + isBoolean, |
| 4 | + isFunction, |
| 5 | + isLike, |
| 6 | + isNull, |
| 7 | + isNullish, |
| 8 | + isNumber, |
| 9 | + isObject, |
| 10 | + isString, |
| 11 | + isUndefined, |
| 12 | + Predicate, |
| 13 | +} from "./is.ts"; |
| 14 | + |
| 15 | +/** |
| 16 | + * An error raised by type assertion functions |
| 17 | + */ |
| 18 | +export class AssertError extends Error { |
| 19 | + constructor(message?: string) { |
| 20 | + super(message); |
| 21 | + |
| 22 | + if (Error.captureStackTrace) { |
| 23 | + Error.captureStackTrace(this, AssertError); |
| 24 | + } |
| 25 | + |
| 26 | + this.name = "AssertError"; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function assert<T>( |
| 31 | + x: unknown, |
| 32 | + pred: Predicate<T>, |
| 33 | + message = "The value is not expected type", |
| 34 | +): asserts x is T { |
| 35 | + if (!pred(x)) { |
| 36 | + throw new AssertError(message); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Ensure that `x` is string, and raise `AssertError` if it is not. |
| 42 | + */ |
| 43 | +export function assertString(x: unknown): asserts x is string { |
| 44 | + return assert(x, isString, "The value must be string"); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Ensure that `x` is number, and raise `AssertError` if it is not. |
| 49 | + */ |
| 50 | +export function assertNumber(x: unknown): asserts x is number { |
| 51 | + return assert(x, isNumber, "The value must be number"); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Ensure that `x` is boolean, and raise `AssertError` if it is not. |
| 56 | + */ |
| 57 | +export function assertBoolean(x: unknown): asserts x is boolean { |
| 58 | + return assert(x, isBoolean, "The value must be boolean"); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Ensure that `x` is array, and raise `AssertError` if it is not. |
| 63 | + * |
| 64 | + * Use `ipred` to predicate the type of items. |
| 65 | + */ |
| 66 | +export function assertArray<T extends unknown>( |
| 67 | + x: unknown, |
| 68 | + ipred?: Predicate<T>, |
| 69 | +): asserts x is T[] { |
| 70 | + const pred = (x: unknown): x is T[] => isArray(x, ipred); |
| 71 | + return assert(x, pred, "The value must be array"); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Ensure that `x` is object, and raise `AssertError` if it is not. |
| 76 | + * |
| 77 | + * Use `ipred` to predicate the type of values. |
| 78 | + */ |
| 79 | +export function assertObject<T>( |
| 80 | + x: unknown, |
| 81 | + ipred?: Predicate<T>, |
| 82 | +): asserts x is Record<string, T> { |
| 83 | + const pred = (x: unknown): x is Record<string, T> => isObject(x, ipred); |
| 84 | + return assert(x, pred, "The value must be object"); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Ensure that `x` is function, and raise `AssertError` if it is not. |
| 89 | + */ |
| 90 | +export function assertFunction( |
| 91 | + x: unknown, |
| 92 | +): asserts x is (...args: unknown[]) => unknown { |
| 93 | + return assert(x, isFunction, "The value must be function"); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Ensure that `x` is null, and raise `AssertError` if it is not. |
| 98 | + */ |
| 99 | +export function assertNull(x: unknown): asserts x is null { |
| 100 | + return assert(x, isNull, "The value must be null"); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Ensure that `x` is undefined, and raise `AssertError` if it is not. |
| 105 | + */ |
| 106 | +export function assertUndefined(x: unknown): asserts x is undefined { |
| 107 | + return assert(x, isUndefined, "The value must be undefined"); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Ensure that `x` is null or undefined, and raise `AssertError` if it is not. |
| 112 | + */ |
| 113 | +export function assertNullish(x: unknown): asserts x is null | undefined { |
| 114 | + return assert(x, isNullish, "The value must be null or undefined"); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Ensure that `x` follows the type of `ref`, and raise `AssertError` if it is not. |
| 119 | + */ |
| 120 | +export function assertLike<R, T extends unknown>( |
| 121 | + ref: R, |
| 122 | + x: unknown, |
| 123 | + ipred?: Predicate<T>, |
| 124 | +): asserts x is R { |
| 125 | + const pred = (x: unknown): x is T[] => isLike(ref, x, ipred); |
| 126 | + return assert(x, pred, "The value must follow the reference"); |
| 127 | +} |
0 commit comments