@briza/illogical 2.2.0 → 2.2.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/lib/illogical.cjs CHANGED
@@ -2074,6 +2074,36 @@ function resolveCompactRef(ref, ctx) {
2074
2074
  }
2075
2075
  return resolveTokens(ref.tokens ?? [], ref.t, ctx);
2076
2076
  }
2077
+ function getKeyFromCompactRef(ref) {
2078
+ if (typeof ref === 'string') {
2079
+ return ref;
2080
+ }
2081
+ if (Array.isArray(ref)) {
2082
+ return ref.map(ref => ref.includes('.') ? `\`${ref}\`` : ref).join('.');
2083
+ }
2084
+ let tokens = ref.tokens;
2085
+ if (ref.d) {
2086
+ let current = ref.k;
2087
+ let match = dynamicKeyRegex.exec(current);
2088
+ while (match) {
2089
+ current = current.replace(dynamicKeyRegex, '').replace('[]', '');
2090
+ match = dynamicKeyRegex.exec(current);
2091
+ }
2092
+ tokens = parseStaticKey(current);
2093
+ }
2094
+ let key = '';
2095
+ for (const token of tokens ?? []) {
2096
+ if (isNumber(token.value)) {
2097
+ return key;
2098
+ }
2099
+ if (token.value.includes('.')) {
2100
+ key += `${key ? '.' : ''}\`${token.value}\``;
2101
+ } else {
2102
+ key += `${key ? '.' : ''}${token.value}`;
2103
+ }
2104
+ }
2105
+ return key;
2106
+ }
2077
2107
 
