fig-tree-evaluator 2.5.0 → 2.5.2

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.
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -19,39 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
20
9
  });
21
10
  };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- 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;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
11
  Object.defineProperty(exports, "__esModule", { value: true });
50
12
  exports.SPLIT = void 0;
51
- var _operatorUtils_1 = require("./_operatorUtils");
52
- var requiredProperties = ['value'];
53
- var operatorAliases = ['split', 'arraySplit'];
54
- var propertyAliases = {
13
+ const _operatorUtils_1 = require("./_operatorUtils");
14
+ const requiredProperties = ['value'];
15
+ const operatorAliases = ['split', 'arraySplit'];
16
+ const propertyAliases = {
55
17
  string: 'value',
56
18
  separator: 'delimiter',
57
19
  trim: 'trimWhiteSpace',
@@ -59,38 +21,31 @@ var propertyAliases = {
59
21
  removeTrailing: 'excludeTrailing',
60
22
  excludeTrailingDelimiter: 'excludeTrailing',
61
23
  };
62
- var DEFAULT_DELIMITER = ' ';
63
- var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
64
- var _a, value, delimiter, trimWhiteSpace, excludeTrailing, splitValues;
65
- var _b, _c, _d;
66
- return __generator(this, function (_e) {
67
- switch (_e.label) {
68
- case 0: return [4, (0, _operatorUtils_1.evaluateArray)([
69
- expression.value,
70
- (_b = expression.delimiter) !== null && _b !== void 0 ? _b : DEFAULT_DELIMITER,
71
- (_c = expression.trimWhiteSpace) !== null && _c !== void 0 ? _c : true,
72
- (_d = expression.excludeTrailing) !== null && _d !== void 0 ? _d : true,
73
- ], config)];
74
- case 1:
75
- _a = (_e.sent()), value = _a[0], delimiter = _a[1], trimWhiteSpace = _a[2], excludeTrailing = _a[3];
76
- config.typeChecker({ name: 'value', value: value, expectedType: 'string' }, { name: 'delimiter', value: delimiter, expectedType: 'string' }, { name: 'trimWhiteSpace', value: trimWhiteSpace, expectedType: 'boolean' }, { name: 'excludeTrailing', value: excludeTrailing, expectedType: 'boolean' });
77
- splitValues = value.split(delimiter);
78
- if (trimWhiteSpace)
79
- splitValues = splitValues.map(function (val) { return val.trim(); });
80
- if (excludeTrailing && splitValues[splitValues.length - 1] === '')
81
- splitValues.pop();
82
- return [2, splitValues];
83
- }
84
- });
85
- }); };
86
- var parseChildren = function (expression) {
87
- var _a = expression.children, value = _a[0], _b = _a[1], delimiter = _b === void 0 ? DEFAULT_DELIMITER : _b;
88
- return __assign(__assign({}, expression), { value: value, delimiter: delimiter });
24
+ const DEFAULT_DELIMITER = ' ';
25
+ const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, function* () {
26
+ var _a, _b, _c;
27
+ const [value, delimiter, trimWhiteSpace, excludeTrailing] = (yield (0, _operatorUtils_1.evaluateArray)([
28
+ expression.value,
29
+ (_a = expression.delimiter) !== null && _a !== void 0 ? _a : DEFAULT_DELIMITER,
30
+ (_b = expression.trimWhiteSpace) !== null && _b !== void 0 ? _b : true,
31
+ (_c = expression.excludeTrailing) !== null && _c !== void 0 ? _c : true,
32
+ ], config));
33
+ config.typeChecker({ name: 'value', value: value, expectedType: 'string' }, { name: 'delimiter', value: delimiter, expectedType: 'string' }, { name: 'trimWhiteSpace', value: trimWhiteSpace, expectedType: 'boolean' }, { name: 'excludeTrailing', value: excludeTrailing, expectedType: 'boolean' });
34
+ let splitValues = value.split(delimiter);
35
+ if (trimWhiteSpace)
36
+ splitValues = splitValues.map((val) => val.trim());
37
+ if (excludeTrailing && splitValues[splitValues.length - 1] === '')
38
+ splitValues.pop();
39
+ return splitValues;
40
+ });
41
+ const parseChildren = (expression) => {
42
+ const [value, delimiter = DEFAULT_DELIMITER] = expression.children;
43
+ return Object.assign(Object.assign({}, expression), { value, delimiter });
89
44
  };
90
45
  exports.SPLIT = {
91
- requiredProperties: requiredProperties,
92
- operatorAliases: operatorAliases,
93
- propertyAliases: propertyAliases,
94
- evaluate: evaluate,
95
- parseChildren: parseChildren,
46
+ requiredProperties,
47
+ operatorAliases,
48
+ propertyAliases,
49
+ evaluate,
50
+ parseChildren,
96
51
  };
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -19,76 +8,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
20
9
  });
21
10
  };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- 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;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
