type-fns
Version:
A set of types, type checks, and type guards for simpler, safer, and easier to read code.
27 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.assure = void 0;
const withAssure_1 = require("../wrappers/withAssure");
/**
* what: a standalone assure function that takes a value and a type.check, returning the value if it passes or throwing if it doesn't
* why:
* - enables inline type narrowing with runtime validation in a single expression
* - useful when you want to assure a value satisfies a type guard without pre-wrapping the guard with `asAssure` or `withAssure`
*
* example:
* ```ts
* const uuid: Uuid = assure('821e33d6-a330-425d-a393-f97e39113046', isUuid); // passes
* const uuid: Uuid = assure('not a uuid', isUuid); // throws AssureIsOfTypeRejectionError
* ```
*/
const assure = (input, check, options) => {
const name = options?.name ?? check.name;
if (check(input))
return input;
throw new withAssure_1.AssureIsOfTypeRejectionError(`assure.rejection: input does not satisfy type.check '${name}'`, {
check: name,
input: input === undefined ? 'undefined' : input,
});
};
exports.assure = assure;
//# sourceMappingURL=assure.js.map