fig-tree-evaluator 1.9.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 +1233 -0
- package/build/.DS_Store +0 -0
- package/build/FigTreeEvaluator.d.ts +13 -0
- package/build/FigTreeEvaluator.js +75 -0
- package/build/evaluate.d.ts +2 -0
- package/build/evaluate.js +63 -0
- package/build/evaluator.d.ts +13 -0
- package/build/evaluator.js +75 -0
- package/build/helpers.d.ts +16 -0
- package/build/helpers.js +63 -0
- package/build/index.d.ts +13 -0
- package/build/index.js +75 -0
- package/build/operators/_operatorAliases.json +86 -0
- package/build/operators/_operatorUtils.d.ts +16 -0
- package/build/operators/_operatorUtils.js +83 -0
- package/build/operators/buildObject.d.ts +11 -0
- package/build/operators/buildObject.js +48 -0
- package/build/operators/conditional.d.ts +7 -0
- package/build/operators/conditional.js +31 -0
- package/build/operators/count.d.ts +2 -0
- package/build/operators/count.js +29 -0
- package/build/operators/customFunctions.d.ts +9 -0
- package/build/operators/customFunctions.js +53 -0
- package/build/operators/divide.d.ts +11 -0
- package/build/operators/divide.js +42 -0
- package/build/operators/equal.d.ts +2 -0
- package/build/operators/equal.js +29 -0
- package/build/operators/getRequest.d.ts +12 -0
- package/build/operators/getRequest.js +47 -0
- package/build/operators/graphQL.d.ts +18 -0
- package/build/operators/graphQL.js +55 -0
- package/build/operators/greaterThan.d.ts +6 -0
- package/build/operators/greaterThan.js +35 -0
- package/build/operators/index.d.ts +23 -0
- package/build/operators/index.js +39 -0
- package/build/operators/lessThan.d.ts +2 -0
- package/build/operators/lessThan.js +35 -0
- package/build/operators/logicalAnd.d.ts +8 -0
- package/build/operators/logicalAnd.js +33 -0
- package/build/operators/logicalOr.d.ts +2 -0
- package/build/operators/logicalOr.js +29 -0
- package/build/operators/multiply.d.ts +2 -0
- package/build/operators/multiply.js +29 -0
- package/build/operators/notEqual.d.ts +2 -0
- package/build/operators/notEqual.js +29 -0
- package/build/operators/objectFunctions.d.ts +9 -0
- package/build/operators/objectFunctions.js +38 -0
- package/build/operators/objectProperties.d.ts +9 -0
- package/build/operators/objectProperties.js +44 -0
- package/build/operators/passThru.d.ts +7 -0
- package/build/operators/passThru.js +30 -0
- package/build/operators/pgSQL.d.ts +24 -0
- package/build/operators/pgSQL.js +66 -0
- package/build/operators/plus.d.ts +10 -0
- package/build/operators/plus.js +39 -0
- package/build/operators/postRequest.d.ts +2 -0
- package/build/operators/postRequest.js +39 -0
- package/build/operators/regex.d.ts +7 -0
- package/build/operators/regex.js +46 -0
- package/build/operators/split.d.ts +11 -0
- package/build/operators/split.js +51 -0
- package/build/operators/stringSubstitution.d.ts +7 -0
- package/build/operators/stringSubstitution.js +40 -0
- package/build/operators/subtract.d.ts +9 -0
- package/build/operators/subtract.js +32 -0
- package/build/typeCheck.d.ts +9 -0
- package/build/typeCheck.js +48 -0
- package/build/types.d.ts +53 -0
- package/build/types.js +28 -0
- package/package.json +45 -0
package/build/.DS_Store
ADDED
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EvaluatorNode, FigTreeOptions } from './types';
|
|
2
|
+
declare class FigTreeEvaluator {
|
|
3
|
+
private options;
|
|
4
|
+
private operators;
|
|
5
|
+
private operatorAliases;
|
|
6
|
+
constructor(options?: FigTreeOptions);
|
|
7
|
+
private typeChecker;
|
|
8
|
+
evaluate(expression: EvaluatorNode, options?: FigTreeOptions): Promise<import("./types").EvaluatorOutput>;
|
|
9
|
+
getOptions(): FigTreeOptions;
|
|
10
|
+
updateOptions(options: FigTreeOptions): void;
|
|
11
|
+
}
|
|
12
|
+
export default FigTreeEvaluator;
|
|
13
|
+
export declare const evaluateExpression: (expression: EvaluatorNode, options?: FigTreeOptions) => Promise<import("./types").EvaluatorOutput>;
|
|
@@ -0,0 +1,75 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.evaluateExpression = void 0;
|
|
39
|
+
const evaluate_1 = require("./evaluate");
|
|
40
|
+
const typeCheck_1 = require("./typeCheck");
|
|
41
|
+
const _operatorAliases_json_1 = __importDefault(require("./operators/_operatorAliases.json"));
|
|
42
|
+
const operators = __importStar(require("./operators"));
|
|
43
|
+
class FigTreeEvaluator {
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
this.typeChecker = (...args) => {
|
|
46
|
+
const result = (0, typeCheck_1.typeCheck)(...args);
|
|
47
|
+
if (result === true)
|
|
48
|
+
return;
|
|
49
|
+
throw new Error(result);
|
|
50
|
+
};
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.operators = operators;
|
|
53
|
+
this.operatorAliases = _operatorAliases_json_1.default;
|
|
54
|
+
}
|
|
55
|
+
evaluate(expression, options = {}) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const instanceOptions = Object.assign(Object.assign({}, this.options), options);
|
|
58
|
+
return yield (0, evaluate_1.evaluatorFunction)(expression, {
|
|
59
|
+
options: instanceOptions,
|
|
60
|
+
operators: this.operators,
|
|
61
|
+
operatorAliases: this.operatorAliases,
|
|
62
|
+
typeChecker: instanceOptions.skipRuntimeTypeCheck ? () => { } : this.typeChecker,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getOptions() {
|
|
67
|
+
return this.options;
|
|
68
|
+
}
|
|
69
|
+
updateOptions(options) {
|
|
70
|
+
this.options = Object.assign(Object.assign({}, this.options), options);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.default = FigTreeEvaluator;
|
|
74
|
+
const evaluateExpression = (expression, options) => new FigTreeEvaluator(options).evaluate(expression);
|
|
75
|
+
exports.evaluateExpression = evaluateExpression;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.evaluatorFunction = void 0;
|
|
13
|
+
const _operatorUtils_1 = require("./operators/_operatorUtils");
|
|
14
|
+
const helpers_1 = require("./helpers");
|
|
15
|
+
const evaluatorFunction = (input, config) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
const { options, operators, operatorAliases } = config;
|
|
18
|
+
let expression = (options === null || options === void 0 ? void 0 : options.allowJSONStringInput) ? (0, helpers_1.parseIfJson)(input) : input;
|
|
19
|
+
if (Array.isArray(expression)) {
|
|
20
|
+
expression = yield (0, _operatorUtils_1.evaluateArray)(expression, config);
|
|
21
|
+
}
|
|
22
|
+
if (!(0, helpers_1.isOperatorNode)(expression))
|
|
23
|
+
return expression;
|
|
24
|
+
const { fallback } = expression;
|
|
25
|
+
const returnErrorAsString = (_a = options === null || options === void 0 ? void 0 : options.returnErrorAsString) !== null && _a !== void 0 ? _a : false;
|
|
26
|
+
try {
|
|
27
|
+
const operator = (0, helpers_1.getOperatorName)(expression.operator, operatorAliases);
|
|
28
|
+
if (!operator)
|
|
29
|
+
return (0, helpers_1.fallbackOrError)(yield (0, exports.evaluatorFunction)(fallback, config), `Invalid operator: ${expression.operator}`, returnErrorAsString);
|
|
30
|
+
const { requiredProperties, propertyAliases, evaluate, parseChildren } = operators[operator];
|
|
31
|
+
expression = (0, helpers_1.mapPropertyAliases)(propertyAliases, expression);
|
|
32
|
+
const validationError = (0, helpers_1.checkRequiredNodes)(requiredProperties, expression);
|
|
33
|
+
if (validationError)
|
|
34
|
+
return (0, helpers_1.fallbackOrError)(yield (0, exports.evaluatorFunction)(fallback, config), `Operator: ${operator}\n- ${validationError}`, returnErrorAsString);
|
|
35
|
+
if ('children' in expression) {
|
|
36
|
+
if (!Array.isArray(expression.children))
|
|
37
|
+
expression.children = yield (0, exports.evaluatorFunction)(expression.children, config);
|
|
38
|
+
if (!Array.isArray(expression.children))
|
|
39
|
+
return (0, helpers_1.fallbackOrError)(yield (0, exports.evaluatorFunction)(fallback, config), `Operator: ${operator}\n- Property "children" is not of type: array`, returnErrorAsString);
|
|
40
|
+
expression = yield parseChildren(expression, config);
|
|
41
|
+
}
|
|
42
|
+
let result;
|
|
43
|
+
try {
|
|
44
|
+
result = yield evaluate(expression, config);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
result = (0, helpers_1.fallbackOrError)(yield (0, exports.evaluatorFunction)(fallback, config), `Operator: ${operator}\n${(0, helpers_1.errorMessage)(err)}`, returnErrorAsString);
|
|
48
|
+
}
|
|
49
|
+
const outputType = (_b = expression === null || expression === void 0 ? void 0 : expression.outputType) !== null && _b !== void 0 ? _b : expression === null || expression === void 0 ? void 0 : expression.type;
|
|
50
|
+
if (!outputType)
|
|
51
|
+
return result;
|
|
52
|
+
const evaluatedOutputType = (yield (0, exports.evaluatorFunction)(outputType, config));
|
|
53
|
+
if (!(evaluatedOutputType in helpers_1.convertOutputMethods))
|
|
54
|
+
return (0, helpers_1.fallbackOrError)(yield (0, exports.evaluatorFunction)(fallback, config), `Operator: ${operator}\n- Invalid output type: ${evaluatedOutputType}`, returnErrorAsString);
|
|
55
|
+
else {
|
|
56
|
+
return helpers_1.convertOutputMethods[evaluatedOutputType](result);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
return (0, helpers_1.fallbackOrError)(yield (0, exports.evaluatorFunction)(fallback, config), (0, helpers_1.errorMessage)(err), returnErrorAsString);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
exports.evaluatorFunction = evaluatorFunction;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EvaluatorNode, EvaluatorOptions } from './types';
|
|
2
|
+
declare class ExpressionEvaluator {
|
|
3
|
+
private options;
|
|
4
|
+
private operators;
|
|
5
|
+
private operatorAliases;
|
|
6
|
+
constructor(options?: EvaluatorOptions);
|
|
7
|
+
private typeChecker;
|
|
8
|
+
evaluate(expression: EvaluatorNode, options?: EvaluatorOptions): Promise<import("./types").EvaluatorOutput>;
|
|
9
|
+
getOptions(): EvaluatorOptions;
|
|
10
|
+
updateOptions(options: EvaluatorOptions): void;
|
|
11
|
+
}
|
|
12
|
+
export default ExpressionEvaluator;
|
|
13
|
+
export declare const evaluateExpression: (expression: EvaluatorNode, options?: EvaluatorOptions) => Promise<import("./types").EvaluatorOutput>;
|
|
@@ -0,0 +1,75 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.evaluateExpression = void 0;
|
|
39
|
+
const evaluate_1 = require("./evaluate");
|
|
40
|
+
const typeCheck_1 = require("./typeCheck");
|
|
41
|
+
const _operatorAliases_json_1 = __importDefault(require("./operators/_operatorAliases.json"));
|
|
42
|
+
const operators = __importStar(require("./operators"));
|
|
43
|
+
class ExpressionEvaluator {
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
this.typeChecker = (...args) => {
|
|
46
|
+
const result = (0, typeCheck_1.typeCheck)(...args);
|
|
47
|
+
if (result === true)
|
|
48
|
+
return;
|
|
49
|
+
throw new Error(result);
|
|
50
|
+
};
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.operators = operators;
|
|
53
|
+
this.operatorAliases = _operatorAliases_json_1.default;
|
|
54
|
+
}
|
|
55
|
+
evaluate(expression, options = {}) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const instanceOptions = Object.assign(Object.assign({}, this.options), options);
|
|
58
|
+
return yield (0, evaluate_1.evaluatorFunction)(expression, {
|
|
59
|
+
options: instanceOptions,
|
|
60
|
+
operators: this.operators,
|
|
61
|
+
operatorAliases: this.operatorAliases,
|
|
62
|
+
typeChecker: instanceOptions.skipRuntimeTypeCheck ? () => { } : this.typeChecker,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getOptions() {
|
|
67
|
+
return this.options;
|
|
68
|
+
}
|
|
69
|
+
updateOptions(options) {
|
|
70
|
+
this.options = Object.assign(Object.assign({}, this.options), options);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.default = ExpressionEvaluator;
|
|
74
|
+
const evaluateExpression = (expression, options) => new ExpressionEvaluator(options).evaluate(expression);
|
|
75
|
+
exports.evaluateExpression = evaluateExpression;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { OutputType, EvaluatorNode, CombinedOperatorNode, Operator, EvaluatorOutput } from './types';
|
|
2
|
+
export declare const parseIfJson: (input: EvaluatorNode) => any;
|
|
3
|
+
export declare const isOperatorNode: (input: EvaluatorNode) => boolean;
|
|
4
|
+
export declare const getOperatorName: (operator: string, operatorAliases: {
|
|
5
|
+
[key: string]: "AND" | "OR" | "EQUAL" | "NOT_EQUAL" | "PLUS" | "SUBTRACT" | "MULTIPLY" | "DIVIDE" | "GREATER_THAN" | "LESS_THAN" | "CONDITIONAL" | "REGEX" | "OBJECT_PROPERTIES" | "STRING_SUBSTITUTION" | "SPLIT" | "COUNT" | "GET" | "POST" | "PG_SQL" | "GRAPHQL" | "BUILD_OBJECT" | "CUSTOM_FUNCTIONS" | "PASSTHRU";
|
|
6
|
+
}) => "AND" | "OR" | "EQUAL" | "NOT_EQUAL" | "PLUS" | "SUBTRACT" | "MULTIPLY" | "DIVIDE" | "GREATER_THAN" | "LESS_THAN" | "CONDITIONAL" | "REGEX" | "OBJECT_PROPERTIES" | "STRING_SUBSTITUTION" | "SPLIT" | "COUNT" | "GET" | "POST" | "PG_SQL" | "GRAPHQL" | "BUILD_OBJECT" | "CUSTOM_FUNCTIONS" | "PASSTHRU";
|
|
7
|
+
export declare const truncateString: (string: string, length?: number) => string;
|
|
8
|
+
export declare const fallbackOrError: (fallback: any, errorMessage: string, returnErrorAsString?: boolean) => any;
|
|
9
|
+
export declare const mapPropertyAliases: (propertyAliases: {
|
|
10
|
+
[key: string]: string;
|
|
11
|
+
}, expression: CombinedOperatorNode) => CombinedOperatorNode;
|
|
12
|
+
export declare const checkRequiredNodes: (requiredProps: readonly string[], expression: CombinedOperatorNode) => string | false;
|
|
13
|
+
export declare const convertOutputMethods: {
|
|
14
|
+
[key in OutputType]: <T>(value: T) => EvaluatorOutput | T[];
|
|
15
|
+
};
|
|
16
|
+
export declare const errorMessage: (err: unknown) => string;
|
package/build/helpers.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorMessage = exports.convertOutputMethods = exports.checkRequiredNodes = exports.mapPropertyAliases = exports.fallbackOrError = exports.truncateString = exports.getOperatorName = exports.isOperatorNode = exports.parseIfJson = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const parseIfJson = (input) => {
|
|
6
|
+
if (typeof input !== 'string')
|
|
7
|
+
return input;
|
|
8
|
+
try {
|
|
9
|
+
const parsedInput = JSON.parse(input);
|
|
10
|
+
return (0, exports.isOperatorNode)(parsedInput) ? parsedInput : input;
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
return input;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.parseIfJson = parseIfJson;
|
|
17
|
+
const isOperatorNode = (input) => input instanceof Object && 'operator' in input;
|
|
18
|
+
exports.isOperatorNode = isOperatorNode;
|
|
19
|
+
const standardiseOperatorName = (name) => {
|
|
20
|
+
const camelCaseName = (0, lodash_1.camelCase)(name);
|
|
21
|
+
return camelCaseName ? camelCaseName : name;
|
|
22
|
+
};
|
|
23
|
+
const getOperatorName = (operator, operatorAliases) => operatorAliases[standardiseOperatorName(operator)];
|
|
24
|
+
exports.getOperatorName = getOperatorName;
|
|
25
|
+
const truncateString = (string, length = 200) => string.length < length ? string : `${string.slice(0, length - 2).trim()}...`;
|
|
26
|
+
exports.truncateString = truncateString;
|
|
27
|
+
const fallbackOrError = (fallback, errorMessage, returnErrorAsString = false) => {
|
|
28
|
+
if (fallback !== undefined)
|
|
29
|
+
return fallback;
|
|
30
|
+
if (returnErrorAsString)
|
|
31
|
+
return (0, exports.truncateString)(errorMessage);
|
|
32
|
+
else
|
|
33
|
+
throw new Error((0, exports.truncateString)(errorMessage));
|
|
34
|
+
};
|
|
35
|
+
exports.fallbackOrError = fallbackOrError;
|
|
36
|
+
const mapPropertyAliases = (propertyAliases, expression) => (0, lodash_1.mapKeys)(expression, (_, key) => key in propertyAliases ? propertyAliases[key] : key);
|
|
37
|
+
exports.mapPropertyAliases = mapPropertyAliases;
|
|
38
|
+
const checkRequiredNodes = (requiredProps, expression) => {
|
|
39
|
+
const missingProps = requiredProps.filter((prop) => !(prop in expression));
|
|
40
|
+
if (missingProps.length === 0)
|
|
41
|
+
return false;
|
|
42
|
+
if (!('children' in expression))
|
|
43
|
+
return `Missing properties: ${missingProps}`;
|
|
44
|
+
return false;
|
|
45
|
+
};
|
|
46
|
+
exports.checkRequiredNodes = checkRequiredNodes;
|
|
47
|
+
exports.convertOutputMethods = {
|
|
48
|
+
number: (value) => extractNumber(value),
|
|
49
|
+
string: (value) => String(value),
|
|
50
|
+
array: (value) => (Array.isArray(value) ? value : [value]),
|
|
51
|
+
boolean: (value) => Boolean(value),
|
|
52
|
+
bool: (value) => Boolean(value),
|
|
53
|
+
};
|
|
54
|
+
const errorMessage = (err) => err.message;
|
|
55
|
+
exports.errorMessage = errorMessage;
|
|
56
|
+
const extractNumber = (input) => {
|
|
57
|
+
if (typeof input !== 'string')
|
|
58
|
+
return Number.isNaN(Number(input)) ? input : Number(input);
|
|
59
|
+
const numberMatch = input.match(/(-?(\d+\.\d+))|(-?((?<!\.)\.\d+))|(-?\d+)/gm);
|
|
60
|
+
if (!numberMatch)
|
|
61
|
+
return 0;
|
|
62
|
+
return Number(numberMatch[0]);
|
|
63
|
+
};
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EvaluatorNode, FigTreeOptions } from './types';
|
|
2
|
+
declare class FigTreeEvaluator {
|
|
3
|
+
private options;
|
|
4
|
+
private operators;
|
|
5
|
+
private operatorAliases;
|
|
6
|
+
constructor(options?: FigTreeOptions);
|
|
7
|
+
private typeChecker;
|
|
8
|
+
evaluate(expression: EvaluatorNode, options?: FigTreeOptions): Promise<import("./types").EvaluatorOutput>;
|
|
9
|
+
getOptions(): FigTreeOptions;
|
|
10
|
+
updateOptions(options: FigTreeOptions): void;
|
|
11
|
+
}
|
|
12
|
+
export default FigTreeEvaluator;
|
|
13
|
+
export declare const evaluateExpression: (expression: EvaluatorNode, options?: FigTreeOptions) => Promise<import("./types").EvaluatorOutput>;
|
package/build/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.evaluateExpression = void 0;
|
|
39
|
+
const evaluate_1 = require("./evaluate");
|
|
40
|
+
const typeCheck_1 = require("./typeCheck");
|
|
41
|
+
const _operatorAliases_json_1 = __importDefault(require("./operators/_operatorAliases.json"));
|
|
42
|
+
const operators = __importStar(require("./operators"));
|
|
43
|
+
class FigTreeEvaluator {
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
this.typeChecker = (...args) => {
|
|
46
|
+
const result = (0, typeCheck_1.typeCheck)(...args);
|
|
47
|
+
if (result === true)
|
|
48
|
+
return;
|
|
49
|
+
throw new Error(result);
|
|
50
|
+
};
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.operators = operators;
|
|
53
|
+
this.operatorAliases = _operatorAliases_json_1.default;
|
|
54
|
+
}
|
|
55
|
+
evaluate(expression, options = {}) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const instanceOptions = Object.assign(Object.assign({}, this.options), options);
|
|
58
|
+
return yield (0, evaluate_1.evaluatorFunction)(expression, {
|
|
59
|
+
options: instanceOptions,
|
|
60
|
+
operators: this.operators,
|
|
61
|
+
operatorAliases: this.operatorAliases,
|
|
62
|
+
typeChecker: instanceOptions.skipRuntimeTypeCheck ? () => { } : this.typeChecker,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getOptions() {
|
|
67
|
+
return this.options;
|
|
68
|
+
}
|
|
69
|
+
updateOptions(options) {
|
|
70
|
+
this.options = Object.assign(Object.assign({}, this.options), options);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.default = FigTreeEvaluator;
|
|
74
|
+
const evaluateExpression = (expression, options) => new FigTreeEvaluator(options).evaluate(expression);
|
|
75
|
+
exports.evaluateExpression = evaluateExpression;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"and": "AND",
|
|
3
|
+
"&": "AND",
|
|
4
|
+
"&&": "AND",
|
|
5
|
+
"or": "OR",
|
|
6
|
+
"|": "OR",
|
|
7
|
+
"||": "OR",
|
|
8
|
+
"=": "EQUAL",
|
|
9
|
+
"eq": "EQUAL",
|
|
10
|
+
"equal": "EQUAL",
|
|
11
|
+
"equals": "EQUAL",
|
|
12
|
+
"!=": "NOT_EQUAL",
|
|
13
|
+
"!": "NOT_EQUAL",
|
|
14
|
+
"ne": "NOT_EQUAL",
|
|
15
|
+
"notEqual": "NOT_EQUAL",
|
|
16
|
+
"+": "PLUS",
|
|
17
|
+
"plus": "PLUS",
|
|
18
|
+
"add": "PLUS",
|
|
19
|
+
"concat": "PLUS",
|
|
20
|
+
"join": "PLUS",
|
|
21
|
+
"merge": "PLUS",
|
|
22
|
+
"-": "SUBTRACT",
|
|
23
|
+
"subtract": "SUBTRACT",
|
|
24
|
+
"minus": "SUBTRACT",
|
|
25
|
+
"takeaway": "SUBTRACT",
|
|
26
|
+
"*": "MULTIPLY",
|
|
27
|
+
"x": "MULTIPLY",
|
|
28
|
+
"multiply": "MULTIPLY",
|
|
29
|
+
"times": "MULTIPLY",
|
|
30
|
+
"/": "DIVIDE",
|
|
31
|
+
"divide": "DIVIDE",
|
|
32
|
+
"÷": "DIVIDE",
|
|
33
|
+
">": "GREATER_THAN",
|
|
34
|
+
"greaterThan": "GREATER_THAN",
|
|
35
|
+
"higher": "GREATER_THAN",
|
|
36
|
+
"larger": "GREATER_THAN",
|
|
37
|
+
"<": "LESS_THAN",
|
|
38
|
+
"lessThan": "LESS_THAN",
|
|
39
|
+
"lower": "LESS_THAN",
|
|
40
|
+
"smaller": "LESS_THAN",
|
|
41
|
+
"?": "CONDITIONAL",
|
|
42
|
+
"conditional": "CONDITIONAL",
|
|
43
|
+
"ifThen": "CONDITIONAL",
|
|
44
|
+
"regex": "REGEX",
|
|
45
|
+
"patternMatch": "REGEX",
|
|
46
|
+
"regexp": "REGEX",
|
|
47
|
+
"matchPattern": "REGEX",
|
|
48
|
+
"objectProperties": "OBJECT_PROPERTIES",
|
|
49
|
+
"objProps": "OBJECT_PROPERTIES",
|
|
50
|
+
"getProperty": "OBJECT_PROPERTIES",
|
|
51
|
+
"getObjProp": "OBJECT_PROPERTIES",
|
|
52
|
+
"stringSubstitution": "STRING_SUBSTITUTION",
|
|
53
|
+
"substitute": "STRING_SUBSTITUTION",
|
|
54
|
+
"stringSub": "STRING_SUBSTITUTION",
|
|
55
|
+
"replace": "STRING_SUBSTITUTION",
|
|
56
|
+
"split": "SPLIT",
|
|
57
|
+
"arraySplit": "SPLIT",
|
|
58
|
+
"count": "COUNT",
|
|
59
|
+
"length": "COUNT",
|
|
60
|
+
"get": "GET",
|
|
61
|
+
"api": "GET",
|
|
62
|
+
"post": "POST",
|
|
63
|
+
"pgSql": "PG_SQL",
|
|
64
|
+
"sql": "PG_SQL",
|
|
65
|
+
"postgres": "PG_SQL",
|
|
66
|
+
"pg": "PG_SQL",
|
|
67
|
+
"pgDb": "PG_SQL",
|
|
68
|
+
"graphQl": "GRAPHQL",
|
|
69
|
+
"graphql": "GRAPHQL",
|
|
70
|
+
"gql": "GRAPHQL",
|
|
71
|
+
"buildObject": "BUILD_OBJECT",
|
|
72
|
+
"build": "BUILD_OBJECT",
|
|
73
|
+
"object": "BUILD_OBJECT",
|
|
74
|
+
"customFunctions": "CUSTOM_FUNCTIONS",
|
|
75
|
+
"customFunction": "CUSTOM_FUNCTIONS",
|
|
76
|
+
"objectFunctions": "CUSTOM_FUNCTIONS",
|
|
77
|
+
"function": "CUSTOM_FUNCTIONS",
|
|
78
|
+
"functions": "CUSTOM_FUNCTIONS",
|
|
79
|
+
"runFunction": "CUSTOM_FUNCTIONS",
|
|
80
|
+
"_": "PASSTHRU",
|
|
81
|
+
"passThru": "PASSTHRU",
|
|
82
|
+
"pass": "PASSTHRU",
|
|
83
|
+
"ignore": "PASSTHRU",
|
|
84
|
+
"coerce": "PASSTHRU",
|
|
85
|
+
"convert": "PASSTHRU"
|
|
86
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GenericObject, EvaluatorNode, EvaluatorOutput, FigTreeConfig } from '../types';
|
|
2
|
+
export declare const evaluateArray: (nodes: EvaluatorNode[], params: FigTreeConfig) => Promise<EvaluatorOutput[]>;
|
|
3
|
+
export declare const zipArraysToObject: <T>(keys: string[], values: T[]) => {
|
|
4
|
+
[x: string]: T;
|
|
5
|
+
};
|
|
6
|
+
export declare const simplifyObject: (item: number | string | boolean | GenericObject) => any;
|
|
7
|
+
export declare const extractAndSimplify: (data: GenericObject | GenericObject[], returnProperty: string | undefined, fallback?: any) => any;
|
|
8
|
+
export declare const isFullUrl: (url: string) => boolean;
|
|
9
|
+
export declare const joinUrlParts: (...urls: string[]) => string;
|
|
10
|
+
export declare const axiosRequest: ({ url, params, data, headers, method, }: {
|
|
11
|
+
url: string;
|
|
12
|
+
params?: GenericObject | undefined;
|
|
13
|
+
data?: GenericObject | undefined;
|
|
14
|
+
headers?: GenericObject | undefined;
|
|
15
|
+
method?: "get" | "post" | undefined;
|
|
16
|
+
}) => Promise<any>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.axiosRequest = exports.joinUrlParts = exports.isFullUrl = exports.extractAndSimplify = exports.simplifyObject = exports.zipArraysToObject = exports.evaluateArray = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
const extract_1 = __importDefault(require("object-property-extractor/build/extract"));
|
|
18
|
+
const evaluate_1 = require("../evaluate");
|
|
19
|
+
const evaluateArray = (nodes, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
if (!Array.isArray(nodes))
|
|
21
|
+
return (yield (0, evaluate_1.evaluatorFunction)(nodes, params));
|
|
22
|
+
return yield Promise.all(nodes.map((node) => (0, evaluate_1.evaluatorFunction)(node, params)));
|
|
23
|
+
});
|
|
24
|
+
exports.evaluateArray = evaluateArray;
|
|
25
|
+
const zipArraysToObject = (keys, values) => {
|
|
26
|
+
const pairs = keys.map((key, index) => [key, values[index]]);
|
|
27
|
+
return Object.fromEntries(pairs);
|
|
28
|
+
};
|
|
29
|
+
exports.zipArraysToObject = zipArraysToObject;
|
|
30
|
+
const simplifyObject = (item) => {
|
|
31
|
+
return item instanceof Object && Object.keys(item).length === 1 ? Object.values(item)[0] : item;
|
|
32
|
+
};
|
|
33
|
+
exports.simplifyObject = simplifyObject;
|
|
34
|
+
const extractAndSimplify = (data, returnProperty, fallback = undefined) => {
|
|
35
|
+
try {
|
|
36
|
+
const selectedProperty = returnProperty ? (0, extract_1.default)(data, returnProperty, fallback) : data;
|
|
37
|
+
if (Array.isArray(selectedProperty))
|
|
38
|
+
return selectedProperty.map((item) => (0, exports.simplifyObject)(item));
|
|
39
|
+
if (returnProperty) {
|
|
40
|
+
if (selectedProperty === null)
|
|
41
|
+
return null;
|
|
42
|
+
return (0, exports.simplifyObject)(selectedProperty);
|
|
43
|
+
}
|
|
44
|
+
return selectedProperty;
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
exports.extractAndSimplify = extractAndSimplify;
|
|
51
|
+
const isFullUrl = (url) => /^https?:\/\/.+/.test(url);
|
|
52
|
+
exports.isFullUrl = isFullUrl;
|
|
53
|
+
const joinUrlParts = (...urls) => {
|
|
54
|
+
return urls.reduce((acc, curr, index) => {
|
|
55
|
+
const startSliceIndex = curr[0] === '/' ? 1 : 0;
|
|
56
|
+
const endSliceIndex = curr.slice(-1) === '/' ? -1 : undefined;
|
|
57
|
+
const newPart = curr.slice(startSliceIndex, endSliceIndex);
|
|
58
|
+
return acc + (index === 0 ? '' : '/') + newPart;
|
|
59
|
+
}, '');
|
|
60
|
+
};
|
|
61
|
+
exports.joinUrlParts = joinUrlParts;
|
|
62
|
+
const axiosRequest = ({ url, params = {}, data = {}, headers = {}, method = 'get', }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
var _a;
|
|
64
|
+
try {
|
|
65
|
+
const response = yield (0, axios_1.default)({
|
|
66
|
+
method,
|
|
67
|
+
url,
|
|
68
|
+
headers: Object.assign({ 'Content-Type': 'application/json', Accept: 'application/json' }, headers),
|
|
69
|
+
params,
|
|
70
|
+
data,
|
|
71
|
+
});
|
|
72
|
+
return response.data;
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
if (axios_1.default.isAxiosError(err)) {
|
|
76
|
+
if (!(err === null || err === void 0 ? void 0 : err.response))
|
|
77
|
+
throw new Error('Network Error');
|
|
78
|
+
console.log((_a = err.response) === null || _a === void 0 ? void 0 : _a.data);
|
|
79
|
+
}
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
exports.axiosRequest = axiosRequest;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseOperatorNode, EvaluatorNode, OperatorObject } from '../types';
|
|
2
|
+
declare const requiredProperties: readonly ["properties"];
|
|
3
|
+
export declare type BuildObjectNode = {
|
|
4
|
+
[key in typeof requiredProperties[number]]: BuildObjectElement[];
|
|
5
|
+
} & BaseOperatorNode;
|
|
6
|
+
declare type BuildObjectElement = {
|
|
7
|
+
key: EvaluatorNode;
|
|
8
|
+
value: EvaluatorNode;
|
|
9
|
+
};
|
|
10
|
+
export declare const BUILD_OBJECT: OperatorObject;
|
|
11
|
+
export {};
|