@briza/illogical 1.4.3 → 1.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.
Files changed (35) hide show
  1. package/changelog.md +13 -0
  2. package/lib/illogical.esm.js +82 -176
  3. package/lib/illogical.js +82 -175
  4. package/package.json +23 -23
  5. package/readme.md +21 -7
  6. package/types/common/evaluable.d.ts +57 -57
  7. package/types/common/type-check.d.ts +28 -32
  8. package/types/common/util.d.ts +11 -11
  9. package/types/expression/comparison/eq.d.ts +18 -22
  10. package/types/expression/comparison/ge.d.ts +18 -22
  11. package/types/expression/comparison/gt.d.ts +18 -22
  12. package/types/expression/comparison/in.d.ts +23 -27
  13. package/types/expression/comparison/index.d.ts +45 -49
  14. package/types/expression/comparison/le.d.ts +18 -22
  15. package/types/expression/comparison/lt.d.ts +18 -22
  16. package/types/expression/comparison/ne.d.ts +18 -22
  17. package/types/expression/comparison/not-in.d.ts +23 -27
  18. package/types/expression/comparison/overlap.d.ts +23 -27
  19. package/types/expression/comparison/prefix.d.ts +23 -27
  20. package/types/expression/comparison/present.d.ts +25 -29
  21. package/types/expression/comparison/suffix.d.ts +23 -27
  22. package/types/expression/comparison/undefined.d.ts +28 -32
  23. package/types/expression/logical/and.d.ts +23 -27
  24. package/types/expression/logical/index.d.ts +32 -36
  25. package/types/expression/logical/nor.d.ts +23 -27
  26. package/types/expression/logical/not.d.ts +23 -27
  27. package/types/expression/logical/or.d.ts +23 -27
  28. package/types/expression/logical/xor.d.ts +23 -27
  29. package/types/index.d.ts +73 -71
  30. package/types/operand/collection.d.ts +36 -40
  31. package/types/operand/index.d.ts +25 -29
  32. package/types/operand/reference.d.ts +44 -48
  33. package/types/operand/value.d.ts +31 -35
  34. package/types/parser/index.d.ts +39 -43
  35. package/types/parser/options.d.ts +61 -65
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: May 17, 2021.
5
+ > Revision: February 1, 2022.
6
6
 
7
7
  ## About
8
8
 
@@ -178,7 +178,7 @@ evaluable.toString()
178
178
  ### Simplify
179
179
 
180
180
  Simplifies an expression with a given context. This is useful when you already have some of
181
- the properties of context and wants to try to eveluate the expression.
181
+ the properties of context and wants to try to evaluate the expression.
182
182
 
183
183
  **Example**
184
184
 
@@ -192,8 +192,8 @@ Values not found in the context will cause the parent operand not to be evaluate
192
192
  as part of the simplified expression.
193
193
 
194
194
  In some situations we might want to evaluate the expression even if referred value is not
195
- present. You can provide a list of keys that will be evaluated even if they are not present
196
- in the context.
195
+ present. You can provide a list of keys that will be strictly evaluated even if they are not
196
+ present in the context.
197
197
 
198
198
  **Example**
199
199
 
@@ -201,10 +201,24 @@ in the context.
201
201
  engine.simplify(
202
202
  ['AND', ['==', '$a', 10], ['==', '$b', 20]],
203
203
  { a: 10 },
204
- ['b'] // '$b' will be evaluted to undefined.
204
+ ['b'] // '$b' will be evaluated to undefined.
205
205
  ) // false
206
206
  ```
207
207
 
208
+ Alternatively we might want to do the opposite and strictly evaluate the expression for all referred
209
+ values not present in the context except for a specified list of optional keys.
210
+
211
+ **Example**
212
+
213
+ ```js
214
+ engine.simplify(
215
+ ['OR', ['==', '$a', 10], ['==', '$b', 20], ['==', '$c', 20]],
216
+ { c: 10 },
217
+ undefined,
218
+ ['b'] // except for '$b' everything not in context will be evaluated to undefined.
219
+ ) // ['==', '$b', 20]
220
+ ```
221
+
208
222
  ## Working with Expressions
209
223
 
210
224
  ### Evaluation Data Context
@@ -311,7 +325,7 @@ Simple value types: string, number, boolean.
311
325
 
312
326
  #### Reference
313
327
 
314
- The reference operand's value is resolved from the [Evaluation Data Context](#evaluation-data-context), where the the operands name is used as key in the context.
328
+ The reference operand value is resolved from the [Evaluation Data Context](#evaluation-data-context), where the the operands name is used as key in the context.
315
329
 
316
330
  The reference operand must be prefixed with `$` symbol, e.g.: `$name`. This might be customized via [Reference Predicate Parser Option](#reference-predicate).
317
331
 
@@ -676,7 +690,7 @@ A function used to transform the operand into the reference annotation stripped
676
690
  referenceTransform: (operand: string) => string
677
691
  ```
