typedash
Version:
modern, type-safe collection of utility functions
52 lines (50 loc) • 1.45 kB
JavaScript
const require_compact = require('./compact-BIRStvYt.cjs');
//#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(require_compact.compact(["Assertion not satisfied", message ? `: "${message}"` : ""]).join(""));
}
};
//#endregion
Object.defineProperty(exports, 'AssertionError', {
enumerable: true,
get: function () {
return AssertionError;
}
});
Object.defineProperty(exports, 'assert', {
enumerable: true,
get: function () {
return assert;
}
});
//# sourceMappingURL=assert-d3rr9bN5.cjs.map