- if (ar || !(i in from)) {
52
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
- ar[i] = from[i];
54
- }
55
- }
56
- return to.concat(ar || Array.prototype.slice.call(from));
57
- };
58
11
  Object.defineProperty(exports, "__esModule", { value: true });
59
12
  exports.STRING_SUBSTITUTION = void 0;
60
- var _operatorUtils_1 = require("./_operatorUtils");
61
- var requiredProperties = ['string', 'substitutions'];
62
- var operatorAliases = ['stringSubstitution', 'substitute', 'stringSub', 'replace'];
63
- var propertyAliases = { replacements: 'substitutions' };
64
- var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
65
- var _a, string, substitutions, regex, parameters, uniqueParameters, replacementsObj;
66
- return __generator(this, function (_b) {
67
- switch (_b.label) {
68
- case 0: return [4, (0, _operatorUtils_1.evaluateArray)(__spreadArray([expression.string], expression.substitutions, true), config)];
69
- case 1:
70
- _a = (_b.sent()), string = _a[0], substitutions = _a.slice(1);
71
- config.typeChecker({ name: 'string', value: string, expectedType: 'string' });
72
- config.typeChecker({ name: 'substitutions', value: substitutions, expectedType: 'array' });
73
- regex = /(%[\d]+)/g;
74
- parameters = (string.match(regex) || []).sort(function (a, b) { return Number(a.slice(1)) - Number(b.slice(1)); });
75
- uniqueParameters = new Set(parameters);
76
- replacementsObj = (0, _operatorUtils_1.zipArraysToObject)(Array.from(uniqueParameters), substitutions);
77
- return [2, string
78
- .split(regex)
79
- .map(function (fragment) { return (fragment in replacementsObj ? replacementsObj[fragment] : fragment); })
80
- .join('')];
81
- }
82
- });
83
- }); };
84
- var parseChildren = function (expression) {
85
- var _a = expression.children, string = _a[0], substitutions = _a.slice(1);
86
- return __assign(__assign({}, expression), { string: string, substitutions: substitutions });
13
+ const _operatorUtils_1 = require("./_operatorUtils");
14
+ const requiredProperties = ['string', 'substitutions'];
15
+ const operatorAliases = ['stringSubstitution', 'substitute', 'stringSub', 'replace'];
16
+ const propertyAliases = { replacements: 'substitutions' };
17
+ const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, function* () {
18
+ const [string, ...substitutions] = (yield (0, _operatorUtils_1.evaluateArray)([expression.string, ...expression.substitutions], config));
19
+ config.typeChecker({ name: 'string', value: string, expectedType: 'string' });
20
+ config.typeChecker({ name: 'substitutions', value: substitutions, expectedType: 'array' });
21
+ const regex = /(%[\d]+)/g;
22
+ const parameters = (string.match(regex) || []).sort((a, b) => Number(a.slice(1)) - Number(b.slice(1)));
23
+ const uniqueParameters = new Set(parameters);
24
+ const replacementsObj = (0, _operatorUtils_1.zipArraysToObject)(Array.from(uniqueParameters), substitutions);
25
+ return string
26
+ .split(regex)
27
+ .map((fragment) => (fragment in replacementsObj ? replacementsObj[fragment] : fragment))
28
+ .join('');
29
+ });
30
+ const parseChildren = (expression) => {
31
+ const [string, ...substitutions] = expression.children;
32
+ return Object.assign(Object.assign({}, expression), { string, substitutions });
87
33
  };
88
34
  exports.STRING_SUBSTITUTION = {
89
- requiredProperties: requiredProperties,
90
- operatorAliases: operatorAliases,
91
- propertyAliases: propertyAliases,
92
- evaluate: evaluate,
93
- parseChildren: parseChildren,
35
+ requiredProperties,
36
+ operatorAliases,
37
+ propertyAliases,
38
+ evaluate,
39
+ parseChildren,
94
40
  };
@@ -8,59 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- 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;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  Object.defineProperty(exports, "__esModule", { value: true });
39
12
  exports.SUBTRACT = void 0;
