@statedelta-libs/expressions 3.0.0 → 3.1.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/README.md +70 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +68 -10
- package/dist/index.d.ts +68 -10
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -140,12 +140,81 @@ O handler é uma closure auto-suficiente — captura o que precisa (outros compi
|
|
|
140
140
|
| **Retorno** | `Expression` (DSL puro) | `CompiledFn` (função pronta) |
|
|
141
141
|
| **Uso típico** | Sugar syntax | DSL estrangeiro, `$raw`, rule engines |
|
|
142
142
|
|
|
143
|
+
### Handlers — services com contexto
|
|
144
|
+
|
|
145
|
+
O `scope` é para funções puras e stateless. Quando o consumer precisa de **services inteligentes** que acessam o sistema (accessor, outros handlers, o compilador, o scope), usa `handlers`.
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import type { HandlerContext } from '@statedelta-libs/expressions';
|
|
149
|
+
|
|
150
|
+
const compiler = new ExpressionCompiler({
|
|
151
|
+
scope: { add: (a, b) => a + b },
|
|
152
|
+
handlers: {
|
|
153
|
+
query: {
|
|
154
|
+
find(key: string) {
|
|
155
|
+
return db.find(key);
|
|
156
|
+
},
|
|
157
|
+
findAll() {
|
|
158
|
+
// chamar outro handler
|
|
159
|
+
const valid = this.handlers.validation.check("all");
|
|
160
|
+
return valid ? db.findAll() : [];
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
validation: {
|
|
164
|
+
check(value: unknown) {
|
|
165
|
+
// chamar scope fn
|
|
166
|
+
return this.scope.add(value != null ? 1 : 0, 0) > 0;
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Handlers são invocados via `$fn` com sintaxe `"namespace:method"`:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{ "$fn": "query:find", "args": [{ "$": "userId" }] }
|
|
177
|
+
{ "$fn": "validation:check", "args": [{ "$": "value" }] }
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
O contexto é acessado via `this`, injetado automaticamente via `.bind()` no construtor. O `HandlerContext` é criado **uma vez** — zero alocação por chamada:
|
|
181
|
+
|
|
182
|
+
| Campo | O que contém |
|
|
183
|
+
|-------|-------------|
|
|
184
|
+
| `this.accessor` | Resolver de paths customizado |
|
|
185
|
+
| `this.handlers` | Todos os handlers (wrapped) — permite composição entre handlers |
|
|
186
|
+
| `this.compiler` | Instância do compilador — permite compilar sub-expressões |
|
|
187
|
+
| `this.scope` | Funções puras do scope |
|
|
188
|
+
|
|
189
|
+
Handlers **devem** ser regular functions ou method shorthand (arrow functions ignoram `.bind()`):
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// method shorthand — funciona
|
|
193
|
+
handlers: { query: { find(key) { this.scope... } } }
|
|
194
|
+
|
|
195
|
+
// regular function — funciona
|
|
196
|
+
handlers: { query: { find: function(key) { this.scope... } } }
|
|
197
|
+
|
|
198
|
+
// arrow function — NÃO funciona (this é undefined)
|
|
199
|
+
handlers: { query: { find: (key) => { this.scope... } } }
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
| | `scope` | `handlers` |
|
|
203
|
+
|---|---|---|
|
|
204
|
+
| **Natureza** | Funções puras (Ramda-style) | Services com contexto |
|
|
205
|
+
| **Acessa** | Apenas args compilados | `this` (HandlerContext) + args |
|
|
206
|
+
| **DSL** | `{ $fn: "add", args: [...] }` | `{ $fn: "query:find", args: [...] }` |
|
|
207
|
+
| **Binding** | Nenhum | `.bind(ctx)` uma vez no construtor |
|
|
208
|
+
| **Overhead** | Zero | Zero (ctx e bindings criados uma vez) |
|
|
209
|
+
|
|
210
|
+
Zero overhead quando nenhum handler é registrado.
|
|
211
|
+
|
|
143
212
|
### Accessor — objetos inteligentes
|
|
144
213
|
|
|
145
214
|
```typescript
|
|
146
215
|
const compiler = new ExpressionCompiler({
|
|
147
216
|
scope,
|
|
148
|
-
accessor: (path
|
|
217
|
+
accessor: (path) => tickContext.get(path), // closure auto-suficiente
|
|
149
218
|
});
|
|
150
219
|
```
|
|
151
220
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var omniAst=require('omni-ast');var xn=Object.defineProperty;var Fn=(n,e)=>{for(var i in e)xn(n,i,{get:e[i],enumerable:true});};var _=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),y=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,T=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,C=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",b=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),$=n=>n!==null&&typeof n=="object"&&"$arrow"in n,E=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&_.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),h=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,Rn=n=>E(n)||h(n),J=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,r="left"in i&&"op"in i&&_.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!("$arrow"in i)&&!r&&!("logic"in i)}return false};var U=new Map;function N(n){let e=[],i=n.length,r=0,o="";for(;r<i;){let s=n[r];if(s===".")o&&(e.push({type:"key",value:o}),o=""),r++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),r++;let t=r;for(;r<i&&n[r]!=="]";)r++;let f=n.slice(t,r);if(r++,f==="*")e.push({type:"wildcard",value:"*"});else {let c=parseInt(f,10);e.push({type:"index",value:isNaN(c)?f:c});}}else o+=s,r++;}return o&&e.push({type:"key",value:o}),e}function X(n){return n.includes("[*]")}function G(n){let e=U.get(n);return e||(e=X(n)?jn(n):On(n),U.set(n,e),e)}function On(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=N(n),i=e.length;if(i===2){let[o,s]=e,t=o.value,f=s.value;return c=>c?.[t]?.[f]}if(i===3){let[o,s,t]=e,f=o.value,c=s.value,l=t.value;return a=>a?.[f]?.[c]?.[l]}let r=e.map(o=>o.value);return o=>{let s=o;for(let t=0;t<i&&s!=null;t++)s=s[r[t]];return s}}function jn(n){let e=N(n),i=[];for(let r=0;r<e.length;r++)e[r].type==="wildcard"&&i.push(r);return i.length===1?Nn(e,i[0]):vn(e,i)}function Nn(n,e){let i=n.slice(0,e).map(t=>t.value),r=n.slice(e+1).map(t=>t.value),o=i.length,s=r.length;if(s===0){if(o===1){let t=i[0];return f=>f?.[t]}return t=>{let f=t;for(let c=0;c<o&&f!=null;c++)f=f[i[c]];return f}}if(s===1){let t=r[0];if(o===1){let f=i[0];return c=>{let l=c?.[f];if(Array.isArray(l))return l.map(a=>a?.[t])}}return f=>{let c=f;for(let l=0;l<o&&c!=null;l++)c=c[i[l]];if(Array.isArray(c))return c.map(l=>l?.[t])}}return t=>{let f=t;for(let c=0;c<o&&f!=null;c++)f=f[i[c]];if(Array.isArray(f))return f.map(c=>{let l=c;for(let a=0;a<s&&l!=null;a++)l=l[r[a]];return l})}}function vn(n,e){let i=[],r=0;for(let s=0;s<e.length;s++){let t=e[s],f=s===e.length-1,c=n.slice(r,t).map(l=>l.value);c.length>0&&i.push({type:"access",keys:c}),i.push({type:f?"map":"flatMap",keys:[]}),r=t+1;}let o=n.slice(r).map(s=>s.value);return s=>{let t=s;for(let f of i){if(t==null)return;if(f.type==="access")for(let c of f.keys){if(t==null)return;t=t[c];}else if(f.type==="flatMap"){if(!Array.isArray(t))return;t=t.flatMap(c=>{let l=c;return Array.isArray(l)?l:[l]});}else if(f.type==="map"){if(!Array.isArray(t))return;o.length>0&&(t=t.map(c=>{let l=c;for(let a of o){if(l==null)return;l=l[a];}return l}));}}return t}}function Dn(n,e){return G(e)(n)}function W(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function R(n){let e=new Set;return A(n,e),Array.from(e)}function A(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o=0;o<n.length;o++)A(n[o],e);return}if(y(n)){e.add(W(n.$));return}if(T(n)){if(typeof n.$if=="string"){let o=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(W(o));}else A(n.$if,e);A(n.then,e),n.else!==void 0&&A(n.else,e);return}if(b(n)){for(let o=0;o<n.$pipe.length;o++)A(n.$pipe[o],e);return}if(C(n)){if(n.args)for(let o=0;o<n.args.length;o++)A(n.args[o],e);return}if($(n)){let o=new Set;A(n.$arrow,o);let s=new Set(n.args??[]);for(let t of o){let f=t.split(".")[0].split("[")[0];s.has(f)||e.add(t);}return}if(E(n)){A(n.left,e),n.right!==void 0&&A(n.right,e);return}if(h(n)){for(let o=0;o<n.conditions.length;o++)A(n.conditions[o],e);return}let i=n,r=Object.keys(i);for(let o=0;o<r.length;o++)A(i[r[o]],e);}function In(n){return R(n).length>0}function Bn(n){return R(n).length===0}var Z;function P(n){Z=n;}function O(n,e,i,r){return Z(n,e,i,r)}function nn(n,e){return e?i=>e(n,i):G(n)}function en(n,e){return nn(n.$,e)}function tn(n,e,i,r){let o;if(typeof n.$if=="string"){let f=n.$if.startsWith("!")?n.$if.slice(1):n.$if,c=nn(f,i);o=n.$if.startsWith("!")?a=>!c(a):a=>!!c(a);}else {let f=O(n.$if,e,i,r);o=c=>!!f(c);}let s=O(n.then,e,i,r),t=n.else!==void 0?O(n.else,e,i,r):()=>{};return f=>o(f)?s(f):t(f)}function on(n,e,i,r){let o=n.$pipe;if(o.length===0)return ()=>{};if(o.length===1)return O(o[0],e,i,r);let s=O(o[0],e,i,r),t=o.slice(1).map(c=>O(c,e,i,r)),f=t.length;if(f===1){let[c]=t;return l=>{let a=s(l),p=c(l);return typeof p=="function"?p(a):p}}if(f===2){let[c,l]=t;return a=>{let p=s(a),d=c(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(f===3){let[c,l,a]=t;return p=>{let d=s(p),m=c(p);return d=typeof m=="function"?m(d):m,m=l(p),d=typeof m=="function"?m(d):m,m=a(p),typeof m=="function"?m(d):m}}return c=>{let l=s(c);for(let a=0;a<f;a++){let p=t[a](c);l=typeof p=="function"?p(l):p;}return l}}function rn(n,e,i,r){let o=n.$fn,s=n.args;if(s===void 0)return ()=>{let c=e[o];if(!c)throw new Error(`Function not found in scope: ${o}`);return c};let t=s.map(c=>O(c,e,i,r)),f=t.length;if(f===0)return ()=>{let c=e[o];if(!c)throw new Error(`Function not found in scope: ${o}`);return c()};if(f===1){let[c]=t;return l=>{let a=e[o];if(!a)throw new Error(`Function not found in scope: ${o}`);return a(c(l))}}if(f===2){let[c,l]=t;return a=>{let p=e[o];if(!p)throw new Error(`Function not found in scope: ${o}`);return p(c(a),l(a))}}if(f===3){let[c,l,a]=t;return p=>{let d=e[o];if(!d)throw new Error(`Function not found in scope: ${o}`);return d(c(p),l(p),a(p))}}return c=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l(...t.map(a=>a(c)))}}function sn(n,e,i,r){let o=n.args??[];if(o.length===0){let t=O(n.$arrow,e,i,r);return f=>()=>t(f)}let s=O(n.$arrow,e,i,r);return t=>(...f)=>{let c={};for(let a=0;a<o.length;a++)c[o[a]]=f[a];let l={...t,...c};return s(l)}}var fn;function cn(n){fn=n;}function L(n,e,i,r){return fn(n,e,i,r)}function un(n,e,i,r){let o=L(n.left,e,i,r),s=n.right!==void 0?L(n.right,e,i,r):()=>{};switch(n.op){case "eq":return t=>o(t)===s(t);case "neq":return t=>o(t)!==s(t);case "gt":return t=>o(t)>s(t);case "gte":return t=>o(t)>=s(t);case "lt":return t=>o(t)<s(t);case "lte":return t=>o(t)<=s(t);case "in":return t=>{let f=s(t);return Array.isArray(f)&&f.includes(o(t))};case "notIn":return t=>{let f=s(t);return !Array.isArray(f)||!f.includes(o(t))};case "contains":return t=>{let f=o(t);return Array.isArray(f)&&f.includes(s(t))};case "notContains":return t=>{let f=o(t);return !Array.isArray(f)||!f.includes(s(t))};case "exists":return t=>o(t)!==void 0;case "notExists":return t=>o(t)===void 0;case "matches":return t=>{let f=o(t),c=s(t);return typeof f!="string"||typeof c!="string"?false:new RegExp(c).test(f)};case "notMatches":return t=>{let f=o(t),c=s(t);return typeof f!="string"||typeof c!="string"?true:!new RegExp(c).test(f)};case "startsWith":return t=>{let f=o(t),c=s(t);return typeof f=="string"&&typeof c=="string"&&f.startsWith(c)};case "endsWith":return t=>{let f=o(t),c=s(t);return typeof f=="string"&&typeof c=="string"&&f.endsWith(c)}}}function ln(n,e,i,r){let o=n.conditions.map(t=>L(t,e,i,r)),s=o.length;if(s===1)return t=>!!o[0](t);if(s===2){let[t,f]=o;return n.logic==="AND"?c=>!!t(c)&&!!f(c):c=>!!t(c)||!!f(c)}if(s===3){let[t,f,c]=o;return n.logic==="AND"?l=>!!t(l)&&!!f(l)&&!!c(l):l=>!!t(l)||!!f(l)||!!c(l)}return n.logic==="AND"?t=>{for(let f=0;f<s;f++)if(!o[f](t))return false;return true}:t=>{for(let f=0;f<s;f++)if(o[f](t))return true;return false}}function zn(n){return JSON.stringify(n)}function an(n,e={}){let i=e.scope??{},r=e.accessor,o=e.boundaries,s=I(n,i,r,o),t=R(n),f=zn(n);return {fn:s,deps:t,hash:f}}function I(n,e,i,r){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let o=n.map(s=>I(s,e,i,r));return s=>o.map(t=>t(s))}if(y(n))return en(n,i);if(T(n))return tn(n,e,i,r);if(b(n))return on(n,e,i,r);if(C(n))return rn(n,e,i,r);if($(n))return sn(n,e,i,r);if(E(n))return un(n,e,i,r);if(h(n))return ln(n,e,i,r);if(r?.length){let o=n;for(let s of r)if(s.check(o))return s.handle(o)}if(J(n)){let o=n,s=Object.keys(o),t=s.map(f=>I(o[f],e,i,r));return f=>{let c={};for(let l=0;l<s.length;l++)c[s[l]]=t[l](f);return c}}return ()=>n}P(I);cn(I);function pn(n){let e=new Set;return w(n,e),e}function w(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)w(r,e);return}if(y(n))return;if(T(n)){w(n.$if,e),w(n.then,e),n.else!==void 0&&w(n.else,e);return}if(b(n)){for(let r of n.$pipe)w(r,e);return}if(C(n)){if(e.add(n.$fn),n.args)for(let r of n.args)w(r,e);return}if($(n)){w(n.$arrow,e);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&w(n.left,e),n.right!==void 0&&typeof n.right=="object"&&w(n.right,e);return}if(h(n)){for(let r of n.conditions)w(r,e);return}let i=n;for(let r of Object.keys(i))w(i[r],e);}function dn(n){let e=new Set;for(let i of n){let r=i.indexOf("."),o=i.indexOf("["),s=i.length;r!==-1&&(s=Math.min(s,r)),o!==-1&&(s=Math.min(s,o));let t=i.slice(0,s);t&&e.add(t);}return e}var _n="data",Gn="scope",B="__",Wn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function gn(n,e={}){let{dataParam:i=_n,scopeParam:r=Gn,noPrefixes:o=false,useAccessor:s=false,lexicalPrefix:t}=e;return g(n,i,r,o,s,t)}function g(n,e,i,r,o,s,t){if(n===null)return omniAst.builders.literal(null);if(typeof n=="string")return omniAst.builders.literal(n);if(typeof n=="number")return omniAst.builders.literal(n);if(typeof n=="boolean")return omniAst.builders.literal(n);if(Array.isArray(n))return omniAst.builders.arrayExpression(n.map(f=>g(f,e,i,r,o,s,t)));if(y(n))return Mn(n.$,e,r,o,s,t);if(T(n))return Vn(n,e,i,r,o,s,t);if(b(n))return Kn(n.$pipe,e,i,r,o,s,t);if(C(n))return qn(n,e,i,r,o,s,t);if($(n))return Ln(n,e,i,r,o,s,t);if(E(n))return Yn(n,e,i,r,o,s,t);if(h(n))return Hn(n,e,i,r,o,s,t);if(typeof n=="object"&&"$__b"in n){let f=n.$__b;return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.memberExpression(omniAst.builders.identifier(B),omniAst.builders.identifier("b")),omniAst.builders.literal(f),true,false),[omniAst.builders.identifier(e)])}if(typeof n=="object"){let c=Object.entries(n).map(([l,a])=>omniAst.builders.property(omniAst.builders.identifier(l),g(a,e,i,r,o,s,t)));return omniAst.builders.objectExpression(c)}return omniAst.builders.literal(null)}var V="accessor";function q(n,e){return e?n===e||n.startsWith(e+"."):false}function K(n){let e=n.indexOf("."),i=n.indexOf("["),r=n.length;return e!==-1&&(r=Math.min(r,e)),i!==-1&&(r=Math.min(r,i)),n.slice(0,r)}function Mn(n,e,i,r,o,s){return s&&s.has(K(n))?x(n,e,true):r?q(n,o)?x(n,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(n),omniAst.builders.identifier(e)]):n.includes("[*]")?Jn(n,e,i):x(n,e,i)}function x(n,e,i){let r=N(n);if(r.length===0)return i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e);let o;if(i){let s=r[0];o=omniAst.builders.identifier(s.value);for(let t=1;t<r.length;t++){let f=r[t];f.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(f.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(f.value),true,true);}}else {o=omniAst.builders.identifier(e);for(let s of r)s.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(s.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(s.value),true,true);}return o}function Jn(n,e,i){let r=n.indexOf("[*]"),o=n.slice(0,r),s=n.slice(r+3),t;if(o?t=x(o,e,i):t=i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e),!s||s==="")return t;if(s.includes("[*]"))return yn(t,s);let f="_i",c=s.startsWith(".")?s.slice(1):s,l=omniAst.builders.identifier(f);if(c){let a=N(c);for(let p of a)p.type==="key"?l=omniAst.builders.memberExpression(l,omniAst.builders.identifier(p.value),false,true):l=omniAst.builders.memberExpression(l,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(t,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],l)])}function yn(n,e){let i=e.indexOf("[*]"),r=e.slice(0,i),o=e.slice(i+3),s="_i",t=r.startsWith(".")?r.slice(1):r,f=omniAst.builders.identifier(s);if(t){let l=N(t);for(let a of l)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}if(o.includes("[*]")){let l=yn(f,o);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],l)])}let c=o.startsWith(".")?o.slice(1):o;if(c){let l=N(c);for(let a of l)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],f)])}function Ln(n,e,i,r,o,s,t){let f=n.args??[],c=new Set(t);for(let p of f)c.add(p);let l=g(n.$arrow,e,i,r,o,s,c),a=f.map(p=>omniAst.builders.identifier(p));return omniAst.builders.arrowFunctionExpression(a,l)}function Vn(n,e,i,r,o,s,t){let f;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;t&&t.has(K(p))?d=x(p,e,true):o?q(p,s)?d=x(p,e,true):d=omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(p),omniAst.builders.identifier(e)]):d=x(p,e,r),f=a?omniAst.builders.unaryExpression("!",d):d;}else f=g(n.$if,e,i,r,o,s,t);let c=g(n.then,e,i,r,o,s,t),l=n.else!==void 0?g(n.else,e,i,r,o,s,t):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(f,c,l)}function qn(n,e,i,r,o,s,t){let f=r?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(i),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return f;let c=n.args.map(l=>g(l,e,i,r,o,s,t));return omniAst.builders.callExpression(f,c)}function Kn(n,e,i,r,o,s,t){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return g(n[0],e,i,r,o,s,t);let f=g(n[0],e,i,r,o,s,t);for(let c=1;c<n.length;c++){let l=g(n[c],e,i,r,o,s,t);f=omniAst.builders.callExpression(l,[f]);}return f}function mn(n,e,i,r,o,s,t){if(y(n)){let f=n.$;return t&&t.has(K(f))?x(f,e,true):o?q(f,s)?x(f,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(f),omniAst.builders.identifier(e)]):x(f,e,r)}return g(n,e,i,r,o,s,t)}function Yn(n,e,i,r,o,s,t){let f=mn(n.left,e,i,r,o,s,t),c=n.right!==void 0?mn(n.right,e,i,r,o,s,t):omniAst.builders.literal(null),l=Wn[n.op];if(l)return omniAst.builders.binaryExpression(l,f,c);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(c,omniAst.builders.identifier("includes")),[f]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(c,omniAst.builders.identifier("includes")),[f]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes"),false,true),[c]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes"),false,true),[c]));case "exists":return omniAst.builders.binaryExpression("!=",f,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",f,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[c]),omniAst.builders.identifier("test")),[f]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[c]),omniAst.builders.identifier("test")),[f]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("startsWith"),false,true),[c]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("endsWith"),false,true),[c]);default:return omniAst.builders.binaryExpression("===",f,c)}}function Hn(n,e,i,r,o,s,t){let{logic:f,conditions:c}=n,l=f==="AND"?"&&":"||";if(c.length===0)return omniAst.builders.literal(f==="AND");if(c.length===1)return g(c[0],e,i,r,o,s,t);let a=g(c[0],e,i,r,o,s,t);for(let p=1;p<c.length;p++){let d=g(c[p],e,i,r,o,s,t);a=omniAst.builders.logicalExpression(l,a,d);}return a}function En(n,e,i,r,o,s=0){let t=gn(n,{noPrefixes:true,useAccessor:r,lexicalPrefix:o}),f=omniAst.generate(t),c="";r?o&&(c=`const{${o}}=data??{};`):e.size>0&&(c=`const{${[...e].join(",")}}=data??{};`);let l=i.size>0?`const{${[...i].join(",")}}=scope;`:"",a=r||s>0,p="";if(a){let D=[];r&&D.push("accessor"),s>0&&D.push("b"),p=`const{${D.join(",")}}=${B};`;}let d=i.size>0,m;d&&a?m=`scope,${B}`:d?m="scope":a?m=`_,${B}`:m="";let v=`${c}return ${f}`,j;return m?j=`(function(${m}){${l}${p}return function(data){${v}}})`:j=`(function(){return function(data){${v}}})`,j}function Un(n){return JSON.stringify(n)}function S(n,e,i){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>S(s,e,i));if(y(n))return n;if(T(n))return {$if:typeof n.$if=="string"?n.$if:S(n.$if,e,i),then:S(n.then,e,i),...n.else!==void 0?{else:S(n.else,e,i)}:{}};if(b(n))return {$pipe:n.$pipe.map(s=>S(s,e,i))};if(C(n))return {$fn:n.$fn,...n.args!==void 0?{args:n.args.map(s=>S(s,e,i))}:{}};if($(n))return {$arrow:S(n.$arrow,e,i),...n.args?{args:n.args}:{},...n.schema?{schema:n.schema}:{}};if(E(n))return {left:S(n.left,e,i),op:n.op,...n.right!==void 0?{right:S(n.right,e,i)}:{}};if(h(n))return {logic:n.logic,conditions:n.conditions.map(s=>S(s,e,i))};let r=n;for(let s of e)if(s.check(r)){let t=i.length;return i.push(s.handle(r)),{$__b:t}}let o={};for(let s of Object.keys(r))o[s]=S(r[s],e,i);return o}function hn(n,e={}){let{scope:i={},accessor:r,returnCode:o=false,useAccessor:s=false,lexicalPrefix:t,boundaries:f}=e,c=n,l=[];if(f?.length){let F=[];c=S(n,f,F),l=F;}let a=R(c),p=dn(a),d=pn(c),m=Un(n),v=En(c,p,d,s,t,l.length);if(o)return {code:v,deps:a,hash:m,dataRoots:[...p],scopeFns:[...d]};let j={};s&&r&&(j.accessor=r),l.length>0&&(j.b=l);let D=Object.keys(j).length>0,Q;try{let F=new Function(`return ${v}`)();Q=D?F(i,j):F(i);}catch(F){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${F instanceof Error?F.message:String(F)}`)}return {fn:Q,deps:a,hash:m}}function Tn(n,e){return M(n,e)}function M(n,e){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>M(s,e));let i=n,r=Object.keys(i);for(let s of r)if(s in e){let t=e[s](i);if(typeof t=="object"&&t!==null&&s in t)throw new Error(`Transform "${s}" returned object with same key \u2014 infinite loop`);return M(t,e)}let o={};for(let s of r)o[s]=M(i[s],e);return o}var z=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}getOrCompile(e,i){let r=JSON.stringify(e),o=this.cache.get(r);if(o)return this.cache.delete(r),this.cache.set(r,o),o;let s=i();if(this.cache.size>=this._maxSize){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}return this.cache.set(r,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}};var Y=class{constructor(e={}){this.scope=e.scope??{},this.accessor=e.accessor,this.boundaries=e.boundaries,this.cacheClosures=new z(e.cacheSize??1e3),this.cacheJIT=new z(e.cacheSize??1e3);}compile(e){return this.cacheClosures.getOrCompile(e,()=>an(e,{scope:this.scope,accessor:this.accessor,boundaries:this.boundaries}))}jit(e,i){return this.cacheJIT.getOrCompile(e,()=>hn(e,{scope:this.scope,accessor:this.accessor,useAccessor:i?.useAccessor,lexicalPrefix:i?.lexicalPrefix,boundaries:this.boundaries}))}evaluate(e,i){return this.compile(e).fn(i)}evaluateJIT(e,i,r){return this.jit(e,r).fn(i)}normalize(e,i){return Tn(e,i)}extractDeps(e){return R(e)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}};var kn={};Fn(kn,{$:()=>Cn,$arrow:()=>wn,$call:()=>Sn,$cond:()=>An,$fn:()=>bn,$if:()=>Pn,$pipe:()=>$n,arrow:()=>te,call:()=>ie,cond:()=>ee,fn:()=>Zn,pipe:()=>ne,ref:()=>Xn});function Cn(n){return {$:n}}var Xn=Cn;function bn(n,e){return e===void 0?{$fn:n}:{$fn:n,args:e}}var Zn=bn;function Pn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function $n(...n){return {$pipe:n}}var ne=$n;function An(n,e,i){return i===void 0?{left:n,op:e}:{left:n,op:e,right:i}}var ee=An;function wn(n,e){return e===void 0||e.length===0?{$arrow:n}:{$arrow:n,args:e}}var te=wn;function Sn(n,e=[]){return {$fn:n,args:e}}var ie=Sn;function H(n,e="root",i={}){let r=[];return k(n,e,r,i),{valid:r.length===0,errors:r}}function k(n,e,i,r){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t=0;t<n.length;t++)k(n[t],`${e}[${t}]`,i,r);return}if(y(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(T(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):k(n.$if,`${e}.$if`,i,r),k(n.then,`${e}.then`,i,r),n.else!==void 0&&k(n.else,`${e}.else`,i,r);return}if(b(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let t=0;t<n.$pipe.length;t++)k(n.$pipe[t],`${e}.$pipe[${t}]`,i,r);return}if(C(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(r.scope&&!(n.$fn in r.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let t=0;t<n.args.length;t++)k(n.args[t],`${e}.args[${t}]`,i,r);return}if($(n)){if(k(n.$arrow,`${e}.$arrow`,i,r),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array of strings`);else for(let t of n.args)(typeof t!="string"||!t)&&i.push(`${e}.args: each arg must be a non-empty string`);return}if(E(n)){_.has(n.op)||i.push(`${e}: invalid operator "${n.op}"`),k(n.left,`${e}.left`,i,r),n.right!==void 0&&k(n.right,`${e}.right`,i,r);return}if(h(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&i.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){i.push(`${e}.conditions: must be an array`);return}for(let t=0;t<n.conditions.length;t++)k(n.conditions[t],`${e}.conditions[${t}]`,i,r);return}if(r.boundaries?.length){let t=n;for(let f of r.boundaries)if(f.check(t))return}let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let f=s[t];k(o[f],`${e}.${f}`,i,r);}}function oe(n,e={}){let i=H(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function re(n,e={}){return H(n,"root",e).valid}var Ve="3.0.0";exports.ExpressionCompiler=Y;exports.VERSION=Ve;exports.assertValid=oe;exports.builders=kn;exports.compilePath=G;exports.extractDeps=R;exports.get=Dn;exports.hasDeps=In;exports.hasWildcard=X;exports.isArrow=$;exports.isCondition=E;exports.isConditionExpr=Rn;exports.isConditionGroup=h;exports.isConditional=T;exports.isFn=C;exports.isLiteral=J;exports.isPipe=b;exports.isPure=Bn;exports.isRef=y;exports.isValid=re;exports.normalizePath=W;exports.validate=H;
|
|
1
|
+
'use strict';var omniAst=require('omni-ast');var jn=Object.defineProperty;var Nn=(n,e)=>{for(var t in e)jn(n,t,{get:e[t],enumerable:true});};var G=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),g=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,y=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,C=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",T=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),b=n=>n!==null&&typeof n=="object"&&"$arrow"in n,h=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&G.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),E=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,vn=n=>h(n)||E(n),L=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let t=n,i="left"in t&&"op"in t&&G.has(t.op);return !("$"in t)&&!("$if"in t)&&!("$fn"in t)&&!("$pipe"in t)&&!("$arrow"in t)&&!i&&!("logic"in t)}return false};var Z=new Map;function I(n){let e=[],t=n.length,i=0,s="";for(;i<t;){let o=n[i];if(o===".")s&&(e.push({type:"key",value:s}),s=""),i++;else if(o==="["){s&&(e.push({type:"key",value:s}),s=""),i++;let r=i;for(;i<t&&n[i]!=="]";)i++;let f=n.slice(r,i);if(i++,f==="*")e.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);e.push({type:"index",value:isNaN(l)?f:l});}}else s+=o,i++;}return s&&e.push({type:"key",value:s}),e}function P(n){return n.includes("[*]")}function _(n){let e=Z.get(n);return e||(e=P(n)?Dn(n):In(n),Z.set(n,e),e)}function In(n){if(!n.includes(".")&&!n.includes("["))return s=>s?.[n];let e=I(n),t=e.length;if(t===2){let[s,o]=e,r=s.value,f=o.value;return l=>l?.[r]?.[f]}if(t===3){let[s,o,r]=e,f=s.value,l=o.value,u=r.value;return a=>a?.[f]?.[l]?.[u]}let i=e.map(s=>s.value);return s=>{let o=s;for(let r=0;r<t&&o!=null;r++)o=o[i[r]];return o}}function Dn(n){let e=I(n),t=[];for(let i=0;i<e.length;i++)e[i].type==="wildcard"&&t.push(i);return t.length===1?Wn(e,t[0]):Mn(e,t)}function Wn(n,e){let t=n.slice(0,e).map(r=>r.value),i=n.slice(e+1).map(r=>r.value),s=t.length,o=i.length;if(o===0){if(s===1){let r=t[0];return f=>f?.[r]}return r=>{let f=r;for(let l=0;l<s&&f!=null;l++)f=f[t[l]];return f}}if(o===1){let r=i[0];if(s===1){let f=t[0];return l=>{let u=l?.[f];if(Array.isArray(u))return u.map(a=>a?.[r])}}return f=>{let l=f;for(let u=0;u<s&&l!=null;u++)l=l[t[u]];if(Array.isArray(l))return l.map(u=>u?.[r])}}return r=>{let f=r;for(let l=0;l<s&&f!=null;l++)f=f[t[l]];if(Array.isArray(f))return f.map(l=>{let u=l;for(let a=0;a<o&&u!=null;a++)u=u[i[a]];return u})}}function Mn(n,e){let t=[],i=0;for(let o=0;o<e.length;o++){let r=e[o],f=o===e.length-1,l=n.slice(i,r).map(u=>u.value);l.length>0&&t.push({type:"access",keys:l}),t.push({type:f?"map":"flatMap",keys:[]}),i=r+1;}let s=n.slice(i).map(o=>o.value);return o=>{let r=o;for(let f of t){if(r==null)return;if(f.type==="access")for(let l of f.keys){if(r==null)return;r=r[l];}else if(f.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(l=>{let u=l;return Array.isArray(u)?u:[u]});}else if(f.type==="map"){if(!Array.isArray(r))return;s.length>0&&(r=r.map(l=>{let u=l;for(let a of s){if(u==null)return;u=u[a];}return u}));}}return r}}function _n(n,e){return _(e)(n)}function J(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function v(n){let e=new Set;return S(n,e),Array.from(e)}function S(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)S(n[s],e);return}if(g(n)){e.add(J(n.$));return}if(y(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(J(s));}else S(n.$if,e);S(n.then,e),n.else!==void 0&&S(n.else,e);return}if(T(n)){for(let s=0;s<n.$pipe.length;s++)S(n.$pipe[s],e);return}if(C(n)){if(n.args)for(let s=0;s<n.args.length;s++)S(n.args[s],e);return}if(b(n)){let s=new Set;S(n.$arrow,s);let o=new Set(n.args??[]);for(let r of s){let f=r.split(".")[0].split("[")[0];o.has(f)||e.add(r);}return}if(h(n)){S(n.left,e),n.right!==void 0&&S(n.right,e);return}if(E(n)){for(let s=0;s<n.conditions.length;s++)S(n.conditions[s],e);return}let t=n,i=Object.keys(t);for(let s=0;s<i.length;s++)S(t[i[s]],e);}function zn(n){return v(n).length>0}function Hn(n){return v(n).length===0}var nn;function en(n){nn=n;}function F(n,e,t){return nn(n,e,t)}function Gn(n){let e=n.indexOf("."),t=n.indexOf("["),i=n.length;return e!==-1&&(i=Math.min(i,e)),t!==-1&&(i=Math.min(i,t)),n.slice(0,i)}function tn(n,e,t){return t&&t.has(Gn(n))?_(n):e?()=>e(n):_(n)}function on(n,e,t){return tn(n.$,e.accessor,t)}function rn(n,e,t){let i;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=tn(r,e.accessor,t);i=n.$if.startsWith("!")?u=>!f(u):u=>!!f(u);}else {let r=F(n.$if,e,t);i=f=>!!r(f);}let s=F(n.then,e,t),o=n.else!==void 0?F(n.else,e,t):()=>{};return r=>i(r)?s(r):o(r)}function sn(n,e,t){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return F(i[0],e,t);let s=F(i[0],e,t),o=i.slice(1).map(f=>F(f,e,t)),r=o.length;if(r===1){let[f]=o;return l=>{let u=s(l),a=f(l);return typeof a=="function"?a(u):a}}if(r===2){let[f,l]=o;return u=>{let a=s(u),p=f(u);return a=typeof p=="function"?p(a):p,p=l(u),typeof p=="function"?p(a):p}}if(r===3){let[f,l,u]=o;return a=>{let p=s(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=u(a),typeof d=="function"?d(p):d}}return f=>{let l=s(f);for(let u=0;u<r;u++){let a=o[u](f);l=typeof a=="function"?a(l):a;}return l}}function fn(n,e,t){let i=n.$fn,s=i.indexOf(":");if(s!==-1)return Jn(i,s,n.args,e,t);let{scope:o}=e,r=n.args;if(r===void 0)return ()=>{let u=o[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let f=r.map(u=>F(u,e,t)),l=f.length;if(l===0)return ()=>{let u=o[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(l===1){let[u]=f;return a=>{let p=o[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(a))}}if(l===2){let[u,a]=f;return p=>{let d=o[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),a(p))}}if(l===3){let[u,a,p]=f;return d=>{let $=o[i];if(!$)throw new Error(`Function not found in scope: ${i}`);return $(u(d),a(d),p(d))}}return u=>{let a=o[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(...f.map(p=>p(u)))}}function Jn(n,e,t,i,s){let o=n.slice(0,e),r=n.slice(e+1),f=i.handlers?.[o]?.[r];if(!f)throw new Error(`Handler not found: ${n}`);if(t===void 0)return ()=>f;let l=t.map(a=>F(a,i,s)),u=l.length;if(u===0)return ()=>f();if(u===1){let[a]=l;return p=>f(a(p))}if(u===2){let[a,p]=l;return d=>f(a(d),p(d))}if(u===3){let[a,p,d]=l;return $=>f(a($),p($),d($))}return a=>f(...l.map(p=>p(a)))}function cn(n,e,t){let i=n.args??[];if(i.length===0){let r=F(n.$arrow,e,t);return f=>()=>r(f)}let s=new Set(t);for(let r of i)s.add(r);let o=F(n.$arrow,e,s);return r=>(...f)=>{let l={};for(let a=0;a<i.length;a++)l[i[a]]=f[a];let u={...r,...l};return o(u)}}var ln;function un(n){ln=n;}function V(n,e,t){return ln(n,e,t)}function an(n,e,t){let i=V(n.left,e,t),s=n.right!==void 0?V(n.right,e,t):()=>{};switch(n.op){case "eq":return o=>i(o)===s(o);case "neq":return o=>i(o)!==s(o);case "gt":return o=>i(o)>s(o);case "gte":return o=>i(o)>=s(o);case "lt":return o=>i(o)<s(o);case "lte":return o=>i(o)<=s(o);case "in":return o=>{let r=s(o);return Array.isArray(r)&&r.includes(i(o))};case "notIn":return o=>{let r=s(o);return !Array.isArray(r)||!r.includes(i(o))};case "contains":return o=>{let r=i(o);return Array.isArray(r)&&r.includes(s(o))};case "notContains":return o=>{let r=i(o);return !Array.isArray(r)||!r.includes(s(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let r=i(o),f=s(o);return typeof r!="string"||typeof f!="string"?false:new RegExp(f).test(r)};case "notMatches":return o=>{let r=i(o),f=s(o);return typeof r!="string"||typeof f!="string"?true:!new RegExp(f).test(r)};case "startsWith":return o=>{let r=i(o),f=s(o);return typeof r=="string"&&typeof f=="string"&&r.startsWith(f)};case "endsWith":return o=>{let r=i(o),f=s(o);return typeof r=="string"&&typeof f=="string"&&r.endsWith(f)}}}function pn(n,e,t){let i=n.conditions.map(o=>V(o,e,t)),s=i.length;if(s===1)return o=>!!i[0](o);if(s===2){let[o,r]=i;return n.logic==="AND"?f=>!!o(f)&&!!r(f):f=>!!o(f)||!!r(f)}if(s===3){let[o,r,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!r(l)&&!!f(l):l=>!!o(l)||!!r(l)||!!f(l)}return n.logic==="AND"?o=>{for(let r=0;r<s;r++)if(!i[r](o))return false;return true}:o=>{for(let r=0;r<s;r++)if(i[r](o))return true;return false}}function Bn(n){return JSON.stringify(n)}function dn(n,e={}){let t={scope:e.scope??{},accessor:e.accessor,boundaries:e.boundaries,handlers:e.handlers},i=z(n,t),s=v(n),o=Bn(n);return {fn:i,deps:s,hash:o}}function z(n,e,t){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>z(s,e,t));return s=>i.map(o=>o(s))}if(g(n))return on(n,e,t);if(y(n))return rn(n,e,t);if(T(n))return sn(n,e,t);if(C(n))return fn(n,e,t);if(b(n))return cn(n,e,t);if(h(n))return an(n,e,t);if(E(n))return pn(n,e,t);if(e.boundaries?.length){let i=n;for(let s of e.boundaries)if(s.check(i))return s.handle(i)}if(L(n)){let i=n,s=Object.keys(i),o=s.map(r=>z(i[r],e,t));return r=>{let f={};for(let l=0;l<s.length;l++)f[s[l]]=o[l](r);return f}}return ()=>n}en(z);un(z);function mn(n){let e=new Set;return w(n,e),e}function w(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)w(i,e);return}if(g(n))return;if(y(n)){w(n.$if,e),w(n.then,e),n.else!==void 0&&w(n.else,e);return}if(T(n)){for(let i of n.$pipe)w(i,e);return}if(C(n)){if(n.$fn.includes(":")||e.add(n.$fn),n.args)for(let i of n.args)w(i,e);return}if(b(n)){w(n.$arrow,e);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&w(n.left,e),n.right!==void 0&&typeof n.right=="object"&&w(n.right,e);return}if(E(n)){for(let i of n.conditions)w(i,e);return}let t=n;for(let i of Object.keys(t))w(t[i],e);}function gn(n){return k(n)}function k(n){if(n===null||typeof n!="object")return false;if(Array.isArray(n)){for(let t of n)if(k(t))return true;return false}if(g(n))return false;if(y(n))return k(n.$if)||k(n.then)||n.else!==void 0&&k(n.else);if(T(n)){for(let t of n.$pipe)if(k(t))return true;return false}if(C(n)){if(n.$fn.includes(":"))return true;if(n.args){for(let t of n.args)if(k(t))return true}return false}if(b(n))return k(n.$arrow);if(h(n))return k(n.left)||n.right!==void 0&&k(n.right);if(E(n)){for(let t of n.conditions)if(k(t))return true;return false}let e=n;for(let t of Object.keys(e))if(k(e[t]))return true;return false}function hn(n){let e=new Set;for(let t of n){let i=t.indexOf("."),s=t.indexOf("["),o=t.length;i!==-1&&(o=Math.min(o,i)),s!==-1&&(o=Math.min(o,s));let r=t.slice(0,o);r&&e.add(r);}return e}var Ln="data",Vn="scope",W="__",qn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function yn(n,e={}){let{dataParam:t=Ln,scopeParam:i=Vn,noPrefixes:s=false,useAccessor:o=false,lexicalPrefix:r}=e;return m(n,t,i,s,o,r)}function m(n,e,t,i,s,o,r){if(n===null)return omniAst.builders.literal(null);if(typeof n=="string")return omniAst.builders.literal(n);if(typeof n=="number")return omniAst.builders.literal(n);if(typeof n=="boolean")return omniAst.builders.literal(n);if(Array.isArray(n))return omniAst.builders.arrayExpression(n.map(f=>m(f,e,t,i,s,o,r)));if(g(n))return Kn(n.$,e,i,s,o,r);if(y(n))return Un(n,e,t,i,s,o,r);if(T(n))return Pn(n.$pipe,e,t,i,s,o,r);if(C(n))return Xn(n,e,t,i,s,o,r);if(b(n))return Qn(n,e,t,i,s,o,r);if(h(n))return ne(n,e,t,i,s,o,r);if(E(n))return ee(n,e,t,i,s,o,r);if(typeof n=="object"&&"$__b"in n){let f=n.$__b;return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.memberExpression(omniAst.builders.identifier(W),omniAst.builders.identifier("b")),omniAst.builders.literal(f),true,false),[omniAst.builders.identifier(e)])}if(typeof n=="object"){let l=Object.entries(n).map(([u,a])=>omniAst.builders.property(omniAst.builders.identifier(u),m(a,e,t,i,s,o,r)));return omniAst.builders.objectExpression(l)}return omniAst.builders.literal(null)}var q="accessor";function K(n,e){return e?n===e||n.startsWith(e+"."):false}function Y(n){let e=n.indexOf("."),t=n.indexOf("["),i=n.length;return e!==-1&&(i=Math.min(i,e)),t!==-1&&(i=Math.min(i,t)),n.slice(0,i)}function Kn(n,e,t,i,s,o){return o&&o.has(Y(n))?O(n,e,true):i?K(n,s)?O(n,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(q),[omniAst.builders.literal(n)]):n.includes("[*]")?Yn(n,e,t):O(n,e,t)}function O(n,e,t){let i=I(n);if(i.length===0)return t?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e);let s;if(t){let o=i[0];s=omniAst.builders.identifier(o.value);for(let r=1;r<i.length;r++){let f=i[r];f.type==="key"?s=omniAst.builders.memberExpression(s,omniAst.builders.identifier(f.value),false,true):s=omniAst.builders.memberExpression(s,omniAst.builders.literal(f.value),true,true);}}else {s=omniAst.builders.identifier(e);for(let o of i)o.type==="key"?s=omniAst.builders.memberExpression(s,omniAst.builders.identifier(o.value),false,true):s=omniAst.builders.memberExpression(s,omniAst.builders.literal(o.value),true,true);}return s}function Yn(n,e,t){let i=n.indexOf("[*]"),s=n.slice(0,i),o=n.slice(i+3),r;if(s?r=O(s,e,t):r=t?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e),!o||o==="")return r;if(o.includes("[*]"))return Cn(r,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,u=omniAst.builders.identifier(f);if(l){let a=I(l);for(let p of a)p.type==="key"?u=omniAst.builders.memberExpression(u,omniAst.builders.identifier(p.value),false,true):u=omniAst.builders.memberExpression(u,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],u)])}function Cn(n,e){let t=e.indexOf("[*]"),i=e.slice(0,t),s=e.slice(t+3),o="_i",r=i.startsWith(".")?i.slice(1):i,f=omniAst.builders.identifier(o);if(r){let u=I(r);for(let a of u)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}if(s.includes("[*]")){let u=Cn(f,s);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],u)])}let l=s.startsWith(".")?s.slice(1):s;if(l){let u=I(l);for(let a of u)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],f)])}function Qn(n,e,t,i,s,o,r){let f=n.args??[],l=new Set(r);for(let p of f)l.add(p);let u=m(n.$arrow,e,t,i,s,o,l),a=f.map(p=>omniAst.builders.identifier(p));return omniAst.builders.arrowFunctionExpression(a,u)}function Un(n,e,t,i,s,o,r){let f;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;r&&r.has(Y(p))?d=O(p,e,true):s?K(p,o)?d=O(p,e,true):d=omniAst.builders.callExpression(omniAst.builders.identifier(q),[omniAst.builders.literal(p)]):d=O(p,e,i),f=a?omniAst.builders.unaryExpression("!",d):d;}else f=m(n.$if,e,t,i,s,o,r);let l=m(n.then,e,t,i,s,o,r),u=n.else!==void 0?m(n.else,e,t,i,s,o,r):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(f,l,u)}function Xn(n,e,t,i,s,o,r){let f=n.$fn.indexOf(":");if(f!==-1)return Zn(n,f,e,t,i,s,o,r);let l=i?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(t),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return l;let u=n.args.map(a=>m(a,e,t,i,s,o,r));return omniAst.builders.callExpression(l,u)}function Zn(n,e,t,i,s,o,r,f){let l=n.$fn.slice(0,e),u=n.$fn.slice(e+1),a=s?omniAst.builders.memberExpression(omniAst.builders.memberExpression(omniAst.builders.identifier("h"),omniAst.builders.identifier(l)),omniAst.builders.identifier(u)):omniAst.builders.memberExpression(omniAst.builders.memberExpression(omniAst.builders.memberExpression(omniAst.builders.identifier(W),omniAst.builders.identifier("h")),omniAst.builders.identifier(l)),omniAst.builders.identifier(u));if(n.args===void 0)return a;let p=n.args.map(d=>m(d,t,i,s,o,r,f));return omniAst.builders.callExpression(a,p)}function Pn(n,e,t,i,s,o,r){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return m(n[0],e,t,i,s,o,r);let f=m(n[0],e,t,i,s,o,r);for(let l=1;l<n.length;l++){let u=m(n[l],e,t,i,s,o,r);f=omniAst.builders.callExpression(u,[f]);}return f}function En(n,e,t,i,s,o,r){if(g(n)){let f=n.$;return r&&r.has(Y(f))?O(f,e,true):s?K(f,o)?O(f,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(q),[omniAst.builders.literal(f)]):O(f,e,i)}return m(n,e,t,i,s,o,r)}function ne(n,e,t,i,s,o,r){let f=En(n.left,e,t,i,s,o,r),l=n.right!==void 0?En(n.right,e,t,i,s,o,r):omniAst.builders.literal(null),u=qn[n.op];if(u)return omniAst.builders.binaryExpression(u,f,l);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(l,omniAst.builders.identifier("includes")),[f]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(l,omniAst.builders.identifier("includes")),[f]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes"),false,true),[l]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes"),false,true),[l]));case "exists":return omniAst.builders.binaryExpression("!=",f,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",f,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[l]),omniAst.builders.identifier("test")),[f]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[l]),omniAst.builders.identifier("test")),[f]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("startsWith"),false,true),[l]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("endsWith"),false,true),[l]);default:return omniAst.builders.binaryExpression("===",f,l)}}function ee(n,e,t,i,s,o,r){let{logic:f,conditions:l}=n,u=f==="AND"?"&&":"||";if(l.length===0)return omniAst.builders.literal(f==="AND");if(l.length===1)return m(l[0],e,t,i,s,o,r);let a=m(l[0],e,t,i,s,o,r);for(let p=1;p<l.length;p++){let d=m(l[p],e,t,i,s,o,r);a=omniAst.builders.logicalExpression(u,a,d);}return a}function Tn(n,e,t,i,s,o=0,r=false){let f=yn(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),l=omniAst.generate(f),u="";i?s&&(u=`const{${s}}=data??{};`):e.size>0&&(u=`const{${[...e].join(",")}}=data??{};`);let a=t.size>0?`const{${[...t].join(",")}}=scope;`:"",p=i||o>0||r,d="";if(p){let x=[];i&&x.push("accessor"),o>0&&x.push("b"),r&&x.push("h"),d=`const{${x.join(",")}}=${W};`;}let $=t.size>0,j;$&&p?j=`scope,${W}`:$?j="scope":p?j=`_,${W}`:j="";let M=`${u}return ${l}`,D;return j?D=`(function(${j}){${a}${d}return function(data){${M}}})`:D=`(function(){return function(data){${M}}})`,D}function ie(n){return JSON.stringify(n)}function A(n,e,t){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>A(o,e,t));if(g(n))return n;if(y(n))return {$if:typeof n.$if=="string"?n.$if:A(n.$if,e,t),then:A(n.then,e,t),...n.else!==void 0?{else:A(n.else,e,t)}:{}};if(T(n))return {$pipe:n.$pipe.map(o=>A(o,e,t))};if(C(n))return {$fn:n.$fn,...n.args!==void 0?{args:n.args.map(o=>A(o,e,t))}:{}};if(b(n))return {$arrow:A(n.$arrow,e,t),...n.args?{args:n.args}:{},...n.schema?{schema:n.schema}:{}};if(h(n))return {left:A(n.left,e,t),op:n.op,...n.right!==void 0?{right:A(n.right,e,t)}:{}};if(E(n))return {logic:n.logic,conditions:n.conditions.map(o=>A(o,e,t))};let i=n;for(let o of e)if(o.check(i)){let r=t.length;return t.push(o.handle(i)),{$__b:r}}let s={};for(let o of Object.keys(i))s[o]=A(i[o],e,t);return s}function bn(n,e={}){let{scope:t={},accessor:i,returnCode:s=false,useAccessor:o=false,lexicalPrefix:r,boundaries:f,handlers:l}=e,u=n,a=[];if(f?.length){let N=[];u=A(n,f,N),a=N;}let p=v(u),d=hn(p),$=mn(u),j=l?gn(u):false,M=ie(n),D=Tn(u,d,$,o,r,a.length,j);if(s)return {code:D,deps:p,hash:M,dataRoots:[...d],scopeFns:[...$]};let x={};o&&i&&(x.accessor=i),a.length>0&&(x.b=a),j&&l&&(x.h=l);let On=Object.keys(x).length>0,X;try{let N=new Function(`return ${D}`)();X=On?N(t,x):N(t);}catch(N){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${N instanceof Error?N.message:String(N)}`)}return {fn:X,deps:p,hash:M}}function $n(n,e){return B(n,e)}function B(n,e){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>B(o,e));let t=n,i=Object.keys(t);for(let o of i)if(o in e){let r=e[o](t);if(typeof r=="object"&&r!==null&&o in r)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return B(r,e)}let s={};for(let o of i)s[o]=B(t[o],e);return s}var H=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}getOrCompile(e,t){let i=JSON.stringify(e),s=this.cache.get(i);if(s)return this.cache.delete(i),this.cache.set(i,s),s;let o=t();if(this.cache.size>=this._maxSize){let r=this.cache.keys().next().value;r&&this.cache.delete(r);}return this.cache.set(i,o),o}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}}};var Q=class{constructor(e={}){this.scope=e.scope??{},this.accessor=e.accessor,this.boundaries=e.boundaries,this.cacheClosures=new H(e.cacheSize??1e3),this.cacheJIT=new H(e.cacheSize??1e3),e.handlers&&(this.wrappedHandlers=this.wrapHandlers(e.handlers));}compile(e){return this.cacheClosures.getOrCompile(e,()=>dn(e,{scope:this.scope,accessor:this.accessor,boundaries:this.boundaries,handlers:this.wrappedHandlers}))}jit(e,t){return this.cacheJIT.getOrCompile(e,()=>bn(e,{scope:this.scope,accessor:this.accessor,useAccessor:t?.useAccessor,lexicalPrefix:t?.lexicalPrefix,boundaries:this.boundaries,handlers:this.wrappedHandlers}))}wrapHandlers(e){let t={},i={};for(let s of Object.keys(e)){i[s]={};for(let o of Object.keys(e[s]))i[s][o]=e[s][o].bind(t);}return t.accessor=this.accessor,t.handlers=i,t.compiler=this,t.scope=this.scope,i}evaluate(e,t){return this.compile(e).fn(t)}evaluateJIT(e,t,i){return this.jit(e,i).fn(t)}normalize(e,t){return $n(e,t)}extractDeps(e){return v(e)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}};var Fn={};Nn(Fn,{$:()=>Sn,$arrow:()=>Rn,$call:()=>xn,$cond:()=>An,$fn:()=>wn,$if:()=>se,$pipe:()=>kn,arrow:()=>le,call:()=>ue,cond:()=>ce,fn:()=>re,pipe:()=>fe,ref:()=>oe});function Sn(n){return {$:n}}var oe=Sn;function wn(n,e){return e===void 0?{$fn:n}:{$fn:n,args:e}}var re=wn;function se(n,e,t){return t===void 0?{$if:n,then:e}:{$if:n,then:e,else:t}}function kn(...n){return {$pipe:n}}var fe=kn;function An(n,e,t){return t===void 0?{left:n,op:e}:{left:n,op:e,right:t}}var ce=An;function Rn(n,e){return e===void 0||e.length===0?{$arrow:n}:{$arrow:n,args:e}}var le=Rn;function xn(n,e=[]){return {$fn:n,args:e}}var ue=xn;function U(n,e="root",t={}){let i=[];return R(n,e,i,t),{valid:i.length===0,errors:i}}function R(n,e,t,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)R(n[r],`${e}[${r}]`,t,i);return}if(g(n)){(!n.$||typeof n.$!="string")&&t.push(`${e}: invalid reference, $ must be non-empty string`);return}if(y(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||t.push(`${e}.$if: empty path in string shorthand`):R(n.$if,`${e}.$if`,t,i),R(n.then,`${e}.then`,t,i),n.else!==void 0&&R(n.else,`${e}.else`,t,i);return}if(T(n)){if(!Array.isArray(n.$pipe)){t.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){t.push(`${e}.$pipe: must have at least one element`);return}for(let r=0;r<n.$pipe.length;r++)R(n.$pipe[r],`${e}.$pipe[${r}]`,t,i);return}if(C(n)){if(!n.$fn||typeof n.$fn!="string"){t.push(`${e}: invalid function, $fn must be non-empty string`);return}let r=n.$fn.indexOf(":");if(r!==-1){let f=n.$fn.slice(0,r),l=n.$fn.slice(r+1);!f||!l||l.includes(":")?t.push(`${e}: invalid handler format "${n.$fn}", expected "namespace:method"`):i.handlers&&!i.handlers[f]?.[l]&&t.push(`${e}: handler "${n.$fn}" not found`);}else i.scope&&!(n.$fn in i.scope)&&t.push(`${e}: function "${n.$fn}" not found in scope`);if(n.args!==void 0)if(!Array.isArray(n.args))t.push(`${e}.args: must be an array`);else for(let f=0;f<n.args.length;f++)R(n.args[f],`${e}.args[${f}]`,t,i);return}if(b(n)){if(R(n.$arrow,`${e}.$arrow`,t,i),n.args!==void 0)if(!Array.isArray(n.args))t.push(`${e}.args: must be an array of strings`);else for(let r of n.args)(typeof r!="string"||!r)&&t.push(`${e}.args: each arg must be a non-empty string`);return}if(h(n)){G.has(n.op)||t.push(`${e}: invalid operator "${n.op}"`),R(n.left,`${e}.left`,t,i),n.right!==void 0&&R(n.right,`${e}.right`,t,i);return}if(E(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&t.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){t.push(`${e}.conditions: must be an array`);return}for(let r=0;r<n.conditions.length;r++)R(n.conditions[r],`${e}.conditions[${r}]`,t,i);return}if(i.boundaries?.length){let r=n;for(let f of i.boundaries)if(f.check(r))return}let s=n,o=Object.keys(s);for(let r=0;r<o.length;r++){let f=o[r];R(s[f],`${e}.${f}`,t,i);}}function ae(n,e={}){let t=U(n,"root",e);if(!t.valid)throw new Error(`Invalid expression: ${t.errors.join("; ")}`)}function pe(n,e={}){return U(n,"root",e).valid}var Xe="3.0.0";exports.ExpressionCompiler=Q;exports.VERSION=Xe;exports.assertValid=ae;exports.builders=Fn;exports.compilePath=_;exports.extractDeps=v;exports.get=_n;exports.hasDeps=zn;exports.hasWildcard=P;exports.isArrow=b;exports.isCondition=h;exports.isConditionExpr=vn;exports.isConditionGroup=E;exports.isConditional=y;exports.isFn=C;exports.isLiteral=L;exports.isPipe=T;exports.isPure=Hn;exports.isRef=g;exports.isValid=pe;exports.normalizePath=J;exports.validate=U;
|
package/dist/index.d.cts
CHANGED
|
@@ -106,14 +106,13 @@ interface CompiledExpression<T = unknown, R = unknown> {
|
|
|
106
106
|
*/
|
|
107
107
|
type PathGetter<T = unknown> = (data: T) => unknown;
|
|
108
108
|
/**
|
|
109
|
-
* Accessor customizado para resolver paths
|
|
110
|
-
*
|
|
109
|
+
* Accessor customizado para resolver paths.
|
|
110
|
+
* Closure auto-suficiente — já sabe de onde ler.
|
|
111
111
|
*
|
|
112
112
|
* @example
|
|
113
|
-
*
|
|
114
|
-
* const accessor: AccessorFn<TickContext> = (path, ctx) => ctx.get(path);
|
|
113
|
+
* const accessor: AccessorFn = (path) => tickContext.get(path);
|
|
115
114
|
*/
|
|
116
|
-
type AccessorFn
|
|
115
|
+
type AccessorFn = (path: string) => unknown;
|
|
117
116
|
/**
|
|
118
117
|
* Validation result
|
|
119
118
|
*/
|
|
@@ -143,13 +142,46 @@ interface CompileOptions<T = unknown> {
|
|
|
143
142
|
* Quando fornecido, substitui o acesso direto por propriedade.
|
|
144
143
|
*
|
|
145
144
|
* @example
|
|
146
|
-
*
|
|
147
|
-
* compile(expr, { accessor: (path, ctx) => ctx.get(path) })
|
|
145
|
+
* compile(expr, { accessor: (path) => ctx.get(path) })
|
|
148
146
|
*/
|
|
149
|
-
accessor?: AccessorFn
|
|
147
|
+
accessor?: AccessorFn;
|
|
150
148
|
/** Boundary definitions for compile-time interception */
|
|
151
149
|
boundaries?: BoundaryDef<T>[];
|
|
150
|
+
/** Wrapped handlers (ctx already bound) for $fn "namespace:method" calls */
|
|
151
|
+
handlers?: WrappedHandlers;
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Handler function — receives HandlerContext via `this` (bound at compile time).
|
|
155
|
+
* Must be a regular function or method shorthand (NOT arrow function).
|
|
156
|
+
* HandlerContext is defined in compiler.ts.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* {
|
|
160
|
+
* query: {
|
|
161
|
+
* find(key: string) { return this.scope.add(key, 1); }
|
|
162
|
+
* }
|
|
163
|
+
* }
|
|
164
|
+
*/
|
|
165
|
+
type HandlerFn = (...args: any[]) => any;
|
|
166
|
+
/**
|
|
167
|
+
* Handlers map — namespaces of handler methods.
|
|
168
|
+
* Provided by consumer in ExpressionCompiler constructor.
|
|
169
|
+
* Handlers access context via `this` (bound automatically via .bind()).
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* {
|
|
173
|
+
* query: {
|
|
174
|
+
* find(filter: string) { return this.scope.transform(filter); },
|
|
175
|
+
* findAll() { return this.handlers.other.list(); },
|
|
176
|
+
* },
|
|
177
|
+
* }
|
|
178
|
+
*/
|
|
179
|
+
type HandlersMap = Record<string, Record<string, HandlerFn>>;
|
|
180
|
+
/**
|
|
181
|
+
* Wrapped handlers (ctx already bound via .bind()).
|
|
182
|
+
* Used internally by both pipelines.
|
|
183
|
+
*/
|
|
184
|
+
type WrappedHandlers = Record<string, Record<string, (...args: any[]) => any>>;
|
|
153
185
|
/**
|
|
154
186
|
* Boundary definition for compile-time interception.
|
|
155
187
|
* Allows foreign DSLs and compilers to plug into the expression walk.
|
|
@@ -225,11 +257,13 @@ interface ExpressionCompilerOptions<T = unknown> {
|
|
|
225
257
|
/** Functions available to $fn expressions */
|
|
226
258
|
scope?: Scope;
|
|
227
259
|
/** Custom accessor for resolving paths */
|
|
228
|
-
accessor?: AccessorFn
|
|
260
|
+
accessor?: AccessorFn;
|
|
229
261
|
/** Max LRU cache size per mode (default: 1000) */
|
|
230
262
|
cacheSize?: number;
|
|
231
263
|
/** Boundary definitions for compile-time interception */
|
|
232
264
|
boundaries?: BoundaryDef<T>[];
|
|
265
|
+
/** Handler namespaces — services with injected context */
|
|
266
|
+
handlers?: HandlersMap;
|
|
233
267
|
}
|
|
234
268
|
interface JITOptions {
|
|
235
269
|
/** Use accessor to resolve paths instead of direct access */
|
|
@@ -237,10 +271,25 @@ interface JITOptions {
|
|
|
237
271
|
/** Prefix for paths that bypass accessor (direct access) */
|
|
238
272
|
lexicalPrefix?: string;
|
|
239
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Context available via `this` inside every handler (bound via .bind()).
|
|
276
|
+
* Created once per ExpressionCompiler instance — zero per-call overhead.
|
|
277
|
+
*/
|
|
278
|
+
interface HandlerContext<T = unknown> {
|
|
279
|
+
/** Custom accessor for resolving paths */
|
|
280
|
+
accessor?: AccessorFn;
|
|
281
|
+
/** All wrapped handlers — handlers can call other handlers */
|
|
282
|
+
handlers: WrappedHandlers;
|
|
283
|
+
/** Compiler instance — can compile sub-expressions */
|
|
284
|
+
compiler: ExpressionCompiler<T>;
|
|
285
|
+
/** Pure scope functions */
|
|
286
|
+
scope: Scope;
|
|
287
|
+
}
|
|
240
288
|
declare class ExpressionCompiler<T = unknown> {
|
|
241
289
|
private readonly scope;
|
|
242
290
|
private readonly accessor?;
|
|
243
291
|
private readonly boundaries?;
|
|
292
|
+
private readonly wrappedHandlers?;
|
|
244
293
|
private readonly cacheClosures;
|
|
245
294
|
private readonly cacheJIT;
|
|
246
295
|
constructor(options?: ExpressionCompilerOptions<T>);
|
|
@@ -248,6 +297,13 @@ declare class ExpressionCompiler<T = unknown> {
|
|
|
248
297
|
compile<R = unknown>(expr: Expression): CompiledExpression<T, R>;
|
|
249
298
|
/** Compile via JS code generation (JIT) */
|
|
250
299
|
jit<R = unknown>(expr: Expression, opts?: JITOptions): CompiledExpression<T, R>;
|
|
300
|
+
/**
|
|
301
|
+
* Wrap raw handlers with ctx via .bind().
|
|
302
|
+
* Creates ctx once, binds each handler method once.
|
|
303
|
+
* Resolves circular reference: ctx.handlers = wrappedHandlers.
|
|
304
|
+
* Handlers access ctx via `this`.
|
|
305
|
+
*/
|
|
306
|
+
private wrapHandlers;
|
|
251
307
|
/** Compile (closures) and execute in one step */
|
|
252
308
|
evaluate<R = unknown>(expr: Expression, data: T): R;
|
|
253
309
|
/** Compile (JIT) and execute in one step */
|
|
@@ -484,6 +540,8 @@ interface ValidateOptions {
|
|
|
484
540
|
scope?: Scope;
|
|
485
541
|
/** Boundary definitions — matched nodes skip validation */
|
|
486
542
|
boundaries?: BoundaryDef[];
|
|
543
|
+
/** Handlers map to validate handler names against (optional) */
|
|
544
|
+
handlers?: HandlersMap;
|
|
487
545
|
}
|
|
488
546
|
/**
|
|
489
547
|
* Validate expression structure
|
|
@@ -558,4 +616,4 @@ declare function isPure(expr: Expression): boolean;
|
|
|
558
616
|
*/
|
|
559
617
|
declare const VERSION = "3.0.0";
|
|
560
618
|
|
|
561
|
-
export { type AccessorFn, type ArrowExpr, type BoundaryDef, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCompiler, type ExpressionCompilerOptions, type FnExpr, type JITOptions, type Literal, type PipeExpr, type RefExpr, type Scope, type TransformFn, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, compilePath, extractDeps, get, hasDeps, hasWildcard, isArrow, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate };
|
|
619
|
+
export { type AccessorFn, type ArrowExpr, type BoundaryDef, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCompiler, type ExpressionCompilerOptions, type FnExpr, type HandlerContext, type HandlerFn, type HandlersMap, type JITOptions, type Literal, type PipeExpr, type RefExpr, type Scope, type TransformFn, type Transforms, VERSION, type ValidateOptions, type ValidationResult, type WrappedHandlers, assertValid, builders, compilePath, extractDeps, get, hasDeps, hasWildcard, isArrow, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate };
|
package/dist/index.d.ts
CHANGED
|
@@ -106,14 +106,13 @@ interface CompiledExpression<T = unknown, R = unknown> {
|
|
|
106
106
|
*/
|
|
107
107
|
type PathGetter<T = unknown> = (data: T) => unknown;
|
|
108
108
|
/**
|
|
109
|
-
* Accessor customizado para resolver paths
|
|
110
|
-
*
|
|
109
|
+
* Accessor customizado para resolver paths.
|
|
110
|
+
* Closure auto-suficiente — já sabe de onde ler.
|
|
111
111
|
*
|
|
112
112
|
* @example
|
|
113
|
-
*
|
|
114
|
-
* const accessor: AccessorFn<TickContext> = (path, ctx) => ctx.get(path);
|
|
113
|
+
* const accessor: AccessorFn = (path) => tickContext.get(path);
|
|
115
114
|
*/
|
|
116
|
-
type AccessorFn
|
|
115
|
+
type AccessorFn = (path: string) => unknown;
|
|
117
116
|
/**
|
|
118
117
|
* Validation result
|
|
119
118
|
*/
|
|
@@ -143,13 +142,46 @@ interface CompileOptions<T = unknown> {
|
|
|
143
142
|
* Quando fornecido, substitui o acesso direto por propriedade.
|
|
144
143
|
*
|
|
145
144
|
* @example
|
|
146
|
-
*
|
|
147
|
-
* compile(expr, { accessor: (path, ctx) => ctx.get(path) })
|
|
145
|
+
* compile(expr, { accessor: (path) => ctx.get(path) })
|
|
148
146
|
*/
|
|
149
|
-
accessor?: AccessorFn
|
|
147
|
+
accessor?: AccessorFn;
|
|
150
148
|
/** Boundary definitions for compile-time interception */
|
|
151
149
|
boundaries?: BoundaryDef<T>[];
|
|
150
|
+
/** Wrapped handlers (ctx already bound) for $fn "namespace:method" calls */
|
|
151
|
+
handlers?: WrappedHandlers;
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Handler function — receives HandlerContext via `this` (bound at compile time).
|
|
155
|
+
* Must be a regular function or method shorthand (NOT arrow function).
|
|
156
|
+
* HandlerContext is defined in compiler.ts.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* {
|
|
160
|
+
* query: {
|
|
161
|
+
* find(key: string) { return this.scope.add(key, 1); }
|
|
162
|
+
* }
|
|
163
|
+
* }
|
|
164
|
+
*/
|
|
165
|
+
type HandlerFn = (...args: any[]) => any;
|
|
166
|
+
/**
|
|
167
|
+
* Handlers map — namespaces of handler methods.
|
|
168
|
+
* Provided by consumer in ExpressionCompiler constructor.
|
|
169
|
+
* Handlers access context via `this` (bound automatically via .bind()).
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* {
|
|
173
|
+
* query: {
|
|
174
|
+
* find(filter: string) { return this.scope.transform(filter); },
|
|
175
|
+
* findAll() { return this.handlers.other.list(); },
|
|
176
|
+
* },
|
|
177
|
+
* }
|
|
178
|
+
*/
|
|
179
|
+
type HandlersMap = Record<string, Record<string, HandlerFn>>;
|
|
180
|
+
/**
|
|
181
|
+
* Wrapped handlers (ctx already bound via .bind()).
|
|
182
|
+
* Used internally by both pipelines.
|
|
183
|
+
*/
|
|
184
|
+
type WrappedHandlers = Record<string, Record<string, (...args: any[]) => any>>;
|
|
153
185
|
/**
|
|
154
186
|
* Boundary definition for compile-time interception.
|
|
155
187
|
* Allows foreign DSLs and compilers to plug into the expression walk.
|
|
@@ -225,11 +257,13 @@ interface ExpressionCompilerOptions<T = unknown> {
|
|
|
225
257
|
/** Functions available to $fn expressions */
|
|
226
258
|
scope?: Scope;
|
|
227
259
|
/** Custom accessor for resolving paths */
|
|
228
|
-
accessor?: AccessorFn
|
|
260
|
+
accessor?: AccessorFn;
|
|
229
261
|
/** Max LRU cache size per mode (default: 1000) */
|
|
230
262
|
cacheSize?: number;
|
|
231
263
|
/** Boundary definitions for compile-time interception */
|
|
232
264
|
boundaries?: BoundaryDef<T>[];
|
|
265
|
+
/** Handler namespaces — services with injected context */
|
|
266
|
+
handlers?: HandlersMap;
|
|
233
267
|
}
|
|
234
268
|
interface JITOptions {
|
|
235
269
|
/** Use accessor to resolve paths instead of direct access */
|
|
@@ -237,10 +271,25 @@ interface JITOptions {
|
|
|
237
271
|
/** Prefix for paths that bypass accessor (direct access) */
|
|
238
272
|
lexicalPrefix?: string;
|
|
239
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Context available via `this` inside every handler (bound via .bind()).
|
|
276
|
+
* Created once per ExpressionCompiler instance — zero per-call overhead.
|
|
277
|
+
*/
|
|
278
|
+
interface HandlerContext<T = unknown> {
|
|
279
|
+
/** Custom accessor for resolving paths */
|
|
280
|
+
accessor?: AccessorFn;
|
|
281
|
+
/** All wrapped handlers — handlers can call other handlers */
|
|
282
|
+
handlers: WrappedHandlers;
|
|
283
|
+
/** Compiler instance — can compile sub-expressions */
|
|
284
|
+
compiler: ExpressionCompiler<T>;
|
|
285
|
+
/** Pure scope functions */
|
|
286
|
+
scope: Scope;
|
|
287
|
+
}
|
|
240
288
|
declare class ExpressionCompiler<T = unknown> {
|
|
241
289
|
private readonly scope;
|
|
242
290
|
private readonly accessor?;
|
|
243
291
|
private readonly boundaries?;
|
|
292
|
+
private readonly wrappedHandlers?;
|
|
244
293
|
private readonly cacheClosures;
|
|
245
294
|
private readonly cacheJIT;
|
|
246
295
|
constructor(options?: ExpressionCompilerOptions<T>);
|
|
@@ -248,6 +297,13 @@ declare class ExpressionCompiler<T = unknown> {
|
|
|
248
297
|
compile<R = unknown>(expr: Expression): CompiledExpression<T, R>;
|
|
249
298
|
/** Compile via JS code generation (JIT) */
|
|
250
299
|
jit<R = unknown>(expr: Expression, opts?: JITOptions): CompiledExpression<T, R>;
|
|
300
|
+
/**
|
|
301
|
+
* Wrap raw handlers with ctx via .bind().
|
|
302
|
+
* Creates ctx once, binds each handler method once.
|
|
303
|
+
* Resolves circular reference: ctx.handlers = wrappedHandlers.
|
|
304
|
+
* Handlers access ctx via `this`.
|
|
305
|
+
*/
|
|
306
|
+
private wrapHandlers;
|
|
251
307
|
/** Compile (closures) and execute in one step */
|
|
252
308
|
evaluate<R = unknown>(expr: Expression, data: T): R;
|
|
253
309
|
/** Compile (JIT) and execute in one step */
|
|
@@ -484,6 +540,8 @@ interface ValidateOptions {
|
|
|
484
540
|
scope?: Scope;
|
|
485
541
|
/** Boundary definitions — matched nodes skip validation */
|
|
486
542
|
boundaries?: BoundaryDef[];
|
|
543
|
+
/** Handlers map to validate handler names against (optional) */
|
|
544
|
+
handlers?: HandlersMap;
|
|
487
545
|
}
|
|
488
546
|
/**
|
|
489
547
|
* Validate expression structure
|
|
@@ -558,4 +616,4 @@ declare function isPure(expr: Expression): boolean;
|
|
|
558
616
|
*/
|
|
559
617
|
declare const VERSION = "3.0.0";
|
|
560
618
|
|
|
561
|
-
export { type AccessorFn, type ArrowExpr, type BoundaryDef, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCompiler, type ExpressionCompilerOptions, type FnExpr, type JITOptions, type Literal, type PipeExpr, type RefExpr, type Scope, type TransformFn, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, compilePath, extractDeps, get, hasDeps, hasWildcard, isArrow, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate };
|
|
619
|
+
export { type AccessorFn, type ArrowExpr, type BoundaryDef, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCompiler, type ExpressionCompilerOptions, type FnExpr, type HandlerContext, type HandlerFn, type HandlersMap, type JITOptions, type Literal, type PipeExpr, type RefExpr, type Scope, type TransformFn, type Transforms, VERSION, type ValidateOptions, type ValidationResult, type WrappedHandlers, assertValid, builders, compilePath, extractDeps, get, hasDeps, hasWildcard, isArrow, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {generate,builders}from'omni-ast';var xn=Object.defineProperty;var Fn=(n,e)=>{for(var i in e)xn(n,i,{get:e[i],enumerable:true});};var _=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),y=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,T=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,C=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",b=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),$=n=>n!==null&&typeof n=="object"&&"$arrow"in n,E=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&_.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),h=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,Rn=n=>E(n)||h(n),J=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,r="left"in i&&"op"in i&&_.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!("$arrow"in i)&&!r&&!("logic"in i)}return false};var U=new Map;function N(n){let e=[],i=n.length,r=0,o="";for(;r<i;){let s=n[r];if(s===".")o&&(e.push({type:"key",value:o}),o=""),r++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),r++;let t=r;for(;r<i&&n[r]!=="]";)r++;let f=n.slice(t,r);if(r++,f==="*")e.push({type:"wildcard",value:"*"});else {let c=parseInt(f,10);e.push({type:"index",value:isNaN(c)?f:c});}}else o+=s,r++;}return o&&e.push({type:"key",value:o}),e}function X(n){return n.includes("[*]")}function G(n){let e=U.get(n);return e||(e=X(n)?jn(n):On(n),U.set(n,e),e)}function On(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=N(n),i=e.length;if(i===2){let[o,s]=e,t=o.value,f=s.value;return c=>c?.[t]?.[f]}if(i===3){let[o,s,t]=e,f=o.value,c=s.value,l=t.value;return a=>a?.[f]?.[c]?.[l]}let r=e.map(o=>o.value);return o=>{let s=o;for(let t=0;t<i&&s!=null;t++)s=s[r[t]];return s}}function jn(n){let e=N(n),i=[];for(let r=0;r<e.length;r++)e[r].type==="wildcard"&&i.push(r);return i.length===1?Nn(e,i[0]):vn(e,i)}function Nn(n,e){let i=n.slice(0,e).map(t=>t.value),r=n.slice(e+1).map(t=>t.value),o=i.length,s=r.length;if(s===0){if(o===1){let t=i[0];return f=>f?.[t]}return t=>{let f=t;for(let c=0;c<o&&f!=null;c++)f=f[i[c]];return f}}if(s===1){let t=r[0];if(o===1){let f=i[0];return c=>{let l=c?.[f];if(Array.isArray(l))return l.map(a=>a?.[t])}}return f=>{let c=f;for(let l=0;l<o&&c!=null;l++)c=c[i[l]];if(Array.isArray(c))return c.map(l=>l?.[t])}}return t=>{let f=t;for(let c=0;c<o&&f!=null;c++)f=f[i[c]];if(Array.isArray(f))return f.map(c=>{let l=c;for(let a=0;a<s&&l!=null;a++)l=l[r[a]];return l})}}function vn(n,e){let i=[],r=0;for(let s=0;s<e.length;s++){let t=e[s],f=s===e.length-1,c=n.slice(r,t).map(l=>l.value);c.length>0&&i.push({type:"access",keys:c}),i.push({type:f?"map":"flatMap",keys:[]}),r=t+1;}let o=n.slice(r).map(s=>s.value);return s=>{let t=s;for(let f of i){if(t==null)return;if(f.type==="access")for(let c of f.keys){if(t==null)return;t=t[c];}else if(f.type==="flatMap"){if(!Array.isArray(t))return;t=t.flatMap(c=>{let l=c;return Array.isArray(l)?l:[l]});}else if(f.type==="map"){if(!Array.isArray(t))return;o.length>0&&(t=t.map(c=>{let l=c;for(let a of o){if(l==null)return;l=l[a];}return l}));}}return t}}function Dn(n,e){return G(e)(n)}function W(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function R(n){let e=new Set;return A(n,e),Array.from(e)}function A(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o=0;o<n.length;o++)A(n[o],e);return}if(y(n)){e.add(W(n.$));return}if(T(n)){if(typeof n.$if=="string"){let o=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(W(o));}else A(n.$if,e);A(n.then,e),n.else!==void 0&&A(n.else,e);return}if(b(n)){for(let o=0;o<n.$pipe.length;o++)A(n.$pipe[o],e);return}if(C(n)){if(n.args)for(let o=0;o<n.args.length;o++)A(n.args[o],e);return}if($(n)){let o=new Set;A(n.$arrow,o);let s=new Set(n.args??[]);for(let t of o){let f=t.split(".")[0].split("[")[0];s.has(f)||e.add(t);}return}if(E(n)){A(n.left,e),n.right!==void 0&&A(n.right,e);return}if(h(n)){for(let o=0;o<n.conditions.length;o++)A(n.conditions[o],e);return}let i=n,r=Object.keys(i);for(let o=0;o<r.length;o++)A(i[r[o]],e);}function In(n){return R(n).length>0}function Bn(n){return R(n).length===0}var Z;function P(n){Z=n;}function O(n,e,i,r){return Z(n,e,i,r)}function nn(n,e){return e?i=>e(n,i):G(n)}function en(n,e){return nn(n.$,e)}function tn(n,e,i,r){let o;if(typeof n.$if=="string"){let f=n.$if.startsWith("!")?n.$if.slice(1):n.$if,c=nn(f,i);o=n.$if.startsWith("!")?a=>!c(a):a=>!!c(a);}else {let f=O(n.$if,e,i,r);o=c=>!!f(c);}let s=O(n.then,e,i,r),t=n.else!==void 0?O(n.else,e,i,r):()=>{};return f=>o(f)?s(f):t(f)}function on(n,e,i,r){let o=n.$pipe;if(o.length===0)return ()=>{};if(o.length===1)return O(o[0],e,i,r);let s=O(o[0],e,i,r),t=o.slice(1).map(c=>O(c,e,i,r)),f=t.length;if(f===1){let[c]=t;return l=>{let a=s(l),p=c(l);return typeof p=="function"?p(a):p}}if(f===2){let[c,l]=t;return a=>{let p=s(a),d=c(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(f===3){let[c,l,a]=t;return p=>{let d=s(p),m=c(p);return d=typeof m=="function"?m(d):m,m=l(p),d=typeof m=="function"?m(d):m,m=a(p),typeof m=="function"?m(d):m}}return c=>{let l=s(c);for(let a=0;a<f;a++){let p=t[a](c);l=typeof p=="function"?p(l):p;}return l}}function rn(n,e,i,r){let o=n.$fn,s=n.args;if(s===void 0)return ()=>{let c=e[o];if(!c)throw new Error(`Function not found in scope: ${o}`);return c};let t=s.map(c=>O(c,e,i,r)),f=t.length;if(f===0)return ()=>{let c=e[o];if(!c)throw new Error(`Function not found in scope: ${o}`);return c()};if(f===1){let[c]=t;return l=>{let a=e[o];if(!a)throw new Error(`Function not found in scope: ${o}`);return a(c(l))}}if(f===2){let[c,l]=t;return a=>{let p=e[o];if(!p)throw new Error(`Function not found in scope: ${o}`);return p(c(a),l(a))}}if(f===3){let[c,l,a]=t;return p=>{let d=e[o];if(!d)throw new Error(`Function not found in scope: ${o}`);return d(c(p),l(p),a(p))}}return c=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l(...t.map(a=>a(c)))}}function sn(n,e,i,r){let o=n.args??[];if(o.length===0){let t=O(n.$arrow,e,i,r);return f=>()=>t(f)}let s=O(n.$arrow,e,i,r);return t=>(...f)=>{let c={};for(let a=0;a<o.length;a++)c[o[a]]=f[a];let l={...t,...c};return s(l)}}var fn;function cn(n){fn=n;}function L(n,e,i,r){return fn(n,e,i,r)}function un(n,e,i,r){let o=L(n.left,e,i,r),s=n.right!==void 0?L(n.right,e,i,r):()=>{};switch(n.op){case "eq":return t=>o(t)===s(t);case "neq":return t=>o(t)!==s(t);case "gt":return t=>o(t)>s(t);case "gte":return t=>o(t)>=s(t);case "lt":return t=>o(t)<s(t);case "lte":return t=>o(t)<=s(t);case "in":return t=>{let f=s(t);return Array.isArray(f)&&f.includes(o(t))};case "notIn":return t=>{let f=s(t);return !Array.isArray(f)||!f.includes(o(t))};case "contains":return t=>{let f=o(t);return Array.isArray(f)&&f.includes(s(t))};case "notContains":return t=>{let f=o(t);return !Array.isArray(f)||!f.includes(s(t))};case "exists":return t=>o(t)!==void 0;case "notExists":return t=>o(t)===void 0;case "matches":return t=>{let f=o(t),c=s(t);return typeof f!="string"||typeof c!="string"?false:new RegExp(c).test(f)};case "notMatches":return t=>{let f=o(t),c=s(t);return typeof f!="string"||typeof c!="string"?true:!new RegExp(c).test(f)};case "startsWith":return t=>{let f=o(t),c=s(t);return typeof f=="string"&&typeof c=="string"&&f.startsWith(c)};case "endsWith":return t=>{let f=o(t),c=s(t);return typeof f=="string"&&typeof c=="string"&&f.endsWith(c)}}}function ln(n,e,i,r){let o=n.conditions.map(t=>L(t,e,i,r)),s=o.length;if(s===1)return t=>!!o[0](t);if(s===2){let[t,f]=o;return n.logic==="AND"?c=>!!t(c)&&!!f(c):c=>!!t(c)||!!f(c)}if(s===3){let[t,f,c]=o;return n.logic==="AND"?l=>!!t(l)&&!!f(l)&&!!c(l):l=>!!t(l)||!!f(l)||!!c(l)}return n.logic==="AND"?t=>{for(let f=0;f<s;f++)if(!o[f](t))return false;return true}:t=>{for(let f=0;f<s;f++)if(o[f](t))return true;return false}}function zn(n){return JSON.stringify(n)}function an(n,e={}){let i=e.scope??{},r=e.accessor,o=e.boundaries,s=I(n,i,r,o),t=R(n),f=zn(n);return {fn:s,deps:t,hash:f}}function I(n,e,i,r){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let o=n.map(s=>I(s,e,i,r));return s=>o.map(t=>t(s))}if(y(n))return en(n,i);if(T(n))return tn(n,e,i,r);if(b(n))return on(n,e,i,r);if(C(n))return rn(n,e,i,r);if($(n))return sn(n,e,i,r);if(E(n))return un(n,e,i,r);if(h(n))return ln(n,e,i,r);if(r?.length){let o=n;for(let s of r)if(s.check(o))return s.handle(o)}if(J(n)){let o=n,s=Object.keys(o),t=s.map(f=>I(o[f],e,i,r));return f=>{let c={};for(let l=0;l<s.length;l++)c[s[l]]=t[l](f);return c}}return ()=>n}P(I);cn(I);function pn(n){let e=new Set;return w(n,e),e}function w(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)w(r,e);return}if(y(n))return;if(T(n)){w(n.$if,e),w(n.then,e),n.else!==void 0&&w(n.else,e);return}if(b(n)){for(let r of n.$pipe)w(r,e);return}if(C(n)){if(e.add(n.$fn),n.args)for(let r of n.args)w(r,e);return}if($(n)){w(n.$arrow,e);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&w(n.left,e),n.right!==void 0&&typeof n.right=="object"&&w(n.right,e);return}if(h(n)){for(let r of n.conditions)w(r,e);return}let i=n;for(let r of Object.keys(i))w(i[r],e);}function dn(n){let e=new Set;for(let i of n){let r=i.indexOf("."),o=i.indexOf("["),s=i.length;r!==-1&&(s=Math.min(s,r)),o!==-1&&(s=Math.min(s,o));let t=i.slice(0,s);t&&e.add(t);}return e}var _n="data",Gn="scope",B="__",Wn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function gn(n,e={}){let{dataParam:i=_n,scopeParam:r=Gn,noPrefixes:o=false,useAccessor:s=false,lexicalPrefix:t}=e;return g(n,i,r,o,s,t)}function g(n,e,i,r,o,s,t){if(n===null)return builders.literal(null);if(typeof n=="string")return builders.literal(n);if(typeof n=="number")return builders.literal(n);if(typeof n=="boolean")return builders.literal(n);if(Array.isArray(n))return builders.arrayExpression(n.map(f=>g(f,e,i,r,o,s,t)));if(y(n))return Mn(n.$,e,r,o,s,t);if(T(n))return Vn(n,e,i,r,o,s,t);if(b(n))return Kn(n.$pipe,e,i,r,o,s,t);if(C(n))return qn(n,e,i,r,o,s,t);if($(n))return Ln(n,e,i,r,o,s,t);if(E(n))return Yn(n,e,i,r,o,s,t);if(h(n))return Hn(n,e,i,r,o,s,t);if(typeof n=="object"&&"$__b"in n){let f=n.$__b;return builders.callExpression(builders.memberExpression(builders.memberExpression(builders.identifier(B),builders.identifier("b")),builders.literal(f),true,false),[builders.identifier(e)])}if(typeof n=="object"){let c=Object.entries(n).map(([l,a])=>builders.property(builders.identifier(l),g(a,e,i,r,o,s,t)));return builders.objectExpression(c)}return builders.literal(null)}var V="accessor";function q(n,e){return e?n===e||n.startsWith(e+"."):false}function K(n){let e=n.indexOf("."),i=n.indexOf("["),r=n.length;return e!==-1&&(r=Math.min(r,e)),i!==-1&&(r=Math.min(r,i)),n.slice(0,r)}function Mn(n,e,i,r,o,s){return s&&s.has(K(n))?x(n,e,true):r?q(n,o)?x(n,e,true):builders.callExpression(builders.identifier(V),[builders.literal(n),builders.identifier(e)]):n.includes("[*]")?Jn(n,e,i):x(n,e,i)}function x(n,e,i){let r=N(n);if(r.length===0)return i?builders.identifier("undefined"):builders.identifier(e);let o;if(i){let s=r[0];o=builders.identifier(s.value);for(let t=1;t<r.length;t++){let f=r[t];f.type==="key"?o=builders.memberExpression(o,builders.identifier(f.value),false,true):o=builders.memberExpression(o,builders.literal(f.value),true,true);}}else {o=builders.identifier(e);for(let s of r)s.type==="key"?o=builders.memberExpression(o,builders.identifier(s.value),false,true):o=builders.memberExpression(o,builders.literal(s.value),true,true);}return o}function Jn(n,e,i){let r=n.indexOf("[*]"),o=n.slice(0,r),s=n.slice(r+3),t;if(o?t=x(o,e,i):t=i?builders.identifier("undefined"):builders.identifier(e),!s||s==="")return t;if(s.includes("[*]"))return yn(t,s);let f="_i",c=s.startsWith(".")?s.slice(1):s,l=builders.identifier(f);if(c){let a=N(c);for(let p of a)p.type==="key"?l=builders.memberExpression(l,builders.identifier(p.value),false,true):l=builders.memberExpression(l,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(t,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],l)])}function yn(n,e){let i=e.indexOf("[*]"),r=e.slice(0,i),o=e.slice(i+3),s="_i",t=r.startsWith(".")?r.slice(1):r,f=builders.identifier(s);if(t){let l=N(t);for(let a of l)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}if(o.includes("[*]")){let l=yn(f,o);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],l)])}let c=o.startsWith(".")?o.slice(1):o;if(c){let l=N(c);for(let a of l)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],f)])}function Ln(n,e,i,r,o,s,t){let f=n.args??[],c=new Set(t);for(let p of f)c.add(p);let l=g(n.$arrow,e,i,r,o,s,c),a=f.map(p=>builders.identifier(p));return builders.arrowFunctionExpression(a,l)}function Vn(n,e,i,r,o,s,t){let f;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;t&&t.has(K(p))?d=x(p,e,true):o?q(p,s)?d=x(p,e,true):d=builders.callExpression(builders.identifier(V),[builders.literal(p),builders.identifier(e)]):d=x(p,e,r),f=a?builders.unaryExpression("!",d):d;}else f=g(n.$if,e,i,r,o,s,t);let c=g(n.then,e,i,r,o,s,t),l=n.else!==void 0?g(n.else,e,i,r,o,s,t):builders.identifier("undefined");return builders.conditionalExpression(f,c,l)}function qn(n,e,i,r,o,s,t){let f=r?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(i),builders.identifier(n.$fn),false,false);if(n.args===void 0)return f;let c=n.args.map(l=>g(l,e,i,r,o,s,t));return builders.callExpression(f,c)}function Kn(n,e,i,r,o,s,t){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return g(n[0],e,i,r,o,s,t);let f=g(n[0],e,i,r,o,s,t);for(let c=1;c<n.length;c++){let l=g(n[c],e,i,r,o,s,t);f=builders.callExpression(l,[f]);}return f}function mn(n,e,i,r,o,s,t){if(y(n)){let f=n.$;return t&&t.has(K(f))?x(f,e,true):o?q(f,s)?x(f,e,true):builders.callExpression(builders.identifier(V),[builders.literal(f),builders.identifier(e)]):x(f,e,r)}return g(n,e,i,r,o,s,t)}function Yn(n,e,i,r,o,s,t){let f=mn(n.left,e,i,r,o,s,t),c=n.right!==void 0?mn(n.right,e,i,r,o,s,t):builders.literal(null),l=Wn[n.op];if(l)return builders.binaryExpression(l,f,c);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(c,builders.identifier("includes")),[f]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(c,builders.identifier("includes")),[f]));case "contains":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes"),false,true),[c]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes"),false,true),[c]));case "exists":return builders.binaryExpression("!=",f,builders.literal(null));case "notExists":return builders.binaryExpression("==",f,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[c]),builders.identifier("test")),[f]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[c]),builders.identifier("test")),[f]));case "startsWith":return builders.callExpression(builders.memberExpression(f,builders.identifier("startsWith"),false,true),[c]);case "endsWith":return builders.callExpression(builders.memberExpression(f,builders.identifier("endsWith"),false,true),[c]);default:return builders.binaryExpression("===",f,c)}}function Hn(n,e,i,r,o,s,t){let{logic:f,conditions:c}=n,l=f==="AND"?"&&":"||";if(c.length===0)return builders.literal(f==="AND");if(c.length===1)return g(c[0],e,i,r,o,s,t);let a=g(c[0],e,i,r,o,s,t);for(let p=1;p<c.length;p++){let d=g(c[p],e,i,r,o,s,t);a=builders.logicalExpression(l,a,d);}return a}function En(n,e,i,r,o,s=0){let t=gn(n,{noPrefixes:true,useAccessor:r,lexicalPrefix:o}),f=generate(t),c="";r?o&&(c=`const{${o}}=data??{};`):e.size>0&&(c=`const{${[...e].join(",")}}=data??{};`);let l=i.size>0?`const{${[...i].join(",")}}=scope;`:"",a=r||s>0,p="";if(a){let D=[];r&&D.push("accessor"),s>0&&D.push("b"),p=`const{${D.join(",")}}=${B};`;}let d=i.size>0,m;d&&a?m=`scope,${B}`:d?m="scope":a?m=`_,${B}`:m="";let v=`${c}return ${f}`,j;return m?j=`(function(${m}){${l}${p}return function(data){${v}}})`:j=`(function(){return function(data){${v}}})`,j}function Un(n){return JSON.stringify(n)}function S(n,e,i){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>S(s,e,i));if(y(n))return n;if(T(n))return {$if:typeof n.$if=="string"?n.$if:S(n.$if,e,i),then:S(n.then,e,i),...n.else!==void 0?{else:S(n.else,e,i)}:{}};if(b(n))return {$pipe:n.$pipe.map(s=>S(s,e,i))};if(C(n))return {$fn:n.$fn,...n.args!==void 0?{args:n.args.map(s=>S(s,e,i))}:{}};if($(n))return {$arrow:S(n.$arrow,e,i),...n.args?{args:n.args}:{},...n.schema?{schema:n.schema}:{}};if(E(n))return {left:S(n.left,e,i),op:n.op,...n.right!==void 0?{right:S(n.right,e,i)}:{}};if(h(n))return {logic:n.logic,conditions:n.conditions.map(s=>S(s,e,i))};let r=n;for(let s of e)if(s.check(r)){let t=i.length;return i.push(s.handle(r)),{$__b:t}}let o={};for(let s of Object.keys(r))o[s]=S(r[s],e,i);return o}function hn(n,e={}){let{scope:i={},accessor:r,returnCode:o=false,useAccessor:s=false,lexicalPrefix:t,boundaries:f}=e,c=n,l=[];if(f?.length){let F=[];c=S(n,f,F),l=F;}let a=R(c),p=dn(a),d=pn(c),m=Un(n),v=En(c,p,d,s,t,l.length);if(o)return {code:v,deps:a,hash:m,dataRoots:[...p],scopeFns:[...d]};let j={};s&&r&&(j.accessor=r),l.length>0&&(j.b=l);let D=Object.keys(j).length>0,Q;try{let F=new Function(`return ${v}`)();Q=D?F(i,j):F(i);}catch(F){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${F instanceof Error?F.message:String(F)}`)}return {fn:Q,deps:a,hash:m}}function Tn(n,e){return M(n,e)}function M(n,e){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>M(s,e));let i=n,r=Object.keys(i);for(let s of r)if(s in e){let t=e[s](i);if(typeof t=="object"&&t!==null&&s in t)throw new Error(`Transform "${s}" returned object with same key \u2014 infinite loop`);return M(t,e)}let o={};for(let s of r)o[s]=M(i[s],e);return o}var z=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}getOrCompile(e,i){let r=JSON.stringify(e),o=this.cache.get(r);if(o)return this.cache.delete(r),this.cache.set(r,o),o;let s=i();if(this.cache.size>=this._maxSize){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}return this.cache.set(r,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}};var Y=class{constructor(e={}){this.scope=e.scope??{},this.accessor=e.accessor,this.boundaries=e.boundaries,this.cacheClosures=new z(e.cacheSize??1e3),this.cacheJIT=new z(e.cacheSize??1e3);}compile(e){return this.cacheClosures.getOrCompile(e,()=>an(e,{scope:this.scope,accessor:this.accessor,boundaries:this.boundaries}))}jit(e,i){return this.cacheJIT.getOrCompile(e,()=>hn(e,{scope:this.scope,accessor:this.accessor,useAccessor:i?.useAccessor,lexicalPrefix:i?.lexicalPrefix,boundaries:this.boundaries}))}evaluate(e,i){return this.compile(e).fn(i)}evaluateJIT(e,i,r){return this.jit(e,r).fn(i)}normalize(e,i){return Tn(e,i)}extractDeps(e){return R(e)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}};var kn={};Fn(kn,{$:()=>Cn,$arrow:()=>wn,$call:()=>Sn,$cond:()=>An,$fn:()=>bn,$if:()=>Pn,$pipe:()=>$n,arrow:()=>te,call:()=>ie,cond:()=>ee,fn:()=>Zn,pipe:()=>ne,ref:()=>Xn});function Cn(n){return {$:n}}var Xn=Cn;function bn(n,e){return e===void 0?{$fn:n}:{$fn:n,args:e}}var Zn=bn;function Pn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function $n(...n){return {$pipe:n}}var ne=$n;function An(n,e,i){return i===void 0?{left:n,op:e}:{left:n,op:e,right:i}}var ee=An;function wn(n,e){return e===void 0||e.length===0?{$arrow:n}:{$arrow:n,args:e}}var te=wn;function Sn(n,e=[]){return {$fn:n,args:e}}var ie=Sn;function H(n,e="root",i={}){let r=[];return k(n,e,r,i),{valid:r.length===0,errors:r}}function k(n,e,i,r){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t=0;t<n.length;t++)k(n[t],`${e}[${t}]`,i,r);return}if(y(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(T(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):k(n.$if,`${e}.$if`,i,r),k(n.then,`${e}.then`,i,r),n.else!==void 0&&k(n.else,`${e}.else`,i,r);return}if(b(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let t=0;t<n.$pipe.length;t++)k(n.$pipe[t],`${e}.$pipe[${t}]`,i,r);return}if(C(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(r.scope&&!(n.$fn in r.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let t=0;t<n.args.length;t++)k(n.args[t],`${e}.args[${t}]`,i,r);return}if($(n)){if(k(n.$arrow,`${e}.$arrow`,i,r),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array of strings`);else for(let t of n.args)(typeof t!="string"||!t)&&i.push(`${e}.args: each arg must be a non-empty string`);return}if(E(n)){_.has(n.op)||i.push(`${e}: invalid operator "${n.op}"`),k(n.left,`${e}.left`,i,r),n.right!==void 0&&k(n.right,`${e}.right`,i,r);return}if(h(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&i.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){i.push(`${e}.conditions: must be an array`);return}for(let t=0;t<n.conditions.length;t++)k(n.conditions[t],`${e}.conditions[${t}]`,i,r);return}if(r.boundaries?.length){let t=n;for(let f of r.boundaries)if(f.check(t))return}let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let f=s[t];k(o[f],`${e}.${f}`,i,r);}}function oe(n,e={}){let i=H(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function re(n,e={}){return H(n,"root",e).valid}var Ve="3.0.0";export{Y as ExpressionCompiler,Ve as VERSION,oe as assertValid,kn as builders,G as compilePath,R as extractDeps,Dn as get,In as hasDeps,X as hasWildcard,$ as isArrow,E as isCondition,Rn as isConditionExpr,h as isConditionGroup,T as isConditional,C as isFn,J as isLiteral,b as isPipe,Bn as isPure,y as isRef,re as isValid,W as normalizePath,H as validate};
|
|
1
|
+
import {generate,builders}from'omni-ast';var jn=Object.defineProperty;var Nn=(n,e)=>{for(var t in e)jn(n,t,{get:e[t],enumerable:true});};var G=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),g=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,y=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,C=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",T=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),b=n=>n!==null&&typeof n=="object"&&"$arrow"in n,h=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&G.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),E=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,vn=n=>h(n)||E(n),L=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let t=n,i="left"in t&&"op"in t&&G.has(t.op);return !("$"in t)&&!("$if"in t)&&!("$fn"in t)&&!("$pipe"in t)&&!("$arrow"in t)&&!i&&!("logic"in t)}return false};var Z=new Map;function I(n){let e=[],t=n.length,i=0,s="";for(;i<t;){let o=n[i];if(o===".")s&&(e.push({type:"key",value:s}),s=""),i++;else if(o==="["){s&&(e.push({type:"key",value:s}),s=""),i++;let r=i;for(;i<t&&n[i]!=="]";)i++;let f=n.slice(r,i);if(i++,f==="*")e.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);e.push({type:"index",value:isNaN(l)?f:l});}}else s+=o,i++;}return s&&e.push({type:"key",value:s}),e}function P(n){return n.includes("[*]")}function _(n){let e=Z.get(n);return e||(e=P(n)?Dn(n):In(n),Z.set(n,e),e)}function In(n){if(!n.includes(".")&&!n.includes("["))return s=>s?.[n];let e=I(n),t=e.length;if(t===2){let[s,o]=e,r=s.value,f=o.value;return l=>l?.[r]?.[f]}if(t===3){let[s,o,r]=e,f=s.value,l=o.value,u=r.value;return a=>a?.[f]?.[l]?.[u]}let i=e.map(s=>s.value);return s=>{let o=s;for(let r=0;r<t&&o!=null;r++)o=o[i[r]];return o}}function Dn(n){let e=I(n),t=[];for(let i=0;i<e.length;i++)e[i].type==="wildcard"&&t.push(i);return t.length===1?Wn(e,t[0]):Mn(e,t)}function Wn(n,e){let t=n.slice(0,e).map(r=>r.value),i=n.slice(e+1).map(r=>r.value),s=t.length,o=i.length;if(o===0){if(s===1){let r=t[0];return f=>f?.[r]}return r=>{let f=r;for(let l=0;l<s&&f!=null;l++)f=f[t[l]];return f}}if(o===1){let r=i[0];if(s===1){let f=t[0];return l=>{let u=l?.[f];if(Array.isArray(u))return u.map(a=>a?.[r])}}return f=>{let l=f;for(let u=0;u<s&&l!=null;u++)l=l[t[u]];if(Array.isArray(l))return l.map(u=>u?.[r])}}return r=>{let f=r;for(let l=0;l<s&&f!=null;l++)f=f[t[l]];if(Array.isArray(f))return f.map(l=>{let u=l;for(let a=0;a<o&&u!=null;a++)u=u[i[a]];return u})}}function Mn(n,e){let t=[],i=0;for(let o=0;o<e.length;o++){let r=e[o],f=o===e.length-1,l=n.slice(i,r).map(u=>u.value);l.length>0&&t.push({type:"access",keys:l}),t.push({type:f?"map":"flatMap",keys:[]}),i=r+1;}let s=n.slice(i).map(o=>o.value);return o=>{let r=o;for(let f of t){if(r==null)return;if(f.type==="access")for(let l of f.keys){if(r==null)return;r=r[l];}else if(f.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(l=>{let u=l;return Array.isArray(u)?u:[u]});}else if(f.type==="map"){if(!Array.isArray(r))return;s.length>0&&(r=r.map(l=>{let u=l;for(let a of s){if(u==null)return;u=u[a];}return u}));}}return r}}function _n(n,e){return _(e)(n)}function J(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function v(n){let e=new Set;return S(n,e),Array.from(e)}function S(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)S(n[s],e);return}if(g(n)){e.add(J(n.$));return}if(y(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(J(s));}else S(n.$if,e);S(n.then,e),n.else!==void 0&&S(n.else,e);return}if(T(n)){for(let s=0;s<n.$pipe.length;s++)S(n.$pipe[s],e);return}if(C(n)){if(n.args)for(let s=0;s<n.args.length;s++)S(n.args[s],e);return}if(b(n)){let s=new Set;S(n.$arrow,s);let o=new Set(n.args??[]);for(let r of s){let f=r.split(".")[0].split("[")[0];o.has(f)||e.add(r);}return}if(h(n)){S(n.left,e),n.right!==void 0&&S(n.right,e);return}if(E(n)){for(let s=0;s<n.conditions.length;s++)S(n.conditions[s],e);return}let t=n,i=Object.keys(t);for(let s=0;s<i.length;s++)S(t[i[s]],e);}function zn(n){return v(n).length>0}function Hn(n){return v(n).length===0}var nn;function en(n){nn=n;}function F(n,e,t){return nn(n,e,t)}function Gn(n){let e=n.indexOf("."),t=n.indexOf("["),i=n.length;return e!==-1&&(i=Math.min(i,e)),t!==-1&&(i=Math.min(i,t)),n.slice(0,i)}function tn(n,e,t){return t&&t.has(Gn(n))?_(n):e?()=>e(n):_(n)}function on(n,e,t){return tn(n.$,e.accessor,t)}function rn(n,e,t){let i;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=tn(r,e.accessor,t);i=n.$if.startsWith("!")?u=>!f(u):u=>!!f(u);}else {let r=F(n.$if,e,t);i=f=>!!r(f);}let s=F(n.then,e,t),o=n.else!==void 0?F(n.else,e,t):()=>{};return r=>i(r)?s(r):o(r)}function sn(n,e,t){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return F(i[0],e,t);let s=F(i[0],e,t),o=i.slice(1).map(f=>F(f,e,t)),r=o.length;if(r===1){let[f]=o;return l=>{let u=s(l),a=f(l);return typeof a=="function"?a(u):a}}if(r===2){let[f,l]=o;return u=>{let a=s(u),p=f(u);return a=typeof p=="function"?p(a):p,p=l(u),typeof p=="function"?p(a):p}}if(r===3){let[f,l,u]=o;return a=>{let p=s(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=u(a),typeof d=="function"?d(p):d}}return f=>{let l=s(f);for(let u=0;u<r;u++){let a=o[u](f);l=typeof a=="function"?a(l):a;}return l}}function fn(n,e,t){let i=n.$fn,s=i.indexOf(":");if(s!==-1)return Jn(i,s,n.args,e,t);let{scope:o}=e,r=n.args;if(r===void 0)return ()=>{let u=o[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let f=r.map(u=>F(u,e,t)),l=f.length;if(l===0)return ()=>{let u=o[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(l===1){let[u]=f;return a=>{let p=o[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(a))}}if(l===2){let[u,a]=f;return p=>{let d=o[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),a(p))}}if(l===3){let[u,a,p]=f;return d=>{let $=o[i];if(!$)throw new Error(`Function not found in scope: ${i}`);return $(u(d),a(d),p(d))}}return u=>{let a=o[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(...f.map(p=>p(u)))}}function Jn(n,e,t,i,s){let o=n.slice(0,e),r=n.slice(e+1),f=i.handlers?.[o]?.[r];if(!f)throw new Error(`Handler not found: ${n}`);if(t===void 0)return ()=>f;let l=t.map(a=>F(a,i,s)),u=l.length;if(u===0)return ()=>f();if(u===1){let[a]=l;return p=>f(a(p))}if(u===2){let[a,p]=l;return d=>f(a(d),p(d))}if(u===3){let[a,p,d]=l;return $=>f(a($),p($),d($))}return a=>f(...l.map(p=>p(a)))}function cn(n,e,t){let i=n.args??[];if(i.length===0){let r=F(n.$arrow,e,t);return f=>()=>r(f)}let s=new Set(t);for(let r of i)s.add(r);let o=F(n.$arrow,e,s);return r=>(...f)=>{let l={};for(let a=0;a<i.length;a++)l[i[a]]=f[a];let u={...r,...l};return o(u)}}var ln;function un(n){ln=n;}function V(n,e,t){return ln(n,e,t)}function an(n,e,t){let i=V(n.left,e,t),s=n.right!==void 0?V(n.right,e,t):()=>{};switch(n.op){case "eq":return o=>i(o)===s(o);case "neq":return o=>i(o)!==s(o);case "gt":return o=>i(o)>s(o);case "gte":return o=>i(o)>=s(o);case "lt":return o=>i(o)<s(o);case "lte":return o=>i(o)<=s(o);case "in":return o=>{let r=s(o);return Array.isArray(r)&&r.includes(i(o))};case "notIn":return o=>{let r=s(o);return !Array.isArray(r)||!r.includes(i(o))};case "contains":return o=>{let r=i(o);return Array.isArray(r)&&r.includes(s(o))};case "notContains":return o=>{let r=i(o);return !Array.isArray(r)||!r.includes(s(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let r=i(o),f=s(o);return typeof r!="string"||typeof f!="string"?false:new RegExp(f).test(r)};case "notMatches":return o=>{let r=i(o),f=s(o);return typeof r!="string"||typeof f!="string"?true:!new RegExp(f).test(r)};case "startsWith":return o=>{let r=i(o),f=s(o);return typeof r=="string"&&typeof f=="string"&&r.startsWith(f)};case "endsWith":return o=>{let r=i(o),f=s(o);return typeof r=="string"&&typeof f=="string"&&r.endsWith(f)}}}function pn(n,e,t){let i=n.conditions.map(o=>V(o,e,t)),s=i.length;if(s===1)return o=>!!i[0](o);if(s===2){let[o,r]=i;return n.logic==="AND"?f=>!!o(f)&&!!r(f):f=>!!o(f)||!!r(f)}if(s===3){let[o,r,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!r(l)&&!!f(l):l=>!!o(l)||!!r(l)||!!f(l)}return n.logic==="AND"?o=>{for(let r=0;r<s;r++)if(!i[r](o))return false;return true}:o=>{for(let r=0;r<s;r++)if(i[r](o))return true;return false}}function Bn(n){return JSON.stringify(n)}function dn(n,e={}){let t={scope:e.scope??{},accessor:e.accessor,boundaries:e.boundaries,handlers:e.handlers},i=z(n,t),s=v(n),o=Bn(n);return {fn:i,deps:s,hash:o}}function z(n,e,t){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>z(s,e,t));return s=>i.map(o=>o(s))}if(g(n))return on(n,e,t);if(y(n))return rn(n,e,t);if(T(n))return sn(n,e,t);if(C(n))return fn(n,e,t);if(b(n))return cn(n,e,t);if(h(n))return an(n,e,t);if(E(n))return pn(n,e,t);if(e.boundaries?.length){let i=n;for(let s of e.boundaries)if(s.check(i))return s.handle(i)}if(L(n)){let i=n,s=Object.keys(i),o=s.map(r=>z(i[r],e,t));return r=>{let f={};for(let l=0;l<s.length;l++)f[s[l]]=o[l](r);return f}}return ()=>n}en(z);un(z);function mn(n){let e=new Set;return w(n,e),e}function w(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)w(i,e);return}if(g(n))return;if(y(n)){w(n.$if,e),w(n.then,e),n.else!==void 0&&w(n.else,e);return}if(T(n)){for(let i of n.$pipe)w(i,e);return}if(C(n)){if(n.$fn.includes(":")||e.add(n.$fn),n.args)for(let i of n.args)w(i,e);return}if(b(n)){w(n.$arrow,e);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&w(n.left,e),n.right!==void 0&&typeof n.right=="object"&&w(n.right,e);return}if(E(n)){for(let i of n.conditions)w(i,e);return}let t=n;for(let i of Object.keys(t))w(t[i],e);}function gn(n){return k(n)}function k(n){if(n===null||typeof n!="object")return false;if(Array.isArray(n)){for(let t of n)if(k(t))return true;return false}if(g(n))return false;if(y(n))return k(n.$if)||k(n.then)||n.else!==void 0&&k(n.else);if(T(n)){for(let t of n.$pipe)if(k(t))return true;return false}if(C(n)){if(n.$fn.includes(":"))return true;if(n.args){for(let t of n.args)if(k(t))return true}return false}if(b(n))return k(n.$arrow);if(h(n))return k(n.left)||n.right!==void 0&&k(n.right);if(E(n)){for(let t of n.conditions)if(k(t))return true;return false}let e=n;for(let t of Object.keys(e))if(k(e[t]))return true;return false}function hn(n){let e=new Set;for(let t of n){let i=t.indexOf("."),s=t.indexOf("["),o=t.length;i!==-1&&(o=Math.min(o,i)),s!==-1&&(o=Math.min(o,s));let r=t.slice(0,o);r&&e.add(r);}return e}var Ln="data",Vn="scope",W="__",qn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function yn(n,e={}){let{dataParam:t=Ln,scopeParam:i=Vn,noPrefixes:s=false,useAccessor:o=false,lexicalPrefix:r}=e;return m(n,t,i,s,o,r)}function m(n,e,t,i,s,o,r){if(n===null)return builders.literal(null);if(typeof n=="string")return builders.literal(n);if(typeof n=="number")return builders.literal(n);if(typeof n=="boolean")return builders.literal(n);if(Array.isArray(n))return builders.arrayExpression(n.map(f=>m(f,e,t,i,s,o,r)));if(g(n))return Kn(n.$,e,i,s,o,r);if(y(n))return Un(n,e,t,i,s,o,r);if(T(n))return Pn(n.$pipe,e,t,i,s,o,r);if(C(n))return Xn(n,e,t,i,s,o,r);if(b(n))return Qn(n,e,t,i,s,o,r);if(h(n))return ne(n,e,t,i,s,o,r);if(E(n))return ee(n,e,t,i,s,o,r);if(typeof n=="object"&&"$__b"in n){let f=n.$__b;return builders.callExpression(builders.memberExpression(builders.memberExpression(builders.identifier(W),builders.identifier("b")),builders.literal(f),true,false),[builders.identifier(e)])}if(typeof n=="object"){let l=Object.entries(n).map(([u,a])=>builders.property(builders.identifier(u),m(a,e,t,i,s,o,r)));return builders.objectExpression(l)}return builders.literal(null)}var q="accessor";function K(n,e){return e?n===e||n.startsWith(e+"."):false}function Y(n){let e=n.indexOf("."),t=n.indexOf("["),i=n.length;return e!==-1&&(i=Math.min(i,e)),t!==-1&&(i=Math.min(i,t)),n.slice(0,i)}function Kn(n,e,t,i,s,o){return o&&o.has(Y(n))?O(n,e,true):i?K(n,s)?O(n,e,true):builders.callExpression(builders.identifier(q),[builders.literal(n)]):n.includes("[*]")?Yn(n,e,t):O(n,e,t)}function O(n,e,t){let i=I(n);if(i.length===0)return t?builders.identifier("undefined"):builders.identifier(e);let s;if(t){let o=i[0];s=builders.identifier(o.value);for(let r=1;r<i.length;r++){let f=i[r];f.type==="key"?s=builders.memberExpression(s,builders.identifier(f.value),false,true):s=builders.memberExpression(s,builders.literal(f.value),true,true);}}else {s=builders.identifier(e);for(let o of i)o.type==="key"?s=builders.memberExpression(s,builders.identifier(o.value),false,true):s=builders.memberExpression(s,builders.literal(o.value),true,true);}return s}function Yn(n,e,t){let i=n.indexOf("[*]"),s=n.slice(0,i),o=n.slice(i+3),r;if(s?r=O(s,e,t):r=t?builders.identifier("undefined"):builders.identifier(e),!o||o==="")return r;if(o.includes("[*]"))return Cn(r,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,u=builders.identifier(f);if(l){let a=I(l);for(let p of a)p.type==="key"?u=builders.memberExpression(u,builders.identifier(p.value),false,true):u=builders.memberExpression(u,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(r,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],u)])}function Cn(n,e){let t=e.indexOf("[*]"),i=e.slice(0,t),s=e.slice(t+3),o="_i",r=i.startsWith(".")?i.slice(1):i,f=builders.identifier(o);if(r){let u=I(r);for(let a of u)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}if(s.includes("[*]")){let u=Cn(f,s);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],u)])}let l=s.startsWith(".")?s.slice(1):s;if(l){let u=I(l);for(let a of u)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],f)])}function Qn(n,e,t,i,s,o,r){let f=n.args??[],l=new Set(r);for(let p of f)l.add(p);let u=m(n.$arrow,e,t,i,s,o,l),a=f.map(p=>builders.identifier(p));return builders.arrowFunctionExpression(a,u)}function Un(n,e,t,i,s,o,r){let f;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;r&&r.has(Y(p))?d=O(p,e,true):s?K(p,o)?d=O(p,e,true):d=builders.callExpression(builders.identifier(q),[builders.literal(p)]):d=O(p,e,i),f=a?builders.unaryExpression("!",d):d;}else f=m(n.$if,e,t,i,s,o,r);let l=m(n.then,e,t,i,s,o,r),u=n.else!==void 0?m(n.else,e,t,i,s,o,r):builders.identifier("undefined");return builders.conditionalExpression(f,l,u)}function Xn(n,e,t,i,s,o,r){let f=n.$fn.indexOf(":");if(f!==-1)return Zn(n,f,e,t,i,s,o,r);let l=i?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(t),builders.identifier(n.$fn),false,false);if(n.args===void 0)return l;let u=n.args.map(a=>m(a,e,t,i,s,o,r));return builders.callExpression(l,u)}function Zn(n,e,t,i,s,o,r,f){let l=n.$fn.slice(0,e),u=n.$fn.slice(e+1),a=s?builders.memberExpression(builders.memberExpression(builders.identifier("h"),builders.identifier(l)),builders.identifier(u)):builders.memberExpression(builders.memberExpression(builders.memberExpression(builders.identifier(W),builders.identifier("h")),builders.identifier(l)),builders.identifier(u));if(n.args===void 0)return a;let p=n.args.map(d=>m(d,t,i,s,o,r,f));return builders.callExpression(a,p)}function Pn(n,e,t,i,s,o,r){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return m(n[0],e,t,i,s,o,r);let f=m(n[0],e,t,i,s,o,r);for(let l=1;l<n.length;l++){let u=m(n[l],e,t,i,s,o,r);f=builders.callExpression(u,[f]);}return f}function En(n,e,t,i,s,o,r){if(g(n)){let f=n.$;return r&&r.has(Y(f))?O(f,e,true):s?K(f,o)?O(f,e,true):builders.callExpression(builders.identifier(q),[builders.literal(f)]):O(f,e,i)}return m(n,e,t,i,s,o,r)}function ne(n,e,t,i,s,o,r){let f=En(n.left,e,t,i,s,o,r),l=n.right!==void 0?En(n.right,e,t,i,s,o,r):builders.literal(null),u=qn[n.op];if(u)return builders.binaryExpression(u,f,l);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(l,builders.identifier("includes")),[f]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(l,builders.identifier("includes")),[f]));case "contains":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes"),false,true),[l]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes"),false,true),[l]));case "exists":return builders.binaryExpression("!=",f,builders.literal(null));case "notExists":return builders.binaryExpression("==",f,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[l]),builders.identifier("test")),[f]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[l]),builders.identifier("test")),[f]));case "startsWith":return builders.callExpression(builders.memberExpression(f,builders.identifier("startsWith"),false,true),[l]);case "endsWith":return builders.callExpression(builders.memberExpression(f,builders.identifier("endsWith"),false,true),[l]);default:return builders.binaryExpression("===",f,l)}}function ee(n,e,t,i,s,o,r){let{logic:f,conditions:l}=n,u=f==="AND"?"&&":"||";if(l.length===0)return builders.literal(f==="AND");if(l.length===1)return m(l[0],e,t,i,s,o,r);let a=m(l[0],e,t,i,s,o,r);for(let p=1;p<l.length;p++){let d=m(l[p],e,t,i,s,o,r);a=builders.logicalExpression(u,a,d);}return a}function Tn(n,e,t,i,s,o=0,r=false){let f=yn(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),l=generate(f),u="";i?s&&(u=`const{${s}}=data??{};`):e.size>0&&(u=`const{${[...e].join(",")}}=data??{};`);let a=t.size>0?`const{${[...t].join(",")}}=scope;`:"",p=i||o>0||r,d="";if(p){let x=[];i&&x.push("accessor"),o>0&&x.push("b"),r&&x.push("h"),d=`const{${x.join(",")}}=${W};`;}let $=t.size>0,j;$&&p?j=`scope,${W}`:$?j="scope":p?j=`_,${W}`:j="";let M=`${u}return ${l}`,D;return j?D=`(function(${j}){${a}${d}return function(data){${M}}})`:D=`(function(){return function(data){${M}}})`,D}function ie(n){return JSON.stringify(n)}function A(n,e,t){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>A(o,e,t));if(g(n))return n;if(y(n))return {$if:typeof n.$if=="string"?n.$if:A(n.$if,e,t),then:A(n.then,e,t),...n.else!==void 0?{else:A(n.else,e,t)}:{}};if(T(n))return {$pipe:n.$pipe.map(o=>A(o,e,t))};if(C(n))return {$fn:n.$fn,...n.args!==void 0?{args:n.args.map(o=>A(o,e,t))}:{}};if(b(n))return {$arrow:A(n.$arrow,e,t),...n.args?{args:n.args}:{},...n.schema?{schema:n.schema}:{}};if(h(n))return {left:A(n.left,e,t),op:n.op,...n.right!==void 0?{right:A(n.right,e,t)}:{}};if(E(n))return {logic:n.logic,conditions:n.conditions.map(o=>A(o,e,t))};let i=n;for(let o of e)if(o.check(i)){let r=t.length;return t.push(o.handle(i)),{$__b:r}}let s={};for(let o of Object.keys(i))s[o]=A(i[o],e,t);return s}function bn(n,e={}){let{scope:t={},accessor:i,returnCode:s=false,useAccessor:o=false,lexicalPrefix:r,boundaries:f,handlers:l}=e,u=n,a=[];if(f?.length){let N=[];u=A(n,f,N),a=N;}let p=v(u),d=hn(p),$=mn(u),j=l?gn(u):false,M=ie(n),D=Tn(u,d,$,o,r,a.length,j);if(s)return {code:D,deps:p,hash:M,dataRoots:[...d],scopeFns:[...$]};let x={};o&&i&&(x.accessor=i),a.length>0&&(x.b=a),j&&l&&(x.h=l);let On=Object.keys(x).length>0,X;try{let N=new Function(`return ${D}`)();X=On?N(t,x):N(t);}catch(N){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${N instanceof Error?N.message:String(N)}`)}return {fn:X,deps:p,hash:M}}function $n(n,e){return B(n,e)}function B(n,e){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>B(o,e));let t=n,i=Object.keys(t);for(let o of i)if(o in e){let r=e[o](t);if(typeof r=="object"&&r!==null&&o in r)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return B(r,e)}let s={};for(let o of i)s[o]=B(t[o],e);return s}var H=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}getOrCompile(e,t){let i=JSON.stringify(e),s=this.cache.get(i);if(s)return this.cache.delete(i),this.cache.set(i,s),s;let o=t();if(this.cache.size>=this._maxSize){let r=this.cache.keys().next().value;r&&this.cache.delete(r);}return this.cache.set(i,o),o}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}}};var Q=class{constructor(e={}){this.scope=e.scope??{},this.accessor=e.accessor,this.boundaries=e.boundaries,this.cacheClosures=new H(e.cacheSize??1e3),this.cacheJIT=new H(e.cacheSize??1e3),e.handlers&&(this.wrappedHandlers=this.wrapHandlers(e.handlers));}compile(e){return this.cacheClosures.getOrCompile(e,()=>dn(e,{scope:this.scope,accessor:this.accessor,boundaries:this.boundaries,handlers:this.wrappedHandlers}))}jit(e,t){return this.cacheJIT.getOrCompile(e,()=>bn(e,{scope:this.scope,accessor:this.accessor,useAccessor:t?.useAccessor,lexicalPrefix:t?.lexicalPrefix,boundaries:this.boundaries,handlers:this.wrappedHandlers}))}wrapHandlers(e){let t={},i={};for(let s of Object.keys(e)){i[s]={};for(let o of Object.keys(e[s]))i[s][o]=e[s][o].bind(t);}return t.accessor=this.accessor,t.handlers=i,t.compiler=this,t.scope=this.scope,i}evaluate(e,t){return this.compile(e).fn(t)}evaluateJIT(e,t,i){return this.jit(e,i).fn(t)}normalize(e,t){return $n(e,t)}extractDeps(e){return v(e)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}};var Fn={};Nn(Fn,{$:()=>Sn,$arrow:()=>Rn,$call:()=>xn,$cond:()=>An,$fn:()=>wn,$if:()=>se,$pipe:()=>kn,arrow:()=>le,call:()=>ue,cond:()=>ce,fn:()=>re,pipe:()=>fe,ref:()=>oe});function Sn(n){return {$:n}}var oe=Sn;function wn(n,e){return e===void 0?{$fn:n}:{$fn:n,args:e}}var re=wn;function se(n,e,t){return t===void 0?{$if:n,then:e}:{$if:n,then:e,else:t}}function kn(...n){return {$pipe:n}}var fe=kn;function An(n,e,t){return t===void 0?{left:n,op:e}:{left:n,op:e,right:t}}var ce=An;function Rn(n,e){return e===void 0||e.length===0?{$arrow:n}:{$arrow:n,args:e}}var le=Rn;function xn(n,e=[]){return {$fn:n,args:e}}var ue=xn;function U(n,e="root",t={}){let i=[];return R(n,e,i,t),{valid:i.length===0,errors:i}}function R(n,e,t,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)R(n[r],`${e}[${r}]`,t,i);return}if(g(n)){(!n.$||typeof n.$!="string")&&t.push(`${e}: invalid reference, $ must be non-empty string`);return}if(y(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||t.push(`${e}.$if: empty path in string shorthand`):R(n.$if,`${e}.$if`,t,i),R(n.then,`${e}.then`,t,i),n.else!==void 0&&R(n.else,`${e}.else`,t,i);return}if(T(n)){if(!Array.isArray(n.$pipe)){t.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){t.push(`${e}.$pipe: must have at least one element`);return}for(let r=0;r<n.$pipe.length;r++)R(n.$pipe[r],`${e}.$pipe[${r}]`,t,i);return}if(C(n)){if(!n.$fn||typeof n.$fn!="string"){t.push(`${e}: invalid function, $fn must be non-empty string`);return}let r=n.$fn.indexOf(":");if(r!==-1){let f=n.$fn.slice(0,r),l=n.$fn.slice(r+1);!f||!l||l.includes(":")?t.push(`${e}: invalid handler format "${n.$fn}", expected "namespace:method"`):i.handlers&&!i.handlers[f]?.[l]&&t.push(`${e}: handler "${n.$fn}" not found`);}else i.scope&&!(n.$fn in i.scope)&&t.push(`${e}: function "${n.$fn}" not found in scope`);if(n.args!==void 0)if(!Array.isArray(n.args))t.push(`${e}.args: must be an array`);else for(let f=0;f<n.args.length;f++)R(n.args[f],`${e}.args[${f}]`,t,i);return}if(b(n)){if(R(n.$arrow,`${e}.$arrow`,t,i),n.args!==void 0)if(!Array.isArray(n.args))t.push(`${e}.args: must be an array of strings`);else for(let r of n.args)(typeof r!="string"||!r)&&t.push(`${e}.args: each arg must be a non-empty string`);return}if(h(n)){G.has(n.op)||t.push(`${e}: invalid operator "${n.op}"`),R(n.left,`${e}.left`,t,i),n.right!==void 0&&R(n.right,`${e}.right`,t,i);return}if(E(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&t.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){t.push(`${e}.conditions: must be an array`);return}for(let r=0;r<n.conditions.length;r++)R(n.conditions[r],`${e}.conditions[${r}]`,t,i);return}if(i.boundaries?.length){let r=n;for(let f of i.boundaries)if(f.check(r))return}let s=n,o=Object.keys(s);for(let r=0;r<o.length;r++){let f=o[r];R(s[f],`${e}.${f}`,t,i);}}function ae(n,e={}){let t=U(n,"root",e);if(!t.valid)throw new Error(`Invalid expression: ${t.errors.join("; ")}`)}function pe(n,e={}){return U(n,"root",e).valid}var Xe="3.0.0";export{Q as ExpressionCompiler,Xe as VERSION,ae as assertValid,Fn as builders,_ as compilePath,v as extractDeps,_n as get,zn as hasDeps,P as hasWildcard,b as isArrow,h as isCondition,vn as isConditionExpr,E as isConditionGroup,y as isConditional,C as isFn,L as isLiteral,T as isPipe,Hn as isPure,g as isRef,pe as isValid,J as normalizePath,U as validate};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statedelta-libs/expressions",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "JSON DSL compiler for optimized functions - StateDelta expression engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"test": "vitest run",
|
|
53
53
|
"test:watch": "vitest",
|
|
54
54
|
"test:coverage": "vitest run --coverage",
|
|
55
|
-
"typecheck": "tsc --noEmit",
|
|
55
|
+
"typecheck": "tsc --noEmit && eslint src/ tests/",
|
|
56
56
|
"clean": "rm -rf dist",
|
|
57
57
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
58
58
|
"format:check": "prettier --check \"src/**/*.ts\""
|