fig-tree-evaluator 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,7 +59,7 @@ A range of built-in operators are available, from simple logic, arithmetic and s
59
59
 
60
60
  Fig-tree evaluates expressions structured in a JSON/Javascript object [expression tree](https://www.geeksforgeeks.org/expression-tree/). A single "node" of the tree consists of an **Operator**, with associated parameters (or child nodes), each of which can itself be another Operator node -- i.e. a recursive tree structure of arbitrary depth and complexity.
61
61
 
62
- A wide range of [operators are available](#operator-reference), but [custom fuctions](#custom_functions) can be added to your implementation if you wish to extend the base functionality.
62
+ A wide range of [operators are available](#operator-reference), but [custom functions](#custom_functions) can be added to your implementation if you wish to extend the base functionality.
63
63
 
64
64
  For example:
65
65
 
@@ -1255,6 +1255,8 @@ Aliases: `customFunctions`, `customFunction`, `objectFunctions`, `functions`, `f
1255
1255
  - `functionPath` (or `functionsPath`, `functionName`, `funcPath`<sup>*</sup>: (string) -- path to where the function resides in the `options.functions` object
1256
1256
  - `args` (or `arguments`, `variables`): (array) -- input arguments for the function
1257
1257
 
1258
+ Custom functions provide a mechanism to extend the functionality of the FigTree evaluator to suit almost any requirements.
1259
+
1258
1260
  Custom functions are stored in the evaluator `options`, in the `functions` property.
1259
1261
 
1260
1262
  For examples, consider the following fig-tree instance:
@@ -1307,7 +1309,7 @@ e.g.
1307
1309
 
1308
1310
  ## Alias Nodes
1309
1311
 
1310
- If you have a node that is used more than once in a complex expression, it's possible to just evaluate the repeated node once, and refer to it throughout using an "alias" reference.
1312
+ If you have a node that is used more than once in a complex expression, it's possible to just evaluate the repeated node once, and refer to it throughout using an "alias" reference. This allows for a simpler expression (reduces duplication) as well as a performance improvement, since the aliased node is only evaluated once, providing a simple [memoization](https://en.wikipedia.org/wiki/Memoization) mechanism.
1311
1313
 
1312
1314
  For example, if you have the expression:
1313
1315
  ```js
@@ -10,13 +10,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CONDITIONAL = void 0;
13
- const _operatorUtils_1 = require("./_operatorUtils");
13
+ const evaluate_1 = require("../evaluate");
14
14
  const requiredProperties = ['condition', 'valueIfTrue', 'valueIfFalse'];
15
15
  const operatorAliases = ['?', 'conditional', 'ifThen'];
16
16
  const propertyAliases = { ifTrue: 'valueIfTrue', ifFalse: 'valueIfFalse', ifNot: 'valueIfFalse' };
17
17
  const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, function* () {
18
- const [condition, valueIfTrue, valueIfFalse] = yield (0, _operatorUtils_1.evaluateArray)([expression.condition, expression.valueIfTrue, expression.valueIfFalse], config);
19
- return !!condition ? valueIfTrue : valueIfFalse;
18
+ const condition = yield (0, evaluate_1.evaluatorFunction)(expression.condition, config);
19
+ return !!condition
20
+ ? yield (0, evaluate_1.evaluatorFunction)(expression.valueIfTrue, config)
21
+ : yield (0, evaluate_1.evaluatorFunction)(expression.valueIfFalse, config);
20
22
  });
21
23
  const parseChildren = (expression) => {
22
24
  const [condition, valueIfTrue, valueIfFalse] = expression.children;
@@ -13,7 +13,7 @@ exports.GRAPHQL = void 0;
13
13
  const _operatorUtils_1 = require("./_operatorUtils");
14
14
  const requiredProperties = ['query'];
15
15
  const operatorAliases = ['graphQl', 'graphql', 'gql'];
16
- const propertyAliases = { endpoint: 'url', outputNode: 'returnNode', returnPropery: 'returnNode' };
16
+ const propertyAliases = { endpoint: 'url', outputNode: 'returnNode', returnProperty: 'returnNode' };
17
17
  const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, function* () {
18
18
  var _a, _b, _c, _d, _e;
19
19
  const [query, urlObj, variables, returnNode, headers] = (yield (0, _operatorUtils_1.evaluateArray)([
@@ -12,11 +12,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MATCH = void 0;
13
13
  const _operatorUtils_1 = require("./_operatorUtils");
14
14
  const evaluate_1 = require("../evaluate");
15
+ const helpers_1 = require("../helpers");
15
16
  const requiredProperties = ['matchExpression'];
16
17
  const operatorAliases = ['match', 'switch'];
17
18
  const propertyAliases = { arms: 'branches', cases: 'branches', match: 'matchExpression' };
18
19
  const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, function* () {
19
- const [matchExpression, branches] = (yield (0, _operatorUtils_1.evaluateArray)([expression.matchExpression, expression.branches], config));
20
+ const matchExpression = (yield (0, evaluate_1.evaluatorFunction)(expression.matchExpression, config));
21
+ const branches = expression.branches;
20
22
  config.typeChecker({
21
23
  name: 'matchExpression',
22
24
  value: matchExpression,
@@ -26,7 +28,9 @@ const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, funct
26
28
  value: branches,
27
29
  expectedType: ['object', 'array', 'undefined'],
28
30
  });
29
- const branchObject = Array.isArray(branches) ? (0, _operatorUtils_1.singleArrayToObject)(branches) : branches !== null && branches !== void 0 ? branches : {};
31
+ let branchObject = Array.isArray(branches) ? (0, _operatorUtils_1.singleArrayToObject)(branches) : branches !== null && branches !== void 0 ? branches : {};
32
+ if ((0, helpers_1.isOperatorNode)(branchObject))
33
+ branchObject = yield (0, evaluate_1.evaluatorFunction)(branchObject, config);
30
34
  if (matchExpression in branchObject)
31
35
  return yield (0, evaluate_1.evaluatorFunction)(branchObject[matchExpression], config);
32
36
  if ('fallback' in branchObject)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fig-tree-evaluator",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Module to evaluate JSON-structured expression trees",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",