trycat
Version:
A lightweight, type-safe, zero-dependency implementation of the Result type.
173 lines (171 loc) • 3.33 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// trycat.ts
var trycat_exports = {};
__export(trycat_exports, {
err: () => err,
ok: () => ok,
tryp: () => tryp,
trys: () => trys
});
module.exports = __toCommonJS(trycat_exports);
var Ok = class _Ok {
constructor(value) {
this.value = value;
}
isOk() {
return true;
}
isErr() {
return false;
}
inspect(f) {
f(this.value);
return this;
}
inspectErr(f) {
return this;
}
map(mapper) {
return new _Ok(mapper(this.value));
}
mapOr(def, mapper) {
return mapper(this.value);
}
mapOrElse(errMapper, mapper) {
return mapper(this.value);
}
mapErr(mapper) {
return this;
}
or(other) {
return this;
}
orElse(op) {
return this;
}
and(other) {
return other;
}
andThen(op) {
return op(this.value);
}
unwrap() {
return this.value;
}
unwrapOr(def) {
return this.value;
}
unwrapOrElse(op) {
return this.value;
}
expect(msg) {
return this.value;
}
unwrapErr() {
throw new Error(`${this.value}`);
}
expectErr(message) {
throw new Error(`${message}: ${this.value}`);
}
};
var Err = class _Err {
constructor(error) {
this.error = error;
}
isOk() {
return false;
}
isErr() {
return true;
}
inspect(f) {
return this;
}
inspectErr(f) {
f(this.error);
return this;
}
map(mapper) {
return this;
}
mapOr(def, mapper) {
return def;
}
mapOrElse(errMapper, mapper) {
return errMapper(this.error);
}
mapErr(mapper) {
return new _Err(mapper(this.error));
}
or(other) {
return other;
}
orElse(op) {
return op(this.error);
}
and(other) {
return this;
}
andThen(op) {
return this;
}
unwrap() {
throw new Error(`${this.error}`);
}
unwrapOr(def) {
return def;
}
unwrapOrElse(op) {
return op(this.error);
}
expect(message) {
throw new Error(`${message}: ${this.error}`);
}
unwrapErr() {
return this.error;
}
expectErr(message) {
return this.error;
}
};
function ok(value) {
return value ? new Ok(value) : new Ok(void 0);
}
function err(error) {
return error ? new Err(error) : new Err(void 0);
}
function trys(fn) {
try {
const retval = fn();
return retval ? ok(retval) : ok();
} catch (e) {
return err(e);
}
}
function tryp(promise) {
return promise.then((value) => ok(value)).catch((e) => err(e));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
err,
ok,
tryp,
trys
});