@briza/illogical 1.5.0 → 1.5.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/changelog.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # illogical changelog
2
2
 
3
+ ## 1.5.3
4
+
5
+ - Fixed an issue where conditions with object data were not being simplified correctly.
6
+
7
+ ## 1.5.2
8
+
9
+ - Update dependencies.
10
+
11
+ ## 1.5.1
12
+
13
+ - Fix issue with nested conditions not being completely simplified.
14
+
3
15
  ## 1.5.0
4
16
 
5
17
  - Add simplify capability to strictly evaluate the expression for all referred values
@@ -45,7 +45,7 @@ function isBoolean(value) {
45
45
  */
46
46
 
47
47
  function isEvaluable(value) {
48
- return typeof value === 'object' && value !== null && !Array.isArray(value);
48
+ return typeof value === 'object' && value !== null && !Array.isArray(value) && typeof value.evaluate === 'function' && typeof value.simplify === 'function' && typeof value.serialize === 'function' && typeof value.toString === 'function';
49
49
  }
50
50
 
51
51
  function _defineProperty(obj, key, value) {
@@ -132,9 +132,9 @@ class Comparison {
132
132
  /**
133
133
  * {@link Evaluable.simplify}
134
134
  */
135
- simplify(...args) {
136
- const left = this.left.simplify(...args);
137
- const right = this.right.simplify(...args);
135
+ simplify() {
136
+ const left = this.left.simplify(...arguments);
137
+ const right = this.right.simplify(...arguments);
138
138
 
139
139
  if (!isEvaluable(left) && !isEvaluable(right)) {
140
140
  return this.comparison(left, right);
@@ -888,17 +888,21 @@ class And extends Logical {
888
888
  */
889
889
 
890
890
 
891
- simplify(...args) {
891
+ simplify() {
892
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
893
+ args[_key] = arguments[_key];
894
+ }
895
+
892
896
  const simplified = this.operands.reduce((result, child) => {
893
897
  if (result !== false) {
894
898
  const childResult = child.simplify(...args);
895
899
 
896
- if (!isBoolean(childResult)) {
900
+ if (isEvaluable(childResult)) {
897
901
  if (isBoolean(result)) {
898
- return [child];
902
+ return [childResult];
899
903
  }
900
904
 
901
- return [...result, child];
905
+ return [...result, childResult];
902
906
  }
903
907
 
904
908
  if (!childResult) {
@@ -960,8 +964,8 @@ class Not extends Logical {
960
964
  */
961
965
 
962
966
 
963
- simplify(...args) {
964
- const simplified = this.operands[0].simplify(...args);
967
+ simplify() {
968
+ const simplified = this.operands[0].simplify(...arguments);
965
969
 
966
970
  if (isBoolean(simplified)) {
967
971
  return !simplified;
@@ -1014,17 +1018,21 @@ class Nor extends Logical {
1014
1018
  */
1015
1019
 
1016
1020
 
1017
- simplify(...args) {
1021
+ simplify() {
1022
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1023
+ args[_key] = arguments[_key];
1024
+ }
1025
+
1018
1026
  const simplified = this.operands.reduce((result, child) => {
1019
1027
  if (result !== false) {
1020
1028
  const childResult = child.simplify(...args);
1021
1029
 
1022
1030
  if (isEvaluable(childResult)) {
1023
1031
  if (isBoolean(result)) {
1024
- return [child];
1032
+ return [childResult];
1025
1033
  }
1026
1034
 
1027
- return [...result, child];
1035
+ return [...result, childResult];
1028
1036
  }
1029
1037
 
1030
1038
  if (childResult) {
@@ -1086,17 +1094,21 @@ class Or extends Logical {
1086
1094
  */
1087
1095
 
1088
1096
 
1089
- simplify(...args) {
1097
+ simplify() {
1098
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1099
+ args[_key] = arguments[_key];
1100
+ }
1101
+
1090
1102
  const simplified = this.operands.reduce((result, child) => {
1091
1103
  if (result !== true) {
1092
1104
  const childResult = child.simplify(...args);
1093
1105
 
1094
- if (!isBoolean(childResult)) {
1106
+ if (isEvaluable(childResult)) {
1095
1107
  if (isBoolean(result)) {
1096
- return [child];
1108
+ return [childResult];
1097
1109
  }
1098
1110
 
1099
- return [...result, child];
1111
+ return [...result, childResult];
1100
1112
  }
1101
1113
 
1102
1114
  if (childResult) {
@@ -1173,16 +1185,22 @@ class Xor extends Logical {
1173
1185
  */
1174
1186
 
1175
1187
 
1176
- simplify(...args) {
1177
- const [evaluablesLeft, trueCount] = this.operands.reduce(([notSimplifiedConditions, trueCount], child) => {
1188
+ simplify() {
1189
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1190
+ args[_key] = arguments[_key];
1191
+ }
1192
+
1193
+ const [evaluablesLeft, trueCount] = this.operands.reduce((_ref, child) => {
1194
+ let [notSimplifiedConditions, trueCount] = _ref;
1195
+
1178
1196
  if (trueCount > 1) {
1179
1197
  return [notSimplifiedConditions, trueCount];
1180
1198
  }
1181
1199
 
1182
1200
  const childResult = child.simplify(...args);
1183
1201
 
1184
- if (!isBoolean(childResult)) {
1185
- return [[...notSimplifiedConditions, child], trueCount];
1202
+ if (isEvaluable(childResult)) {
1203
+ return [[...notSimplifiedConditions, childResult], trueCount];
1186
1204
  }
1187
1205
 
1188
1206
  if (childResult) {
@@ -1244,11 +1262,11 @@ class Collection extends Operand {
1244
1262
  */
1245
1263
 
1246
1264
 
1247
- simplify(...args) {
1265
+ simplify() {
1248
1266
  const values = [];
1249
1267
 
1250
1268
  for (const item of this.items) {
1251
- const simplifiedItem = item.simplify(...args);
1269
+ const simplifiedItem = item.simplify(...arguments);
1252
1270
 
1253
1271
  if (isEvaluable(simplifiedItem)) {
1254
1272
  return this;
@@ -1455,9 +1473,10 @@ class Reference extends Operand {
1455
1473
  */
1456
1474
 
1457
1475
 
1458
- serialize({
1459
- referenceSerialization
1460
- }) {
1476
+ serialize(_ref) {
1477
+ let {
1478
+ referenceSerialization
1479
+ } = _ref;
1461
1480
  const key = this.dataType ? `${this.key}.(${this.dataType})` : this.key;
1462
1481
  return referenceSerialization(key);
1463
1482
  }
@@ -1596,6 +1615,8 @@ class Parser {
1596
1615
 
1597
1616
 
1598
1617
  parseRawExp(raw) {
1618
+ var _this = this;
1619
+
1599
1620
  // Value / Reference
1600
1621
  if (!Array.isArray(raw)) {
1601
1622
  return this.getOperand(raw);
@@ -1617,9 +1638,11 @@ class Parser {
1617
1638
  * @param collapsible
1618
1639
  */
1619
1640
 
1620
- const logicalExpressionReducer = (operands, collapsible = false) => {
1641
+ const logicalExpressionReducer = function (operands) {
1642
+ let collapsible = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1643
+
1621
1644
  if (operands.length === 0 || operands.filter(operand => operand.type === EvaluableType.Expression).length === 0) {
1622
- return this.getOperand(raw);
1645
+ return _this.getOperand(raw);
1623
1646
  }
1624
1647
 
1625
1648
  return collapsible && operands.length === 1 ? operands[0] : undefined;
package/lib/illogical.js CHANGED
@@ -49,7 +49,7 @@ function isBoolean(value) {
49
49
  */
50
50
 
51
51
  function isEvaluable(value) {
52
- return typeof value === 'object' && value !== null && !Array.isArray(value);
52
+ return typeof value === 'object' && value !== null && !Array.isArray(value) && typeof value.evaluate === 'function' && typeof value.simplify === 'function' && typeof value.serialize === 'function' && typeof value.toString === 'function';
53
53
  }
54
54
 
55
55
  function _defineProperty(obj, key, value) {
@@ -136,9 +136,9 @@ class Comparison {
136
136
  /**
137
137
  * {@link Evaluable.simplify}
138
138
  */
139
- simplify(...args) {
140
- const left = this.left.simplify(...args);
141
- const right = this.right.simplify(...args);
139
+ simplify() {
140
+ const left = this.left.simplify(...arguments);
141
+ const right = this.right.simplify(...arguments);
142
142
 
143
143
  if (!isEvaluable(left) && !isEvaluable(right)) {
144
144
  return this.comparison(left, right);
@@ -892,17 +892,21 @@ class And extends Logical {
892
892
  */
893
893
 
894
894
 
895
- simplify(...args) {
895
+ simplify() {
896
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
897
+ args[_key] = arguments[_key];
898
+ }
899
+
896
900
  const simplified = this.operands.reduce((result, child) => {
897
901
  if (result !== false) {
898
902
  const childResult = child.simplify(...args);
899
903
 
900
- if (!isBoolean(childResult)) {
904
+ if (isEvaluable(childResult)) {
901
905
  if (isBoolean(result)) {
902
- return [child];
906
+ return [childResult];
903
907
  }
904
908
 
905
- return [...result, child];
909
+ return [...result, childResult];
906
910
  }
907
911
 
908
912
  if (!childResult) {
@@ -964,8 +968,8 @@ class Not extends Logical {
964
968
  */
965
969
 
966
970
 
967
- simplify(...args) {
968
- const simplified = this.operands[0].simplify(...args);
971
+ simplify() {
972
+ const simplified = this.operands[0].simplify(...arguments);
969
973
 
970
974
  if (isBoolean(simplified)) {
971
975
  return !simplified;
@@ -1018,17 +1022,21 @@ class Nor extends Logical {
1018
1022
  */
1019
1023
 
1020
1024
 
1021
- simplify(...args) {
1025
+ simplify() {
1026
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1027
+ args[_key] = arguments[_key];
1028
+ }
1029
+
1022
1030
  const simplified = this.operands.reduce((result, child) => {
1023
1031
  if (result !== false) {
1024
1032
  const childResult = child.simplify(...args);
1025
1033
 
1026
1034
  if (isEvaluable(childResult)) {
1027
1035
  if (isBoolean(result)) {
1028
- return [child];
1036
+ return [childResult];
1029
1037
  }
1030
1038
 
1031
- return [...result, child];
1039
+ return [...result, childResult];
1032
1040
  }
1033
1041
 
1034
1042
  if (childResult) {
@@ -1090,17 +1098,21 @@ class Or extends Logical {
1090
1098
  */
1091
1099
 
1092
1100
 
1093
- simplify(...args) {
1101
+ simplify() {
1102
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1103
+ args[_key] = arguments[_key];
1104
+ }
1105
+
1094
1106
  const simplified = this.operands.reduce((result, child) => {
1095
1107
  if (result !== true) {
1096
1108
  const childResult = child.simplify(...args);
1097
1109
 
1098
- if (!isBoolean(childResult)) {
1110
+ if (isEvaluable(childResult)) {
1099
1111
  if (isBoolean(result)) {
1100
- return [child];
1112
+ return [childResult];
1101
1113
  }
1102
1114
 
1103
- return [...result, child];
1115
+ return [...result, childResult];
1104
1116
  }
1105
1117
 
1106
1118
  if (childResult) {
@@ -1177,16 +1189,22 @@ class Xor extends Logical {
1177
1189
  */
1178
1190
 
1179
1191
 
1180
- simplify(...args) {
1181
- const [evaluablesLeft, trueCount] = this.operands.reduce(([notSimplifiedConditions, trueCount], child) => {
1192
+ simplify() {
1193
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1194
+ args[_key] = arguments[_key];
1195
+ }
1196
+
1197
+ const [evaluablesLeft, trueCount] = this.operands.reduce((_ref, child) => {
1198
+ let [notSimplifiedConditions, trueCount] = _ref;
1199
+
1182
1200
  if (trueCount > 1) {
1183
1201
  return [notSimplifiedConditions, trueCount];
1184
1202
  }
1185
1203
 
1186
1204
  const childResult = child.simplify(...args);
1187
1205
 
1188
- if (!isBoolean(childResult)) {
1189
- return [[...notSimplifiedConditions, child], trueCount];
1206
+ if (isEvaluable(childResult)) {
1207
+ return [[...notSimplifiedConditions, childResult], trueCount];
1190
1208
  }
1191
1209
 
1192
1210
  if (childResult) {
@@ -1248,11 +1266,11 @@ class Collection extends Operand {
1248
1266
  */
1249
1267
 
1250
1268
 
1251
- simplify(...args) {
1269
+ simplify() {
1252
1270
  const values = [];
1253
1271
 
1254
1272
  for (const item of this.items) {
1255
- const simplifiedItem = item.simplify(...args);
1273
+ const simplifiedItem = item.simplify(...arguments);
1256
1274
 
1257
1275
  if (isEvaluable(simplifiedItem)) {
1258
1276
  return this;
@@ -1459,9 +1477,10 @@ class Reference extends Operand {
1459
1477
  */
1460
1478
 
1461
1479
 
1462
- serialize({
1463
- referenceSerialization
1464
- }) {
1480
+ serialize(_ref) {
1481
+ let {
1482
+ referenceSerialization
1483
+ } = _ref;
1465
1484
  const key = this.dataType ? `${this.key}.(${this.dataType})` : this.key;
1466
1485
  return referenceSerialization(key);
1467
1486
  }
@@ -1600,6 +1619,8 @@ class Parser {
1600
1619
 
1601
1620
 
1602
1621
  parseRawExp(raw) {
1622
+ var _this = this;
1623
+
1603
1624
  // Value / Reference
1604
1625
  if (!Array.isArray(raw)) {
1605
1626
  return this.getOperand(raw);
@@ -1621,9 +1642,11 @@ class Parser {
1621
1642
  * @param collapsible
1622
1643
  */
1623
1644
 
1624
- const logicalExpressionReducer = (operands, collapsible = false) => {
1645
+ const logicalExpressionReducer = function (operands) {
1646
+ let collapsible = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1647
+
1625
1648
  if (operands.length === 0 || operands.filter(operand => operand.type === EvaluableType.Expression).length === 0) {
1626
- return this.getOperand(raw);
1649
+ return _this.getOperand(raw);
1627
1650
  }
1628
1651
 
1629
1652
  return collapsible && operands.length === 1 ? operands[0] : undefined;
@@ -1855,4 +1878,4 @@ exports.OPERATOR_PRESENT = OPERATOR$7;
1855
1878
  exports.OPERATOR_SUFFIX = OPERATOR$6;
1856
1879
  exports.OPERATOR_UNDEFINED = OPERATOR$5;
1857
1880
  exports.OPERATOR_XOR = OPERATOR;
1858
- exports['default'] = Engine;
1881
+ exports["default"] = Engine;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@briza/illogical",
3
- "version": "1.5.0",
3
+ "version": "1.5.3",
4
4
  "description": "A micro conditional javascript engine used to parse the raw logical and comparison expressions, evaluate the expression in the given data context, and provide access to a text form of the given expressions.",
5
5
  "main": "lib/illogical.js",
6
6
  "module": "lib/illogical.esm.js",
@@ -25,7 +25,7 @@
25
25
  "lint": "eslint --max-warnings 0 \"src/**/*.{ts,js}\"",
26
26
  "lint:fix": "eslint --max-warnings 0 \"src/**/*.{ts,js}\" --fix",
27
27
  "prepublishOnly": "npm run test && npm run build",
28
- "check-licenses": "license-checker --summary --excludePrivatePackages --onlyAllow \"MIT;MIT OR X11;Apache-2.0;ISC;BSD-3-Clause;BSD-2-Clause;CC-BY-4.0;Public Domain;BSD;CC-BY-3.0;CC0-1.0;Unlicense\""
28
+ "check-licenses": "license-checker --summary --excludePrivatePackages --onlyAllow \"MIT;MIT OR X11;Apache-2.0;ISC;BSD-3-Clause;BSD-2-Clause;CC-BY-4.0;Public Domain;BSD;CC-BY-3.0;CC0-1.0;Python-2.0;Unlicense\""
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
@@ -42,31 +42,31 @@
42
42
  "rules"
43
43
  ],
44
44
  "devDependencies": {
45
- "@babel/core": "^7.15.0",
46
- "@babel/plugin-proposal-class-properties": "^7.14.5",
47
- "@babel/preset-env": "^7.15.0",
48
- "@babel/preset-typescript": "^7.15.0",
49
- "@rollup/plugin-babel": "^5.2.3",
50
- "@rollup/plugin-commonjs": "^20.0.0",
51
- "@rollup/plugin-node-resolve": "^13.0.4",
52
- "@types/jest": "^27.0.1",
53
- "@typescript-eslint/eslint-plugin": "^4.29.2",
54
- "@typescript-eslint/parser": "^4.29.2",
55
- "eslint": "^7.32.0",
56
- "eslint-config-prettier": "^8.3.0",
57
- "eslint-import-resolver-typescript": "^2.3.0",
58
- "eslint-plugin-import": "^2.24.0",
45
+ "@babel/core": "^7.17.5",
46
+ "@babel/plugin-proposal-class-properties": "^7.16.7",
47
+ "@babel/preset-env": "^7.16.11",
48
+ "@babel/preset-typescript": "^7.16.7",
49
+ "@rollup/plugin-babel": "^5.3.1",
50
+ "@rollup/plugin-commonjs": "^21.0.2",
51
+ "@rollup/plugin-node-resolve": "^13.1.3",
52
+ "@types/jest": "^27.4.1",
53
+ "@typescript-eslint/eslint-plugin": "^5.13.0",
54
+ "@typescript-eslint/parser": "^5.13.0",
55
+ "eslint": "^8.10.0",
56
+ "eslint-config-prettier": "^8.4.0",
57
+ "eslint-import-resolver-typescript": "^2.5.0",
58
+ "eslint-plugin-import": "^2.25.4",
59
59
  "eslint-plugin-node": "^11.1.0",
60
- "eslint-plugin-prettier": "^3.4.0",
61
- "eslint-plugin-promise": "^5.1.0",
60
+ "eslint-plugin-prettier": "^3.4.1",
61
+ "eslint-plugin-promise": "^6.0.0",
62
62
  "eslint-plugin-simple-import-sort": "^7.0.0",
63
- "jest": "^27.0.6",
63
+ "jest": "^27.5.1",
64
64
  "license-checker": "^25.0.1",
65
- "prettier": "^2.3.2",
66
- "rollup": "^2.56.2",
65
+ "prettier": "^2.5.1",
66
+ "rollup": "^2.69.0",
67
67
  "rollup-plugin-eslint": "^7.0.0",
68
- "ts-jest": "^27.0.5",
69
- "typedoc": "^0.21.6",
70
- "typescript": "^4.3.5"
68
+ "ts-jest": "^27.1.3",
69
+ "typedoc": "^0.22.12",
70
+ "typescript": "4.5.5"
71
71
  }
72
72
  }
package/readme.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A micro conditional javascript engine used to parse the raw logical and comparison expressions, evaluate the expression in the given data context, and provide access to a text form of the given expressions.
4
4
 
5
- > Revision: August 17, 2021.
5
+ > Revision: February 1, 2022.
6
6
 
7
7
  ## About
8
8
 
@@ -15,7 +15,7 @@ export interface Context {
15
15
  /**
16
16
  * Evaluation result
17
17
  */
18
- export declare type Result = undefined | null | string | number | boolean | Array<Result>;
18
+ export declare type Result = undefined | null | string | number | boolean | Array<Result> | Record<string, unknown>;
19
19
  export declare enum EvaluableType {
20
20
  Operand = "Operand",
21
21
  Expression = "Expression"