@briza/illogical 1.5.8 → 1.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@briza/illogical",
3
- "version": "1.5.8",
3
+ "version": "1.6.0",
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;Python-2.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;BlueOak-1.0.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.23.6",
45
+ "@babel/core": "^7.25.2",
46
46
  "@babel/plugin-proposal-class-properties": "^7.18.6",
47
- "@babel/preset-env": "^7.23.6",
48
- "@babel/preset-typescript": "^7.23.3",
47
+ "@babel/preset-env": "^7.25.3",
48
+ "@babel/preset-typescript": "^7.24.7",
49
49
  "@rollup/plugin-babel": "^6.0.4",
50
- "@rollup/plugin-commonjs": "^25.0.7",
50
+ "@rollup/plugin-commonjs": "^26.0.1",
51
51
  "@rollup/plugin-eslint": "^9.0.5",
52
52
  "@rollup/plugin-node-resolve": "^15.2.3",
53
- "@types/jest": "^28.1.8",
54
- "@typescript-eslint/eslint-plugin": "^6.14.0",
55
- "@typescript-eslint/parser": "^6.14.0",
56
- "eslint": "^8.55.0",
53
+ "@types/jest": "^29.5.12",
54
+ "@typescript-eslint/eslint-plugin": "^8.0.1",
55
+ "@typescript-eslint/parser": "^8.0.1",
56
+ "eslint": "^8.57.0",
57
57
  "eslint-config-prettier": "^9.1.0",
58
58
  "eslint-import-resolver-typescript": "^3.6.1",
59
- "eslint-plugin-import": "^2.29.0",
59
+ "eslint-plugin-import": "^2.29.1",
60
60
  "eslint-plugin-node": "^11.1.0",
61
- "eslint-plugin-prettier": "^5.0.1",
62
- "eslint-plugin-promise": "^6.1.1",
63
- "eslint-plugin-simple-import-sort": "^10.0.0",
61
+ "eslint-plugin-prettier": "^5.2.1",
62
+ "eslint-plugin-promise": "^7.1.0",
63
+ "eslint-plugin-simple-import-sort": "^12.1.1",
64
64
  "jest": "^29.7.0",
65
65
  "license-checker": "^25.0.1",
66
- "prettier": "^3.1.1",
67
- "rollup": "^4.8.0",
68
- "ts-jest": "^29.1.1",
69
- "typedoc": "^0.25.4",
70
- "typescript": "^5.3.3"
66
+ "prettier": "^3.3.3",
67
+ "rollup": "^4.20.0",
68
+ "ts-jest": "^29.2.4",
69
+ "typedoc": "^0.26.5",
70
+ "typescript": "^5.5.4"
71
71
  }
72
72
  }
