result-guard
Version:
Type-safe error handling with discriminated unions and type guards for TypeScript
178 lines (177 loc) • 3.99 kB
JavaScript
function E(r) {
return r instanceof Error ? r : typeof r == "string" ? new Error(r) : new Error(`Non-error value thrown: ${P(r)}`);
}
function P(r) {
if (r === null) return "null";
if (r === void 0) return "undefined";
try {
return JSON.stringify(r);
} catch {
return String(r);
}
}
function w(r) {
return { data: r, error: null, isError: !1 };
}
function h(r) {
return { data: null, error: r, isError: !0 };
}
function m(r) {
return r instanceof Error ? r : E(r);
}
async function v(r) {
try {
const t = await r;
return w(t);
} catch (t) {
return h(m(t));
}
}
function I(r) {
return Promise.resolve(r).catch((t) => {
throw m(t);
});
}
function k(r) {
return !r.isError;
}
function g(r) {
return r.isError;
}
function p(r, t) {
return new Promise((i, o) => {
const e = setTimeout(() => {
o(new Error(`${t} after ${r}ms`));
}, r);
Promise.prototype.cancel = () => clearTimeout(e);
});
}
function y(r, t, i = "Operation timed out") {
const o = p(t, i);
return Promise.race([r, o]).finally(() => {
var e;
(e = o.cancel) == null || e.call(o);
});
}
function x(r, t, i = {}) {
const { errorEvent: o = "error", timeout: e, cleanup: s } = i;
return f(async () => {
let u = !1;
const n = async () => {
if (!u) {
u = !0, r.removeAllListeners(o);
try {
await (s == null ? void 0 : s());
} catch {
}
}
};
try {
const a = new Promise((O, d) => {
r.once(o, d);
}), l = [t(), a];
return e && l.push(p(e, "Operation timed out")), await Promise.race(l);
} finally {
await n();
}
});
}
async function C(r, t = {}) {
const { timeout: i, maxItems: o, onItem: e } = t;
return f(async () => {
const s = [], u = async () => {
var n;
try {
await ((n = r.return) == null ? void 0 : n.call(r));
} catch {
}
};
try {
const n = (async () => {
for (; ; ) {
let a;
try {
a = await r.next();
} catch (c) {
throw await u(), c;
}
if (a.done || e && !await e(a.value) || (s.push(a.value), o && s.length >= o)) break;
}
return s;
})();
return i ? await y(n, i, "Iterator timed out") : await n;
} finally {
await u();
}
});
}
function R(r, t = {}) {
return f(async () => new Promise((i, o) => {
let e = () => {
};
if (e = r({ resolve: (n) => {
e(), i(n);
}, reject: (n) => {
e(), o(n);
} }) || (() => {
}), t.timeout) {
const n = setTimeout(() => {
e(), o(new Error(`Operation timed out after ${t.timeout}ms`));
}, t.timeout), a = e;
e = () => {
clearTimeout(n), a();
};
}
}));
}
async function T(r, t = {}) {
const { maxConcurrent: i = 1 / 0, timeout: o, stopOnError: e = !1 } = t, s = (n) => f(async () => {
const a = n();
return o ? await y(a, o, "Operation timed out") : await a;
});
if (i === 1 / 0 || r.length <= i) {
const n = await Promise.all(r.map(s));
return e && n.some((a) => a.isError) ? [n.find((a) => a.isError)] : n;
}
const u = [];
for (let n = 0; n < r.length; n += i) {
const a = r.slice(n, n + i), c = await Promise.all(a.map(s));
if (u.push(...c), e && c.some((l) => l.isError))
return [c.find((l) => l.isError)];
}
return u;
}
async function S(r, t) {
let i = r;
for (const o of t) {
const e = await Promise.resolve(o(i));
if (g(e))
return e;
i = e.data;
}
return {
data: i,
error: null,
isError: !1
};
}
function f(r) {
try {
const t = r();
return t instanceof Promise ? v(I(t)) : w(t);
} catch (t) {
return h(m(t));
}
}
export {
T as concurrent,
h as createFailure,
w as createSuccess,
g as isFailure,
k as isSuccess,
S as pipe,
f as tryCatch,
R as withCallbacks,
x as withEvents,
C as withIterator
};