40
- var logicalAnd_1 = require("./logicalAnd");
41
- var _operatorUtils_1 = require("./_operatorUtils");
42
- var requiredProperties = [];
43
- var operatorAliases = ['-', 'subtract', 'minus', 'takeaway'];
44
- var propertyAliases = { subtractFrom: 'from' };
45
- var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () {
46
- var values;
13
+ const logicalAnd_1 = require("./logicalAnd");
14
+ const _operatorUtils_1 = require("./_operatorUtils");
15
+ const requiredProperties = [];
16
+ const operatorAliases = ['-', 'subtract', 'minus', 'takeaway'];
17
+ const propertyAliases = { subtractFrom: 'from' };
18
+ const evaluate = (expression, config) => __awaiter(void 0, void 0, void 0, function* () {
47
19
  var _a;
48
- return __generator(this, function (_b) {
49
- switch (_b.label) {
50
- case 0: return [4, (0, _operatorUtils_1.evaluateArray)((_a = expression.values) !== null && _a !== void 0 ? _a : [expression.from, expression.subtract], config)];
51
- case 1:
52
- values = (_b.sent());
53
- config.typeChecker({ name: 'values', value: values, expectedType: 'array' });
54
- if (values.length < 2)
55
- throw new Error('- Not enough values provided');
56
- return [2, values[0] - values[1]];
57
- }
58
- });
59
- }); };
20
+ const values = (yield (0, _operatorUtils_1.evaluateArray)((_a = expression.values) !== null && _a !== void 0 ? _a : [expression.from, expression.subtract], config));
21
+ config.typeChecker({ name: 'values', value: values, expectedType: 'array' });
22
+ if (values.length < 2)
23
+ throw new Error('- Not enough values provided');
24
+ return values[0] - values[1];
25
+ });
60
26
  exports.SUBTRACT = {
61
- requiredProperties: requiredProperties,
62
- operatorAliases: operatorAliases,
63
- propertyAliases: propertyAliases,
64
- evaluate: evaluate,
27
+ requiredProperties,
28
+ operatorAliases,
29
+ propertyAliases,
30
+ evaluate,
65
31
  parseChildren: logicalAnd_1.parseChildren,
66
32
  };
@@ -1,53 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.typeCheck = void 0;
4
- var helpers_1 = require("./helpers");
5
- var typeCheckItem = function (_a) {
6
- var value = _a.value, name = _a.name, _b = _a.not, not = _b === void 0 ? false : _b, expectedType = _a.expectedType;
7
- var result;
4
+ const helpers_1 = require("./helpers");
5
+ const typeCheckItem = ({ value, name, not = false, expectedType, }) => {
6
+ let result;
8
7
  if (Array.isArray(expectedType)) {
9
- result = expectedType.some(function (type) { return typeCheckItem({ value: value, name: name, expectedType: type }) === true; });
8
+ result = expectedType.some((type) => typeCheckItem({ value, name, expectedType: type }) === true);
10
9
  if (not)
11
10
  result = !result;
12
11
  return result ? true : makeErrorString(name, value, expectedType.join('|'), not);
13
12
  }
14
13
  else {
15
- var checker = typeCheckMap[expectedType];
14
+ const checker = typeCheckMap[expectedType];
16
15
  result = checker(value);
17
16
  if (not)
18
17
  result = !result;
19
18
  return result ? true : makeErrorString(name, value, expectedType, not);
20
19
  }
21
20
  };
22
- var typeCheck = function () {
23
- var args = [];
24
- for (var _i = 0; _i < arguments.length; _i++) {
25
- args[_i] = arguments[_i];
26
- }
27
- var errorStrings = [];
28
- args.forEach(function (item) {
29
- var result = typeCheckItem(item);
21
+ const typeCheck = (...args) => {
22
+ const errorStrings = [];
23
+ args.forEach((item) => {
24
+ const result = typeCheckItem(item);
30
25
  if (result !== true)
31
26
  errorStrings.push(result);
32
27
  });
33
28
  return errorStrings.length > 0 ? errorStrings.join('\n') : true;
34
29
  };
35
30
  exports.typeCheck = typeCheck;
36
- var typeCheckMap = {
37
- string: function (value) { return typeof value === 'string'; },
38
- number: function (value) { return typeof value === 'number'; },
39
- boolean: function (value) { return typeof value === 'boolean'; },
40
- array: function (value) { return Array.isArray(value); },
41
- undefined: function (value) { return value === undefined; },
42
- null: function (value) { return value === null; },
43
- object: function (value) { return typeof value === 'object' && !Array.isArray(value) && value !== null; },
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,
44
39
  };
45
- var makeErrorString = function (name, value, type, not) {
40
+ const makeErrorString = (name, value, type, not) => {
46
41
  if (name !== undefined)
47
- return "- Property \"".concat(name, "\" (value: ").concat(stringifyValue(value), ") is not of type: ").concat(not ? "!(".concat(type, ")") : type);
48
- return "- ".concat(stringifyValue(value), " is not of type: ").concat(not ? "!(".concat(type, ")") : type);
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}`;
49
44
  };
50
- var stringifyValue = function (value) {
51
- var stringValue = value === undefined || Number.isNaN(value) ? String(value) : JSON.stringify(value);
45
+ const stringifyValue = (value) => {
46
+ const stringValue = value === undefined || Number.isNaN(value) ? String(value) : JSON.stringify(value);
52
47
  return (0, helpers_1.truncateString)(stringValue, 50);
53
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fig-tree-evaluator",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
4
4
  "description": "Module to evaluate JSON-structured expression trees",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "build": "yarn generate && rimraf ./build && tsc",
32
32
  "dev": "cp -n src/dev/playground_example.ts src/dev/playground.ts || ts-node src/dev/playground.ts",
33
33
  "demo": "cd demo && yarn start",
34
- "prepublish": "yarn build"
34
+ "prepublishOnly": "yarn build"
35
35
  },
36
36
  "dependencies": {
37
37
  "axios": "^0.27.2",