@duydang2311/attempt
Version:
A functional type for error handling in JS/TS
186 lines (182 loc) • 4.71 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __moduleCache = /* @__PURE__ */ new WeakMap;
var __toCommonJS = (from) => {
var entry = __moduleCache.get(from), desc;
if (entry)
return entry;
entry = __defProp({}, "__esModule", { value: true });
if (from && typeof from === "object" || typeof from === "function")
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
}));
__moduleCache.set(from, entry);
return entry;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: true,
configurable: true,
set: (newValue) => all[name] = () => newValue
});
};
// lib/index.ts
var exports_lib = {};
__export(exports_lib, {
isAttempt: () => isAttempt,
attemptUnwrapOrElse: () => attemptUnwrapOrElse,
attemptUnwrapOr: () => attemptUnwrapOr,
attemptUnwrap: () => attemptUnwrap,
attemptSync: () => attemptSync,
attemptOk: () => attemptOk,
attemptMapError: () => attemptMapError,
attemptMap: () => attemptMap,
attemptFlatMapError: () => attemptFlatMapError,
attemptFlatMap: () => attemptFlatMap,
attemptFail: () => attemptFail,
attemptAsync: () => attemptAsync,
attempt: () => attempt
});
module.exports = __toCommonJS(exports_lib);
// lib/pipe.ts
function pipe(value, ...fns) {
let current = value;
for (let i = 0, size = fns.length;i < size; ++i) {
current = fns[i](current);
if (current instanceof Promise) {
return current.then(async (resolved) => {
for (let j = i + 1;j < size; ++j) {
resolved = fns[j](resolved);
if (resolved instanceof Promise) {
resolved = await resolved;
}
}
return resolved;
});
}
}
return current;
}
// lib/attempt.ts
function attemptSync(fn) {
return (mapException) => {
try {
return attemptOk(fn());
} catch (e) {
return attemptFail(mapException ? mapException(e) : e);
}
};
}
function attemptAsync(fn) {
return async (mapException) => {
try {
const ret = await fn();
if (isAttempt(ret)) {
return ret;
}
return attemptOk(ret);
} catch (e) {
return attemptFail(mapException ? mapException(e) : e);
}
};
}
function attemptOk(data) {
return {
ok: true,
data,
pipe: attemptPipe
};
}
function attemptFail(error) {
return {
ok: false,
error,
pipe: attemptPipe
};
}
function attemptMap(f) {
return (attempt) => {
if (!attempt.ok) {
return attempt;
}
const ret = f(attempt.data);
if (ret instanceof Promise) {
return ret.then(attemptOk);
}
return attemptOk(ret);
};
}
function attemptFlatMap(f) {
return (attempt) => {
if (!attempt.ok) {
return attempt;
}
return f(attempt.data);
};
}
function attemptMapError(f) {
return (attempt) => {
if (attempt.ok) {
return attempt;
}
const ret = f(attempt.error);
if (ret instanceof Promise) {
return ret.then(attemptFail);
}
return attemptFail(ret);
};
}
function attemptFlatMapError(f) {
return (attempt) => {
if (attempt.ok) {
return attempt;
}
return f(attempt.error);
};
}
function attemptUnwrap(attempt) {
if (!attempt.ok) {
throw attempt.error;
}
return attempt.data;
}
function attemptUnwrapOr(attemptOrDefault, defaultValue) {
if (defaultValue) {
const attempt = attemptOrDefault;
return attempt.ok ? attempt.data : defaultValue;
}
return (attempt) => attempt.ok ? attempt.data : attemptOrDefault;
}
function attemptUnwrapOrElse(attemptOrF, f) {
if (f) {
const attempt = attemptOrF;
return attempt.ok ? attempt.data : f(attempt.error);
}
return (attempt) => attempt.ok ? attempt.data : attemptOrF(attempt.error);
}
function isAttempt(obj) {
return typeof obj === "object" && obj !== null && "ok" in obj && typeof obj.ok === "boolean" && (obj.ok === true && ("data" in obj) || obj.ok === false && ("error" in obj));
}
var attempt = {
sync: attemptSync,
async: attemptAsync,
ok: attemptOk,
fail: attemptFail,
map: attemptMap,
flatMap: attemptFlatMap,
check: isAttempt,
mapError: attemptMapError,
flatMapError: attemptFlatMapError,
orElse: attemptFlatMapError,
unwrap: attemptUnwrap,
unwrapOr: attemptUnwrapOr,
unwrapOrElse: attemptUnwrapOrElse
};
function attemptPipe(...fns) {
return pipe(this, ...fns);
}