678
692
 
679
- > **Default reference transform:**
693
+ > **Default reference transform:**
680
694
  > It removes the `$` symbol at the begging of the operand name.
681
695
 
682
696
  #### Operator Mapping
@@ -1,57 +1,57 @@
1
- /**
2
- * Common module.
3
- * @module illogical/common
4
- */
5
- import { Input } from '../parser';
6
- import { Options } from '../parser/options';
7
- /**
8
- * Valid types for context members
9
- */
10
- declare type ContextValue = Record<string, unknown> | string | number | boolean | null | undefined | ContextValue[];
11
- /**
12
- * Evaluation Context
13
- * Holds references used during the evaluation process.
14
- * Format: key: value.
15
- */
16
- export interface Context {
17
- [k: string]: ContextValue;
18
- }
19
- /**
20
- * Evaluation result
21
- */
22
- export declare type Result = undefined | null | string | number | boolean | Array<Result>;
23
- export declare enum EvaluableType {
24
- Operand = "Operand",
25
- Expression = "Expression"
26
- }
27
- /**
28
- * Evaluable
29
- */
30
- export interface Evaluable {
31
- type: EvaluableType;
32
- /**
33
- * Evaluate in the given context.
34
- * @param {Context} ctx
35
- * @return {Result}
36
- */
37
- evaluate(ctx: Context): Result;
38
- /**
39
- * Simplifies this Evaluable when possible.
40
- *
41
- * @param {Context} ctx context for the evaluation
42
- * @param {string[]} ignoreKeys keys to be considered present even if their not present in the context
43
- * @returns {Result | Evaluable} simplified value or itself
44
- */
45
- simplify(ctx: Context, ignoreKeys: string[]): Result | Evaluable;
46
- /**
47
- * Serializes the Evaluable to its input format.
48
- *
49
- * @param {Options} options parser options
50
- */
51
- serialize(options: Options): Input;
52
- /**
53
- * Get the strict representation of the evaluable expression.
54
- */
55
- toString(): string;
56
- }
57
- export {};
1
+ import { Input } from '../parser';
2
+ import { Options } from '../parser/options';
3
+ /**
4
+ * Valid types for context members
5
+ */
6
+ declare type ContextValue = Record<string, unknown> | string | number | boolean | null | undefined | ContextValue[];
7
+ /**
8
+ * Evaluation Context
9
+ * Holds references used during the evaluation process.
10
+ * Format: key: value.
11
+ */
12
+ export interface Context {
13
+ [k: string]: ContextValue;
14
+ }
15
+ /**
16
+ * Evaluation result
17
+ */
18
+ export declare type Result = undefined | null | string | number | boolean | Array<Result>;
19
+ export declare enum EvaluableType {
20
+ Operand = "Operand",
21
+ Expression = "Expression"
22
+ }
23
+ /**
24
+ * Evaluable
25
+ */
26
+ export interface Evaluable {
27
+ type: EvaluableType;
28
+ /**
29
+ * Evaluate in the given context.
30
+ * @param {Context} ctx
31
+ * @return {Result}
32
+ */
33
+ evaluate(ctx: Context): Result;
34
+ /**
35
+ * Simplifies this Evaluable when possible.
36
+ *
37
+ * @param {Context} ctx context for the evaluation
38
+ * @param {string[]} strictKeys keys to be considered present even if they are not present in the context
39
+ * @param {string[]} optionalKeys keys to be considered not present unless they are in the context or in
40
+ * `strictKeys`; when `strictKeys` is `undefined` and `optionalKeys` is an array, every key that is not in
41
+ * `optionalKeys` is considered to be present and thus will be evaluated
42
+ * @returns {Result | Evaluable} simplified value or itself
43
+ */
44
+ simplify(ctx: Context, strictKeys?: string[], optionalKeys?: string[]): Result | Evaluable;
45
+ /**
46
+ * Serializes the Evaluable to its input format.
47
+ *
48
+ * @param {Options} options parser options
49
+ */
50
+ serialize(options: Options): Input;
51
+ /**
52
+ * Get the strict representation of the evaluable expression.
53
+ */
54
+ toString(): string;
55
+ }
56
+ export declare type SimplifyArgs = Parameters<Evaluable['simplify']>;
57
+ export {};
@@ -1,32 +1,28 @@
1
- /**
2
- * Common module.
3
- * @module illogical/common
4
- */
5
- import { Evaluable, Result } from './evaluable';
6
- /**
7
- * Is number predicate.
8
- * @param value Tested value.
9
- */
10
- export declare function isNumber(value: Result): value is number;
11
- /**
12
- * Is string type predicate.
13
- * @param value Tested value.
14
- */
15
- export declare function isString(value: Result): value is string;
16
- /**
17
- * Is Object
18
- * @param value tested value result of the test
19
- */
20
- export declare function isObject(value: unknown): value is Record<string, unknown>;
21
- /**
22
- * Is Boolean predicate.
23
- * @param value tested value.
24
- * @return result of the test
25
- */
26
- export declare function isBoolean(value: unknown): value is boolean;
27
- /**
28
- * Check if a value is a an Evaluable
29
- * @param {Result | Evaluable} value value to check if is Evaluable
30
- * @returns {Evaluable}
31
- */
32
- export declare function isEvaluable(value: Result | Evaluable): value is Evaluable;
1
+ import { Evaluable, Result } from './evaluable';
2
+ /**
3
+ * Is number predicate.
4
+ * @param value Tested value.
5
+ */
6
+ export declare function isNumber(value: Result): value is number;
7
+ /**
8
+ * Is string type predicate.
9
+ * @param value Tested value.
10
+ */
11
+ export declare function isString(value: Result): value is string;
12
+ /**
13
+ * Is Object
14
+ * @param value tested value result of the test
15
+ */
16
+ export declare function isObject(value: unknown): value is Record<string, unknown>;
17
+ /**
18
+ * Is Boolean predicate.
19
+ * @param value tested value.
20
+ * @return result of the test
21
+ */
22
+ export declare function isBoolean(value: unknown): value is boolean;
23
+ /**
24
+ * Check if a value is a an Evaluable
25
+ * @param {Result | Evaluable} value value to check if is Evaluable
26
+ * @returns {Evaluable}
27
+ */
28
+ export declare function isEvaluable(value: Result | Evaluable): value is Evaluable;
@@ -1,11 +1,11 @@
1
- import { Result } from './evaluable';
2
- /**
3
- * Convert a value to number if possible, otherwise return undefined
4
- * @param value value to be converted to number
5
- */
6
- export declare const toNumber: (value: Result) => number | undefined;
7
- /**
8
- * Convert a value to string if possible, otherwise return undefined
9
- * @param value value to be converted to string
10
- */
11
- export declare const toString: (value: Result) => string | undefined;
1
+ import { Result } from './evaluable';
2
+ /**
3
+ * Convert a value to number if possible, otherwise return undefined
4
+ * @param value value to be converted to number
5
+ */
6
+ export declare const toNumber: (value: Result) => number | undefined;
7
+ /**
8
+ * Convert a value to string if possible, otherwise return undefined
9
+ * @param value value to be converted to string
10
+ */
11
+ export declare const toString: (value: Result) => string | undefined;
@@ -1,22 +1,18 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Evaluable, Result } from '../../common/evaluable';
6
- import { Comparison } from '../comparison';
7
- export declare const OPERATOR: unique symbol;
8
- /**
9
- * Equal comparison expression
10
- */
11
- export declare class Equal extends Comparison {
12
- /**
13
- * @constructor
14
- * @param {Evaluable} left Left operand.
15
- * @param {Evaluable} right Right operand.
16
- */
17
- constructor(...args: Evaluable[]);
18
- /**
19
- * {@link Comparison.comparison}
20
- */
21
- comparison(left: Result, right: Result): boolean;
22
- }
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Comparison } from '../comparison';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Equal comparison expression
6
+ */
7
+ export declare class Equal extends Comparison {
8
+ /**
9
+ * @constructor
10
+ * @param {Evaluable} left Left operand.
11
+ * @param {Evaluable} right Right operand.
12
+ */
13
+ constructor(...args: Evaluable[]);
14
+ /**
15
+ * {@link Comparison.comparison}
16
+ */
17
+ comparison(left: Result, right: Result): boolean;
18
+ }
@@ -1,22 +1,18 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Evaluable, Result } from '../../common/evaluable';
6
- import { Comparison } from '../comparison';
7
- export declare const OPERATOR: unique symbol;
8
- /**
9
- * Greater than or equal comparison expression
10
- */
11
- export declare class GreaterThanOrEqual extends Comparison {
12
- /**
13
- * @constructor
14
- * @param {Evaluable} left Left operand.
15
- * @param {Evaluable} right Right operand.
16
- */
17
- constructor(...args: Evaluable[]);
18
- /**
19
- * {@link Comparison.comparison}
20
- */
21
- comparison(left: Result, right: Result): boolean;
22
- }
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Comparison } from '../comparison';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Greater than or equal comparison expression
6
+ */
7
+ export declare class GreaterThanOrEqual extends Comparison {
8
+ /**
9
+ * @constructor
10
+ * @param {Evaluable} left Left operand.
11
+ * @param {Evaluable} right Right operand.
12
+ */
13
+ constructor(...args: Evaluable[]);
14
+ /**
15
+ * {@link Comparison.comparison}
16
+ */
17
+ comparison(left: Result, right: Result): boolean;
18
+ }
@@ -1,22 +1,18 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Evaluable, Result } from '../../common/evaluable';
6
- import { Comparison } from '../comparison';
7
- export declare const OPERATOR: unique symbol;
8
- /**
9
- * Greater than comparison expression
10
- */
11
- export declare class GreaterThan extends Comparison {
12
- /**
13
- * @constructor
14
- * @param {Evaluable} left Left operand.
15
- * @param {Evaluable} right Right operand.
16
- */
17
- constructor(...args: Evaluable[]);
18
- /**
19
- * {@link Comparison.comparison}
20
- */
21
- comparison(left: Result, right: Result): boolean;
22
- }
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Comparison } from '../comparison';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Greater than comparison expression
6
+ */
7
+ export declare class GreaterThan extends Comparison {
8
+ /**
9
+ * @constructor
10
+ * @param {Evaluable} left Left operand.
11
+ * @param {Evaluable} right Right operand.
12
+ */
13
+ constructor(...args: Evaluable[]);
14
+ /**
15
+ * {@link Comparison.comparison}
16
+ */
17
+ comparison(left: Result, right: Result): boolean;
18
+ }
@@ -1,27 +1,23 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Evaluable, Result } from '../../common/evaluable';
6
- import { Comparison } from '../comparison';
7
- export declare const OPERATOR: unique symbol;
8
- /**
9
- * In comparison expression
10
- */
11
- export declare class In extends Comparison {
12
- /**
13
- * @constructor
14
- * @param {Evaluable} left Left operand.
15
- * @param {Evaluable} right Right operand.
16
- */
17
- constructor(...args: Evaluable[]);
18
- /**
19
- * {@link Comparison.comparison}
20
- */
21
- comparison(left: Result, right: Result): boolean;
22
- /**
23
- * Get the strict representation of the expression.
24
- * @return {string}
25
- */
26
- toString(): string;
27
- }
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Comparison } from '../comparison';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * In comparison expression
6
+ */
7
+ export declare class In extends Comparison {
8
+ /**
9
+ * @constructor
10
+ * @param {Evaluable} left Left operand.
11
+ * @param {Evaluable} right Right operand.
12
+ */
13
+ constructor(...args: Evaluable[]);
14
+ /**
15
+ * {@link Comparison.comparison}
16
+ */
17
+ comparison(left: Result, right: Result): boolean;
18
+ /**
19
+ * Get the strict representation of the expression.
20
+ * @return {string}
21
+ */
22
+ toString(): string;
23
+ }
@@ -1,49 +1,45 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Context, Evaluable, EvaluableType, Result } from '../../common/evaluable';
6
- import { Operand } from '../../operand';
7
- import { ExpressionInput } from '../../parser';
8
- import { Options } from '../../parser/options';
9
- /**
10
- * Abstract comparison expression
11
- */
12
- export declare abstract class Comparison implements Evaluable {
13
- protected readonly operator: string;
14
- protected readonly operatorSymbol: symbol;
15
- protected readonly left: Operand;
16
- protected readonly right: Operand;
17
- type: EvaluableType;
18
- /**
19
- * @constructor
20
- * @param {string} operator String representation of the operator.
21
- * @param {Operand} left Left operand.
22
- * @param {Operand} right Right operand.
23
- */
24
- constructor(operator: string, operatorSymbol: symbol, left: Operand, right: Operand);
25
- /**
26
- * {@link Evaluable.evaluate}
27
- */
28
- evaluate(ctx: Context): Result;
29
- /**
30
- * Get the strict representation of the expression.
31
- * @return {string}
32
- */
33
- toString(): string;
34
- /**
35
- * Compares left and right operands evaluated values.
36
- * @param {Result} left left operand result value
37
- * @param {Result} right right operand result value
38
- * @returns {boolean}
39
- */
40
- abstract comparison(left: Result, right: Result): boolean;
41
- /**
42
- * {@link Evaluable.simplify}
43
- */
44
- simplify(...args: [Context, string[]]): Result | Evaluable;
45
- /**
46
- * {@link Evaluable.serialize}
47
- */
48
- serialize(options: Options): ExpressionInput;
49
- }
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 comparison expression
7
+ */
8
+ export declare abstract class Comparison implements Evaluable {
9
+ protected readonly operator: string;
10
+ protected readonly operatorSymbol: symbol;
11
+ protected readonly left: Operand;
12
+ protected readonly right: Operand;
13
+ type: EvaluableType;
14
+ /**
15
+ * @constructor
16
+ * @param {string} operator String representation of the operator.
17
+ * @param {Operand} left Left operand.
18
+ * @param {Operand} right Right operand.
19
+ */
20
+ constructor(operator: string, operatorSymbol: symbol, left: Operand, right: Operand);
21
+ /**
22
+ * {@link Evaluable.evaluate}
23
+ */
24
+ evaluate(ctx: Context): Result;
25
+ /**
26
+ * Get the strict representation of the expression.
27
+ * @return {string}
28
+ */
29
+ toString(): string;
30
+ /**
31
+ * Compares left and right operands evaluated values.
32
+ * @param {Result} left left operand result value
33
+ * @param {Result} right right operand result value
34
+ * @returns {boolean}
35
+ */
36
+ abstract comparison(left: Result, right: Result): boolean;
37
+ /**
38
+ * {@link Evaluable.simplify}
39
+ */
40
+ simplify(...args: SimplifyArgs): Result | Evaluable;
41
+ /**
42
+ * {@link Evaluable.serialize}
43
+ */
44
+ serialize(options: Options): ExpressionInput;
45
+ }
@@ -1,22 +1,18 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Evaluable, Result } from '../../common/evaluable';
6
- import { Comparison } from '../comparison';
7
- export declare const OPERATOR: unique symbol;
8
- /**
9
- * Less than or equal comparison expression
10
- */
11
- export declare class LessThanOrEqual extends Comparison {
12
- /**
13
- * @constructor
14
- * @param {Evaluable} left Left operand.
15
- * @param {Evaluable} right Right operand.
16
- */
17
- constructor(...args: Evaluable[]);
18
- /**
19
- * {@link Comparison.comparison}
20
- */
21
- comparison(left: Result, right: Result): boolean;
22
- }
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Comparison } from '../comparison';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Less than or equal comparison expression
6
+ */
7
+ export declare class LessThanOrEqual extends Comparison {
8
+ /**
9
+ * @constructor
10
+ * @param {Evaluable} left Left operand.
11
+ * @param {Evaluable} right Right operand.
12
+ */
13
+ constructor(...args: Evaluable[]);
14
+ /**
15
+ * {@link Comparison.comparison}
16
+ */
17
+ comparison(left: Result, right: Result): boolean;
18
+ }
@@ -1,22 +1,18 @@
1
- /**
2
- * Comparison expression module.
3
- * @module illogical/expression/comparison
4
- */
5
- import { Evaluable, Result } from '../../common/evaluable';
6
- import { Comparison } from '../comparison';
7
- export declare const OPERATOR: unique symbol;
8
- /**
9
- * Less than comparison expression
10
- */
11
- export declare class LessThan extends Comparison {
12
- /**
13
- * @constructor
14
- * @param {Evaluable} left Left operand.
15
- * @param {Evaluable} right Right operand.
16
- */
17
- constructor(...args: Evaluable[]);
18
- /**
19
- * {@link Comparison.comparison}
20
- */
21
- comparison(left: Result, right: Result): boolean;
22
- }
1
+ import { Evaluable, Result } from '../../common/evaluable';
2
+ import { Comparison } from '../comparison';
3
+ export declare const OPERATOR: unique symbol;
4
+ /**
5
+ * Less than comparison expression
6
+ */
7
+ export declare class LessThan extends Comparison {
8
+ /**
9
+ * @constructor
10
+ * @param {Evaluable} left Left operand.
11
+ * @param {Evaluable} right Right operand.
12
+ */
13
+ constructor(...args: Evaluable[]);
14
+ /**
15
+ * {@link Comparison.comparison}
16
+ */
17
+ comparison(left: Result, right: Result): boolean;
18
+ }