resultable
Version:
A small package to handle errors as values
185 lines (182 loc) • 5.07 kB
JavaScript
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/result.ts
var result_exports = {};
__export(result_exports, {
BrandedError: () => BrandedError,
TypeId: () => TypeId,
UnknownException: () => UnknownException,
catchAllBrands: () => catchAllBrands,
catchAllErr: () => catchAllErr,
err: () => err,
exhaustiveSwitchGuard: () => exhaustiveSwitchGuard,
fail: () => fail,
isBrandedError: () => isBrandedError,
isErr: () => isErr,
map: () => map,
mapErr: () => mapErr,
ok: () => ok,
okVoid: () => okVoid,
resultableFn: () => resultableFn,
tryCatch: () => tryCatch,
unwrap: () => unwrap,
unwrapErr: () => unwrapErr
});
// src/match.ts
var match_exports = {};
__export(match_exports, {
makeMatchI: () => makeMatchI,
matchBrand: () => matchBrand
});
function makeMatchI(d) {
return (v) => {
return (matchObj) => {
return matchObj[v[d]](v);
};
};
}
var matchBrand = makeMatchI("__brand");
// src/result.ts
var TypeId = Symbol.for("@Shared/BaseError");
function BrandedError(brand) {
var _a, _b;
class BaseBrandedError extends (_b = Error, _a = TypeId, _b) {
constructor(args) {
super((args == null ? void 0 : args.message) || "An error occurred");
this[_a] = TypeId;
this.__brand = brand;
if (args) {
Object.assign(this, args);
}
}
}
BaseBrandedError.prototype.name = brand;
return BaseBrandedError;
}
var UnknownException = class extends BrandedError("@Shared/UnknownException") {
constructor(args = { message: "Unknown exception" }) {
super(args);
}
};
var resultableFn = (fn) => {
const callback = (...args) => __async(null, null, function* () {
const result = yield fn(...args);
if (isBrandedError(result)) {
return err(result);
}
return result;
});
return callback;
};
function ok(value) {
return [value, void 0];
}
function err(error) {
return [void 0, error];
}
function okVoid() {
return [void 0, void 0];
}
function fail(args = {}) {
return err(new UnknownException(args));
}
function tryCatch(fn, errorFn) {
return __async(this, null, function* () {
var _a;
try {
return ok(yield fn());
} catch (e) {
return err((_a = errorFn == null ? void 0 : errorFn(e)) != null ? _a : new UnknownException({ cause: e }));
}
});
}
function map(resultOrMapper, mapper) {
const finalMapper = typeof resultOrMapper === "function" ? resultOrMapper : mapper;
const fn = (result) => {
const [value, error] = result;
if (error) {
return err(error);
}
return ok(finalMapper(value));
};
return typeof resultOrMapper === "function" ? fn : fn(resultOrMapper);
}
function mapErr(resultOrMapper, mapper) {
const finalMapper = typeof resultOrMapper === "function" ? resultOrMapper : mapper;
const fn = (result) => {
const [value, error] = result;
if (error) {
return err(finalMapper(error));
}
return ok(value);
};
return typeof resultOrMapper === "function" ? fn : fn(resultOrMapper);
}
function catchAllErr(resultOrMapper, mapper) {
const finalMapper = typeof resultOrMapper === "function" ? resultOrMapper : mapper;
const fn = (result) => {
const [value, error] = result;
if (error) {
return ok(finalMapper(error));
}
return ok(value);
};
return typeof resultOrMapper === "function" ? fn : fn(resultOrMapper);
}
function catchAllBrands(resultOrBrandMappers, brandMatchers) {
const isResult = (value) => Array.isArray(value);
const finalMatchers = isResult(resultOrBrandMappers) ? brandMatchers : resultOrBrandMappers;
const fn = (result) => {
const [value, error] = result;
if (error) {
return ok(matchBrand(error)(finalMatchers));
}
return ok(value);
};
return isResult(resultOrBrandMappers) ? fn(resultOrBrandMappers) : fn;
}
function unwrap(result) {
return result[0];
}
function unwrapErr(result) {
return result[1];
}
function isErr(result) {
const [, error] = result;
return typeof error !== "undefined" && error[TypeId] === TypeId;
}
function isBrandedError(value) {
return typeof value === "object" && value !== null && TypeId in value && value[TypeId] === TypeId;
}
function exhaustiveSwitchGuard(_) {
throw new Error("Exhaustive switch guard, should not have reached here.");
}
export {
match_exports as Match,
result_exports as Result,
TypeId
};
//# sourceMappingURL=index.mjs.map