UNPKG

typed-utilities

Version:
79 lines (66 loc) 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Result = void 0; var _Either = require("./Either"); function isOk(result) { return _Either.Either.is.left(result); } function assertOk(result, message) { if (!isOk(result)) { throw new TypeError(message ?? `result should be 'ok'`); } } function isErr(result) { return _Either.Either.is.right(result); } function assertErr(result, message) { if (!isErr(result)) { throw new TypeError(message ?? `result should be 'err'`); } } const toOkValue = result => result[1]; const toErrError = result => result[1]; const is = { ok: isOk, err: isErr }; const assert = { ok: assertOk, err: assertErr }; const of = { ok: value => _Either.Either.of.left(value), err: error => _Either.Either.of.right(error) }; const to = { okValue: toOkValue, errError: toErrError }; const match = (result, match) => isOk(result) ? match.ok(result[1]) : match.err(result[1]); const tryCatch = fn => { try { return of.ok(fn()); } catch (error) { return of.err(error); } }; const tryCatchAsync = async fn => { try { return of.ok(await fn()); } catch (error) { return of.err(error); } }; const Result = { is, assert, of, to, match, tryCatch, tryCatchAsync }; exports.Result = Result; //# sourceMappingURL=Result.js.map