2078
2108
  /**
2079
2109
  * Bytecode compiler.
@@ -3757,7 +3787,6 @@ function interpretSimplify(compiled, ctx, strictKeys, optionalKeys) {
3757
3787
  case OP_PUSH_REF_DYNAMIC:
3758
3788
  {
3759
3789
  const idx = numAt(bytecode[i++]);
3760
- const rawKey = refRawKeys[idx];
3761
3790
  let val;
3762
3791
  if (resolvedRefDirty[idx]) {
3763
3792
  val = resolvedRefCache[idx];
@@ -3776,12 +3805,12 @@ function interpretSimplify(compiled, ctx, strictKeys, optionalKeys) {
3776
3805
  // Ref is absent from context.
3777
3806
  // Check if it should be treated as a concrete undefined (evaluated as undefined)
3778
3807
  // or as a residual expression (preserved for later simplification).
3779
- if (strictSet?.has(rawKey)) {
3808
+ if (strictSet?.has(getKeyFromCompactRef(refs[idx]))) {
3780
3809
  // strictKeys: force-evaluate as undefined (not a residual)
3781
3810
  stack[++stackTop] = undefined;
3782
3811
  break;
3783
3812
  }
3784
- if (optionalSet && !optionalSet.has(rawKey)) {
3813
+ if (optionalSet && !optionalSet.has(getKeyFromCompactRef(refs[idx]))) {
3785
3814
  // Key not in optionalKeys: treat as definitely-present but absent → undefined
3786
3815
  stack[++stackTop] = undefined;
3787
3816
  break;
@@ -2070,6 +2070,36 @@ function resolveCompactRef(ref, ctx) {
2070
2070
  }
2071
2071
  return resolveTokens(ref.tokens ?? [], ref.t, ctx);
2072
2072
  }
2073
+ function getKeyFromCompactRef(ref) {
2074
+ if (typeof ref === 'string') {
2075
+ return ref;
2076
+ }
2077
+ if (Array.isArray(ref)) {
2078
+ return ref.map(ref => ref.includes('.') ? `\`${ref}\`` : ref).join('.');
2079
+ }
2080
+ let tokens = ref.tokens;
2081
+ if (ref.d) {
2082
+ let current = ref.k;
2083
+ let match = dynamicKeyRegex.exec(current);
2084
+ while (match) {
2085
+ current = current.replace(dynamicKeyRegex, '').replace('[]', '');
2086
+ match = dynamicKeyRegex.exec(current);
2087
+ }
2088
+ tokens = parseStaticKey(current);
2089
+ }
2090
+ let key = '';
2091
+ for (const token of tokens ?? []) {
2092
+ if (isNumber(token.value)) {
2093
+ return key;
2094
+ }
2095
+ if (token.value.includes('.')) {
2096
+ key += `${key ? '.' : ''}\`${token.value}\``;
2097
+ } else {
2098
+ key += `${key ? '.' : ''}${token.value}`;
2099
+ }
2100
+ }
2101
+ return key;
2102
+ }
2073
2103
 
2074
2104
  /**
2075
2105
  * Bytecode compiler.
@@ -3753,7 +3783,6 @@ function interpretSimplify(compiled, ctx, strictKeys, optionalKeys) {
3753
3783
  case OP_PUSH_REF_DYNAMIC:
3754
3784
  {
3755
3785
  const idx = numAt(bytecode[i++]);
3756
- const rawKey = refRawKeys[idx];
3757
3786
  let val;
3758
3787
  if (resolvedRefDirty[idx]) {
3759
3788
  val = resolvedRefCache[idx];
@@ -3772,12 +3801,12 @@ function interpretSimplify(compiled, ctx, strictKeys, optionalKeys) {
3772
3801
  // Ref is absent from context.
3773
3802
  // Check if it should be treated as a concrete undefined (evaluated as undefined)
3774
3803
  // or as a residual expression (preserved for later simplification).
3775
- if (strictSet?.has(rawKey)) {
3804
+ if (strictSet?.has(getKeyFromCompactRef(refs[idx]))) {
3776
3805
  // strictKeys: force-evaluate as undefined (not a residual)
3777
3806
  stack[++stackTop] = undefined;
3778
3807
  break;
3779
3808
  }
3780
- if (optionalSet && !optionalSet.has(rawKey)) {
3809
+ if (optionalSet && !optionalSet.has(getKeyFromCompactRef(refs[idx]))) {
3781
3810
  // Key not in optionalKeys: treat as definitely-present but absent → undefined
3782
3811
  stack[++stackTop] = undefined;
3783
3812
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@briza/illogical",
3
- "version": "2.2.0",
3
+ "version": "2.2.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
  "type": "module",
6
6
  "main": "./lib/illogical.cjs",
package/readme.md CHANGED
@@ -8,33 +8,37 @@
8
8
  <div align="center">
9
9
  <h3 align="center">illogical</h3>
10
10
  </div>
11
+ </div>
11
12
 
12
- [![build status](https://github.com/briza-insurance/illogical/actions/workflows/test.yml/badge.svg)](https://github.com/briza-insurance/illogical/actions?branch=master)
13
- [![npm version](https://badge.fury.io/js/@briza%2Fillogical.svg?icon=si%3Anpm)](https://badge.fury.io/js/@briza%2Fillogical)
14
- [![install size](https://packagephobia.com/badge?p=@briza/illogical)](https://packagephobia.com/result?p=@briza/illogical)
15
- ![zero dependencies](https://img.shields.io/badge/0-dependencies-green)
16
- ![npm downloads](https://img.shields.io/npm/dm/%40briza%2Fillogical)
17
-
13
+ <div align="center">
14
+ <p>
15
+ **illogical** is a JSON DSL (domain-specific language) for expressing and evaluating business rules in the insurance industry. Underwriters use illogical to model business rules for their question sets, enabling distributors to render great user experiences.
16
+ </p>
18
17
  </div>
19
18
 
20
19
  <div align="center">
21
- <p>
22
- <br />
23
- JSON DSL for expressing and evaluating business rules. Underwriters use illogical to model business rules for their question sets, enabling distributors to render great user experiences.
24
- </p>
20
+ [![build status](https://github.com/briza-insurance/illogical/actions/workflows/test.yml/badge.svg)](https://github.com/briza-insurance/illogical/actions?branch=master)
21
+ [![npm version](https://badge.fury.io/js/@briza%2Fillogical.svg?icon=si%3Anpm)](https://badge.fury.io/js/@briza%2Fillogical)
22
+ [![install size](https://packagephobia.com/badge?p=@briza/illogical)](https://packagephobia.com/result?p=@briza/illogical)
23
+ ![zero dependencies](https://img.shields.io/badge/0-dependencies-green)
24
+ ![npm downloads](https://img.shields.io/npm/dm/%40briza%2Fillogical)
25
25
  </div>
26
26
 
27
27
  ---
28
28
 
29
- This project is designed such that business rules can be shared between the front-end and back-end, serialized in JSON.
29
+ ## 🚀 Getting Started
30
30
 
31
- ## 🚀 Get Started
31
+ Get up and running with illogical in just a few steps.
32
+
33
+ ### Installation
32
34
 
33
35
  ```sh
34
36
  # install illogical
35
37
  npm install @briza/illogical
36
38
  ```
37
39
 
40
+ ### Basic Usage
41
+
38
42
  ```js
39
43
  // Import the illogical engine
40
44
  import Engine from '@briza/illogical'
@@ -65,9 +69,11 @@ engine.evaluate(['==', '$age.(String)', '21'], ctx) // true
65
69
  engine.evaluate(['AND', ['>', '$age', 20], ['==', '$name', 'peter']]) // true
66
70
  ```
67
71
 
68
- ## 🖼️ Resources
72
+ ## 📚 Documentation
69
73
 
70
- Understand supported expressions:
74
+ ### Core Concepts
75
+
76
+ Explore the supported expressions and their usage:
71
77
 
72
78
  - [Comparison Expressions](./specs/comparison-expressions.md)
73
79
  - [Logical Expressions](./specs/logical-expressions.md)
@@ -75,19 +81,25 @@ Understand supported expressions:
75
81
  - [Evaluation Data Context](./specs/evaluation-data-context.md)
76
82
  - [Operand Types](./specs/operand-types.md)
77
83
 
78
- Learn about usages:
84
+ ### API Reference
85
+
86
+ Learn how to use the engine and its methods:
79
87
 
80
88
  - [Evaluate](./specs/evaluate.md)
81
89
  - [Statement](./specs/statement.md)
82
90
  - [Parse](./specs/parse.md)
83
91
  - [Simplify](./specs/simplify.md)
84
92
 
93
+ ### Customization
94
+
85
95
  Customize the engine and the documentation:
86
96
 
87
97
  - [Engine Options](./specs/engine.md)
88
98
  - [Code Documentation](https://briza-insurance.github.io/illogical/index.html)
89
99
 
90
- Bytecode evaluation and debugging tools:
100
+ ### Development Tools
101
+
102
+ For advanced usage like bytecode evaluation and debugging:
91
103
 
92
104
  - [Bytecode Evaluator](./specs/bytecode-evaluator.md)
93
105
  - [Debugger Tools](./specs/debugger-tools.md)
@@ -71,3 +71,4 @@ export declare function resolveDynamic(key: string, dataType: DataType | undefin
71
71
  * (OP_OVERLAP_SCAN_REFS_CONST, OP_OR_AND_IN_CONST_2).
72
72
  */
73
73
  export declare function resolveCompactRef(ref: CompactRef, ctx: Context): Result;
74
+ export declare function getKeyFromCompactRef(ref: CompactRef): string;