Skip to content
Permalink
Browse files

fix(jexl): add custom intersects operator to jexl AST parser (#183)

  • Loading branch information...
czosel committed May 1, 2019
1 parent a50a442 commit 30ddf10f55988eb6c4028c9de2c191899333f623
Showing with 33 additions and 4 deletions.
  1. +2 −4 addon/lib/document.js
  2. +4 −0 addon/utils/jexl.js
  3. +27 −0 tests/unit/utils/jexl-test.js
@@ -5,7 +5,7 @@ import Field from "ember-caluma/lib/field";
import jexl from "jexl";
import { atob } from "ember-caluma/helpers/atob";
import { inject as service } from "@ember/service";
// import { findFieldInTree } from "ember-caluma/utils/tree";
import { intersects } from "ember-caluma/utils/jexl";

const STATE_PRECEDENCE = ["invalid", "unfinished", "untouched", "valid"];

@@ -72,9 +72,7 @@ export default EmberObject.extend({
questionJexl.addTransform("answer", slugWithPath =>
this.findAnswer(slugWithPath)
);
questionJexl.addBinaryOp("intersects", 20, (left, right) =>
left.some(val => right.includes(val))
);
questionJexl.addBinaryOp("intersects", 20, intersects);

return questionJexl;
}),
@@ -1,6 +1,9 @@
import Parser from "jexl/lib/parser/Parser";
import jexl from "jexl";

export const intersects = (left, right) =>
left.some(val => right.includes(val));

/**
* Transform a JEXL expression into it's AST
*
@@ -9,6 +12,7 @@ import jexl from "jexl";
* @return {Object} AST
*/
export const getAST = expression => {
jexl.addBinaryOp("intersects", 20, intersects);
let grammar = jexl._getGrammar();
let parser = new Parser(grammar);

@@ -53,4 +53,31 @@ module("Unit | Utility | jexl", function(hooks) {
]
);
});

test("custom intersects operator works", async function(assert) {
assert.expect(1);

assert.deepEqual(getAST("[1] intersects [1]"), {
left: {
type: "ArrayLiteral",
value: [
{
type: "Literal",
value: 1
}
]
},
operator: "intersects",
right: {
type: "ArrayLiteral",
value: [
{
type: "Literal",
value: 1
}
]
},
type: "BinaryExpression"
});
});
});

0 comments on commit 30ddf10

Please sign in to comment.
You can’t perform that action at this time.