Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/assert. Assertion library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import type { Falsy, NonFalsy } from "@samual/types"
type Nullish = undefined | nulltype MessageFn<T> = (value: T) => string
export class AssertError extends Error {}Object.defineProperty(AssertError.prototype, `name`, { value: `AssertError` })
/** * Assertion function that ensures value is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). * @throws If value is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). */export function assert<T>( value: T, message: string | MessageFn<T & Falsy> = "Assertion failed"): asserts value { if (!value) throw new AssertError(typeof message == `string` ? message : message(value as T & Falsy))}
/** * Assertion function that ensures value is not [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish). * @throws If value is nullish. */export function assertNonNull<T>( value: T, message: string | MessageFn<T & Nullish> = "Non-null assertion failed"): asserts value is NonNullable<T> { assert(value != null, typeof message == `string` ? message : () => message(value as T & Nullish))}
/** * An alias for {@linkcode assert()} but with a type signature that explicitly narrows with {@linkcode NonFalsy<>}. * * This is useful because in this code: * ```ts * function foo<T>(value: T) { * assert(value) * * value * // ^? (parameter) value: NonNullable<T> * } * ``` * * TypeScript incorrectly narrows the type of `value` with `NonNullable<>` when it should be using something like * {@linkcode NonFalsy<>}. * * ```ts * function foo<T>(value: T) { * assertTruthy(value) * * value * // ^? (parameter) value: NonFalsy<T> * } * ``` * @alias */export const assertTruthy: <T>(value: T, message?: string | MessageFn<T & Falsy>) => asserts value is NonFalsy<T> = assert
/** * Assertion function that ensures value is not [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish). * @throws If value is nullish. * @returns Given value. */export const expect = <T>( value: T, message: string | MessageFn<T & Nullish> = `Expectation failed`): NonNullable<T> => { assertNonNull(value, message)
return value}
/** * Assertion function that ensures value is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). * @throws If value is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). * @returns Given value. */export const expectTruthy = <T>( value: T, message: string | MessageFn<T & Falsy> = `Truthy expectation failed`): NonFalsy<T> => { assertTruthy(value, message)
return value}
export function unreachable(message: string = "Hit unreachable"): never { throw new AssertError(message)}
if (import.meta.vitest) { const { test, expect: vitestExpect } = import.meta.vitest
test(`assert()`, () => { assert(true)
vitestExpect(() => { assert(false) }).toThrow(AssertError) })
test(`assertNonNull()`, () => { assertNonNull(false)
vitestExpect(() => { assertNonNull(undefined) }).toThrow(AssertError) })
test(`expect()`, () => { vitestExpect(expect(false)).toBe(false) vitestExpect(() => expect(undefined)).toThrow(AssertError) })
test(`expectTruthy()`, () => { vitestExpect(expectTruthy(true)).toBe(true) vitestExpect(() => expectTruthy(false)).toThrow(AssertError) })
test(`unreachable()`, () => { vitestExpect(() => unreachable()).toThrow(AssertError) })}