@briza/illogical 1.6.1 → 1.7.1

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.1",
3
+ "version": "1.7.1",
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",
@@ -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
@@ -2,8 +2,6 @@
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: March 22, 2022.
6
-
7
5
  ## About
8
6
 
9
7
  This project has been developed to provide a shared conditional logic between front-end and back-end code, stored in JSON or in any other data serialization format.
@@ -224,6 +222,19 @@ engine.simplify(
224
222
  ) // ['==', '$b', 20]
225
223
  ```
226
224
 
225
+ ### Unsafe Simplify
226
+
227
+ Simplifies an expression with a given context, but without parsing and ensuring the condition is
228
+ syntatically correct. This can be used in situations where you can decouple the parsing from
229
+ the simplification and extra performance is needed in runtime.
230
+
231
+ ```js
232
+ engine.unsafeSimplify(
233
+ ['OR', ['==', '$a', 10], ['==', '$b', 20], ['==', '$c', 20]],
234
+ { a: 10 }
235
+ ) // true due to $a. Expressions for $b and $c won't even be parsed.
236
+ ```
237
+
227
238
  ## Working with Expressions
228
239
 
229
240
  ### Evaluation Data Context
@@ -237,6 +248,7 @@ To reference the nested reference, please use "." delimiter, e.g.:
237
248
 
238
249
  If the key of the nested reference includes the "." delimiter, please wrap the whole key with backticks `` ` ``, e.g.:
239
250
  `` $address.`city.code` `` can reference the object
251
+
240
252
  ```javascript
241
253
  {
242
254
  address: {
@@ -245,7 +257,8 @@ If the key of the nested reference includes the "." delimiter, please wrap the w
245
257
  }
246
258
  ```
247
259
 
248
- `` $address.`city.code`[0] `` can reference the object
260
+ ``$address.`city.code`[0]`` can reference the object
261
+
249
262
  ```javascript
250
263
  {
251
264
  address: {
@@ -253,6 +266,7 @@ If the key of the nested reference includes the "." delimiter, please wrap the w
253
266
  }
254
267
  }
255
268
  ```
269
+
256
270
  when the value of the nested reference is an array.
257
271
 
258
272
  #### Accessing Array Element:
@@ -705,7 +719,7 @@ Expression format: `["/", First Operand, Second Operand, ... , Nth Operand]`.
705
719
  ```
706
720
 
707
721
  ```js
708
- engine.evaluate(["==", ["/", 100, 10], 10]) // true
722
+ engine.evaluate(['==', ['/', 100, 10], 10]) // true
709
723
  ```
710
724
 
711
725
  #### Multiplication
@@ -721,7 +735,7 @@ Expression format: `["*", First Operand, Second Operand, ... , Nth Operand]`.
721
735
  ```
722
736
 
723
737
  ```js
724
- engine.evaluate(["==", ["*", 10, 10], 100]) // true
738
+ engine.evaluate(['==', ['*', 10, 10], 100]) // true
725
739
  ```
726
740
 
727
741
  #### Subtraction
@@ -737,10 +751,9 @@ Expression format: `["-", First Operand, Second Operand, ... , Nth Operand]`.
737
751
  ```
738
752
 
739
753
  ```js
740
- engine.evaluate(["==", ["-", 20, 10], 10]) // true
754
+ engine.evaluate(['==', ['-', 20, 10], 10]) // true
741
755
  ```
742
756
 
743
-
744
757
  #### Addition
745
758
 
746
759
  The arithmetical operator for addition produces the sum of the operands.
@@ -754,10 +767,9 @@ Expression format: `["+", First Operand, Second Operand, ... , Nth Operand]`.
754
767
  ```
755
768
 
756
769
  ```js
757
- engine.evaluate(["==", ["+", 5, 5], 10]) // true
770
+ engine.evaluate(['==', ['+', 5, 5], 10]) // true
758
771
  ```
759
772
 
760
-
761
773
  ## Engine Options
762
774
 
763
775
  ### 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;
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
  /**
@@ -10,6 +10,7 @@ export declare class Parser {
10
10
  private readonly opts;
11
11
  private readonly expectedRootOperators;
12
12
  private readonly unexpectedRootSymbols;
13
+ private readonly referenceCache;
13
14
  /**
14
15
  * @constructor
15
16
  * @param {Options?} options Parser options.
@@ -20,6 +21,8 @@ export declare class Parser {
20
21
  * @type {Options}
21
22
  */
22
23
  get options(): Options;
24
+ private getReference;
25
+ private resolve;
23
26
  /**
24
27
  * Parse raw expression into evaluable expression.
25
28
  * @param {ExpressionInput} raw Raw expression.
@@ -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;