mathsteps-experimental-fork
Version:
Step by step math solutions. Experimental Fork
34 lines (31 loc) • 1.15 kB
JavaScript
import { makeKeyFromNode } from './nodeServices/nodeCacher.js';
import { applyRules } from '../simplifyExpression/kemuSimplifyCommonServices.js';
function createFunctionForEveryRule(rules, onApplyRuleFunction = applyRules) {
const fns = [];
rules.forEach((rule) => {
rule.ogId = rule.id;
if (fns.some((f) => f.ogId === rule.id)) {
const length = fns.filter((f) => f.ogId === rule.id).length + 1;
rule.id = `${rule.id}__CASE_${length}`;
}
const cache = /* @__PURE__ */ new Map();
const fn = (node, arg2, arg3, arg4) => {
const isOptionsObjWithGetMistakes = arg2?.getMistakes || arg3?.getMistakes || arg4?.getMistakes || false;
const key = makeKeyFromNode(node);
if (cache.has(key)) {
return cache.get(key);
}
const result = onApplyRuleFunction(node, [rule], { getMistakes: isOptionsObjWithGetMistakes });
cache.set(key, result);
return result;
};
Object.defineProperty(fn, "name", { value: rule.id, writable: false });
fns.push({
id: rule.id,
ogId: rule.ogId,
fn
});
});
return fns;
}
export { createFunctionForEveryRule };