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.
Files changed (70) hide show
  1. package/README.md +1233 -0
  2. package/build/.DS_Store +0 -0
  3. package/build/FigTreeEvaluator.d.ts +13 -0
  4. package/build/FigTreeEvaluator.js +75 -0
  5. package/build/evaluate.d.ts +2 -0
  6. package/build/evaluate.js +63 -0
  7. package/build/evaluator.d.ts +13 -0
  8. package/build/evaluator.js +75 -0
  9. package/build/helpers.d.ts +16 -0
  10. package/build/helpers.js +63 -0
  11. package/build/index.d.ts +13 -0
  12. package/build/index.js +75 -0
  13. package/build/operators/_operatorAliases.json +86 -0
  14. package/build/operators/_operatorUtils.d.ts +16 -0
  15. package/build/operators/_operatorUtils.js +83 -0
  16. package/build/operators/buildObject.d.ts +11 -0
  17. package/build/operators/buildObject.js +48 -0
  18. package/build/operators/conditional.d.ts +7 -0
  19. package/build/operators/conditional.js +31 -0
  20. package/build/operators/count.d.ts +2 -0
  21. package/build/operators/count.js +29 -0
  22. package/build/operators/customFunctions.d.ts +9 -0
  23. package/build/operators/customFunctions.js +53 -0
  24. package/build/operators/divide.d.ts +11 -0
  25. package/build/operators/divide.js +42 -0
  26. package/build/operators/equal.d.ts +2 -0
  27. package/build/operators/equal.js +29 -0
  28. package/build/operators/getRequest.d.ts +12 -0
  29. package/build/operators/getRequest.js +47 -0
  30. package/build/operators/graphQL.d.ts +18 -0
  31. package/build/operators/graphQL.js +55 -0
  32. package/build/operators/greaterThan.d.ts +6 -0
  33. package/build/operators/greaterThan.js +35 -0
  34. package/build/operators/index.d.ts +23 -0
  35. package/build/operators/index.js +39 -0
  36. package/build/operators/lessThan.d.ts +2 -0
  37. package/build/operators/lessThan.js +35 -0
  38. package/build/operators/logicalAnd.d.ts +8 -0
  39. package/build/operators/logicalAnd.js +33 -0
  40. package/build/operators/logicalOr.d.ts +2 -0
  41. package/build/operators/logicalOr.js +29 -0
  42. package/build/operators/multiply.d.ts +2 -0
  43. package/build/operators/multiply.js +29 -0
  44. package/build/operators/notEqual.d.ts +2 -0
  45. package/build/operators/notEqual.js +29 -0
  46. package/build/operators/objectFunctions.d.ts +9 -0
  47. package/build/operators/objectFunctions.js +38 -0
  48. package/build/operators/objectProperties.d.ts +9 -0
  49. package/build/operators/objectProperties.js +44 -0
  50. package/build/operators/passThru.d.ts +7 -0
  51. package/build/operators/passThru.js +30 -0
  52. package/build/operators/pgSQL.d.ts +24 -0
  53. package/build/operators/pgSQL.js +66 -0
  54. package/build/operators/plus.d.ts +10 -0
  55. package/build/operators/plus.js +39 -0
  56. package/build/operators/postRequest.d.ts +2 -0
  57. package/build/operators/postRequest.js +39 -0
  58. package/build/operators/regex.d.ts +7 -0
  59. package/build/operators/regex.js +46 -0
  60. package/build/operators/split.d.ts +11 -0
  61. package/build/operators/split.js +51 -0
  62. package/build/operators/stringSubstitution.d.ts +7 -0
  63. package/build/operators/stringSubstitution.js +40 -0
  64. package/build/operators/subtract.d.ts +9 -0
  65. package/build/operators/subtract.js +32 -0
  66. package/build/typeCheck.d.ts +9 -0
  67. package/build/typeCheck.js +48 -0
  68. package/build/types.d.ts +53 -0
  69. package/build/types.js +28 -0
  70. package/package.json +45 -0
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typeCheck = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ const typeCheckItem = ({ value, name, not = false, expectedType, }) => {
6
+ let result;
7
+ if (Array.isArray(expectedType)) {
8
+ result = expectedType.some((type) => typeCheckItem({ value, name, expectedType: type }) === true);
9
+ if (not)
10
+ result = !result;
11
+ return result ? true : makeErrorString(name, value, expectedType.join('|'), not);
12
+ }
13
+ else {
14
+ const checker = typeCheckMap[expectedType];
15
+ result = checker(value);
16
+ if (not)
17
+ result = !result;
18
+ return result ? true : makeErrorString(name, value, expectedType, not);
19
+ }
20
+ };
21
+ const typeCheck = (...args) => {
22
+ const errorStrings = [];
23
+ args.forEach((item) => {
24
+ const result = typeCheckItem(item);
25
+ if (result !== true)
26
+ errorStrings.push(result);
27
+ });
28
+ return errorStrings.length > 0 ? errorStrings.join('\n') : true;
29
+ };
30
+ exports.typeCheck = typeCheck;
31
+ const typeCheckMap = {
32
+ string: (value) => typeof value === 'string',
33
+ number: (value) => typeof value === 'number',
34
+ boolean: (value) => typeof value === 'boolean',
35
+ array: (value) => Array.isArray(value),
36
+ undefined: (value) => value === undefined,
37
+ null: (value) => value === null,
38
+ object: (value) => typeof value === 'object' && !Array.isArray(value) && value !== null,
39
+ };
40
+ const makeErrorString = (name, value, type, not) => {
41
+ if (name !== undefined)
42
+ return `- Property "${name}" (value: ${stringifyValue(value)}) is not of type: ${not ? `!(${type})` : type}`;
43
+ return `- ${stringifyValue(value)} is not of type: ${not ? `!(${type})` : type}`;
44
+ };
45
+ const stringifyValue = (value) => {
46
+ const stringValue = value === undefined || Number.isNaN(value) ? String(value) : JSON.stringify(value);
47
+ return (0, helpers_1.truncateString)(stringValue, 50);
48
+ };
@@ -0,0 +1,53 @@
1
+ import { BasicExtendedNode, SubtractionNode, DivisionNode, ComparatorNode, ConditionalNode, RegexNode, StringSubNode, SplitNode, ObjPropNode, APINode, PGNode, GraphQLNode, BuildObjectNode, FunctionNode, PassThruNode, PGConnection, GraphQLConnection } from './operators';
2
+ import { TypeCheckInput } from './typeCheck';
3
+ export declare const Operators: readonly ["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"];
4
+ export declare type Operator = typeof Operators[number];
5
+ export declare type GenericObject = {
6
+ [key: string]: any;
7
+ };
8
+ export interface FigTreeOptions {
9
+ objects?: GenericObject;
10
+ functions?: {
11
+ [key: string]: Function;
12
+ };
13
+ pgConnection?: PGConnection;
14
+ graphQLConnection?: GraphQLConnection;
15
+ baseEndpoint?: string;
16
+ headers?: {
17
+ [key: string]: string;
18
+ };
19
+ returnErrorAsString?: boolean;
20
+ allowJSONStringInput?: boolean;
21
+ skipRuntimeTypeCheck?: boolean;
22
+ }
23
+ export interface FigTreeConfig {
24
+ options: FigTreeOptions;
25
+ operators: OperatorReference;
26
+ operatorAliases: {
27
+ [key: string]: Operator;
28
+ };
29
+ typeChecker: (...input: TypeCheckInput[]) => void;
30
+ }
31
+ export declare type OutputType = 'string' | 'number' | 'boolean' | 'bool' | 'array';
32
+ export interface BaseOperatorNode {
33
+ operator: Operator;
34
+ outputType?: OutputType;
35
+ children?: Array<EvaluatorNode>;
36
+ fallback?: any;
37
+ }
38
+ export declare type CombinedOperatorNode = BaseOperatorNode & BasicExtendedNode & SubtractionNode & DivisionNode & ComparatorNode & ConditionalNode & RegexNode & StringSubNode & SplitNode & ObjPropNode & APINode & PGNode & GraphQLNode & BuildObjectNode & FunctionNode & PassThruNode;
39
+ export declare type OperatorNodeUnion = BasicExtendedNode | SubtractionNode | DivisionNode | ComparatorNode | ConditionalNode | RegexNode | StringSubNode | SplitNode | ObjPropNode | PGNode | GraphQLNode | APINode | BuildObjectNode | FunctionNode | PassThruNode;
40
+ export declare type EvaluatorOutput = string | boolean | number | GenericObject | null | undefined | any[];
41
+ export declare type EvaluatorNode = CombinedOperatorNode | EvaluatorOutput;
42
+ export declare type OperatorObject = {
43
+ requiredProperties: readonly string[];
44
+ operatorAliases: string[];
45
+ propertyAliases: {
46
+ [key: string]: string;
47
+ };
48
+ evaluate: (expression: CombinedOperatorNode, config: FigTreeConfig) => Promise<EvaluatorOutput>;
49
+ parseChildren: (expression: CombinedOperatorNode, config: FigTreeConfig) => OperatorNodeUnion | Promise<OperatorNodeUnion>;
50
+ };
51
+ export declare type OperatorReference = {
52
+ [key in Operator]: OperatorObject;
53
+ };
package/build/types.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Operators = void 0;
4
+ exports.Operators = [
5
+ 'AND',
6
+ 'OR',
7
+ 'EQUAL',
8
+ 'NOT_EQUAL',
9
+ 'PLUS',
10
+ 'SUBTRACT',
11
+ 'MULTIPLY',
12
+ 'DIVIDE',
13
+ 'GREATER_THAN',
14
+ 'LESS_THAN',
15
+ 'CONDITIONAL',
16
+ 'REGEX',
17
+ 'OBJECT_PROPERTIES',
18
+ 'STRING_SUBSTITUTION',
19
+ 'SPLIT',
20
+ 'COUNT',
21
+ 'GET',
22
+ 'POST',
23
+ 'PG_SQL',
24
+ 'GRAPHQL',
25
+ 'BUILD_OBJECT',
26
+ 'CUSTOM_FUNCTIONS',
27
+ 'PASSTHRU',
28
+ ];
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "fig-tree-evaluator",
3
+ "version": "1.9.0",
4
+ "description": "Module to evaluate JSON-structured expression trees",
5
+ "main": "build/index.js",
6
+ "types": "build/index.d.ts",
7
+ "files": [
8
+ "build/**/*"
9
+ ],
10
+ "repository": "git+https://github.com/CarlosNZ/fig-tree.git",
11
+ "author": "Carl Smith <5456533+CarlosNZ@users.noreply.github.com>",
12
+ "license": "GPL-3.0",
13
+ "devDependencies": {
14
+ "@testing-library/jest-dom": "^5.14.1",
15
+ "@testing-library/react": "^13.0.0",
16
+ "@types/jest": "^27.5.1",
17
+ "@types/lodash": "^4.14.182",
18
+ "@types/node": "^17.0.38",
19
+ "@types/pg": "^8.6.5",
20
+ "dotenv": "^16.0.1",
21
+ "jest": "^28.1.0",
22
+ "pg": "^8.7.3",
23
+ "rimraf": "^3.0.2",
24
+ "ts-jest": "^28.0.3",
25
+ "ts-node": "^10.8.0",
26
+ "typescript": "^4.7.2"
27
+ },
28
+ "scripts": {
29
+ "setup": "yarn install && cd demo && yarn install",
30
+ "test": "jest",
31
+ "generate": "ts-node ./codegen/buildOperatorAliasReference.ts",
32
+ "build": "yarn generate && rimraf ./lib && tsc",
33
+ "dev": "cp -n src/dev/playground_example.ts src/dev/playground.ts || ts-node src/dev/playground.ts",
34
+ "demo": "cd demo && yarn start"
35
+ },
36
+ "dependencies": {
37
+ "axios": "^0.27.2",
38
+ "lodash": "^4.17.21",
39
+ "object-property-extractor": "^1.0.3"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/CarlosNZ/fig-tree/issues"
43
+ },
44
+ "homepage": "https://github.com/CarlosNZ/fig-tree#readme"
45
+ }