UNPKG

@tsxo/assure

Version:

A lightweight, zero dependancy, and typesafe suite of assert functions.

104 lines 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assert = assert; exports.assert_eq = assert_eq; exports.assert_ne = assert_ne; exports.assert_gt = assert_gt; exports.assert_lt = assert_lt; exports.assert_gte = assert_gte; exports.assert_lte = assert_lte; /** * Asserts that a given value is truthy and type narrows. * If the value is falsy, it throws an error with an optional custom message. * * @param {unknown} v - The value to check for truthiness. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the value is falsy, throws an error. */ function assert(v, msg) { const err = "Assertion Error"; if (!v) { const out = msg ? `${err}: ${msg}` : err; throw new Error(out); } } /** * Asserts that two values are strictly equal. * * @template T * @param {T} l - The left-hand value to compare. * @param {T} r - The right-hand value to compare. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the values are not strictly equal. */ function assert_eq(l, r, msg) { assert(l === r, msg); } /** * Asserts that two values are strictly not equal. * * @template T * @param {T} l - The left-hand value to compare. * @param {T} r - The right-hand value to compare. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the values are strictly equal. */ function assert_ne(l, r, msg) { assert(l !== r, msg); } /** * Asserts that the left-hand value is greater than the right-hand value. * * @template T * @param {T} l - The left-hand value to compare. * @param {T} r - The right-hand value to compare. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the left-hand value is not greater than the right-hand value. */ function assert_gt(l, r, msg) { assert(l > r, msg); } /** * Asserts that the left-hand value is less than the right-hand value. * * @template T * @param {T} l - The left-hand value to compare. * @param {T} r - The right-hand value to compare. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the left-hand value is not less than the right-hand value. */ function assert_lt(l, r, msg) { assert(l < r, msg); } /** * Asserts that the left-hand value is greater than or equal to the right-hand value. * * @template T * @param {T} l - The left-hand value to compare. * @param {T} r - The right-hand value to compare. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the left-hand value is not greater than or equal to the right-hand value. */ function assert_gte(l, r, msg) { assert(l >= r, msg); } /** * Asserts that the left-hand value is less than or equal to the right-hand value. * * @template T * @param {T} l - The left-hand value to compare. * @param {T} r - The right-hand value to compare. * @param {string} [msg] - An optional custom error message to include in the thrown error. * * @throws {Error} If the left-hand value is not less than or equal to the right-hand value. */ function assert_lte(l, r, msg) { assert(l <= r, msg); } //# sourceMappingURL=index.js.map