Skip to content

Commit

Permalink
Faster rule creation
Browse files Browse the repository at this point in the history
  • Loading branch information
akmjenkins committed Mar 7, 2022
1 parent f681d8c commit 314bb2e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/rule.js
Expand Up @@ -34,13 +34,21 @@ const createStatefulRule = (
{ when, then, otherwise, options: ruleOpts = {} },
options,
) => {
let t, o;
const interpolatableOptions = { ...options, ...ruleOpts };
when = when.map((w) => createStatefulMap(w, interpolatableOptions));
then = interpolatable(then, interpolatableOptions);
otherwise = interpolatable(otherwise, interpolatableOptions);

return (context) =>
(when.some((w) => w(context)) ? then : otherwise)(context);
return (context) => {
let which;
const pass = when.some((w) => w(context));

if (pass) {
which = t || (t = interpolatable(then, interpolatableOptions));
} else {
which = o || (o = interpolatable(otherwise, interpolatableOptions));
}
return which(context);
};
};

export const createStatefulRules = (rules, options) => {
Expand Down

0 comments on commit 314bb2e

Please sign in to comment.