fig-tree-evaluator 2.3.0 → 2.3.2

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
@@ -1314,7 +1314,7 @@ e.g.
1314
1314
 
1315
1315
  ## Alias Nodes
1316
1316
 
1317
- 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.
1317
+ 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 code 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. (See also [Caching/Memoization](#caching-memoization))
1318
1318
 
1319
1319
  For example, if you have the expression:
1320
1320
  ```js
@@ -1385,7 +1385,7 @@ Like all expression nodes, alias nodes can themselves contain complex expression
1385
1385
 
1386
1386
  ## Caching (Memoization)
1387
1387
 
1388
- FigTree Evaluator has basic memoization functionality for certain nodes to speed up re-evaluation of previously evaluated nodes. There is a single cache store per FigTree instance which persists for the lifetime of the instance. By default, it remembers the last 50 results, but this can be modified using the `maxCacheSize` option.
1388
+ FigTree Evaluator has basic [memoization](https://en.wikipedia.org/wiki/Memoization) functionality for certain nodes to speed up re-evaluation when the input parameters haven't changed. There is a single cache store per FigTree instance which persists for the lifetime of the instance. By default, it remembers the last 50 results, but this can be modified using the `maxCacheSize` option.
1389
1389
 
1390
1390
  Currently, caching is only implemented for the following operators, since they perform requests to external resources, which are inherently slow:
1391
1391
 
@@ -1395,6 +1395,10 @@ Currently, caching is only implemented for the following operators, since they p
1395
1395
  - GRAPH_QL (`useCache` default: `true`)
1396
1396
  - CUSTOM_FUNCTIONS (`useCache` default: `false`)
1397
1397
 
1398
+ This is different to the memoization provided by [Alias Nodes](#alias-nodes):
1399
+ - Alias nodes are still evaluated once for every evaluation -- they're more for re-use *within* a complex expression.
1400
+ - Cached nodes will persist *between* different evaluations as long as the input values are the same as a previously evaluated node.
1401
+
1398
1402
  ## More examples
1399
1403
 
1400
1404
  More examples, included large, complex expressions can be found within the test suites in the [repository](https://github.com/CarlosNZ/fig-tree-evaluator).
@@ -1427,6 +1431,7 @@ Please open an issue: https://github.com/CarlosNZ/fig-tree-evaluator/issues
1427
1431
 
1428
1432
  ## Changelog
1429
1433
 
1434
+ - **v2.3.2**: Bug fix: alias nodes not working with `evaluateFullObject` (#72)
1430
1435
  - **v2.3.0**: Implement [caching/memoization](#caching-memoization) (#68)
1431
1436
  - **v2.2.3**: Change option `objects` name to `data` (but keep backward compatibility) (#66)
1432
1437
  - **v2.2.2**: Option to evaluate whole object if operator nodes are deep within it (#64)
package/build/cache.js CHANGED
@@ -59,7 +59,6 @@ var FigTreeCache = (function () {
59
59
  if (key in this.store) {
60
60
  this.queue = this.queue.filter(function (val) { return val !== key; });
61
61
  this.queue.unshift(key);
62
- console.log('Using cached result:', this.store[key]);
63
62
  return [2, this.store[key]];
64
63
  }
65
64
  return [4, action.apply(void 0, args)];
package/build/evaluate.js CHANGED
@@ -51,99 +51,100 @@ exports.evaluatorFunction = void 0;
51
51
  var _operatorUtils_1 = require("./operators/_operatorUtils");
52
52
  var helpers_1 = require("./helpers");
53
53
  var evaluatorFunction = function (input, config) { return __awaiter(void 0, void 0, void 0, function () {
54
- var options, operators, operatorAliases, expression, fallback, returnErrorAsString, operator, _a, _b, requiredProperties, propertyAliases, evaluate, parseChildren, _c, _d, validationError, _e, _f, _g, result, err_1, _h, outputType, evaluatedOutputType, _j, err_2, _k;
55
- var _l, _m;
56
- return __generator(this, function (_o) {
57
- switch (_o.label) {
54
+ var options, operators, operatorAliases, expression, _a, fallback, returnErrorAsString, operator, _b, _c, requiredProperties, propertyAliases, evaluate, parseChildren, _d, _e, validationError, _f, _g, _h, result, err_1, _j, outputType, evaluatedOutputType, _k, err_2, _l;
55
+ var _m, _o;
56
+ return __generator(this, function (_p) {
57
+ switch (_p.label) {
58
58
  case 0:
59
59
  options = config.options, operators = config.operators, operatorAliases = config.operatorAliases;
60
60
  expression = (options === null || options === void 0 ? void 0 : options.allowJSONStringInput) ? (0, helpers_1.parseIfJson)(input) : input;
61
61
  if (!Array.isArray(expression)) return [3, 2];
62
62
  return [4, (0, _operatorUtils_1.evaluateArray)(expression, config)];
63
63
  case 1:
64
- expression = _o.sent();
65
- _o.label = 2;
64
+ expression = _p.sent();
65
+ _p.label = 2;
66
66
  case 2:
67
67
  if (!(options.evaluateFullObject && !(0, helpers_1.isOperatorNode)(expression))) return [3, 4];
68
+ _a = helpers_1.replaceAliasNodeValues;
68
69
  return [4, (0, helpers_1.evaluateObject)(expression, config)];
69
- case 3: return [2, _o.sent()];
70
+ case 3: return [2, _a.apply(void 0, [_p.sent(), config])];
70
71
  case 4:
71
72
  if (!(0, helpers_1.isOperatorNode)(expression))
72
73
  return [2, (0, helpers_1.replaceAliasNodeValues)(expression, config)];
73
74
  fallback = expression.fallback;
74
- returnErrorAsString = (_l = options === null || options === void 0 ? void 0 : options.returnErrorAsString) !== null && _l !== void 0 ? _l : false;
75
- _o.label = 5;
75
+ returnErrorAsString = (_m = options === null || options === void 0 ? void 0 : options.returnErrorAsString) !== null && _m !== void 0 ? _m : false;
76
+ _p.label = 5;
76
77
  case 5:
77
- _o.trys.push([5, 26, , 28]);
78
+ _p.trys.push([5, 26, , 28]);
78
79
  operator = (0, helpers_1.getOperatorName)(expression.operator, operatorAliases);
79
80
  if (!!operator) return [3, 7];
80
- _a = helpers_1.fallbackOrError;
81
+ _b = helpers_1.fallbackOrError;
81
82
  return [4, (0, exports.evaluatorFunction)(fallback, config)];
82
- case 6: return [2, _a.apply(void 0, [_o.sent(), "Invalid operator: ".concat(expression.operator), returnErrorAsString])];
83
+ case 6: return [2, _b.apply(void 0, [_p.sent(), "Invalid operator: ".concat(expression.operator), returnErrorAsString])];
83
84
  case 7:
84
- _b = operators[operator], requiredProperties = _b.requiredProperties, propertyAliases = _b.propertyAliases, evaluate = _b.evaluate, parseChildren = _b.parseChildren;
85
+ _c = operators[operator], requiredProperties = _c.requiredProperties, propertyAliases = _c.propertyAliases, evaluate = _c.evaluate, parseChildren = _c.parseChildren;
85
86
  expression = (0, helpers_1.mapPropertyAliases)(propertyAliases, expression);
86
- _c = config;
87
- _d = [__assign({}, config.resolvedAliasNodes)];
87
+ _d = config;
88
+ _e = [__assign({}, config.resolvedAliasNodes)];
88
89
  return [4, (0, helpers_1.evaluateNodeAliases)(expression, config)];
89
90
  case 8:
90
- _c.resolvedAliasNodes = __assign.apply(void 0, _d.concat([(_o.sent())]));
91
+ _d.resolvedAliasNodes = __assign.apply(void 0, _e.concat([(_p.sent())]));
91
92
  validationError = (0, helpers_1.checkRequiredNodes)(requiredProperties, expression);
92
93
  if (!validationError) return [3, 10];
93
- _e = helpers_1.fallbackOrError;
94
+ _f = helpers_1.fallbackOrError;
94
95
  return [4, (0, exports.evaluatorFunction)(fallback, config)];
95
- case 9: return [2, _e.apply(void 0, [_o.sent(), "Operator: ".concat(operator, "\n- ").concat(validationError), returnErrorAsString])];
96
+ case 9: return [2, _f.apply(void 0, [_p.sent(), "Operator: ".concat(operator, "\n- ").concat(validationError), returnErrorAsString])];
96
97
  case 10:
97
98
  if (!('children' in expression)) return [3, 16];
98
99
  if (!!Array.isArray(expression.children)) return [3, 12];
99
- _f = expression;
100
+ _g = expression;
100
101
  return [4, (0, exports.evaluatorFunction)(expression.children, config)];
101
102
  case 11:
102
- _f.children = _o.sent();
103
- _o.label = 12;
103
+ _g.children = _p.sent();
104
+ _p.label = 12;
104
105
  case 12:
105
106
  if (!!Array.isArray(expression.children)) return [3, 14];
106
- _g = helpers_1.fallbackOrError;
107
+ _h = helpers_1.fallbackOrError;
107
108
  return [4, (0, exports.evaluatorFunction)(fallback, config)];
108
- case 13: return [2, _g.apply(void 0, [_o.sent(), "Operator: ".concat(operator, "\n- Property \"children\" is not of type: array"), returnErrorAsString])];
109
+ case 13: return [2, _h.apply(void 0, [_p.sent(), "Operator: ".concat(operator, "\n- Property \"children\" is not of type: array"), returnErrorAsString])];
109
110
  case 14: return [4, parseChildren(expression, config)];
110
111
  case 15:
111
- expression = _o.sent();
112
- _o.label = 16;
112
+ expression = _p.sent();
113
+ _p.label = 16;
113
114
  case 16:
114
115
  result = void 0;
115
- _o.label = 17;
116
+ _p.label = 17;
116
117
  case 17:
117
- _o.trys.push([17, 19, , 21]);
118
+ _p.trys.push([17, 19, , 21]);
118
119
  return [4, evaluate(expression, config)];
119
120
  case 18:
120
- result = _o.sent();
121
+ result = _p.sent();
121
122
  return [3, 21];
122
123
  case 19:
123
- err_1 = _o.sent();
124
- _h = helpers_1.fallbackOrError;
124
+ err_1 = _p.sent();
125
+ _j = helpers_1.fallbackOrError;
125
126
  return [4, (0, exports.evaluatorFunction)(fallback, config)];
126
127
  case 20:
127
- result = _h.apply(void 0, [_o.sent(), "Operator: ".concat(operator, "\n").concat((0, helpers_1.errorMessage)(err_1)), returnErrorAsString]);
128
+ result = _j.apply(void 0, [_p.sent(), "Operator: ".concat(operator, "\n").concat((0, helpers_1.errorMessage)(err_1)), returnErrorAsString]);
128
129
  return [3, 21];
129
130
  case 21:
130
- outputType = (_m = expression === null || expression === void 0 ? void 0 : expression.outputType) !== null && _m !== void 0 ? _m : expression === null || expression === void 0 ? void 0 : expression.type;
131
+ outputType = (_o = expression === null || expression === void 0 ? void 0 : expression.outputType) !== null && _o !== void 0 ? _o : expression === null || expression === void 0 ? void 0 : expression.type;
131
132
  if (!outputType)
132
133
  return [2, result];
133
134
  return [4, (0, exports.evaluatorFunction)(outputType, config)];
134
135
  case 22:
135
- evaluatedOutputType = (_o.sent());
136
+ evaluatedOutputType = (_p.sent());
136
137
  if (!!(evaluatedOutputType in helpers_1.convertOutputMethods)) return [3, 24];
137
- _j = helpers_1.fallbackOrError;
138
+ _k = helpers_1.fallbackOrError;
138
139
  return [4, (0, exports.evaluatorFunction)(fallback, config)];
139
- case 23: return [2, _j.apply(void 0, [_o.sent(), "Operator: ".concat(operator, "\n- Invalid output type: ").concat(evaluatedOutputType), returnErrorAsString])];
140
+ case 23: return [2, _k.apply(void 0, [_p.sent(), "Operator: ".concat(operator, "\n- Invalid output type: ").concat(evaluatedOutputType), returnErrorAsString])];
140
141
  case 24: return [2, helpers_1.convertOutputMethods[evaluatedOutputType](result)];
141
142
  case 25: return [3, 28];
142
143
  case 26:
143
- err_2 = _o.sent();
144
- _k = helpers_1.fallbackOrError;
144
+ err_2 = _p.sent();
145
+ _l = helpers_1.fallbackOrError;
145
146
  return [4, (0, exports.evaluatorFunction)(fallback, config)];
146
- case 27: return [2, _k.apply(void 0, [_o.sent(), (0, helpers_1.errorMessage)(err_2),
147
+ case 27: return [2, _l.apply(void 0, [_p.sent(), (0, helpers_1.errorMessage)(err_2),
147
148
  returnErrorAsString])];
148
149
  case 28: return [2];
149
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fig-tree-evaluator",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "description": "Module to evaluate JSON-structured expression trees",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",