@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
52 lines (51 loc) • 1.64 kB
JavaScript
import { current } from "../parser/parser-context.js";
const COMPARISON_PRECEDENCE = {
"==": 30,
"!=": 30,
"<": 40,
">": 40,
"<=": 40,
">=": 40,
};
export const comparisonPlugin = {
name: "comparison",
infixPrecedence: (ctx) => {
const tok = current(ctx);
if (tok.type === "Operator") {
return COMPARISON_PRECEDENCE[tok.value];
}
return undefined;
},
optimize: (node, opt) => {
if (node.type === "BinaryExpression" && node.operator in COMPARISON_PRECEDENCE) {
const left = opt(node.left);
const right = opt(node.right);
return { ...node, left, right };
}
return null;
},
evaluate: (node, context, evalFn) => {
if (node.type === "BinaryExpression") {
const op = node.operator;
if (op in COMPARISON_PRECEDENCE) {
const left = evalFn(node.left, context);
const right = evalFn(node.right, context);
switch (op) {
case "==":
return { value: left === right };
case "!=":
return { value: left !== right };
case "<":
return { value: left < right };
case ">":
return { value: left > right };
case "<=":
return { value: left <= right };
case ">=":
return { value: left >= right };
}
}
}
return null;
},
};