package/readme.md CHANGED
@@ -58,6 +58,11 @@ yarn add @briza/illogical -D
58
58
  - [Nor](#nor)
59
59
  - [Xor](#xor)
60
60
  - [Not](#not)
61
+ - [Arithmetic Expressions](#arithmetic-expressions)
62
+ - [Divide](#divide)
63
+ - [Multiply](#multiply)
64
+ - [Subtract](#subtract)
65
+ - [Sum](#sum)
61
66
  - [Engine Options](#engine-options)
62
67
  - [Parser Options](#parser-options)
63
68
  - [Reference Predicate](#reference-predicate)
@@ -683,6 +688,76 @@ Expression format: `["NOT", Operand]`
683
688
  engine.evaluate(['NOT', ['==', 5, 5]]) // true
684
689
  ```
685
690
 
691
+ ### Arithmetic Expressions
692
+
693
+ Arithmetic Expressions are not supported as root level expressions since they must evaluate to a boolean. But it can be used nested within [Comparisson Expressions](#comparison-expressions).
694
+
695
+ #### Division
696
+
697
+ The arithmetical operator for division produces the quotient of its operands where the left-most operand is the dividend and the subsequent one is the divisor, done from left to right.
698
+
699
+ Expression format: `["/", First Operand, Second Operand, ... , Nth Operand]`.
700
+
701
+ > Valid operand types: [Arithmetic Expressions](#arithmetic-expressions) or [Operands](#operand-types).
702
+
703
+ ```json
704
+ ["==", ["/", 100, 10], 10]
705
+ ```
706
+
707
+ ```js
708
+ engine.evaluate(["==", ["/", 100, 10], 10]) // true
709
+ ```
710
+
711
+ #### Multiplication
712
+
713
+ The arithmetical operator for multiplication produces the product of the operands.
714
+
715
+ Expression format: `["*", First Operand, Second Operand, ... , Nth Operand]`.
716
+
717
+ > Valid operand types: [Arithmetic Expressions](#arithmetic-expressions) or [Operands](#operand-types).
718
+
719
+ ```json
720
+ ["==", ["*", 100, 10], 10]
721
+ ```
722
+
723
+ ```js
724
+ engine.evaluate(["==", ["*", 10, 10], 100]) // true
725
+ ```
726
+
727
+ #### Subtraction
728
+
729
+ The arithmetical operator for subtraction subtracts the operands, producing their difference.
730
+
731
+ Expression format: `["-", First Operand, Second Operand, ... , Nth Operand]`.
732
+
733
+ > Valid operand types: [Arithmetic Expressions](#arithmetic-expressions) or [Operands](#operand-types).
734
+
735
+ ```json
736
+ ["==", ["-", 20, 10], 10]
737
+ ```
738
+
739
+ ```js
740
+ engine.evaluate(["==", ["-", 20, 10], 10]) // true
741
+ ```
742
+
743
+
744
+ #### Addition
745
+
746
+ The arithmetical operator for addition produces the sum of the operands.
747
+
748
+ Expression format: `["+", First Operand, Second Operand, ... , Nth Operand]`.
749
+
750
+ > Valid operand types: [Arithmetic Expressions](#arithmetic-expressions) or [Operands](#operand-types).
751
+
752
+ ```json
753
+ ["==", ["+", 5, 5], 10]
754
+ ```
755
+
756
+ ```js
757
+ engine.evaluate(["==", ["+", 5, 5], 10]) // true
758
+ ```
759
+
760
+
686
761
  ## Engine Options
687
762
 
688
763
  ### Parser Options
@@ -741,19 +816,31 @@ operatorMapping: Map<symbol, string>
741
816
  **Default operator mapping:**
742
817
 
743
818
  ```typescript
744
- // Comparison
745
- ;[OPERATOR_EQ, '=='][(OPERATOR_NE, '!=')][(OPERATOR_GT, '>')][
746
- (OPERATOR_GE, '>=')
747
- ][(OPERATOR_LT, '<')][(OPERATOR_LE, '<=')][(OPERATOR_IN, 'IN')][
748
- (OPERATOR_NOT_IN, 'NOT IN')
749
- ][(OPERATOR_PREFIX, 'PREFIX')][(OPERATOR_SUFFIX, 'SUFFIX')][
750
- (OPERATOR_OVERLAP, 'OVERLAP')
751
- ][(OPERATOR_UNDEFINED, 'UNDEFINED')][(OPERATOR_PRESENT, 'PRESENT')][
819
+ // Comparison
820
+ [OPERATOR_EQ, '=='],
821
+ [OPERATOR_NE, '!='],
822
+ [OPERATOR_GT, '>'],
823
+ [OPERATOR_GE, '>='],
824
+ [OPERATOR_LT, '<'],
825
+ [OPERATOR_LE, '<='],
826
+ [OPERATOR_IN, 'IN'],
827
+ [OPERATOR_NOT_IN, 'NOT IN'],
828
+ [OPERATOR_PREFIX, 'PREFIX'],
829
+ [OPERATOR_SUFFIX, 'SUFFIX'],
830
+ [OPERATOR_OVERLAP, 'OVERLAP'],
831
+ [OPERATOR_UNDEFINED, 'UNDEFINED'],
832
+ [OPERATOR_PRESENT, 'PRESENT'],
752
833
  // Logical
753
- (OPERATOR_AND, 'AND')
754
- ][(OPERATOR_OR, 'OR')][(OPERATOR_NOR, 'NOR')][(OPERATOR_XOR, 'XOR')][
755
- (OPERATOR_NOT, 'NOT')
756
- ]
834
+ [OPERATOR_AND, 'AND'],
835
+ [OPERATOR_OR, 'OR'],
836
+ [OPERATOR_NOR, 'NOR'],
837
+ [OPERATOR_XOR, 'XOR'],
838
+ [OPERATOR_NOT, 'NOT'],
839
+ // Arithmetic
840
+ [OPERATOR_SUM, '+'],
841
+ [OPERATOR_SUBTRACT, '-'],
842
+ [OPERATOR_MULTIPLY, '*'],
843
+ [OPERATOR_DIVIDE, '/'],
757
844
  ```
758
845
 
759
846
  > The operator keys are unique symbols which could be imported from the engine package:
@@ -778,6 +865,10 @@ import {
778
865
  OPERATOR_NOR,
779
866
  OPERATOR_XOR,
780
867
  OPERATOR_NOT,
868
+ OPERATOR_DIVIDE,
869
+ OPERATOR_MULTIPLY,
870
+ OPERATOR_SUBTRACT,
871
+ OPERATOR_SUM,
781
872
  } from '@briza/illogical'
782
873
  ```
783
874
 
@@ -4,6 +4,11 @@ import { Evaluable, Result } from './evaluable';
4
4
  * @param value Tested value.
5
5
  */
6
6
  export declare function isNumber(value: Result): value is number;
7
+ /**
8
+ * Is number predicate.
9
+ * @param value Tested value.
10
+ */
11
+ export declare function isInfinite(value: Result): value is typeof Infinity;
7
12
  /**
8
13
  * Is string type predicate.
9
14
  * @param value Tested value.
@@ -26,3 +31,15 @@ export declare function isBoolean(value: unknown): value is boolean;
26
31
  * @returns {Evaluable}
27
32
  */
28
33
  export declare function isEvaluable(value: Result | Evaluable): value is Evaluable;
34
+ /**
35
+ * Ensures all values are results.
36
+ * @param {(Result | Evaluable)[]} values results or evaluables
37
+ * @returns {boolean} type guard
38
+ */
39
+ export declare function areAllResults(values: (Result | Evaluable)[]): values is Result[];
40
+ /**
41
+ * Ensures all values are numbers.
42
+ * @param {Result[]} results results or evaluables
43
+ * @returns {boolean} type guard
44
+ */
45
+ export declare function areAllNumbers(results: Result[]): results is number[];
@@ -0,0 +1,14 @@
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Arithmetic } from '.';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Divide operation expression
6
+ */
7
+ export declare class Divide extends Arithmetic {
8
+ /**
9
+ * @constructor Generic constructor
10
+ * @param {Evaluable[]} args
11
+ */
12
+ constructor(...args: Evaluable[]);
13
+ operate(results: Result[]): Result;
14
+ }
@@ -0,0 +1,42 @@
1
+ import { Context, Evaluable, EvaluableType, Result, SimplifyArgs } from '../../common/evaluable';
2
+ import { Operand } from '../../operand';
3
+ import { ExpressionInput } from '../../parser';
4
+ import { Options } from '../../parser/options';
5
+ /**
6
+ * Abstract arithmetic expression
7
+ */
8
+ export declare abstract class Arithmetic implements Evaluable {
9
+ protected readonly operator: string;
10
+ protected readonly operatorSymbol: symbol;
11
+ protected readonly operands: Operand[];
12
+ type: EvaluableType;
13
+ /**
14
+ * @constructor
15
+ * @param {string} operator String representation of the operator.
16
+ * @param {symbol} operatorSymbol Operator symbol.
17
+ * @param {Operand[]} operands Operands.
18
+ */
19
+ constructor(operator: string, operatorSymbol: symbol, operands: Operand[]);
20
+ /**
21
+ * Performs the arithmetic operation on the operands evaluated values.
22
+ * @param {Result[]} results Operand result values.
23
+ * @returns {Result}
24
+ */
25
+ abstract operate(results: Result[]): Result;
26
+ /**
27
+ * {@link Evaluable.evaluate}
28
+ */
29
+ evaluate(ctx: Context): Result;
30
+ /**
31
+ * {@link Evaluable.toString}
32
+ */
33
+ toString(): string;
34
+ /**
35
+ * {@link Evaluable.simplify}
36
+ */
37
+ simplify(...args: SimplifyArgs): Result | Evaluable;
38
+ /**
39
+ * {@link Evaluable.serialize}
40
+ */
41
+ serialize(options: Options): ExpressionInput;
42
+ }
@@ -0,0 +1,3 @@
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Operand } from '../../operand';
3
+ export declare const isSimplifiedArithmeticExpression: (operand: Operand, result: Result | Evaluable) => result is Result;
@@ -0,0 +1,14 @@
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Arithmetic } from '.';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Multiply operation expression
6
+ */
7
+ export declare class Multiply extends Arithmetic {
8
+ /**
9
+ * @constructor Generic constructor
10
+ * @param {Evaluable[]} args
11
+ */
12
+ constructor(...args: Evaluable[]);
13
+ operate(results: Result[]): Result;
14
+ }
@@ -0,0 +1 @@
1
+ export declare const operateWithExpectedDecimals: (operation: "sum" | "subtract" | "multiply") => (first: number, second: number) => number;
@@ -0,0 +1,14 @@
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Arithmetic } from '.';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Subtract operation expression
6
+ */
7
+ export declare class Subtract extends Arithmetic {
8
+ /**
9
+ * @constructor Generic constructor
10
+ * @param {Evaluable[]} args
11
+ */
12
+ constructor(...args: Evaluable[]);
13
+ operate(results: Result[]): Result;
14
+ }
@@ -0,0 +1,14 @@
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Arithmetic } from '.';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Sum operation expression
6
+ */
7
+ export declare class Sum extends Arithmetic {
8
+ /**
9
+ * @constructor Generic constructor
10
+ * @param {Evaluable[]} args
11
+ */
12
+ constructor(...args: Evaluable[]);
13
+ operate(results: Result[]): Result;
14
+ }
package/types/index.d.ts CHANGED
@@ -4,6 +4,10 @@
4
4
  */
5
5
  import { Context, Evaluable } from './common/evaluable';
6
6
  import { isEvaluable } from './common/type-check';
7
+ import { OPERATOR as OPERATOR_DIVIDE } from './expression/arithmetic/divide';
8
+ import { OPERATOR as OPERATOR_MULTIPLY } from './expression/arithmetic/multiply';
9
+ import { OPERATOR as OPERATOR_SUBTRACT } from './expression/arithmetic/subtract';
10
+ import { OPERATOR as OPERATOR_SUM } from './expression/arithmetic/sum';
7
11
  import { OPERATOR as OPERATOR_EQ } from './expression/comparison/eq';
8
12
  import { OPERATOR as OPERATOR_GE } from './expression/comparison/ge';
9
13
  import { OPERATOR as OPERATOR_GT } from './expression/comparison/gt';
@@ -25,7 +29,7 @@ import { OPERATOR as OPERATOR_XOR } from './expression/logical/xor';
25
29
  import { ExpressionInput, Input } from './parser';
26
30
  import { Options } from './parser/options';
27
31
  export { defaultOptions } from './parser/options';
28
- export { isEvaluable, OPERATOR_EQ, OPERATOR_NE, OPERATOR_GT, OPERATOR_GE, OPERATOR_LT, OPERATOR_LE, OPERATOR_IN, OPERATOR_NOT_IN, OPERATOR_PREFIX, OPERATOR_SUFFIX, OPERATOR_OVERLAP, OPERATOR_UNDEFINED, OPERATOR_PRESENT, OPERATOR_AND, OPERATOR_OR, OPERATOR_NOR, OPERATOR_XOR, OPERATOR_NOT, };
32
+ export { isEvaluable, OPERATOR_EQ, OPERATOR_NE, OPERATOR_GT, OPERATOR_GE, OPERATOR_LT, OPERATOR_LE, OPERATOR_IN, OPERATOR_NOT_IN, OPERATOR_PREFIX, OPERATOR_SUFFIX, OPERATOR_OVERLAP, OPERATOR_UNDEFINED, OPERATOR_PRESENT, OPERATOR_AND, OPERATOR_OR, OPERATOR_NOR, OPERATOR_XOR, OPERATOR_NOT, OPERATOR_DIVIDE, OPERATOR_MULTIPLY, OPERATOR_SUBTRACT, OPERATOR_SUM, };
29
33
  /**
30
34
  * Condition engine
31
35
  */
@@ -8,7 +8,8 @@ export type ExpressionInput = [string, ...Input[]];
8
8
  */
9
9
  export declare class Parser {
10
10
  private readonly opts;
11
- private readonly expectedOperators;
11
+ private readonly expectedRootOperators;
12
+ private readonly unexpectedRootSymbols;
12
13
  /**
13
14
  * @constructor
14
15
  * @param {Options?} options Parser options.