typedash
Version:
modern, type-safe collection of utility functions
41 lines (39 loc) • 1.24 kB
JavaScript
import { t as compact } from "./compact-DkVw8bE5.js";
//#region src/functions/assert/assert.ts
/**
* Asserts that a condition is true, throwing an Error if it is not.
* @param condition A condition that should be true
* @param message A message to use in the Error that will be thrown if the condition is falsy
* @throws An `AssertionError` if the condition is falsy
* @example
* ```ts
* function doWork(value: number | undefined) {
* assert(value !== undefined, 'value should be defined');
* // value is now narrowed to number
*
* return value + 1;
* }
* ```
* @example
* ```ts
* assert(1 === 1); // OK
* assert(1 === 2); // throws AssertionError
* assert(1 === 2, '1 should equal 2'); // throws AssertionError: 1 should equal 2
* ```
*/
function assert(condition, message) {
if (arguments.length === 0) return;
if (!condition) throw new AssertionError(message);
}
/**
* An error that is thrown when an assertion is not satisfied.
* Thrown by {@link assert}.
*/
var AssertionError = class extends Error {
constructor(message) {
super(compact(["Assertion not satisfied", message ? `: "${message}"` : ""]).join(""));
}
};
//#endregion
export { assert as n, AssertionError as t };
//# sourceMappingURL=assert-BMSuascf.js.map