Skip to content

Commit

Permalink
Refactor case functions to separate declarations, add BTE, NBTE
Browse files Browse the repository at this point in the history
  • Loading branch information
brocococonut committed Mar 16, 2024
1 parent b92833a commit 377d95f
Showing 1 changed file with 123 additions and 193 deletions.
316 changes: 123 additions & 193 deletions util/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ const paramLimiter = (
val: unknown,
): boolean => (params.length !== limit ? false : isTruthy(val));

const handleLen = (opts: FuncArgsType, fn: FuncType) => {
const val_len = getLen(opts.value);

if (Number.isNaN(val_len)) return false;
return fn({ ...opts, value: val_len });
};

/**
* Converts the input value to an array based on its type.
*
* @param b - The input value to be converted to an array.
* @returns An array representation of the input value.
*/
const arrayify = (b: Any): Any[] => {
switch (true) {
case isArray(b):
Expand All @@ -53,199 +52,130 @@ const arrayify = (b: Any): Any[] => {
}
};

/**
* Map of functions with their corresponding names and implementations.
* The key is the function name and the value is the function implementation.
*/
const FUNCS: Map<string, FuncType<Any, Any[]>> = new Map([
["GT", ({ value: a, params }) => a > addNumbers(params)],
[
"GTE",
({ value: a, params }) => {
return a >= addNumbers(params);
},
],
["NGT", (opts) => !(({ value: a, params }) => a > addNumbers(params))(opts)],
[
"NGTE",
(opts) =>
!(({ value: a, params }) => {
return a >= addNumbers(params);
})(opts),
],
[
"LEN_GT",
(opts) => handleLen(opts, ({ value: a, params }) => a > addNumbers(params)),
],
[
"LEN_GTE",
(opts) =>
handleLen(opts, ({ value: a, params }) => {
return a >= addNumbers(params);
}),
],
[
"LEN_NGT",
(opts) =>
handleLen(
opts,
(opts) => !(({ value: a, params }) => a > addNumbers(params))(opts),
),
],
[
"LEN_NGTE",
(opts) =>
handleLen(
opts,
(opts) =>
!(({ value: a, params }) => {
return a >= addNumbers(params);
})(opts),
),
],
["LT", ({ value: a, params }) => a < addNumbers(params)],
[
"LTE",
({ value: a, params }) => {
return a <= addNumbers(params);
},
],
["NLT", (opts) => !(({ value: a, params }) => a < addNumbers(params))(opts)],
[
"NLTE",
(opts) =>
!(({ value: a, params }) => {
return a <= addNumbers(params);
})(opts),
],
[
"LEN_LT",
(opts) => handleLen(opts, ({ value: a, params }) => a < addNumbers(params)),
],
[
"LEN_LTE",
(opts) =>
handleLen(opts, ({ value: a, params }) => {
return a <= addNumbers(params);
}),
],
[
"LEN_NLT",
(opts) =>
handleLen(
opts,
(opts) => !(({ value: a, params }) => a < addNumbers(params))(opts),
),
],
[
"LEN_NLTE",
(opts) =>
handleLen(
opts,
(opts) =>
!(({ value: a, params }) => {
return a <= addNumbers(params);
})(opts),
),
],
["EQ", ({ value: a, params: [b] }) => a === b],
["NEQ", ({ value: a, params: [b] }) => a !== b],
["LEN_EQ", (opts) => handleLen(opts, ({ value: a, params: [b] }) => a === b)],
[
"LEN_NEQ",
(opts) => handleLen(opts, ({ value: a, params: [b] }) => a !== b),
],
[
"BT",
({ value, params }) =>
paramLimiter(2, params, value > params[0] && value < params[1]),
],
[
"NBT",
({ value, params }) =>
!paramLimiter(2, params, value > params[0] && value < params[1]),
],
[
"LEN_BT",
(opts) =>
handleLen(opts, ({ params, value }) =>
paramLimiter(2, params, value > params[0] && value < params[1]),
),
],
[
"LEN_NBT",
(opts) =>
handleLen(
opts,
({ params, value }) =>
!paramLimiter(2, params, value > params[0] && value < params[1]),
),
],
["IN", ({ value: a, params: [b] }) => arrayify(b).includes(a)],
["NIN", ({ value: a, params: [b] }) => !arrayify(b).includes(a)],
[
"LEN_IN",
(opts) => {
return handleLen(opts, ({ value: a, params: [b] }) =>
arrayify(b).includes(a),
);
},
],
[
"LEN_NIN",
(opts) => {
return handleLen(
opts,
({ value: a, params: [b] }) => !arrayify(b).includes(a),
);
},
],
[
"AND",
({ value: a, params: p }) => {
if (!isTruthy(a)) return false;
const FN_LEN = (opts: FuncArgsType, fn: FuncType) => {
const val_len = getLen(opts.value);

return p.every(isTruthy);
},
],
[
"OR",
({ value: a, params }) => {
if (isTruthy(a)) return true;
if (Number.isNaN(val_len)) return false;
return fn({ ...opts, value: val_len });
};

return params.some(isTruthy);
},
],
[
"XOR",
({ value: a, params }) => {
let has_truthy = isTruthy(a);
// Greater than
const FN_GT: FuncType<number, number[]> = ({ value: a, params: p }) =>
a > addNumbers(p);
const FN_GTE: FuncType<number, number[]> = ({ value: a, params: p }) =>
a >= addNumbers(p);
const FN_NGT: FuncType<number, number[]> = (opts) => !FN_GT(opts);
const FN_NGTE: FuncType<number, number[]> = (opts) => !FN_GTE(opts);
// Less than
const FN_LT: FuncType<number, number[]> = FN_NGTE;
const FN_LTE: FuncType<number, number[]> = ({ value: a, params: p }) =>
a <= addNumbers(p);
const FN_NLT: FuncType<number, number[]> = FN_GTE;
const FN_NLTE: FuncType<number, number[]> = FN_GT;
// Equal
const FN_EQ: FuncType<Any, Any[]> = ({ value: a, params: [b] }) => a === b;
const FN_NEQ: FuncType<Any, Any[]> = ({ value: a, params: [b] }) => a !== b;
// Between
const FN_BT: FuncType<Any, Any[]> = ({ value: a, params: p }) =>
paramLimiter(2, p, a > p[0] && a < p[1]);
const FN_BTE: FuncType<Any, Any[]> = ({ value: a, params: p }) =>
paramLimiter(2, p, a >= p[0] && a <= p[1]);
const FN_NBT: FuncType<Any, Any[]> = (opts) => !FN_BT(opts);
const FN_NBTE: FuncType<Any, Any[]> = (opts) => !FN_BTE(opts);
// Contained in
const FN_IN: FuncType<Any, Any[]> = ({ value: a, params: [b] }) =>
arrayify(b).includes(a);
const FN_NIN: FuncType<Any, Any[]> = (opts) => !FN_IN(opts);
// All truthy
const FN_AND: FuncType<Any, Any[]> = ({ value: a, params: p }) =>
isTruthy(a) && p.every(isTruthy);
// Any truthy
const FN_OR: FuncType<Any, Any[]> = ({ value: a, params: p }) =>
isTruthy(a) || p.some(isTruthy);
// Only one truthy
const FN_XOR: FuncType<Any, Any[]> = ({ value: a, params: pa }) => {
let has_truthy = isTruthy(a);

for (const p of params) {
const is_truthy = isTruthy(p);
for (const p of pa) {
const is_truthy = isTruthy(p);

if (has_truthy === is_truthy) return false;

if (!has_truthy && is_truthy) {
has_truthy = true;
}
}

if (has_truthy === is_truthy) {
return false;
}
return has_truthy;
};
// Custom (all truthy)
const FN_CUSTOM: FuncType<Any, Any[]> = (opts) => {
// const filtered = opts.params.filter((p) => typeof p === "boolean");
if (opts.params.length === 0) return false;

if (!has_truthy && is_truthy) {
has_truthy = true;
}
}
// Make sure that all the values are truthy
return opts.params.every(isTruthy);
};

return has_truthy;
},
],
[
"CUSTOM",
(opts) => {
// const filtered = opts.params.filter((p) => typeof p === "boolean");
if (opts.params.length === 0) return false;
// Key length variants
const FN_LEN__GT: FuncType<Any, Any[]> = (opts) => FN_LEN(opts, FN_GT);
const FN_LEN__GTE: FuncType<Any, Any[]> = (opts) => FN_LEN(opts, FN_GTE);
const FN_LEN__NGT: FuncType<Any, Any[]> = (opts) => !FN_LEN__GT(opts);
const FN_LEN__NGTE: FuncType<Any, Any[]> = (opts) => !FN_LEN__GTE(opts);
const FN_LEN__LT: FuncType<Any, Any[]> = (opts) => !FN_LEN__GTE(opts);
const FN_LEN__LTE: FuncType<Any, Any[]> = (opts) => !FN_LEN__GT(opts);
const FN_LEN__NLT: FuncType<Any, Any[]> = (opts) => FN_LEN__GTE(opts);
const FN_LEN__NLTE: FuncType<Any, Any[]> = (opts) => FN_LEN__GT(opts);
const FN_LEN__EQ: FuncType<Any, Any[]> = (opts) => FN_LEN(opts, FN_EQ);
const FN_LEN__NEQ: FuncType<Any, Any[]> = (opts) => !FN_LEN__EQ(opts);
const FN_LEN__BT: FuncType<Any, Any[]> = (opts) => FN_LEN(opts, FN_BT);
const FN_LEN__BTE: FuncType<Any, Any[]> = (opts) => FN_LEN(opts, FN_BTE);
const FN_LEN__NBT: FuncType<Any, Any[]> = (opts) => !FN_LEN__BT(opts);
const FN_LEN__NBTE: FuncType<Any, Any[]> = (opts) => !FN_LEN__BTE(opts);
const FN_LEN__IN: FuncType<Any, Any[]> = (opts) => FN_LEN(opts, FN_IN);
const FN_LEN__NIN: FuncType<Any, Any[]> = (opts) => !FN_LEN__IN(opts);

// Make sure that all the values are truthy
return opts.params.every(isTruthy);
},
],
/**
* Map of functions with their corresponding names and implementations.
* The key is the function name and the value is the function implementation.
*/
const FUNCS: Map<string, FuncType<Any, Any[]>> = new Map([
["GT", FN_GT],
["GTE", FN_GTE],
["NGT", FN_NGT],
["NGTE", FN_NGTE],
["LEN_GT", FN_LEN__GT],
["LEN_GTE", FN_LEN__GTE],
["LEN_NGT", FN_LEN__NGT],
["LEN_NGTE", FN_LEN__NGTE],
["LT", FN_LT],
["LTE", FN_LTE],
["NLT", FN_NLT],
["NLTE", FN_NLTE],
["LEN_LT", FN_LEN__LT],
["LEN_LTE", FN_LEN__LTE],
["LEN_NLT", FN_LEN__NLT],
["LEN_NLTE", FN_LEN__NLTE],
["EQ", FN_EQ],
["NEQ", FN_NEQ],
["LEN_EQ", FN_LEN__EQ],
["LEN_NEQ", FN_LEN__NEQ],
["BT", FN_BT],
["BTE", FN_BTE],
["NBT", FN_NBT],
["NBTE", FN_NBTE],
["LEN_BT", FN_LEN__BT],
["LEN_BTE", FN_LEN__BTE],
["LEN_NBT", FN_LEN__NBT],
["LEN_NBTE", FN_LEN__NBTE],
["IN", FN_IN],
["NIN", FN_NIN],
["LEN_IN", FN_LEN__IN],
["LEN_NIN", FN_LEN__NIN],
["AND", FN_AND],
["OR", FN_OR],
["XOR", FN_XOR],
["CUSTOM", FN_CUSTOM],
]);

export { FUNCS, getLen };

0 comments on commit 377d95f

Please sign in to comment.