@briza/illogical 1.6.0 → 1.7.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.6.0",
3
+ "version": "1.7.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",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "git@github.com:briza-insurance/illogical.git"
32
+ "url": "git+ssh://git@github.com/briza-insurance/illogical.git"
33
33
  },
34
34
  "bugs": {
35
35
  "url": "https://github.com/briza-insurance/illogical/issues"
@@ -42,7 +42,7 @@
42
42
  "rules"
43
43
  ],
44
44
  "devDependencies": {
45
- "@babel/core": "^7.25.2",
45
+ "@babel/core": "^7.27.3",
46
46
  "@babel/plugin-proposal-class-properties": "^7.18.6",
47
47
  "@babel/preset-env": "^7.25.3",
48
48
  "@babel/preset-typescript": "^7.24.7",
@@ -64,7 +64,7 @@
64
64
  "jest": "^29.7.0",
65
65
  "license-checker": "^25.0.1",
66
66
  "prettier": "^3.3.3",
67
- "rollup": "^4.20.0",
67
+ "rollup": "^4.41.1",
68
68
  "ts-jest": "^29.2.4",
69
69
  "typedoc": "^0.26.5",
70
70
  "typescript": "^5.5.4"
package/readme.md CHANGED
@@ -224,6 +224,19 @@ engine.simplify(
224
224
  ) // ['==', '$b', 20]
225
225
  ```
226
226
 
227
+ ### Unsafe Simplify
228
+
229
+ Simplifies an expression with a given context, but without parsing and ensuring the condition is
230
+ syntatically correct. This can be used in situations where you can decouple the parsing from
231
+ the simplification and extra performance is needed in runtime.
232
+
233
+ ```js
234
+ engine.unsafeSimplify(
235
+ ['OR', ['==', '$a', 10], ['==', '$b', 20], ['==', '$c', 20]],
236
+ { a: 10 }
237
+ ) // true due to $a. Expressions for $b and $c won't even be parsed.
238
+ ```
239
+
227
240
  ## Working with Expressions
228
241
 
229
242
  ### Evaluation Data Context
@@ -237,6 +250,7 @@ To reference the nested reference, please use "." delimiter, e.g.:
237
250
 
238
251
  If the key of the nested reference includes the "." delimiter, please wrap the whole key with backticks `` ` ``, e.g.:
239
252
  `` $address.`city.code` `` can reference the object
253
+
240
254
  ```javascript
241
255
  {
242
256
  address: {
@@ -245,7 +259,8 @@ If the key of the nested reference includes the "." delimiter, please wrap the w
245
259
  }
246
260
  ```
247
261
 
248
- `` $address.`city.code`[0] `` can reference the object
262
+ ``$address.`city.code`[0]`` can reference the object
263
+
249
264
  ```javascript
250
265
  {
251
266
  address: {
@@ -253,6 +268,7 @@ If the key of the nested reference includes the "." delimiter, please wrap the w
253
268
  }
254
269
  }
255
270
  ```
271
+
256
272
  when the value of the nested reference is an array.
257
273
 
258
274
  #### Accessing Array Element:
@@ -705,7 +721,7 @@ Expression format: `["/", First Operand, Second Operand, ... , Nth Operand]`.
705
721
  ```
706
722
 
707
723
  ```js
708
- engine.evaluate(["==", ["/", 100, 10], 10]) // true
724
+ engine.evaluate(['==', ['/', 100, 10], 10]) // true
709
725
  ```
710
726
 
711
727
  #### Multiplication
@@ -721,7 +737,7 @@ Expression format: `["*", First Operand, Second Operand, ... , Nth Operand]`.
721
737
  ```
722
738
 
723
739
  ```js
724
- engine.evaluate(["==", ["*", 10, 10], 100]) // true
740
+ engine.evaluate(['==', ['*', 10, 10], 100]) // true
725
741
  ```
726
742
 
727
743
  #### Subtraction
@@ -737,10 +753,9 @@ Expression format: `["-", First Operand, Second Operand, ... , Nth Operand]`.
737
753
  ```
738
754
 
739
755
  ```js
740
- engine.evaluate(["==", ["-", 20, 10], 10]) // true
756
+ engine.evaluate(['==', ['-', 20, 10], 10]) // true
741
757
  ```
742
758
 
743
-
744
759
  #### Addition
745
760
 
746
761
  The arithmetical operator for addition produces the sum of the operands.
@@ -754,10 +769,9 @@ Expression format: `["+", First Operand, Second Operand, ... , Nth Operand]`.
754
769
  ```
755
770
 
756
771
  ```js
757
- engine.evaluate(["==", ["+", 5, 5], 10]) // true
772
+ engine.evaluate(['==', ['+', 5, 5], 10]) // true
758
773
  ```
759
774
 
760
-
761
775
  ## Engine Options
762
776
 
763
777
  ### Parser Options
@@ -43,3 +43,5 @@ export declare function areAllResults(values: (Result | Evaluable)[]): values is
43
43
  * @returns {boolean} type guard
44
44
  */
45
45
  export declare function areAllNumbers(results: Result[]): results is number[];
46
+ export declare function isUndefined(value: unknown): value is undefined;
47
+ export declare function isNull(value: unknown): value is null;
@@ -17,6 +17,16 @@ export declare abstract class Arithmetic implements Evaluable {
17
17
  * @param {Operand[]} operands Operands.
18
18
  */
19
19
  constructor(operator: string, operatorSymbol: symbol, operands: Operand[]);
20
+ /**
21
+ * Helper function to assist with arithmetic evaluation. Ensures that all
22
+ * operands are present and are numbers. Throws error if any operand is not a
23
+ * number.
24
+ *
25
+ * @param {Result[]} results
26
+ * @returns {number[] | false} false if any operand is missing, otherwise the
27
+ * array of numbers
28
+ */
29
+ protected getResultValues(results: Result[]): number[] | false;
20
30
  /**
21
31
  * Performs the arithmetic operation on the operands evaluated values.
22
32
  * @param {Result[]} results Operand result values.
package/types/index.d.ts CHANGED
@@ -75,5 +75,6 @@ declare class Engine {
75
75
  * @returns {Inpunt | boolean}
76
76
  */
77
77
  simplify(exp: ExpressionInput, context: Context, strictKeys?: string[], optionalKeys?: string[]): Input | boolean;
78
+ unsafeSimplify(exp: ExpressionInput, context: Context, strictKeys?: string[], optionalKeys?: string[]): Input | boolean;
78
79
  }
79
80
  export default Engine;
@@ -9,6 +9,11 @@ import { Value } from './value';
9
9
  */
10
10
  export declare class Collection extends Operand {
11
11
  private readonly items;
12
+ /**
13
+ * Get the items in the collection.
14
+ * @returns {Array<Value | Reference>}
15
+ */
16
+ getItems(): Array<Value | Reference>;
12
17
  /**
13
18
  * @constructor
14
19
  * @param {Operand[]} items Collection of operands.
@@ -1,6 +1,6 @@
1
1
  import { Evaluable } from '../common/evaluable';
2
2
  import { Options } from './options';
3
- export type Input = string | number | boolean | null | Input[] | [string, ...Input[]];
3
+ export type Input = string | number | boolean | null | Input[] | [string, ...Input[]] | Record<string, unknown>;
4
4
  export type ArrayInput = Input[];
5
5
  export type ExpressionInput = [string, ...Input[]];
6
6
  /**
@@ -0,0 +1,3 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyAnd: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,6 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifySum: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
4
+ export declare const simplifySubtract: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
5
+ export declare const simplifyMultiply: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
6
+ export declare const simplifyDivide: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,7 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ import { Options } from '../parser/options';
4
+ export declare const simplifyGt: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
5
+ export declare const simplifyGe: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
6
+ export declare const simplifyLt: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
7
+ export declare const simplifyLe: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,5 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ import { Options } from '../parser/options';
4
+ export declare const simplifyEq: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
5
+ export declare const simplifyNe: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,3 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyIn: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,4 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ import { Options } from '../parser/options';
4
+ export declare const simplifyNor: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,3 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyNotIn: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,3 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyNot: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,3 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyOr: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,3 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyOverlap: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,5 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ import { Options } from '../parser/options';
4
+ export declare const simplifyPrefix: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
5
+ export declare const simplifySuffix: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,4 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const simplifyPresent: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
4
+ export declare const simplifyUndefined: (simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;
@@ -0,0 +1,4 @@
1
+ import { Context, Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ import { Options } from '../parser/options';
4
+ export declare const unsafeSimplify: (context: Context, opts: Options, strictKeys?: string[], optionalKeys?: string[]) => (input: Input) => Input | Evaluable;
@@ -0,0 +1,12 @@
1
+ import { Evaluable, Result } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ export declare const isTrueResult: (value: Input) => value is true;
4
+ export declare const isFalseResult: (value: Input) => value is false;
5
+ export declare const isNonFalseResult: (operand: Input | Evaluable) => operand is Input;
6
+ export declare const isNonTrueResult: (operand: Input | Evaluable) => operand is Input;
7
+ export declare const resultToInputInternal: (value: Result) => Input | undefined;
8
+ export declare const resultToInput: (value: Result) => Input | undefined;
9
+ export declare const areAllNumbers: (results: Input[]) => results is number[];
10
+ export declare const areAllInputs: (values: (Input | Evaluable)[]) => values is Input[];
11
+ export declare const getInputValues: (results: Input[]) => number[] | false;
12
+ export declare const extractValues: (input: Input | Evaluable) => Result[];
@@ -0,0 +1,4 @@
1
+ import { Evaluable } from '../common/evaluable';
2
+ import { Input } from '../parser';
3
+ import { Options } from '../parser/options';
4
+ export declare const simplifyXor: (opts: Options, simplifyInput: (input: Input) => Input | Evaluable) => (input: Input[] | [string, ...Input[]]) => Input | Evaluable;