fig-tree-evaluator 2.2.2 → 2.2.3
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 +13 -12
- package/build/helpers.js +2 -2
- package/build/index.d.ts +0 -1
- package/build/index.js +12 -7
- package/build/operators/_operatorAliases.json +3 -0
- package/build/operators/customFunctions.js +3 -3
- package/build/operators/objectProperties.d.ts +1 -1
- package/build/operators/objectProperties.js +19 -9
- package/build/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -135,7 +135,7 @@ const result = await exp.evaluate(expression, [options])
|
|
|
135
135
|
|
|
136
136
|
The `options` parameter is an object with the following available properties (all optional):
|
|
137
137
|
|
|
138
|
-
- `
|
|
138
|
+
- `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.
|
|
139
139
|
- `functions` -- a single object containing any *custom functions* available for use by the [customFunctions](#custom_functions) operator.
|
|
140
140
|
- `pgConnection` -- if you wish to make calls to a Postgres database using the [`pgSQL` operator](#pg_sql), pass a [node-postres](https://node-postgres.com/) connection object here.
|
|
141
141
|
- `graphQLConnection` -- a GraphQL connection object, if using the [`graphQL` operator](#graphql). See operator details below.
|
|
@@ -659,16 +659,16 @@ e.g.
|
|
|
659
659
|
----
|
|
660
660
|
### OBJECT_PROPERTIES
|
|
661
661
|
|
|
662
|
-
*Extracts values from objects in your application*
|
|
662
|
+
*Extracts values from data objects in your application*
|
|
663
663
|
|
|
664
|
-
Aliases: `objectProperties`, `objProps`, `getProperty`, `getObjProp`
|
|
664
|
+
Aliases: `objectProperties`, `dataProperties`,`data`, `getData`, `objProps`, `getProperty`, `getObjProp`
|
|
665
665
|
|
|
666
666
|
#### Properties
|
|
667
667
|
|
|
668
668
|
- `property` (or `path`, `propertyName`)<sup>*</sup>: (string) -- the path to the required property in the object
|
|
669
|
-
- `
|
|
669
|
+
- `additionalData` (or `additionalObjects`, `additional`, `data`): (object) -- any other objects whose properties can be referenced in `property` (see below)
|
|
670
670
|
|
|
671
|
-
|
|
671
|
+
Data objects are normally expected to be passed in to the evaluator as part of the [options](#available-options), not as part of the expression itself. This is because the source objects are expected to be values internal to your application, whereas the evaluator provides an externally configurable mechanism to extract (and process) application data. (However, it is possible to pass data objects directly as part of the expression using the `additionalObjects` property, so (in theory) data objects could be dynamically generated from other expressions.)
|
|
672
672
|
|
|
673
673
|
For example, consider a `user` object and an fig-tree evaluator instance:
|
|
674
674
|
|
|
@@ -688,7 +688,7 @@ const exp = new FigTreeEvaluator()
|
|
|
688
688
|
|
|
689
689
|
const expression = getExpressionFromConfig()
|
|
690
690
|
|
|
691
|
-
exp.evaluate(expression, {
|
|
691
|
+
exp.evaluate(expression, { data: { user } })
|
|
692
692
|
```
|
|
693
693
|
|
|
694
694
|
Here is the result of various values of `expression:`
|
|
@@ -701,7 +701,7 @@ Here is the result of various values of `expression:`
|
|
|
701
701
|
// => "Peter"
|
|
702
702
|
|
|
703
703
|
{
|
|
704
|
-
operator: '
|
|
704
|
+
operator: 'getData',
|
|
705
705
|
path: 'user.friends[1]',
|
|
706
706
|
}
|
|
707
707
|
// => "MJ"
|
|
@@ -721,7 +721,7 @@ result.map((e) => e.name)
|
|
|
721
721
|
|
|
722
722
|
The "objectProperties" operator uses [`object-property-extractor`](https://www.npmjs.com/package/object-property-extractor) internally, so please see the documentation of that package for more information.
|
|
723
723
|
|
|
724
|
-
The "objectProperties" operator will throw an error if an invalid path is provided, so it is recommended to provide a `fallback` value for the expression:
|
|
724
|
+
The "objectProperties" operator will throw an error if an invalid path is provided, so it is recommended to provide a [`fallback`](#other-common-properties) value for the expression:
|
|
725
725
|
```js
|
|
726
726
|
{
|
|
727
727
|
operator: 'objectProperties',
|
|
@@ -734,13 +734,13 @@ The "objectProperties" operator will throw an error if an invalid path is provid
|
|
|
734
734
|
`children` array: `[property]`
|
|
735
735
|
|
|
736
736
|
|
|
737
|
-
Example using "
|
|
737
|
+
Example using "data" passed in dynamically as part of expression:
|
|
738
738
|
|
|
739
739
|
```js
|
|
740
740
|
{
|
|
741
741
|
operator: 'objectProperties',
|
|
742
742
|
property: 'user.name',
|
|
743
|
-
|
|
743
|
+
additionalData: {
|
|
744
744
|
operator: '?',
|
|
745
745
|
condition: { operator: '=', values: [{ operator: '+', values: [7, 8, 9] }, 25] },
|
|
746
746
|
valueIfTrue: { user: { name: 'Bilbo' } },
|
|
@@ -1178,8 +1178,8 @@ e.g.
|
|
|
1178
1178
|
},
|
|
1179
1179
|
}
|
|
1180
1180
|
// With:
|
|
1181
|
-
//
|
|
1182
|
-
//
|
|
1181
|
+
// data = { weather: "sunny", humidity: "high" } => "NO"
|
|
1182
|
+
// data = { weather: "rainy", wind: "weak" } => "YES"
|
|
1183
1183
|
|
|
1184
1184
|
```
|
|
1185
1185
|
|
|
@@ -1411,6 +1411,7 @@ Please open an issue: https://github.com/CarlosNZ/fig-tree-evaluator/issues
|
|
|
1411
1411
|
|
|
1412
1412
|
## Changelog
|
|
1413
1413
|
|
|
1414
|
+
- **v2.2.3**: Change option `objects` name to `data` (but keep backward compatibility) (#66)
|
|
1414
1415
|
- **v2.2.2**: Option to evaluate whole object if operator nodes are deep within it (#64)
|
|
1415
1416
|
- **v2.2.1**: More efficient branch evaluation for condition/match operators (#63)
|
|
1416
1417
|
- **v2.2.0**:
|
package/build/helpers.js
CHANGED
|
@@ -142,8 +142,8 @@ var checkRequiredNodes = function (requiredProps, expression) {
|
|
|
142
142
|
exports.checkRequiredNodes = checkRequiredNodes;
|
|
143
143
|
var mergeOptions = function (origOptions, newOptions) {
|
|
144
144
|
var combinedOptions = __assign(__assign({}, origOptions), newOptions);
|
|
145
|
-
if (origOptions.
|
|
146
|
-
combinedOptions.
|
|
145
|
+
if (origOptions.data || newOptions.data)
|
|
146
|
+
combinedOptions.data = __assign(__assign({}, origOptions.data), newOptions.data);
|
|
147
147
|
if (origOptions.functions || newOptions.functions)
|
|
148
148
|
combinedOptions.functions = __assign(__assign({}, origOptions.functions), newOptions.functions);
|
|
149
149
|
if (origOptions.headers || newOptions.headers)
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -73,15 +73,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
73
73
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
74
74
|
};
|
|
75
75
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
76
|
-
exports.evaluateExpression =
|
|
76
|
+
exports.evaluateExpression = void 0;
|
|
77
77
|
var evaluate_1 = require("./evaluate");
|
|
78
78
|
var typeCheck_1 = require("./typeCheck");
|
|
79
79
|
var _operatorAliases_json_1 = __importDefault(require("./operators/_operatorAliases.json"));
|
|
80
80
|
var operators = __importStar(require("./operators"));
|
|
81
81
|
var helpers_1 = require("./helpers");
|
|
82
82
|
var pkg = require('../package.json');
|
|
83
|
-
var getVersion = function () { return pkg.version; };
|
|
84
|
-
exports.getVersion = getVersion;
|
|
85
83
|
var FigTreeEvaluator = (function () {
|
|
86
84
|
function FigTreeEvaluator(options) {
|
|
87
85
|
if (options === void 0) { options = {}; }
|
|
@@ -95,8 +93,8 @@ var FigTreeEvaluator = (function () {
|
|
|
95
93
|
return;
|
|
96
94
|
throw new Error(result);
|
|
97
95
|
};
|
|
98
|
-
this.getVersion =
|
|
99
|
-
this.options = options;
|
|
96
|
+
this.getVersion = function () { return pkg.version; };
|
|
97
|
+
this.options = standardiseOptionNames(options);
|
|
100
98
|
this.operators = operators;
|
|
101
99
|
this.operatorAliases = _operatorAliases_json_1.default;
|
|
102
100
|
}
|
|
@@ -107,7 +105,7 @@ var FigTreeEvaluator = (function () {
|
|
|
107
105
|
return __generator(this, function (_a) {
|
|
108
106
|
switch (_a.label) {
|
|
109
107
|
case 0:
|
|
110
|
-
currentOptions = (0, helpers_1.mergeOptions)(this.options, options);
|
|
108
|
+
currentOptions = (0, helpers_1.mergeOptions)(this.options, standardiseOptionNames(options));
|
|
111
109
|
return [4, (0, evaluate_1.evaluatorFunction)(expression, {
|
|
112
110
|
options: currentOptions,
|
|
113
111
|
operators: this.operators,
|
|
@@ -124,7 +122,7 @@ var FigTreeEvaluator = (function () {
|
|
|
124
122
|
return this.options;
|
|
125
123
|
};
|
|
126
124
|
FigTreeEvaluator.prototype.updateOptions = function (options) {
|
|
127
|
-
this.options = __assign(__assign({}, this.options), options);
|
|
125
|
+
this.options = __assign(__assign({}, this.options), standardiseOptionNames(options));
|
|
128
126
|
};
|
|
129
127
|
return FigTreeEvaluator;
|
|
130
128
|
}());
|
|
@@ -133,3 +131,10 @@ var evaluateExpression = function (expression, options) {
|
|
|
133
131
|
return new FigTreeEvaluator(options).evaluate(expression);
|
|
134
132
|
};
|
|
135
133
|
exports.evaluateExpression = evaluateExpression;
|
|
134
|
+
var standardiseOptionNames = function (options) {
|
|
135
|
+
if ('objects' in options) {
|
|
136
|
+
options.data = options.objects;
|
|
137
|
+
delete options.objects;
|
|
138
|
+
}
|
|
139
|
+
return options;
|
|
140
|
+
};
|
|
@@ -45,6 +45,9 @@
|
|
|
45
45
|
"patternMatch": "REGEX",
|
|
46
46
|
"regexp": "REGEX",
|
|
47
47
|
"matchPattern": "REGEX",
|
|
48
|
+
"dataProperties": "OBJECT_PROPERTIES",
|
|
49
|
+
"data": "OBJECT_PROPERTIES",
|
|
50
|
+
"getData": "OBJECT_PROPERTIES",
|
|
48
51
|
"objectProperties": "OBJECT_PROPERTIES",
|
|
49
52
|
"objProps": "OBJECT_PROPERTIES",
|
|
50
53
|
"getProperty": "OBJECT_PROPERTIES",
|
|
@@ -80,7 +80,7 @@ var propertyAliases = {
|
|
|
80
80
|
variables: 'args',
|
|
81
81
|
};
|
|
82
82
|
var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
83
|
-
var _a, functionPath, args, _b,
|
|
83
|
+
var _a, functionPath, args, _b, data, functions, func;
|
|
84
84
|
var _c, _d;
|
|
85
85
|
return __generator(this, function (_e) {
|
|
86
86
|
switch (_e.label) {
|
|
@@ -88,8 +88,8 @@ var evaluate = function (expression, config) { return __awaiter(void 0, void 0,
|
|
|
88
88
|
case 1:
|
|
89
89
|
_a = (_e.sent()), functionPath = _a[0], args = _a.slice(1);
|
|
90
90
|
config.typeChecker({ name: 'functionPath', value: functionPath, expectedType: 'string' });
|
|
91
|
-
_b = config.options,
|
|
92
|
-
func = ((_d = (_c = (0, object_property_extractor_1.default)(functions, functionPath, null)) !== null && _c !== void 0 ? _c : (0, object_property_extractor_1.default)(
|
|
91
|
+
_b = config.options, data = _b.data, functions = _b.functions;
|
|
92
|
+
func = ((_d = (_c = (0, object_property_extractor_1.default)(functions, functionPath, null)) !== null && _c !== void 0 ? _c : (0, object_property_extractor_1.default)(data, functionPath, null)) !== null && _d !== void 0 ? _d : (0, object_property_extractor_1.default)(config.options, functionPath));
|
|
93
93
|
return [4, func.apply(void 0, args)];
|
|
94
94
|
case 2: return [2, _e.sent()];
|
|
95
95
|
}
|
|
@@ -3,7 +3,7 @@ declare const requiredProperties: string[];
|
|
|
3
3
|
export type ObjPropNode = {
|
|
4
4
|
[key in typeof requiredProperties[number]]: EvaluatorNode;
|
|
5
5
|
} & BaseOperatorNode & {
|
|
6
|
-
|
|
6
|
+
additionalData: object;
|
|
7
7
|
};
|
|
8
8
|
export declare const OBJECT_PROPERTIES: OperatorObject;
|
|
9
9
|
export {};
|
|
@@ -54,24 +54,34 @@ exports.OBJECT_PROPERTIES = void 0;
|
|
|
54
54
|
var object_property_extractor_1 = __importDefault(require("object-property-extractor"));
|
|
55
55
|
var _operatorUtils_1 = require("./_operatorUtils");
|
|
56
56
|
var requiredProperties = ['property'];
|
|
57
|
-
var operatorAliases = [
|
|
57
|
+
var operatorAliases = [
|
|
58
|
+
'dataProperties',
|
|
59
|
+
'data',
|
|
60
|
+
'getData',
|
|
61
|
+
'objectProperties',
|
|
62
|
+
'objProps',
|
|
63
|
+
'getProperty',
|
|
64
|
+
'getObjProp',
|
|
65
|
+
];
|
|
58
66
|
var propertyAliases = {
|
|
59
67
|
path: 'property',
|
|
60
68
|
propertyName: 'property',
|
|
61
|
-
additional: '
|
|
62
|
-
objects: '
|
|
69
|
+
additional: 'additionalData',
|
|
70
|
+
objects: 'additionalData',
|
|
71
|
+
data: 'additionalData',
|
|
72
|
+
additionalObjects: 'additionalData',
|
|
63
73
|
};
|
|
64
74
|
var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
65
|
-
var _a, property,
|
|
75
|
+
var _a, property, additionalData, inputObject;
|
|
66
76
|
var _b, _c, _d, _e;
|
|
67
77
|
return __generator(this, function (_f) {
|
|
68
78
|
switch (_f.label) {
|
|
69
|
-
case 0: return [4, (0, _operatorUtils_1.evaluateArray)([expression.property, expression.
|
|
79
|
+
case 0: return [4, (0, _operatorUtils_1.evaluateArray)([expression.property, expression.additionalData], config)];
|
|
70
80
|
case 1:
|
|
71
|
-
_a = (_f.sent()), property = _a[0],
|
|
72
|
-
config.typeChecker({ name: 'property', value: property, expectedType: 'string' }, { name: '
|
|
73
|
-
inputObject =
|
|
74
|
-
? __assign(__assign({}, ((_c = (_b = config.options) === null || _b === void 0 ? void 0 : _b.
|
|
81
|
+
_a = (_f.sent()), property = _a[0], additionalData = _a[1];
|
|
82
|
+
config.typeChecker({ name: 'property', value: property, expectedType: 'string' }, { name: 'additionalData', value: additionalData, expectedType: ['object', 'undefined'] });
|
|
83
|
+
inputObject = additionalData
|
|
84
|
+
? __assign(__assign({}, ((_c = (_b = config.options) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {})), additionalData) : (_e = (_d = config.options) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {};
|
|
75
85
|
return [2, (0, object_property_extractor_1.default)(inputObject, property)];
|
|
76
86
|
}
|
|
77
87
|
});
|
package/build/types.d.ts
CHANGED