fig-tree-evaluator 1.9.2 → 2.0.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 +24 -1
- package/build/helpers.js +8 -7
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# fig-tree-evaluator
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+
|
|
3
5
|
**FigTree Evaluator** is a module to evaluate JSON-structured expression trees.
|
|
4
6
|
|
|
5
7
|
A typical use case would be for evaluating **configuration** files, where you need to store dynamic values or arbitrary logic without allowing users to inject executable code. For example, a [form-builder app](https://github.com/openmsupply/conforma-web-app) might need to allow complex conditional logic for form element visibility based on previous responses, or for validation beyond what is available in standard validation libraries.
|
|
@@ -45,6 +47,8 @@ A range of built-in operators are available, from simple logic, arithmetic and s
|
|
|
45
47
|
- [Development environment](#development-environment)
|
|
46
48
|
- [Tests](#tests)
|
|
47
49
|
- [Help, Feedback, Suggestions](#help-feedback-suggestions)
|
|
50
|
+
- [Changelog](#changelog)
|
|
51
|
+
- [Credit](#credit)
|
|
48
52
|
|
|
49
53
|
<!-- /TOC -->
|
|
50
54
|
## The basics
|
|
@@ -1225,6 +1229,8 @@ After cloning:
|
|
|
1225
1229
|
|
|
1226
1230
|
`yarn demo` -- launch a local version of the demo playground in your browser for building and testing expressions
|
|
1227
1231
|
|
|
1232
|
+
`yarn dev` -- execute anything in `/dev/playground.ts`. Use for testing/debugging.
|
|
1233
|
+
|
|
1228
1234
|
## Tests
|
|
1229
1235
|
|
|
1230
1236
|
There is a comprehensive [Jest](https://jestjs.io/) test suite for all aspects of fig-tree. To run all tests:
|
|
@@ -1237,4 +1243,21 @@ Individual tests can be run by string matching the argument to the test filename
|
|
|
1237
1243
|
|
|
1238
1244
|
## Help, Feedback, Suggestions
|
|
1239
1245
|
|
|
1240
|
-
Please open an issue: https://github.com/CarlosNZ/fig-tree-evaluator/issues
|
|
1246
|
+
Please open an issue: https://github.com/CarlosNZ/fig-tree-evaluator/issues
|
|
1247
|
+
|
|
1248
|
+
## Changelog
|
|
1249
|
+
|
|
1250
|
+
- **v2.0.0**: Re-write as stand-alone package. Major improvements include:
|
|
1251
|
+
- more [operators](#operator-reference)
|
|
1252
|
+
- operator (and property) [aliases](#operator--property-aliases)
|
|
1253
|
+
- more appropriately-named properties associated with each operator (as opposed to a single `children` array)
|
|
1254
|
+
- class-based Evaluator instances
|
|
1255
|
+
- runtime type-checking
|
|
1256
|
+
- better error handling and error reporting
|
|
1257
|
+
- more flexible output conversion
|
|
1258
|
+
- more well-organised codebase
|
|
1259
|
+
- **v1.x.x**: created specifically for [Conforma](https://github.com/openmsupply/conforma-server/wiki/Query-Syntax) application manager by [mSupplyFoundation](https://github.com/openmsupply). v2 is a complete re-write with numerous improvments, but should be 99% backwards compatible.
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
## Credit
|
|
1263
|
+
Icon: Tree by ka reemov from <a href="https://thenounproject.com/icon/tree-2665898/" target="_blank" title="Tree Icon">Noun Project</a>
|
package/build/helpers.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.errorMessage = exports.convertOutputMethods = exports.checkRequiredNodes = exports.mapPropertyAliases = exports.fallbackOrError = exports.truncateString = exports.getOperatorName = exports.isOperatorNode = exports.parseIfJson = void 0;
|
|
7
|
-
const
|
|
8
|
-
const camelCase_1 = __importDefault(require("lodash/camelCase"));
|
|
4
|
+
const change_case_1 = require("change-case");
|
|
9
5
|
const parseIfJson = (input) => {
|
|
10
6
|
if (typeof input !== 'string')
|
|
11
7
|
return input;
|
|
@@ -21,7 +17,7 @@ exports.parseIfJson = parseIfJson;
|
|
|
21
17
|
const isOperatorNode = (input) => input instanceof Object && 'operator' in input;
|
|
22
18
|
exports.isOperatorNode = isOperatorNode;
|
|
23
19
|
const standardiseOperatorName = (name) => {
|
|
24
|
-
const camelCaseName = (0,
|
|
20
|
+
const camelCaseName = (0, change_case_1.camelCase)(name);
|
|
25
21
|
return camelCaseName ? camelCaseName : name;
|
|
26
22
|
};
|
|
27
23
|
const getOperatorName = (operator, operatorAliases) => operatorAliases[standardiseOperatorName(operator)];
|
|
@@ -37,8 +33,13 @@ const fallbackOrError = (fallback, errorMessage, returnErrorAsString = false) =>
|
|
|
37
33
|
throw new Error((0, exports.truncateString)(errorMessage));
|
|
38
34
|
};
|
|
39
35
|
exports.fallbackOrError = fallbackOrError;
|
|
40
|
-
const mapPropertyAliases = (propertyAliases, expression) => (
|
|
36
|
+
const mapPropertyAliases = (propertyAliases, expression) => mapObjectKeys(expression, (key) => key in propertyAliases ? propertyAliases[key] : key);
|
|
41
37
|
exports.mapPropertyAliases = mapPropertyAliases;
|
|
38
|
+
const mapObjectKeys = (inputObj, mapFunction) => {
|
|
39
|
+
const keyVals = Object.entries(inputObj);
|
|
40
|
+
const mappedKeys = keyVals.map(([key, value]) => [mapFunction(key), value]);
|
|
41
|
+
return Object.fromEntries(mappedKeys);
|
|
42
|
+
};
|
|
42
43
|
const checkRequiredNodes = (requiredProps, expression) => {
|
|
43
44
|
const missingProps = requiredProps.filter((prop) => !(prop in expression));
|
|
44
45
|
if (missingProps.length === 0)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fig-tree-evaluator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Module to evaluate JSON-structured expression trees",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"@testing-library/jest-dom": "^5.14.1",
|
|
15
15
|
"@testing-library/react": "^13.0.0",
|
|
16
16
|
"@types/jest": "^27.5.1",
|
|
17
|
-
"@types/lodash": "^4.14.182",
|
|
18
17
|
"@types/node": "^17.0.38",
|
|
19
18
|
"@types/pg": "^8.6.5",
|
|
20
19
|
"dotenv": "^16.0.1",
|
|
@@ -35,7 +34,7 @@
|
|
|
35
34
|
},
|
|
36
35
|
"dependencies": {
|
|
37
36
|
"axios": "^0.27.2",
|
|
38
|
-
"
|
|
37
|
+
"change-case": "^4.1.2",
|
|
39
38
|
"object-property-extractor": "^1.0.3"
|
|
40
39
|
},
|
|
41
40
|
"bugs": {
|