subflow
Version:
Safety your data flow in TypeScript with flow-based primitives.
474 lines (473 loc) • 9.57 kB
JavaScript
var j = Object.defineProperty;
var R = (t, e, h) => e in t ? j(t, e, { enumerable: !0, configurable: !0, writable: !0, value: h }) : t[e] = h;
var p = (t, e, h) => R(t, typeof e != "symbol" ? e + "" : e, h);
const a = Symbol("FLOW_TYPE"), r = Symbol("value"), s = Symbol("customMethods"), w = (t, e) => typeof t == "object" && t !== null && a in t && t[a] === e, M = (t) => w(t, "error");
class E {
static add(e) {
this.errors.push(e);
}
static view() {
return this.errors;
}
static clear() {
this.errors = [];
}
}
p(E, "errors", []);
const l = (t) => {
const e = {
timestamp: Date.now(),
traceId: crypto.randomUUID(),
...t
}, h = {
_value: e.value,
[a]: "error",
getError: () => e,
get: () => e.value
};
return E.add(e), new Proxy(h, {
get(c, f) {
return f in h ? h[f] : () => {
};
}
});
};
function N(t) {
return typeof t == "object" && t !== null && a in t ? M(t) : !1;
}
const S = {
[a]: "boolean",
not() {
return i(!this[r], this[s]);
},
and(t) {
return i(this[r] && t, this[s]);
},
or(t) {
return i(this[r] || t, this[s]);
},
nor(t) {
return i(!this[r] || !t, this[s]);
},
xor(t) {
return i(this[r] !== t, this[s]);
},
notEqual(t) {
return i(this[r] !== t, this[s]);
},
equal(t) {
return i(this[r] === t, this[s]);
},
flowString() {
return o(this[r].toString());
},
flowNumber() {
return n(this[r] ? 1 : 0);
}
}, i = (t, e) => typeof t != "boolean" ? l({
type: "boolean",
value: t,
message: "Value must be a boolean",
code: "BOOLEAN_FLOW_ERROR"
}) : b("boolean", t, e), u = (t, e) => Array.isArray(t) ? b("array", t, e) : l({
type: "array",
value: t,
message: "Value must be an array",
code: "ARRAY_FLOW_ERROR"
}), F = {
[a]: "object",
keys() {
return u(Object.keys(this[r]));
},
values() {
return u(Object.values(this[r]));
},
entries() {
return u(Object.entries(this[r]));
},
has(t) {
return i(this[r].hasOwnProperty(t));
},
set(t, e) {
return g({ ...this[r], [t]: e }, this[s]);
},
delete(t) {
const e = { ...this[r] };
return delete e[t], g(e, this[s]);
},
flowString() {
return o(JSON.stringify(this[r]));
}
}, g = (t, e) => i(!!t).and(typeof t == "object").not().get() ? l({
type: "object",
value: t,
message: "Value must be an object",
code: "OBJECT_FLOW_ERROR"
}) : b("object", t, e), _ = {
[a]: "array",
push(...t) {
return u([...this[r], ...t], this[s]);
},
pop() {
const t = this[r];
return u(t.slice(0, -1), this[s]);
},
shift() {
return u(this[r].slice(1), this[s]);
},
unshift(...t) {
return u([...t, ...this[r]], this[s]);
},
join(t) {
return o(this[r].join(t));
},
map(t) {
return u(this[r].map(t), this[s]);
},
filter(t) {
return u(this[r].filter(t), this[s]);
},
reduce(t, e) {
return this[r].reduce(
t,
e
);
},
sort(t) {
return u(
this[r].toSorted(t),
this[s]
);
},
reverse() {
return u(
this[r].reduce((t, e) => [e, ...t], []),
this[s]
);
},
concat(...t) {
return u(this[r].concat(...t), this[s]);
},
slice(t, e) {
return u(this[r].slice(t, e), this[s]);
},
splice(t, e, ...h) {
return u(
this[r].reduce((c, f, O) => O === t ? [...c, ...h] : O > t && O < t + e ? c : [...c, f], []),
this[s]
);
},
indexOf(t, e) {
return n(this[r].indexOf(t, e));
},
findLastIndex(t) {
const e = this[r].findLastIndex(
(h) => h === t
);
return n(e);
},
includes(t, e) {
return i(this[r].includes(t, e));
},
find(t) {
return this[r].find(t);
},
findIndex(t) {
return n(this[r].findIndex(t));
},
forEach(t) {
return this[r].forEach(t), u(this[r], this[s]);
},
every(t) {
return i(this[r].every(t));
},
some(t) {
return i(this[r].some(t));
},
flowString() {
return o(this[r].toString());
},
flowLocaleString() {
return o(this[r].toLocaleString());
},
flowStringfy() {
return o(JSON.stringify(this[r]));
},
flowObject() {
try {
return g(
Object.fromEntries(Object.entries(this[r]))
);
} catch (t) {
return l({
message: "Value must be entriesable of array",
code: "ARRAY_FLOW_ERROR",
type: "array",
value: this[r],
cause: t
});
}
},
flowObjectEntries() {
try {
return g(Object.fromEntries(this[r]));
} catch (t) {
return l({
message: "Value must be entriesable of array",
code: "ARRAY_FLOW_ERROR",
type: "array",
value: this[r],
cause: t
});
}
},
flowBoolean() {
return i(this[r].length > 0);
},
flowNumber() {
return n(this[r].length);
}
}, L = {
[a]: "string",
toUpper() {
return o(this[r].toUpperCase(), this[s]);
},
toLower() {
return o(this[r].toLowerCase(), this[s]);
},
length() {
return n(this[r].length);
},
reverse() {
return o(this[r].split("").reverse().join(""), this[s]);
},
replace(t, e) {
return o(this[r].replace(t, e), this[s]);
},
trim() {
return o(this[r].trim(), this[s]);
},
padStart(t, e) {
return o(this[r].padStart(t, e), this[s]);
},
padEnd(t, e) {
return o(this[r].padEnd(t, e), this[s]);
},
startsWith(t) {
return i(this[r].startsWith(t));
},
endsWith(t) {
return i(this[r].endsWith(t));
},
includes(t) {
return i(this[r].includes(t));
},
indexOf(t) {
return n(this[r].indexOf(t));
},
lastIndexOf(t) {
return n(this[r].lastIndexOf(t));
},
charAt(t) {
return o(this[r].charAt(t), this[s]);
},
charCodeAt(t) {
return n(this[r].charCodeAt(t));
},
concat(...t) {
return o(this[r].concat(...t), this[s]);
},
split(t) {
return u(this[r].split(t));
},
slice(t, e) {
return o(this[r].slice(t, e), this[s]);
},
substring(t, e) {
return o(this[r].substring(t, e), this[s]);
},
equals(t) {
return i(this[r] === t);
},
notEqual(t) {
return i(this[r] !== t);
},
flowNumber() {
return n(Number(this[r]));
},
flowFloatNumber() {
return n(Number(this[r]));
},
flowBoolean() {
return i(!!this[r]);
},
flowArray() {
return u(this[r].split(""));
},
flowObject() {
return g(JSON.parse(this[r]));
}
}, o = (t, e) => typeof t != "string" ? l({
type: "string",
value: t,
message: "Value must be a string",
code: "STRING_FLOW_ERROR"
}) : b("string", t, e), x = {
[a]: "number",
add(t) {
return n(this[r] + t, this[s]);
},
subtract(t) {
return n(this[r] - t, this[s]);
},
multiply(t) {
return n(this[r] * t, this[s]);
},
divide(t) {
return n(this[r] / t, this[s]);
},
modulo(t) {
return n(this[r] % t, this[s]);
},
power(t) {
return n(this[r] ** t, this[s]);
},
sqrt() {
return n(Math.sqrt(this[r]), this[s]);
},
round() {
return n(Math.round(this[r]), this[s]);
},
floor() {
return n(Math.floor(this[r]), this[s]);
},
ceil() {
return n(Math.ceil(this[r]), this[s]);
},
abs() {
return n(Math.abs(this[r]), this[s]);
},
random() {
return n(Math.random(), this[s]);
},
min(t) {
return n(Math.min(this[r], t), this[s]);
},
max(t) {
return n(Math.max(this[r], t), this[s]);
},
clamp(t, e) {
return n(Math.min(Math.max(this[r], t), e), this[s]);
},
sign() {
return n(Math.sign(this[r]), this[s]);
},
toFixed(t) {
return o(this[r].toFixed(t));
},
toExponential(t) {
return o(this[r].toExponential(t));
},
toPrecision(t) {
return o(this[r].toPrecision(t));
},
lessThan(t) {
return i(this[r] < t);
},
greaterThan(t) {
return i(this[r] > t);
},
lessThanOrEqual(t) {
return i(this[r] <= t);
},
greaterThanOrEqual(t) {
return i(this[r] >= t);
},
flowBoolean() {
return i(this[r] !== 0);
},
flowLocaleString(t, e) {
return o(this[r].toLocaleString(t, e));
},
flowString(t) {
return o(this[r].toString(t));
}
}, n = (t, e) => typeof t != "number" ? l({
type: "number",
value: t,
message: "Value must be a number",
code: "NUMBER_FLOW_ERROR"
}) : b("number", t, e), A = {
[a]: "bigint"
}, z = (t, e) => typeof t != "bigint" ? l({
type: "bigint",
value: t,
message: "Value must be a bigint",
code: "BIGINT_FLOW_ERROR"
}) : b("bigint", t, e), d = Object.freeze({
isError: !1,
get() {
return this[r];
},
getError() {
}
}), y = {
number: Object.freeze({
...d,
...x
}),
string: Object.freeze({
...d,
...L
}),
boolean: Object.freeze({
...d,
...S
}),
bigint: Object.freeze({
...d,
...A
}),
array: Object.freeze({
...d,
..._
}),
object: Object.freeze({
...d,
...F
})
}, m = /* @__PURE__ */ new WeakMap(), W = (t, e) => {
if (!e)
return y[t];
if (m.has(e))
return m.get(e);
const h = { ...y[t], ...e }, c = Object.freeze(h);
return m.set(e, c), c;
}, b = (t, e, h) => {
const c = W(t, h);
return Object.freeze({
...c,
[r]: e,
[a]: t,
[s]: h
});
};
export {
E as ErrorManager,
u as arrayFlow,
_ as arrayMethods,
A as bigIntMethods,
z as bigintFlow,
i as booleanFlow,
S as booleanMethods,
b as createFlow,
l as errorFlow,
N as isError,
n as numberFlow,
x as numberMethods,
g as objectFlow,
F as objectMethods,
o as stringFlow,
L as stringMethods
};
//# sourceMappingURL=index.es.js.map