fig-tree-evaluator 2.4.2 → 2.5.0
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 +16 -5
- package/build/FigTreeEvaluator.d.ts +15 -0
- package/build/FigTreeEvaluator.js +134 -0
- package/build/evaluate.js +10 -5
- package/build/helpers.d.ts +1 -0
- package/build/helpers.js +3 -2
- package/build/index.d.ts +3 -14
- package/build/index.js +3 -108
- package/build/operators/_operatorUtils.js +2 -0
- package/build/operators/divide.d.ts +2 -1
- package/build/operators/divide.js +8 -11
- package/build/operators/graphQL.js +12 -8
- package/build/operators/pgSQL.js +11 -10
- package/build/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -105,7 +105,7 @@ Or, with a deeper structure that results in the same final output:
|
|
|
105
105
|
// -> 6
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
-
Which would be represented
|
|
108
|
+
Which would be represented diagrammatically with the following expression tree:
|
|
109
109
|
|
|
110
110
|

|
|
111
111
|
|
|
@@ -135,6 +135,12 @@ exp.evaluate(expression, [options]) // Options over-ride initial options for thi
|
|
|
135
135
|
const result = await exp.evaluate(expression, [options])
|
|
136
136
|
```
|
|
137
137
|
|
|
138
|
+
FigTreeEvaluator is written in **Typescript**, and the following types are available to import from the package:
|
|
139
|
+
- `FigTreeOptions`: `options` object, as per [options](#available-options) below
|
|
140
|
+
- `Operator`: string literal canonical [Operator](#operator-nodes) names (`AND`, `OR`, `EQUAL`, etc.)
|
|
141
|
+
- `EvaluatorNode`: Evaluator input
|
|
142
|
+
- `EvaluatorOutput`
|
|
143
|
+
|
|
138
144
|
## Available options
|
|
139
145
|
|
|
140
146
|
The `options` parameter is an object with the following available properties (all optional):
|
|
@@ -142,10 +148,10 @@ The `options` parameter is an object with the following available properties (al
|
|
|
142
148
|
- `data` -- a single object containing any *objects* in your application that may wish to be inspected using the [objectProperties](#object_properties) operator. (See [playground](LINK) for examples). If these objects are regularly changing, you'll probably want to pass them into each separate evaluation rather than with the initial constructor.
|
|
143
149
|
- `functions` -- a single object containing any *custom functions* available for use by the [customFunctions](#custom_functions) operator.
|
|
144
150
|
- `fragments` -- commonly-used expressions (with optional parameters) that can be re-used in any other expression. See [Fragments](#fragments)
|
|
145
|
-
- `pgConnection` -- if you wish to make calls to a Postgres database using the [`pgSQL` operator](#pg_sql), pass a [node-
|
|
151
|
+
- `pgConnection` -- if you wish to make calls to a Postgres database using the [`pgSQL` operator](#pg_sql), pass a [node-postgres](https://node-postgres.com/) connection object here.
|
|
146
152
|
- `graphQLConnection` -- a GraphQL connection object, if using the [`graphQL` operator](#graphql). See operator details below.
|
|
147
153
|
- `baseEndpoint` -- A general http headers object that will be passed to *all* http-based operators (`GET`, `POST`, `GraphQL`). Useful if all http queries are to a common server -- then each individual node will only require a relative url. See specific operator for more details.
|
|
148
|
-
- `headers` -- A general http headers object that will be passed to *all* http-based operators. Useful for
|
|
154
|
+
- `headers` -- A general http headers object that will be passed to *all* http-based operators. Useful for authentication headers, for example. Each operator and instance can have its own headers, though, so see specific operator reference for details.
|
|
149
155
|
- `returnErrorAsString` -- by default the evaluator will throw errors with invalid evaluation expressions (with helpful error messages indicating the node which threw the error and what the problem was). But if you have `returnErrorAsString: true` set, the evaluator will never throw, but instead return error messages as a valid string output. (See also the [`fallback`](#other-common-properties) parameter below)
|
|
150
156
|
- `allowJSONStringInput` -- the evaluator is expecting the input expression to be a javascript object. However, it will also accept JSON strings if this option is set to `true`. We have to perform additional logic on every evaluation input to determine if a string is a JSON expression or a standard string, so this is skipped by default for performance reasons. However, if you want to send (for example) user input directly to the evaluator without running it through your own `JSON.parse()`, then enable this option.
|
|
151
157
|
- `skipRuntimeTypeCheck` -- we perform comprehensive type checking at runtime to ensure that each operator only performs its operation on valid inputs. If type checking fails, we throw an error detailing the explicit problem. However, if `skipRuntimeTypeCheck` is set to `true`, then all inputs are passed to the operator regardless, and any errors will come from whatever standard javascript errors might be encountered (e.g. trying to pass a primitive value when an array is expected => `.map is not a function`)
|
|
@@ -1043,7 +1049,7 @@ Aliases: `pgSql`, `sql`, `postgres`, `pg`, `pgDb`
|
|
|
1043
1049
|
|
|
1044
1050
|
- `query`<sup>*</sup>: (string) -- SQL query string, with parameterised replacements (i.e. `$1`, `$2`, etc)
|
|
1045
1051
|
- `values` (or `replacements`): (array) -- replacements for the `query` parameters
|
|
1046
|
-
- `type
|
|
1052
|
+
- `type` (or `queryType`): (`"array" | "string" | "number"`) -- determines the shape of the resulting data. To quote `node-postgres`:
|
|
1047
1053
|
> By default node-postgres reads rows and collects them into JavaScript objects with the keys matching the column names and the values matching the corresponding row value for each column. If you do not need or do not want this behavior you can pass rowMode: 'array' to a query object. This will inform the result parser to bypass collecting rows into a JavaScript object, and instead will return each row as an array of values.
|
|
1048
1054
|
|
|
1049
1055
|
We extend this a step further by flattening the array, and (if `"string"` or `"number"`) converting the result to a concatenated string or (if possible) number.
|
|
@@ -1468,6 +1474,8 @@ This is different to the memoization provided by [Alias Nodes](#alias-nodes):
|
|
|
1468
1474
|
- Alias nodes are still evaluated once for every evaluation -- they're more for re-use *within* a complex expression.
|
|
1469
1475
|
- Cached nodes will persist *between* different evaluations as long as the input values are the same as a previously evaluated node.
|
|
1470
1476
|
|
|
1477
|
+
Caching is enabled by default for most of the above operators, but this can be overridden by setting `useCache: false` in [options](#available-options), either globally or per expression. If you're querying a database or API that is likely to have a different result for the same request (i.e. data has changed), then you probably want to turn the cache off.
|
|
1478
|
+
|
|
1471
1479
|
## More examples
|
|
1472
1480
|
|
|
1473
1481
|
More examples, included large, complex expressions can be found within the test suites in the [repository](https://github.com/CarlosNZ/fig-tree-evaluator).
|
|
@@ -1500,8 +1508,11 @@ Please open an issue: https://github.com/CarlosNZ/fig-tree-evaluator/issues
|
|
|
1500
1508
|
|
|
1501
1509
|
## Changelog
|
|
1502
1510
|
|
|
1503
|
-
*Trivial upgrades (e.g. documentation
|
|
1511
|
+
*Trivial upgrades (e.g. documentation, small re-factors, types, etc.) not included*
|
|
1504
1512
|
|
|
1513
|
+
- **v2.5.0**:
|
|
1514
|
+
- Bug fixes for edge cases (mainly related to backwards compatibility)
|
|
1515
|
+
- More backwards compatibility for very old (pre-v1) syntax (undocumented)
|
|
1505
1516
|
- **v2.4.1**: Small bug fix for Fragments edge case
|
|
1506
1517
|
- **v2.4.0**: Implement [Fragments](#fragments) (#74)
|
|
1507
1518
|
- **v2.3.2**: Bug fix: alias nodes not working with `evaluateFullObject` (#72)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EvaluatorNode, FigTreeOptions } from './types';
|
|
2
|
+
declare class FigTreeEvaluator {
|
|
3
|
+
private options;
|
|
4
|
+
private operators;
|
|
5
|
+
private operatorAliases;
|
|
6
|
+
private cache;
|
|
7
|
+
constructor(options?: FigTreeOptions);
|
|
8
|
+
private typeChecker;
|
|
9
|
+
evaluate(expression: EvaluatorNode, options?: FigTreeOptions): Promise<import("./types").EvaluatorOutput>;
|
|
10
|
+
getOptions(): FigTreeOptions;
|
|
11
|
+
updateOptions(options: FigTreeOptions): void;
|
|
12
|
+
getVersion: () => any;
|
|
13
|
+
}
|
|
14
|
+
export default FigTreeEvaluator;
|
|
15
|
+
export declare const evaluateExpression: (expression: EvaluatorNode, options?: FigTreeOptions) => Promise<import("./types").EvaluatorOutput>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
62
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
63
|
+
};
|
|
64
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
+
exports.evaluateExpression = void 0;
|
|
66
|
+
var evaluate_1 = require("./evaluate");
|
|
67
|
+
var typeCheck_1 = require("./typeCheck");
|
|
68
|
+
var _operatorAliases_json_1 = __importDefault(require("./operators/_operatorAliases.json"));
|
|
69
|
+
var operators = __importStar(require("./operators"));
|
|
70
|
+
var helpers_1 = require("./helpers");
|
|
71
|
+
var cache_1 = __importDefault(require("./cache"));
|
|
72
|
+
var pkg = require('../package.json');
|
|
73
|
+
var FigTreeEvaluator = (function () {
|
|
74
|
+
function FigTreeEvaluator(options) {
|
|
75
|
+
if (options === void 0) { options = {}; }
|
|
76
|
+
this.typeChecker = function () {
|
|
77
|
+
var args = [];
|
|
78
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
79
|
+
args[_i] = arguments[_i];
|
|
80
|
+
}
|
|
81
|
+
var result = typeCheck_1.typeCheck.apply(void 0, args);
|
|
82
|
+
if (result === true)
|
|
83
|
+
return;
|
|
84
|
+
throw new Error(result);
|
|
85
|
+
};
|
|
86
|
+
this.getVersion = function () { return pkg.version; };
|
|
87
|
+
this.options = standardiseOptionNames(options);
|
|
88
|
+
this.operators = operators;
|
|
89
|
+
this.operatorAliases = _operatorAliases_json_1.default;
|
|
90
|
+
this.cache = new cache_1.default(options.maxCacheSize);
|
|
91
|
+
}
|
|
92
|
+
FigTreeEvaluator.prototype.evaluate = function (expression, options) {
|
|
93
|
+
if (options === void 0) { options = {}; }
|
|
94
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
95
|
+
var currentOptions;
|
|
96
|
+
return __generator(this, function (_a) {
|
|
97
|
+
switch (_a.label) {
|
|
98
|
+
case 0:
|
|
99
|
+
currentOptions = (0, helpers_1.mergeOptions)(this.options, standardiseOptionNames(options));
|
|
100
|
+
if (currentOptions.maxCacheSize && currentOptions.maxCacheSize !== this.cache.getMax())
|
|
101
|
+
this.cache.setMax(currentOptions.maxCacheSize);
|
|
102
|
+
return [4, (0, evaluate_1.evaluatorFunction)(expression, {
|
|
103
|
+
options: currentOptions,
|
|
104
|
+
operators: this.operators,
|
|
105
|
+
operatorAliases: this.operatorAliases,
|
|
106
|
+
typeChecker: currentOptions.skipRuntimeTypeCheck ? function () { } : this.typeChecker,
|
|
107
|
+
resolvedAliasNodes: {},
|
|
108
|
+
cache: this.cache,
|
|
109
|
+
})];
|
|
110
|
+
case 1: return [2, _a.sent()];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
FigTreeEvaluator.prototype.getOptions = function () {
|
|
116
|
+
return this.options;
|
|
117
|
+
};
|
|
118
|
+
FigTreeEvaluator.prototype.updateOptions = function (options) {
|
|
119
|
+
this.options = (0, helpers_1.mergeOptions)(this.options, standardiseOptionNames(options));
|
|
120
|
+
};
|
|
121
|
+
return FigTreeEvaluator;
|
|
122
|
+
}());
|
|
123
|
+
exports.default = FigTreeEvaluator;
|
|
124
|
+
var evaluateExpression = function (expression, options) {
|
|
125
|
+
return new FigTreeEvaluator(options).evaluate(expression);
|
|
126
|
+
};
|
|
127
|
+
exports.evaluateExpression = evaluateExpression;
|
|
128
|
+
var standardiseOptionNames = function (options) {
|
|
129
|
+
if ('objects' in options) {
|
|
130
|
+
options.data = options.objects;
|
|
131
|
+
delete options.objects;
|
|
132
|
+
}
|
|
133
|
+
return options;
|
|
134
|
+
};
|
package/build/evaluate.js
CHANGED
|
@@ -51,7 +51,7 @@ 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, _a, fallback, returnErrorAsString, _b, fragment, parameters, fragmentReplacement, _c, operator, _d, _e, requiredProperties, propertyAliases, evaluate, parseChildren, _f, _g, validationError, _h, _j, _k, result, err_1, _l, outputType, evaluatedOutputType, _m, err_2, _o;
|
|
54
|
+
var options, operators, operatorAliases, expression, isOperator, isFragment, _a, fallback, returnErrorAsString, _b, fragment, parameters, fragmentReplacement, _c, operator, _d, _e, requiredProperties, propertyAliases, evaluate, parseChildren, _f, _g, validationError, _h, _j, _k, result, err_1, _l, outputType, evaluatedOutputType, _m, err_2, _o;
|
|
55
55
|
var _p, _q, _r;
|
|
56
56
|
return __generator(this, function (_s) {
|
|
57
57
|
switch (_s.label) {
|
|
@@ -64,16 +64,21 @@ var evaluatorFunction = function (input, config) { return __awaiter(void 0, void
|
|
|
64
64
|
expression = _s.sent();
|
|
65
65
|
_s.label = 2;
|
|
66
66
|
case 2:
|
|
67
|
-
|
|
67
|
+
isOperator = (0, helpers_1.isOperatorNode)(expression);
|
|
68
|
+
isFragment = (0, helpers_1.isFragmentNode)(expression);
|
|
69
|
+
if (!(options.evaluateFullObject && !isOperator && !isFragment)) return [3, 4];
|
|
68
70
|
_a = helpers_1.replaceAliasNodeValues;
|
|
69
71
|
return [4, (0, helpers_1.evaluateObject)(expression, config)];
|
|
70
72
|
case 3: return [2, _a.apply(void 0, [_s.sent(), config])];
|
|
71
73
|
case 4:
|
|
72
|
-
if (!
|
|
74
|
+
if (!isOperator && !isFragment) {
|
|
75
|
+
if (options.supportDeprecatedValueNodes && (0, helpers_1.isObject)(expression) && 'value' in expression)
|
|
76
|
+
return [2, expression.value];
|
|
73
77
|
return [2, (0, helpers_1.replaceAliasNodeValues)(expression, config)];
|
|
78
|
+
}
|
|
74
79
|
fallback = expression.fallback;
|
|
75
80
|
returnErrorAsString = (_p = options === null || options === void 0 ? void 0 : options.returnErrorAsString) !== null && _p !== void 0 ? _p : false;
|
|
76
|
-
if (!
|
|
81
|
+
if (!isFragment) return [3, 8];
|
|
77
82
|
return [4, (0, _operatorUtils_1.evaluateArray)([expression.fragment, expression.parameters], config)];
|
|
78
83
|
case 5:
|
|
79
84
|
_b = (_s.sent()), fragment = _b[0], parameters = _b[1];
|
|
@@ -138,7 +143,7 @@ var evaluatorFunction = function (input, config) { return __awaiter(void 0, void
|
|
|
138
143
|
case 22:
|
|
139
144
|
err_1 = _s.sent();
|
|
140
145
|
_l = helpers_1.fallbackOrError;
|
|
141
|
-
return [4, (0, exports.evaluatorFunction)(fallback, config)];
|
|
146
|
+
return [4, (0, exports.evaluatorFunction)(expression.fallback, config)];
|
|
142
147
|
case 23:
|
|
143
148
|
result = _l.apply(void 0, [_s.sent(), "Operator: ".concat(operator, "\n").concat((0, helpers_1.errorMessage)(err_1)), returnErrorAsString]);
|
|
144
149
|
return [3, 24];
|
package/build/helpers.d.ts
CHANGED
|
@@ -20,4 +20,5 @@ export declare const convertOutputMethods: {
|
|
|
20
20
|
[key in OutputType]: (value: EvaluatorOutput) => EvaluatorOutput | EvaluatorOutput[];
|
|
21
21
|
};
|
|
22
22
|
export declare const errorMessage: (err: unknown) => string;
|
|
23
|
+
export declare const isObject: (input: unknown) => boolean;
|
|
23
24
|
export declare const evaluateObject: (input: EvaluatorNode, config: FigTreeConfig) => Promise<EvaluatorOutput>;
|
package/build/helpers.js
CHANGED
|
@@ -47,7 +47,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.evaluateObject = exports.errorMessage = exports.convertOutputMethods = exports.mergeOptions = exports.checkRequiredNodes = exports.replaceAliasNodeValues = exports.evaluateNodeAliases = exports.mapPropertyAliases = exports.fallbackOrError = exports.truncateString = exports.getOperatorName = exports.isFragmentNode = exports.isOperatorNode = exports.parseIfJson = void 0;
|
|
50
|
+
exports.evaluateObject = exports.isObject = exports.errorMessage = exports.convertOutputMethods = exports.mergeOptions = exports.checkRequiredNodes = exports.replaceAliasNodeValues = exports.evaluateNodeAliases = exports.mapPropertyAliases = exports.fallbackOrError = exports.truncateString = exports.getOperatorName = exports.isFragmentNode = exports.isOperatorNode = exports.parseIfJson = void 0;
|
|
51
51
|
var change_case_1 = require("change-case");
|
|
52
52
|
var evaluate_1 = require("./evaluate");
|
|
53
53
|
var _operatorUtils_1 = require("./operators/_operatorUtils");
|
|
@@ -178,12 +178,13 @@ var extractNumber = function (input) {
|
|
|
178
178
|
var isObject = function (input) {
|
|
179
179
|
return typeof input === 'object' && input !== null && !Array.isArray(input);
|
|
180
180
|
};
|
|
181
|
+
exports.isObject = isObject;
|
|
181
182
|
var evaluateObject = function (input, config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
182
183
|
var newObjectEntries, result;
|
|
183
184
|
return __generator(this, function (_a) {
|
|
184
185
|
switch (_a.label) {
|
|
185
186
|
case 0:
|
|
186
|
-
if (!isObject(input))
|
|
187
|
+
if (!(0, exports.isObject)(input))
|
|
187
188
|
return [2, input];
|
|
188
189
|
newObjectEntries = [];
|
|
189
190
|
Object.entries(input).forEach(function (_a) {
|
package/build/index.d.ts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
private operators;
|
|
5
|
-
private operatorAliases;
|
|
6
|
-
private cache;
|
|
7
|
-
constructor(options?: FigTreeOptions);
|
|
8
|
-
private typeChecker;
|
|
9
|
-
evaluate(expression: EvaluatorNode, options?: FigTreeOptions): Promise<import("./types").EvaluatorOutput>;
|
|
10
|
-
getOptions(): FigTreeOptions;
|
|
11
|
-
updateOptions(options: FigTreeOptions): void;
|
|
12
|
-
getVersion: () => any;
|
|
13
|
-
}
|
|
1
|
+
import FigTreeEvaluator, { evaluateExpression } from './FigTreeEvaluator';
|
|
2
|
+
import { Operator, GenericObject, FigTreeOptions, EvaluatorNode, EvaluatorOutput } from './types';
|
|
3
|
+
export { evaluateExpression, type Operator, type GenericObject, type FigTreeOptions, type EvaluatorNode, type EvaluatorOutput, };
|
|
14
4
|
export default FigTreeEvaluator;
|
|
15
|
-
export declare const evaluateExpression: (expression: EvaluatorNode, options?: FigTreeOptions) => Promise<import("./types").EvaluatorOutput>;
|
package/build/index.js
CHANGED
|
@@ -22,113 +22,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
-
function step(op) {
|
|
39
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
42
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
-
switch (op[0]) {
|
|
44
|
-
case 0: case 1: t = op; break;
|
|
45
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
-
default:
|
|
49
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
-
if (t[2]) _.ops.pop();
|
|
54
|
-
_.trys.pop(); continue;
|
|
55
|
-
}
|
|
56
|
-
op = body.call(thisArg, _);
|
|
57
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
62
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
63
|
-
};
|
|
64
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
26
|
exports.evaluateExpression = void 0;
|
|
66
|
-
var
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var operators = __importStar(require("./operators"));
|
|
70
|
-
var helpers_1 = require("./helpers");
|
|
71
|
-
var cache_1 = __importDefault(require("./cache"));
|
|
72
|
-
var pkg = require('../package.json');
|
|
73
|
-
var FigTreeEvaluator = (function () {
|
|
74
|
-
function FigTreeEvaluator(options) {
|
|
75
|
-
if (options === void 0) { options = {}; }
|
|
76
|
-
this.typeChecker = function () {
|
|
77
|
-
var args = [];
|
|
78
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
79
|
-
args[_i] = arguments[_i];
|
|
80
|
-
}
|
|
81
|
-
var result = typeCheck_1.typeCheck.apply(void 0, args);
|
|
82
|
-
if (result === true)
|
|
83
|
-
return;
|
|
84
|
-
throw new Error(result);
|
|
85
|
-
};
|
|
86
|
-
this.getVersion = function () { return pkg.version; };
|
|
87
|
-
this.options = standardiseOptionNames(options);
|
|
88
|
-
this.operators = operators;
|
|
89
|
-
this.operatorAliases = _operatorAliases_json_1.default;
|
|
90
|
-
this.cache = new cache_1.default(options.maxCacheSize);
|
|
91
|
-
}
|
|
92
|
-
FigTreeEvaluator.prototype.evaluate = function (expression, options) {
|
|
93
|
-
if (options === void 0) { options = {}; }
|
|
94
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
95
|
-
var currentOptions;
|
|
96
|
-
return __generator(this, function (_a) {
|
|
97
|
-
switch (_a.label) {
|
|
98
|
-
case 0:
|
|
99
|
-
currentOptions = (0, helpers_1.mergeOptions)(this.options, standardiseOptionNames(options));
|
|
100
|
-
if (currentOptions.maxCacheSize && currentOptions.maxCacheSize !== this.cache.getMax())
|
|
101
|
-
this.cache.setMax(currentOptions.maxCacheSize);
|
|
102
|
-
return [4, (0, evaluate_1.evaluatorFunction)(expression, {
|
|
103
|
-
options: currentOptions,
|
|
104
|
-
operators: this.operators,
|
|
105
|
-
operatorAliases: this.operatorAliases,
|
|
106
|
-
typeChecker: currentOptions.skipRuntimeTypeCheck ? function () { } : this.typeChecker,
|
|
107
|
-
resolvedAliasNodes: {},
|
|
108
|
-
cache: this.cache,
|
|
109
|
-
})];
|
|
110
|
-
case 1: return [2, _a.sent()];
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
FigTreeEvaluator.prototype.getOptions = function () {
|
|
116
|
-
return this.options;
|
|
117
|
-
};
|
|
118
|
-
FigTreeEvaluator.prototype.updateOptions = function (options) {
|
|
119
|
-
this.options = (0, helpers_1.mergeOptions)(this.options, standardiseOptionNames(options));
|
|
120
|
-
};
|
|
121
|
-
return FigTreeEvaluator;
|
|
122
|
-
}());
|
|
123
|
-
exports.default = FigTreeEvaluator;
|
|
124
|
-
var evaluateExpression = function (expression, options) {
|
|
125
|
-
return new FigTreeEvaluator(options).evaluate(expression);
|
|
126
|
-
};
|
|
127
|
-
exports.evaluateExpression = evaluateExpression;
|
|
128
|
-
var standardiseOptionNames = function (options) {
|
|
129
|
-
if ('objects' in options) {
|
|
130
|
-
options.data = options.objects;
|
|
131
|
-
delete options.objects;
|
|
132
|
-
}
|
|
133
|
-
return options;
|
|
134
|
-
};
|
|
27
|
+
var FigTreeEvaluator_1 = __importStar(require("./FigTreeEvaluator"));
|
|
28
|
+
Object.defineProperty(exports, "evaluateExpression", { enumerable: true, get: function () { return FigTreeEvaluator_1.evaluateExpression; } });
|
|
29
|
+
exports.default = FigTreeEvaluator_1.default;
|
|
@@ -124,6 +124,8 @@ var joinUrlParts = function () {
|
|
|
124
124
|
var startSliceIndex = curr[0] === '/' ? 1 : 0;
|
|
125
125
|
var endSliceIndex = curr.slice(-1) === '/' ? -1 : undefined;
|
|
126
126
|
var newPart = curr.slice(startSliceIndex, endSliceIndex);
|
|
127
|
+
if (newPart === '')
|
|
128
|
+
return acc;
|
|
127
129
|
return acc + (index === 0 ? '' : '/') + newPart;
|
|
128
130
|
}, '');
|
|
129
131
|
};
|
|
@@ -4,8 +4,9 @@ interface DivisionNodeWithProps extends BaseOperatorNode {
|
|
|
4
4
|
dividend: EvaluatorNode;
|
|
5
5
|
divisor: EvaluatorNode;
|
|
6
6
|
}
|
|
7
|
+
type DivisionOutput = 'quotient' | 'remainder';
|
|
7
8
|
export type DivisionNode = BasicExtendedNode & DivisionNodeWithProps & {
|
|
8
|
-
output?:
|
|
9
|
+
output?: DivisionOutput;
|
|
9
10
|
};
|
|
10
11
|
export declare const DIVIDE: OperatorObject;
|
|
11
12
|
export {};
|
|
@@ -39,26 +39,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.DIVIDE = void 0;
|
|
40
40
|
var logicalAnd_1 = require("./logicalAnd");
|
|
41
41
|
var _operatorUtils_1 = require("./_operatorUtils");
|
|
42
|
-
var __1 = require("..");
|
|
43
42
|
var requiredProperties = [];
|
|
44
43
|
var operatorAliases = ['/', 'divide', '÷'];
|
|
45
44
|
var propertyAliases = { divide: 'dividend', by: 'divisor', divideBy: 'divisor' };
|
|
46
45
|
var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
47
|
-
var values;
|
|
48
|
-
var
|
|
49
|
-
return __generator(this, function (
|
|
50
|
-
switch (
|
|
51
|
-
case 0: return [4, (0, _operatorUtils_1.evaluateArray)((
|
|
46
|
+
var _a, values, output;
|
|
47
|
+
var _b;
|
|
48
|
+
return __generator(this, function (_c) {
|
|
49
|
+
switch (_c.label) {
|
|
50
|
+
case 0: return [4, (0, _operatorUtils_1.evaluateArray)([(_b = expression.values) !== null && _b !== void 0 ? _b : [expression.dividend, expression.divisor], expression.output], config)];
|
|
52
51
|
case 1:
|
|
53
|
-
|
|
54
|
-
config.typeChecker({ name: 'values', value: values, expectedType: 'array' });
|
|
52
|
+
_a = (_c.sent()), values = _a[0], output = _a[1];
|
|
53
|
+
config.typeChecker({ name: 'values', value: values, expectedType: 'array' }, { name: 'output', value: output, expectedType: ['string', 'undefined'] });
|
|
55
54
|
if (values.length < 2)
|
|
56
55
|
throw new Error('- Not enough values provided');
|
|
57
56
|
if (!values[1])
|
|
58
57
|
throw new Error('Division by zero!');
|
|
59
|
-
|
|
60
|
-
case 2:
|
|
61
|
-
switch (_b.sent()) {
|
|
58
|
+
switch (output) {
|
|
62
59
|
case 'quotient':
|
|
63
60
|
return [2, Math.floor(values[0] / values[1])];
|
|
64
61
|
case 'remainder':
|
|
@@ -90,16 +90,20 @@ var evaluate = function (expression, config) { return __awaiter(void 0, void 0,
|
|
|
90
90
|
}
|
|
91
91
|
});
|
|
92
92
|
}); };
|
|
93
|
-
var parseChildren = function (expression) { return __awaiter(void 0, void 0, void 0, function () {
|
|
93
|
+
var parseChildren = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
94
94
|
var _a, query, _b, url, fieldNames, rest, values, variables, output;
|
|
95
95
|
return __generator(this, function (_c) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
96
|
+
switch (_c.label) {
|
|
97
|
+
case 0: return [4, (0, _operatorUtils_1.evaluateArray)(expression.children, config)];
|
|
98
|
+
case 1:
|
|
99
|
+
_a = (_c.sent()), query = _a[0], _b = _a[1], url = _b === void 0 ? '' : _b, fieldNames = _a[2], rest = _a.slice(3);
|
|
100
|
+
values = rest.slice(0, fieldNames.length);
|
|
101
|
+
variables = (0, _operatorUtils_1.zipArraysToObject)(fieldNames, values);
|
|
102
|
+
output = __assign(__assign({}, expression), { query: query, url: url, variables: variables });
|
|
103
|
+
if (rest.length > fieldNames.length)
|
|
104
|
+
output.returnNode = rest.pop();
|
|
105
|
+
return [2, output];
|
|
106
|
+
}
|
|
103
107
|
});
|
|
104
108
|
}); };
|
|
105
109
|
exports.GRAPHQL = {
|
package/build/operators/pgSQL.js
CHANGED
|
@@ -60,37 +60,38 @@ exports.PG_SQL = void 0;
|
|
|
60
60
|
var _operatorUtils_1 = require("./_operatorUtils");
|
|
61
61
|
var requiredProperties = ['query'];
|
|
62
62
|
var operatorAliases = ['pgSql', 'sql', 'postgres', 'pg', 'pgDb'];
|
|
63
|
-
var propertyAliases = { replacements: 'values' };
|
|
63
|
+
var propertyAliases = { replacements: 'values', queryType: 'type' };
|
|
64
64
|
var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
65
|
-
var _a, query, values, shouldUseCache, result;
|
|
65
|
+
var _a, query, type, values, shouldUseCache, result;
|
|
66
66
|
var _b;
|
|
67
67
|
var _c, _d, _e;
|
|
68
68
|
return __generator(this, function (_f) {
|
|
69
69
|
switch (_f.label) {
|
|
70
|
-
case 0: return [4, (0, _operatorUtils_1.evaluateArray)(__spreadArray([expression.query], (expression.values || []), true), config)];
|
|
70
|
+
case 0: return [4, (0, _operatorUtils_1.evaluateArray)(__spreadArray([expression.query, expression.type], (expression.values || []), true), config)];
|
|
71
71
|
case 1:
|
|
72
|
-
_a = (_f.sent()), query = _a[0], values = _a.slice(
|
|
73
|
-
config.typeChecker({ name: 'query', value: query, expectedType: 'string' }, { name: 'values', value: values, expectedType: 'array' });
|
|
72
|
+
_a = (_f.sent()), query = _a[0], type = _a[1], values = _a.slice(2);
|
|
73
|
+
config.typeChecker({ name: 'query', value: query, expectedType: 'string' }, { name: 'values', value: values, expectedType: 'array' }, { name: 'type', value: type, expectedType: ['string', 'undefined'] });
|
|
74
74
|
if (!((_c = config.options) === null || _c === void 0 ? void 0 : _c.pgConnection))
|
|
75
75
|
throw new Error('No Postgres database connection provided');
|
|
76
76
|
shouldUseCache = (_e = (_d = expression.useCache) !== null && _d !== void 0 ? _d : config.options.useCache) !== null && _e !== void 0 ? _e : true;
|
|
77
77
|
try {
|
|
78
78
|
result = (_b = config.cache).useCache.apply(_b, __spreadArray([shouldUseCache,
|
|
79
|
-
function (query) {
|
|
79
|
+
function (query, type) {
|
|
80
80
|
var values = [];
|
|
81
|
-
for (var _i =
|
|
82
|
-
values[_i -
|
|
81
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
82
|
+
values[_i - 2] = arguments[_i];
|
|
83
83
|
}
|
|
84
84
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
85
85
|
return __generator(this, function (_a) {
|
|
86
86
|
switch (_a.label) {
|
|
87
|
-
case 0: return [4, processPgSQL(__spreadArray([query], values, true), config.options.pgConnection,
|
|
87
|
+
case 0: return [4, processPgSQL(__spreadArray([query], values, true), config.options.pgConnection, type)];
|
|
88
88
|
case 1: return [2, _a.sent()];
|
|
89
89
|
}
|
|
90
90
|
});
|
|
91
91
|
});
|
|
92
92
|
},
|
|
93
|
-
query
|
|
93
|
+
query,
|
|
94
|
+
type], values, false));
|
|
94
95
|
return [2, result];
|
|
95
96
|
}
|
|
96
97
|
catch (err) {
|
package/build/types.d.ts
CHANGED