@statedelta-libs/expressions 3.1.0 → 3.2.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 +82 -7
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +245 -16
- package/dist/index.d.ts +245 -16
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,9 +40,12 @@ const compiled = compiler.compile({
|
|
|
40
40
|
]
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
+
// Bind ao contexto → BoundFn callable
|
|
44
|
+
const fn = compiled.bind();
|
|
45
|
+
|
|
43
46
|
// Executa milhões de vezes
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
fn({ items: [{ active: true, price: 10 }, { active: false, price: 20 }] });
|
|
48
|
+
fn.deps; // ["items"]
|
|
46
49
|
```
|
|
47
50
|
|
|
48
51
|
## Os 5 Primitivos
|
|
@@ -68,6 +71,70 @@ compiler.jit(expr); // JIT — compilação lenta, ~25-27M ops/s de exec
|
|
|
68
71
|
|
|
69
72
|
**JIT** gera código JavaScript via AST e `new Function()`. Ideal para hot paths executados muitas vezes. Break-even em ~8 execuções.
|
|
70
73
|
|
|
74
|
+
## 3 Fases: Compile → Bind → Run
|
|
75
|
+
|
|
76
|
+
Compilação e contexto são independentes. Compile uma vez, bind N vezes com contextos diferentes — zero recompilação.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const compiler = new ExpressionCompiler({ scope });
|
|
80
|
+
|
|
81
|
+
// Fase 1: Compile (pesado — uma vez) → Artifact
|
|
82
|
+
const artifact = compiler.jit(expr);
|
|
83
|
+
|
|
84
|
+
// Fase 2: Bind (barato — só DI) → BoundFn callable
|
|
85
|
+
const forTenant1 = artifact.bind({ scope, accessor: tenant1Accessor });
|
|
86
|
+
const forTenant2 = artifact.bind({ scope, accessor: tenant2Accessor });
|
|
87
|
+
|
|
88
|
+
// Fase 3: Run
|
|
89
|
+
forTenant1(data); // usa accessor do tenant 1
|
|
90
|
+
forTenant2(data); // usa accessor do tenant 2
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`BoundFn` é callable direto — sem `.fn`. Funciona com `.map()`, `.filter()` e qualquer API que espera funções. Metadata como propriedades:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const fn = compiler.compile(expr).bind();
|
|
97
|
+
fn(data); // executa
|
|
98
|
+
fn.deps; // paths observados
|
|
99
|
+
fn.hash; // hash estrutural
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### DI-first — construtor sem contexto
|
|
103
|
+
|
|
104
|
+
O compilador funciona sem contexto no construtor. Contexto é injetado via `bind()`:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const compiler = new ExpressionCompiler(); // sem scope/accessor/handlers
|
|
108
|
+
|
|
109
|
+
const artifact = compiler.compile(expr);
|
|
110
|
+
const fn = artifact.bind({ scope, accessor });
|
|
111
|
+
fn(data);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Compile cache compartilhável
|
|
115
|
+
|
|
116
|
+
`CompileCache` armazena artefatos context-free compartilháveis entre instâncias:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { ExpressionCompiler, CompileCache } from '@statedelta-libs/expressions';
|
|
120
|
+
|
|
121
|
+
const sharedCache = new CompileCache(5000);
|
|
122
|
+
|
|
123
|
+
const tenantA = new ExpressionCompiler({ scope: scopeA, compileCache: sharedCache });
|
|
124
|
+
const tenantB = new ExpressionCompiler({ scope: scopeB, compileCache: sharedCache });
|
|
125
|
+
|
|
126
|
+
// Mesma expressão compilada uma vez, bind por tenant
|
|
127
|
+
tenantA.jit(expr).bind()(data); // compile cache miss → compile + bind
|
|
128
|
+
tenantB.jit(expr).bind()(data); // compile cache hit → só bind
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Cenários habilitados:**
|
|
132
|
+
|
|
133
|
+
- **Multi-tenant** — `CompileCache` compartilhado, bind por tenant
|
|
134
|
+
- **Hot-swap de accessor por tick** — compile uma vez, bind a cada tick
|
|
135
|
+
- **Testing** — mesmo artifact, scope mockado via bind
|
|
136
|
+
- **Lazy bind** — compile no boot, bind sob demanda
|
|
137
|
+
|
|
71
138
|
## Extensibilidade
|
|
72
139
|
|
|
73
140
|
### Scope — funções disponíveis
|
|
@@ -100,7 +167,7 @@ compiler.compile(pure);
|
|
|
100
167
|
|
|
101
168
|
### Boundaries — compiladores externos
|
|
102
169
|
|
|
103
|
-
Intercepta nós **durante** a compilação e terceiriza para outro algoritmo. Para quando o nó precisa de um compilador completamente diferente.
|
|
170
|
+
Intercepta nós **durante** a compilação e terceiriza para outro algoritmo. Para quando o nó precisa de um compilador completamente diferente. Boundaries são **compile-time** — ficam fixos entre `bind()` calls.
|
|
104
171
|
|
|
105
172
|
```typescript
|
|
106
173
|
import type { BoundaryDef } from '@statedelta-libs/expressions';
|
|
@@ -139,6 +206,7 @@ O handler é uma closure auto-suficiente — captura o que precisa (outros compi
|
|
|
139
206
|
| **Quando roda** | Antes da compilação | Durante a compilação (no walk) |
|
|
140
207
|
| **Retorno** | `Expression` (DSL puro) | `CompiledFn` (função pronta) |
|
|
141
208
|
| **Uso típico** | Sugar syntax | DSL estrangeiro, `$raw`, rule engines |
|
|
209
|
+
| **bind()** | N/A | Fixo (compile-time) |
|
|
142
210
|
|
|
143
211
|
### Handlers — services com contexto
|
|
144
212
|
|
|
@@ -206,6 +274,7 @@ handlers: { query: { find: (key) => { this.scope... } } }
|
|
|
206
274
|
| **DSL** | `{ $fn: "add", args: [...] }` | `{ $fn: "query:find", args: [...] }` |
|
|
207
275
|
| **Binding** | Nenhum | `.bind(ctx)` uma vez no construtor |
|
|
208
276
|
| **Overhead** | Zero | Zero (ctx e bindings criados uma vez) |
|
|
277
|
+
| **bind()** | Trocável via `ctx.scope` | Trocável via `ctx.handlers` |
|
|
209
278
|
|
|
210
279
|
Zero overhead quando nenhum handler é registrado.
|
|
211
280
|
|
|
@@ -216,16 +285,22 @@ const compiler = new ExpressionCompiler({
|
|
|
216
285
|
scope,
|
|
217
286
|
accessor: (path) => tickContext.get(path), // closure auto-suficiente
|
|
218
287
|
});
|
|
288
|
+
|
|
289
|
+
// Hot-swap por tick via bind()
|
|
290
|
+
const artifact = compiler.jit(expr, { useAccessor: true });
|
|
291
|
+
const fn = artifact.bind({
|
|
292
|
+
scope,
|
|
293
|
+
accessor: (path) => newTickContext.get(path),
|
|
294
|
+
});
|
|
219
295
|
```
|
|
220
296
|
|
|
221
297
|
## Documentação
|
|
222
298
|
|
|
223
299
|
| Doc | Conteúdo |
|
|
224
300
|
|-----|----------|
|
|
225
|
-
| [
|
|
226
|
-
| [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) |
|
|
227
|
-
| [docs/
|
|
228
|
-
| [docs/JIT.md](docs/JIT.md) | Internals do pipeline JIT |
|
|
301
|
+
| [EXPRESSIONS-API.md](EXPRESSIONS-API.md) | Referência completa da API pública |
|
|
302
|
+
| [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) | Arquitetura interna (IR, backends, cache) |
|
|
303
|
+
| [docs/TEMPLATE.md](docs/TEMPLATE.md) | Template compiler (compileDefinition) |
|
|
229
304
|
|
|
230
305
|
## Licença
|
|
231
306
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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;
|
|
1
|
+
'use strict';var omniAst=require('omni-ast');var be=Object.defineProperty;var Te=(e,n)=>{for(var t in n)be(e,t,{get:n[t],enumerable:true});};function j(e,n,t){let i=e;return Object.defineProperty(i,"deps",{value:n,enumerable:true,configurable:true}),Object.defineProperty(i,"hash",{value:t,enumerable:true,configurable:true}),i}var K=Symbol("$");function P(e){return e!==null&&typeof e=="object"&&e[K]===true}function y(e,n){if(e===null||typeof e!="object")return n.rawPrimitive(e);if(P(e))return Ae(e,n);if(Array.isArray(e)){let o=e.map(r=>y(r,n));return n.rawArray(e,o)}let t=e,i=Object.keys(t),s=new Array(i.length);for(let o=0;o<i.length;o++){let r=i[o];s[o]=[r,y(t[r],n)];}return n.rawObject(t,s)}function Ae(e,n){switch(e.t){case "d":return n.data(e);case "a":return n.arg(e);case "x":return n.accessor(e);case "c":{let t=e,i=t.a.map(s=>y(s,n));return n.call(t,i)}case "r":return n.ref(e);case "ch":{let t=e,i=t.a.map(s=>y(s,n));return n.callH(t,i)}case "rh":return n.refH(e);case "?":{let t=e;return n.cond(t,y(t.test,n),y(t.then,n),y(t.else,n))}case "|":{let t=e;return n.pipe(t,y(t.head,n),t.steps.map(i=>y(i,n)))}case "=>":{let t=e;return n.arrow(t,y(t.body,n))}case "!":{let t=e;return n.not(t,y(t.v,n))}case "cmp":{let t=e;return n.compare(t,y(t.l,n),y(t.r,n))}case "lg":{let t=e;return n.logic(t,t.c.map(i=>y(i,n)))}case "v":return n.literal(e);case "s":return n.static_(e);case "b":return n.boundary(e);default:throw new Error(`Unknown IR node type: ${e.t}`)}}function ne(e,n){return {data:()=>e,arg:()=>e,accessor:()=>e,call:()=>e,ref:()=>e,callH:()=>e,refH:()=>e,cond:()=>e,pipe:()=>e,arrow:()=>e,not:()=>e,compare:()=>e,logic:()=>e,literal:()=>e,static_:()=>e,boundary:()=>e,rawPrimitive:()=>e,rawObject:()=>e,rawArray:()=>e,...n}}var te=new Map;function S(e){let n=[],t=e.length,i=0,s="";for(;i<t;){let o=e[i];if(o===".")s&&(n.push({type:"key",value:s}),s=""),i++;else if(o==="["){s&&(n.push({type:"key",value:s}),s=""),i++;let r=i;for(;i<t&&e[i]!=="]";)i++;let a=e.slice(r,i);if(i++,a==="*")n.push({type:"wildcard",value:"*"});else {let l=parseInt(a,10);n.push({type:"index",value:isNaN(l)?a:l});}}else s+=o,i++;}return s&&n.push({type:"key",value:s}),n}function N(e){return e.includes("[*]")}function H(e){let n=te.get(e);return n||(n=N(e)?Ie(e):we(e),te.set(e,n),n)}function we(e){if(!e.includes(".")&&!e.includes("["))return s=>s?.[e];let n=S(e),t=n.length;if(t===2){let[s,o]=n,r=s.value,a=o.value;return l=>l?.[r]?.[a]}if(t===3){let[s,o,r]=n,a=s.value,l=o.value,c=r.value;return u=>u?.[a]?.[l]?.[c]}let i=n.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 Ie(e){let n=S(e),t=[];for(let i=0;i<n.length;i++)n[i].type==="wildcard"&&t.push(i);return t.length===1?Ee(n,t[0]):$e(n,t)}function Ee(e,n){let t=e.slice(0,n).map(r=>r.value),i=e.slice(n+1).map(r=>r.value),s=t.length,o=i.length;if(o===0){if(s===1){let r=t[0];return a=>a?.[r]}return r=>{let a=r;for(let l=0;l<s&&a!=null;l++)a=a[t[l]];return a}}if(o===1){let r=i[0];if(s===1){let a=t[0];return l=>{let c=l?.[a];if(Array.isArray(c))return c.map(u=>u?.[r])}}return a=>{let l=a;for(let c=0;c<s&&l!=null;c++)l=l[t[c]];if(Array.isArray(l))return l.map(c=>c?.[r])}}return r=>{let a=r;for(let l=0;l<s&&a!=null;l++)a=a[t[l]];if(Array.isArray(a))return a.map(l=>{let c=l;for(let u=0;u<o&&c!=null;u++)c=c[i[u]];return c})}}function $e(e,n){let t=[],i=0;for(let o=0;o<n.length;o++){let r=n[o],a=o===n.length-1,l=e.slice(i,r).map(c=>c.value);l.length>0&&t.push({type:"access",keys:l}),t.push({type:a?"map":"flatMap",keys:[]}),i=r+1;}let s=e.slice(i).map(o=>o.value);return o=>{let r=o;for(let a of t){if(r==null)return;if(a.type==="access")for(let l of a.keys){if(r==null)return;r=r[l];}else if(a.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(l=>{let c=l;return Array.isArray(c)?c:[c]});}else if(a.type==="map"){if(!Array.isArray(r))return;s.length>0&&(r=r.map(l=>{let c=l;for(let u of s){if(c==null)return;c=c[u];}return c}));}}return r}}function Fe(e,n){return H(n)(e)}function A(e){let n=e.indexOf("[*]");return n===-1?e:e.slice(0,n)}function re(e){let{scope:n,accessor:t,handlers:i,boundaryFns:s}=e;return {data(o){return H(o.p)},arg(o){return H(o.p)},accessor(o){let r=t,a=o.p;return ()=>r(a)},call(o,r){let a=n[o.n];if(!a){let c=o.n;return ()=>{throw new Error(`Function not found in scope: ${c}`)}}let l=r.length;if(l===0)return ()=>a();if(l===1){let[c]=r;return u=>a(c(u))}if(l===2){let[c,u]=r;return f=>a(c(f),u(f))}if(l===3){let[c,u,f]=r;return d=>a(c(d),u(d),f(d))}return c=>a(...r.map(u=>u(c)))},ref(o){let r=n[o.n];if(!r){let a=o.n;return ()=>{throw new Error(`Function not found in scope: ${a}`)}}return ()=>r},callH(o,r){let a=i?.[o.ns]?.[o.m];if(!a)throw new Error(`Handler not found: ${o.ns}:${o.m}`);let l=r.length;if(l===0)return ()=>a();if(l===1){let[c]=r;return u=>a(c(u))}if(l===2){let[c,u]=r;return f=>a(c(f),u(f))}if(l===3){let[c,u,f]=r;return d=>a(c(d),u(d),f(d))}return c=>a(...r.map(u=>u(c)))},refH(o){let r=i?.[o.ns]?.[o.m];if(!r)throw new Error(`Handler not found: ${o.ns}:${o.m}`);return ()=>r},cond(o,r,a,l){return c=>r(c)?a(c):l(c)},pipe(o,r,a){let l=a.length;if(l===1){let[c]=a;return u=>{let f=r(u),d=c(u);return typeof d=="function"?d(f):d}}if(l===2){let[c,u]=a;return f=>{let d=r(f),m=c(f);return d=typeof m=="function"?m(d):m,m=u(f),typeof m=="function"?m(d):m}}if(l===3){let[c,u,f]=a;return d=>{let m=r(d),h=c(d);return m=typeof h=="function"?h(m):h,h=u(d),m=typeof h=="function"?h(m):h,h=f(d),typeof h=="function"?h(m):h}}return c=>{let u=r(c);for(let f=0;f<l;f++){let d=a[f](c);u=typeof d=="function"?d(u):d;}return u}},arrow(o,r){let a=o.params;if(a.length===0)return c=>()=>r(c);let l=a.length;return c=>(...u)=>{let f={...c};for(let d=0;d<l;d++)f[a[d]]=u[d];return r(f)}},not(o,r){return a=>!r(a)},compare(o,r,a){switch(o.op){case "eq":return l=>r(l)===a(l);case "neq":return l=>r(l)!==a(l);case "gt":return l=>r(l)>a(l);case "gte":return l=>r(l)>=a(l);case "lt":return l=>r(l)<a(l);case "lte":return l=>r(l)<=a(l);case "in":return l=>{let c=a(l);return Array.isArray(c)&&c.includes(r(l))};case "notIn":return l=>{let c=a(l);return !Array.isArray(c)||!c.includes(r(l))};case "contains":return l=>{let c=r(l);return Array.isArray(c)&&c.includes(a(l))};case "notContains":return l=>{let c=r(l);return !Array.isArray(c)||!c.includes(a(l))};case "exists":return l=>r(l)!==void 0;case "notExists":return l=>r(l)===void 0;case "matches":return l=>{let c=r(l),u=a(l);return typeof c!="string"||typeof u!="string"?false:new RegExp(u).test(c)};case "notMatches":return l=>{let c=r(l),u=a(l);return typeof c!="string"||typeof u!="string"?true:!new RegExp(u).test(c)};case "startsWith":return l=>{let c=r(l),u=a(l);return typeof c=="string"&&typeof u=="string"&&c.startsWith(u)};case "endsWith":return l=>{let c=r(l),u=a(l);return typeof c=="string"&&typeof u=="string"&&c.endsWith(u)};default:return l=>r(l)===a(l)}},logic(o,r){let a=r.length;if(a===0)return ()=>o.op==="AND";if(a===1)return l=>!!r[0](l);if(a===2){let[l,c]=r;return o.op==="AND"?u=>!!l(u)&&!!c(u):u=>!!l(u)||!!c(u)}if(a===3){let[l,c,u]=r;return o.op==="AND"?f=>!!l(f)&&!!c(f)&&!!u(f):f=>!!l(f)||!!c(f)||!!u(f)}return o.op==="AND"?l=>{for(let c=0;c<a;c++)if(!r[c](l))return false;return true}:l=>{for(let c=0;c<a;c++)if(r[c](l))return true;return false}},literal(o){let r=o.v;return ()=>r},static_(o){let r=o.v;return ()=>r},boundary(o){return s[o.i]},rawPrimitive(o){return ()=>o},rawObject(o,r){let a=r.length;return l=>{let c={};for(let u=0;u<a;u++)c[r[u][0]]=r[u][1](l);return c}},rawArray(o,r){return a=>r.map(l=>l(a))}}}function ie(e,n,t,i){return {deps:n,hash:i,bind:o=>{let r=re({scope:o?.scope??{},accessor:o?.accessor,handlers:o?.handlers,boundaryFns:t}),a=y(e,r);return j(a,n,i)}}}var Se="data",ke="accessor",Oe={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function q(e){let n=S(e);if(n.length===0)return omniAst.builders.identifier("undefined");let t=n[0],i=omniAst.builders.identifier(t.value);for(let s=1;s<n.length;s++){let o=n[s];o.type==="key"?i=omniAst.builders.memberExpression(i,omniAst.builders.identifier(o.value),false,true):i=omniAst.builders.memberExpression(i,omniAst.builders.literal(o.value),true,true);}return i}function De(e){let n=e.indexOf("[*]"),t=e.slice(0,n),i=e.slice(n+3),s;if(t)s=q(t);else return omniAst.builders.identifier("undefined");if(!i||i==="")return s;if(i.includes("[*]"))return oe(s,i);let o="_i",r=i.startsWith(".")?i.slice(1):i,a=omniAst.builders.identifier(o);if(r){let l=S(r);for(let c of l)c.type==="key"?a=omniAst.builders.memberExpression(a,omniAst.builders.identifier(c.value),false,true):a=omniAst.builders.memberExpression(a,omniAst.builders.literal(c.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],a)])}function oe(e,n){let t=n.indexOf("[*]"),i=n.slice(0,t),s=n.slice(t+3),o="_i",r=i.startsWith(".")?i.slice(1):i,a=omniAst.builders.identifier(o);if(r){let c=S(r);for(let u of c)u.type==="key"&&(a=omniAst.builders.memberExpression(a,omniAst.builders.identifier(u.value),false,true));}if(s.includes("[*]")){let c=oe(a,s);return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],c)])}let l=s.startsWith(".")?s.slice(1):s;if(l){let c=S(l);for(let u of c)u.type==="key"&&(a=omniAst.builders.memberExpression(a,omniAst.builders.identifier(u.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],a)])}function z(e){if(e===void 0)return omniAst.builders.identifier("undefined");if(e===null)return omniAst.builders.literal(null);if(typeof e!="object")return omniAst.builders.literal(e);if(Array.isArray(e))return omniAst.builders.arrayExpression(e.map(z));let t=Object.entries(e).map(([i,s])=>omniAst.builders.property(omniAst.builders.identifier(i),z(s)));return omniAst.builders.objectExpression(t)}function se(){return {data(e){return e.w?De(e.p):q(e.p)},arg(e){return q(e.p)},accessor(e){return omniAst.builders.callExpression(omniAst.builders.identifier(ke),[omniAst.builders.literal(e.p)])},call(e,n){let t=omniAst.builders.identifier(e.n);return omniAst.builders.callExpression(t,n)},ref(e){return omniAst.builders.identifier(e.n)},callH(e,n){return omniAst.builders.callExpression(omniAst.builders.identifier(`h_${e.ns}_${e.m}`),n)},refH(e){return omniAst.builders.identifier(`h_${e.ns}_${e.m}`)},cond(e,n,t,i){return omniAst.builders.conditionalExpression(n,t,i)},pipe(e,n,t){let i=n;for(let s of t)i=omniAst.builders.callExpression(s,[i]);return i},arrow(e,n){let t=e.params.map(i=>omniAst.builders.identifier(i));return omniAst.builders.arrowFunctionExpression(t,n)},not(e,n){return omniAst.builders.unaryExpression("!",n)},compare(e,n,t){let i=Oe[e.op];if(i)return omniAst.builders.binaryExpression(i,n,t);switch(e.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(t,omniAst.builders.identifier("includes")),[n]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(t,omniAst.builders.identifier("includes")),[n]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("includes"),false,true),[t]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("includes"),false,true),[t]));case "exists":return omniAst.builders.binaryExpression("!=",n,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",n,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[t]),omniAst.builders.identifier("test")),[n]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[t]),omniAst.builders.identifier("test")),[n]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("startsWith"),false,true),[t]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("endsWith"),false,true),[t]);default:return omniAst.builders.binaryExpression("===",n,t)}},logic(e,n){let t=e.op==="AND"?"&&":"||";if(n.length===0)return omniAst.builders.literal(e.op==="AND");if(n.length===1)return n[0];let i=n[0];for(let s=1;s<n.length;s++)i=omniAst.builders.logicalExpression(t,i,n[s]);return i},literal(e){return z(e.v)},static_(e){return z(e.v)},boundary(e){return omniAst.builders.callExpression(omniAst.builders.identifier(`b${e.i}`),[omniAst.builders.identifier(Se)])},rawPrimitive(e){return e===void 0?omniAst.builders.identifier("undefined"):e===null?omniAst.builders.literal(null):omniAst.builders.literal(e)},rawObject(e,n){let t=n.map(([i,s])=>omniAst.builders.property(omniAst.builders.identifier(i),s));return omniAst.builders.objectExpression(t)},rawArray(e,n){return omniAst.builders.arrayExpression(n)}}}function je(e){let n=e.indexOf("."),t=e.indexOf("["),i=e.length;return n!==-1&&(i=Math.min(i,n)),t!==-1&&(i=Math.min(i,t)),e.slice(0,i)}function ae(e,n){let t=new Set,i=new Set,s=new Set,o=false;return y(e,ne(void 0,{data(r){let a=je(r.p);a&&i.add(a);},accessor(){o=true;},call(r){t.add(r.n);},ref(r){t.add(r.n);},callH(r){s.add(`${r.ns}:${r.m}`);},refH(r){s.add(`${r.ns}:${r.m}`);}})),{scopeFns:t,dataRoots:i,hasAccessor:o,handlerRefs:s,boundaryCount:n}}function le(e){let n=[],t=[];for(let i of e.scopeFns)n.push({kind:"scope",name:i}),t.push(i);e.hasAccessor&&(n.push({kind:"accessor"}),t.push("accessor"));for(let i of e.handlerRefs){let[s,o]=i.split(":");n.push({kind:"handler",ns:s,m:o}),t.push(`h_${s}_${o}`);}for(let i=0;i<e.boundaryCount;i++)n.push({kind:"boundary",index:i}),t.push(`b${i}`);return {params:n,paramNames:t}}function ce(e,n,t){let i=omniAst.generate(e),s="";n.dataRoots.size>0&&(s=`const{${[...n.dataRoots].join(",")}}=data??{};`);let o=`${s}return ${i}`;return t.length>0?`(function(${t.join(",")}){return function(data){${o}}})`:`(function(){return function(data){${o}}})`}function Ne(e,n,t){let i=new Array(e.length);for(let s=0;s<e.length;s++){let o=e[s];switch(o.kind){case "scope":i[s]=n.scope?.[o.name];break;case "accessor":i[s]=n.accessor;break;case "handler":i[s]=n.handlers?.[o.ns]?.[o.m];break;case "boundary":i[s]=t[o.index];break}}return i}function U(e,n,t,i){let{hash:s,returnCode:o=false}=i,r=ae(e,t.length),a=y(e,se()),{params:l,paramNames:c}=le(r),u=ce(a,r,c);if(o)return {code:u,deps:n,hash:s,dataRoots:[...r.dataRoots],scopeFns:[...r.scopeFns],paramNames:c};let f;try{f=new Function(`return ${u}`)();}catch(m){throw new Error(`JIT compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${m instanceof Error?m.message:String(m)}`)}return {deps:n,hash:s,bind:m=>{let h=Ne(l,m??{},t),J=f(...h);return j(J,n,s)}}}var V=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),w=e=>e!==null&&typeof e=="object"&&"$"in e&&typeof e.$=="string"&&Object.keys(e).length===1,I=e=>e!==null&&typeof e=="object"&&"$if"in e&&"then"in e,E=e=>e!==null&&typeof e=="object"&&"$fn"in e&&typeof e.$fn=="string",$=e=>e!==null&&typeof e=="object"&&"$pipe"in e&&Array.isArray(e.$pipe),F=e=>e!==null&&typeof e=="object"&&"$arrow"in e,b=e=>e!==null&&typeof e=="object"&&"left"in e&&"op"in e&&V.has(e.op)&&!("$"in e)&&!("$if"in e)&&!("$fn"in e),T=e=>e!==null&&typeof e=="object"&&"logic"in e&&"conditions"in e,He=e=>b(e)||T(e),Be=e=>e!==null&&typeof e=="object"&&(w(e)||E(e)||I(e)||$(e)||F(e)||b(e)||T(e)),ze=e=>{if(e===null)return true;let n=typeof e;if(n==="string"||n==="number"||n==="boolean"||Array.isArray(e))return true;if(n==="object"&&e!==null){let t=e,i="left"in t&&"op"in t&&V.has(t.op);return !("$"in t)&&!("$if"in t)&&!("$fn"in t)&&!("$pipe"in t)&&!("$arrow"in t)&&!i&&!("logic"in t)}return false};function v(e,n){return _(e,n)}function _(e,n){if(e===null)return null;if(typeof e!="object")return e;if(Array.isArray(e))return e.map(o=>_(o,n));let t=e,i=Object.keys(t);for(let o of i)if(o in n){let r=n[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 _(r,n)}let s={};for(let o of i)s[o]=_(t[o],n);return s}function Ve(e){let n=e.indexOf("."),t=e.indexOf("["),i=e.length;return n!==-1&&(i=Math.min(i,n)),t!==-1&&(i=Math.min(i,t)),e.slice(0,i)}function g(e,n,t){return e[K]=true,t&&n&&(e.loc=n),e}function Q(e,n={}){let t=n.transforms?v(e,n.transforms):e,i=new Set,s=[],o=n.trackLocations===true;return {ir:C(t,n,new Set,i,s,o,o?"root":void 0),deps:Array.from(i),boundaryFns:s}}function C(e,n,t,i,s,o,r){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return Ue(e,n,t,i,s,o,r);let a=e;if(n.boundaries){for(let l of n.boundaries)if(l.check(a)){let c=l.handle(a),u=s.length;return s.push(c),g({t:"b",i:u},r,o)}}return w(e)?pe(e.$,n,t,i,o,r):I(e)?ve(e,n,t,i,s,o,r):$(e)?We(e,n,t,i,s,o,r):E(e)?Me(e,n,t,i,s,o,r):F(e)?Ge(e,n,t,i,s,o,r):b(e)?Je(e,n,t,i,s,o,r):T(e)?Ke(e,n,t,i,s,o,r):qe(a,n,t,i,s,o,r)}function _e(e,n){return n?e===n||e.startsWith(n+"."):false}function pe(e,n,t,i,s,o){let r=Ve(e);if(t.has(r))return g({t:"a",n:r,p:e},o,s);if(n.accessor){if(_e(e,n.lexicalPrefix)){i.add(A(e));let l={t:"d",p:e};return N(e)&&(l.w=true),g(l,o,s)}return i.add(A(e)),g({t:"x",p:e},o,s)}i.add(A(e));let a={t:"d",p:e};return N(e)&&(a.w=true),g(a,o,s)}function ve(e,n,t,i,s,o,r){let a;if(typeof e.$if=="string"){let u=e.$if.startsWith("!"),f=u?e.$if.slice(1):e.$if,d=pe(f,n,t,i,o,o?`${r}.$if`:void 0);a=u?g({t:"!",v:d},r,o):d;}else a=C(e.$if,n,t,i,s,o,o?`${r}.$if`:void 0);let l=C(e.then,n,t,i,s,o,o?`${r}.then`:void 0),c=e.else!==void 0?C(e.else,n,t,i,s,o,o?`${r}.else`:void 0):void 0;return g({t:"?",test:a,then:l,else:c},r,o)}function We(e,n,t,i,s,o,r){let a=e.$pipe;if(a.length===0)return;if(a.length===1)return C(a[0],n,t,i,s,o,o?`${r}.$pipe[0]`:void 0);let l=C(a[0],n,t,i,s,o,o?`${r}.$pipe[0]`:void 0),c=[];for(let u=1;u<a.length;u++)c.push(C(a[u],n,t,i,s,o,o?`${r}.$pipe[${u}]`:void 0));return g({t:"|",head:l,steps:c},r,o)}function Me(e,n,t,i,s,o,r){let a=e.$fn,l=a.indexOf(":"),c=e.args!==void 0;if(l!==-1){let f=a.slice(0,l),d=a.slice(l+1);if(!c)return g({t:"rh",ns:f,m:d},r,o);let m=e.args.map((h,J)=>C(h,n,t,i,s,o,o?`${r}.$fn.args[${J}]`:void 0));return g({t:"ch",ns:f,m:d,a:m},r,o)}if(!c)return g({t:"r",n:a},r,o);let u=e.args.map((f,d)=>C(f,n,t,i,s,o,o?`${r}.$fn.args[${d}]`:void 0));return g({t:"c",n:a,a:u},r,o)}function Ge(e,n,t,i,s,o,r){let a=e.args??[],l;if(a.length===0)l=t;else {l=new Set(t);for(let u of a)l.add(u);}let c=C(e.$arrow,n,l,i,s,o,o?`${r}.$arrow`:void 0);return g({t:"=>",params:a,body:c},r,o)}function Je(e,n,t,i,s,o,r){let a=C(e.left,n,t,i,s,o,o?`${r}.left`:void 0),l=e.right!==void 0?C(e.right,n,t,i,s,o,o?`${r}.right`:void 0):void 0;return g({t:"cmp",op:e.op,l:a,r:l},r,o)}function Ke(e,n,t,i,s,o,r){let a=e.conditions.map((l,c)=>C(l,n,t,i,s,o,o?`${r}.conditions[${c}]`:void 0));return g({t:"lg",op:e.logic,c:a},r,o)}function qe(e,n,t,i,s,o,r){let a=Object.keys(e);if(a.length===0)return g({t:"s",v:e},r,o);let l=false,c={};for(let u of a){let f=C(e[u],n,t,i,s,o,o?`${r}.${u}`:void 0);c[u]=f,l||(l=ue(f));}return l?c:g({t:"s",v:e},r,o)}function Ue(e,n,t,i,s,o,r){if(e.length===0)return g({t:"s",v:e},r,o);let a=false,l=[];for(let c=0;c<e.length;c++){let u=C(e[c],n,t,i,s,o,o?`${r}[${c}]`:void 0);l[c]=u,a||(a=ue(u));}return a?l:g({t:"s",v:e},r,o)}function ue(e){return e===null||typeof e!="object"?false:P(e)?true:Y(e)}function Y(e){if(P(e))return true;if(Array.isArray(e)){for(let n=0;n<e.length;n++)if(Y(e[n]))return true;return false}if(typeof e=="object"&&e!==null){let n=Object.keys(e);for(let t=0;t<n.length;t++)if(Y(e[n[t]]))return true}return false}function B(e){let n=new Set;return R(e,n),Array.from(n)}function R(e,n){if(e===null||typeof e!="object")return;if(Array.isArray(e)){for(let s=0;s<e.length;s++)R(e[s],n);return}if(w(e)){n.add(A(e.$));return}if(I(e)){if(typeof e.$if=="string"){let s=e.$if.startsWith("!")?e.$if.slice(1):e.$if;n.add(A(s));}else R(e.$if,n);R(e.then,n),e.else!==void 0&&R(e.else,n);return}if($(e)){for(let s=0;s<e.$pipe.length;s++)R(e.$pipe[s],n);return}if(E(e)){if(e.args)for(let s=0;s<e.args.length;s++)R(e.args[s],n);return}if(F(e)){let s=new Set;R(e.$arrow,s);let o=new Set(e.args??[]);for(let r of s){let a=r.split(".")[0].split("[")[0];o.has(a)||n.add(r);}return}if(b(e)){R(e.left,n),e.right!==void 0&&R(e.right,n);return}if(T(e)){for(let s=0;s<e.conditions.length;s++)R(e.conditions[s],n);return}let t=e,i=Object.keys(t);for(let s=0;s<i.length;s++)R(t[i[s]],n);}function Ye(e){return B(e).length>0}function Qe(e){return B(e).length===0}var k=class{constructor(n=1e3){this.cache=new Map,this._maxSize=n;}getOrCompile(n,t){let i=this.cache.get(n);if(i!==void 0)return this.cache.delete(n),this.cache.set(n,i),i;let s=t();if(this.cache.size>=this._maxSize){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}return this.cache.set(n,s),s}has(n){return this.cache.has(n)}delete(n){return this.cache.delete(n)}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(n){for(this._maxSize=n;this.cache.size>this._maxSize;){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}}},X=class{constructor(n=1e3){this.closures=new k(n),this.jit=new k(n);}getOrCompile(n,t,i){return (t==="closures"?this.closures:this.jit).getOrCompile(n,i)}get size(){return {closures:this.closures.size,jit:this.jit.size}}clear(){this.closures.clear(),this.jit.clear();}};function Z(e){return JSON.stringify(e)}var L=class{constructor(n={}){this.scope=n.scope??{},this.accessor=n.accessor,this.boundaries=n.boundaries,this.compileCache=n.compileCache,this.cacheClosures=new k(n.cacheSize??1e3),this.cacheJIT=new k(n.cacheSize??1e3),n.handlers&&(this.wrappedHandlers=this.wrapHandlers(n.handlers));}compile(n,t){let i=Z(n),s=t?.useAccessor??!!this.accessor,o=this.compileCacheKey(i,s);return this.cacheClosures.getOrCompile(o,()=>{let r=this.getArtifact(n,i,s,"closures"),a=this.defaultCtx(s);return {deps:r.deps,hash:r.hash,bind:l=>r.bind(l??a)}})}jit(n,t){let i=Z(n),s=t?.useAccessor??false,o=this.compileCacheKey(i,s,t?.lexicalPrefix);return this.cacheJIT.getOrCompile(o,()=>{let r=this.getArtifact(n,i,s,"jit",t?.lexicalPrefix),a=this.defaultCtx(s);return {deps:r.deps,hash:r.hash,bind:l=>r.bind(l??a)}})}jitCode(n,t){let s=t?.useAccessor??false?this.accessor??(()=>{}):void 0,{ir:o,deps:r,boundaryFns:a}=Q(n,{accessor:s,boundaries:this.boundaries,handlers:this.wrappedHandlers,lexicalPrefix:t?.lexicalPrefix});return U(o,r,a,{hash:JSON.stringify(n),returnCode:true})}eval(n,t,i){return i?.mode==="jit"?this.jit(n,{useAccessor:i?.useAccessor}).bind()(t):this.compile(n,{useAccessor:i?.useAccessor}).bind()(t)}wrapHandlers(n){let t={},i={};for(let s of Object.keys(n)){i[s]={};for(let o of Object.keys(n[s]))i[s][o]=n[s][o].bind(t);}return t.accessor=this.accessor,t.handlers=i,t.compiler=this,t.scope=this.scope,i}normalize(n,t){return v(n,t)}extractDeps(n){return B(n)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}getArtifact(n,t,i,s,o){let r=this.compileCacheKey(t,i,o);return this.compileCache?this.compileCache.getOrCompile(r,s,()=>this.doCompile(n,i,s,o)):this.doCompile(n,i,s,o)}doCompile(n,t,i,s){let o=t?this.accessor??(()=>{}):void 0,{ir:r,deps:a,boundaryFns:l}=Q(n,{accessor:o,boundaries:this.boundaries,handlers:this.wrappedHandlers,lexicalPrefix:s}),c=JSON.stringify(n);return i==="jit"?U(r,a,l,{hash:c}):ie(r,a,l,c)}defaultCtx(n){return {scope:this.scope,accessor:n?this.accessor:void 0,handlers:this.wrappedHandlers}}compileCacheKey(n,t,i){return i?`${t?"a":"n"}:${i}:${n}`:t?`a:${n}`:n}};var Ce={};Te(Ce,{$:()=>fe,$arrow:()=>ge,$call:()=>he,$cond:()=>ye,$fn:()=>de,$if:()=>Le,$pipe:()=>me,arrow:()=>tn,call:()=>rn,cond:()=>nn,fn:()=>Ze,pipe:()=>en,ref:()=>Xe});function fe(e){return {$:e}}var Xe=fe;function de(e,n){return n===void 0?{$fn:e}:{$fn:e,args:n}}var Ze=de;function Le(e,n,t){return t===void 0?{$if:e,then:n}:{$if:e,then:n,else:t}}function me(...e){return {$pipe:e}}var en=me;function ye(e,n,t){return t===void 0?{left:e,op:n}:{left:e,op:n,right:t}}var nn=ye;function ge(e,n){return n===void 0||n.length===0?{$arrow:e}:{$arrow:e,args:n}}var tn=ge;function he(e,n=[]){return {$fn:e,args:n}}var rn=he;function ee(e,n="root",t={}){let i=[];return x(e,n,i,t),{valid:i.length===0,errors:i}}function x(e,n,t,i){if(e===null||typeof e!="object")return;if(Array.isArray(e)){for(let r=0;r<e.length;r++)x(e[r],`${n}[${r}]`,t,i);return}if(w(e)){(!e.$||typeof e.$!="string")&&t.push(`${n}: invalid reference, $ must be non-empty string`);return}if(I(e)){typeof e.$if=="string"?(e.$if.startsWith("!")?e.$if.slice(1):e.$if)||t.push(`${n}.$if: empty path in string shorthand`):x(e.$if,`${n}.$if`,t,i),x(e.then,`${n}.then`,t,i),e.else!==void 0&&x(e.else,`${n}.else`,t,i);return}if($(e)){if(!Array.isArray(e.$pipe)){t.push(`${n}.$pipe: must be an array`);return}if(e.$pipe.length===0){t.push(`${n}.$pipe: must have at least one element`);return}for(let r=0;r<e.$pipe.length;r++)x(e.$pipe[r],`${n}.$pipe[${r}]`,t,i);return}if(E(e)){if(!e.$fn||typeof e.$fn!="string"){t.push(`${n}: invalid function, $fn must be non-empty string`);return}let r=e.$fn.indexOf(":");if(r!==-1){let a=e.$fn.slice(0,r),l=e.$fn.slice(r+1);!a||!l||l.includes(":")?t.push(`${n}: invalid handler format "${e.$fn}", expected "namespace:method"`):i.handlers&&!i.handlers[a]?.[l]&&t.push(`${n}: handler "${e.$fn}" not found`);}else i.scope&&!(e.$fn in i.scope)&&t.push(`${n}: function "${e.$fn}" not found in scope`);if(e.args!==void 0)if(!Array.isArray(e.args))t.push(`${n}.args: must be an array`);else for(let a=0;a<e.args.length;a++)x(e.args[a],`${n}.args[${a}]`,t,i);return}if(F(e)){if(x(e.$arrow,`${n}.$arrow`,t,i),e.args!==void 0)if(!Array.isArray(e.args))t.push(`${n}.args: must be an array of strings`);else for(let r of e.args)(typeof r!="string"||!r)&&t.push(`${n}.args: each arg must be a non-empty string`);return}if(b(e)){V.has(e.op)||t.push(`${n}: invalid operator "${e.op}"`),x(e.left,`${n}.left`,t,i),e.right!==void 0&&x(e.right,`${n}.right`,t,i);return}if(T(e)){if(e.logic!=="AND"&&e.logic!=="OR"&&t.push(`${n}: invalid logic "${e.logic}", must be "AND" or "OR"`),!Array.isArray(e.conditions)){t.push(`${n}.conditions: must be an array`);return}for(let r=0;r<e.conditions.length;r++)x(e.conditions[r],`${n}.conditions[${r}]`,t,i);return}if(i.boundaries?.length){let r=e;for(let a of i.boundaries)if(a.check(r))return}let s=e,o=Object.keys(s);for(let r=0;r<o.length;r++){let a=o[r];x(s[a],`${n}.${a}`,t,i);}}function on(e,n={}){let t=ee(e,"root",n);if(!t.valid)throw new Error(`Invalid expression: ${t.errors.join("; ")}`)}function sn(e,n={}){return ee(e,"root",n).valid}function D(e){return "compile"in e}function O(e){return "type"in e&&!("compile"in e)}function Re(e){return typeof e.compile=="string"}function W(e){return typeof e.compile=="function"}function G(e,n){let t=[];for(let i of Object.keys(e))i in n||t.push({field:i,message:`Unknown field "${i}"`,type:"unknown_field"});for(let[i,s]of Object.entries(n)){let o=e[i],r=i in e&&o!==void 0;if(O(s)){if(s.required&&!r&&s.default===void 0){t.push({field:i,message:`Missing required field "${i}"`,type:"missing_required"});continue}if(r){let a=an(i,o,s);a&&t.push(a);}}}return {valid:t.length===0,errors:t}}function an(e,n,t){switch(t.type){case "string":if(typeof n!="string")return {field:e,message:`Field "${e}" expected string, got ${typeof n}`,type:"type_mismatch"};break;case "number":if(typeof n!="number")return {field:e,message:`Field "${e}" expected number, got ${typeof n}`,type:"type_mismatch"};break;case "boolean":if(typeof n!="boolean")return {field:e,message:`Field "${e}" expected boolean, got ${typeof n}`,type:"type_mismatch"};break;case "string[]":if(!Array.isArray(n)||!n.every(i=>typeof i=="string"))return {field:e,message:`Field "${e}" expected string[], got ${M(n)}`,type:"type_mismatch"};break;case "number[]":if(!Array.isArray(n)||!n.every(i=>typeof i=="number"))return {field:e,message:`Field "${e}" expected number[], got ${M(n)}`,type:"type_mismatch"};break;case "enum":if(!t.values||!t.values.includes(n))return {field:e,message:`Field "${e}" expected one of [${t.values?.join(", ")}], got "${String(n)}"`,type:"invalid_enum"};break;case "object":if(typeof n!="object"||n===null||Array.isArray(n))return {field:e,message:`Field "${e}" expected object, got ${M(n)}`,type:"type_mismatch"};break;case "schema":if(typeof n!="object"||n===null||Array.isArray(n))return {field:e,message:`Field "${e}" expected schema object, got ${M(n)}`,type:"type_mismatch"};break;}return null}function M(e){return e===null?"null":Array.isArray(e)?"array":typeof e}function xe(e,n,t,i){if(i?.validate!==false){let l=G(e,n);if(!l.valid){let c=l.errors.map(u=>u.message).join("; ");throw new Error(`Template validation failed: ${c}`)}}let s=ln(e,n),o={},r={},a=new Set;for(let[l,c]of Object.entries(n)){let u=s[l];if(D(c)){if(u===void 0)continue;let f=cn(l,u,c,t,i);o[l]=f.value,f.artifact&&(r[l]=f.artifact),f.isCondition&&a.add(l);}else O(c)&&u!==void 0&&(o[l]=u);}return pn(o,r,a)}function ln(e,n){let t={...e};for(let[i,s]of Object.entries(n))O(s)&&!(i in t)&&s.default!==void 0&&(t[i]=s.default);return t}function cn(e,n,t,i,s){if(W(t)){let a={allow:t.allow,deny:t.deny,transforms:s?.transforms,mode:s?.mode};return {value:t.compile(n,i,a),artifact:null,isCondition:false}}let o=s?.mode??"closures";if(t.compile==="condition"){let l={$arrow:typeof n=="string"?{$if:n,then:true,else:false}:n},c=o==="jit"?i.jit(l,{useAccessor:true}):i.compile(l,{useAccessor:true});return {value:c.bind()({}),artifact:c,isCondition:true}}if(t.compile==="expression"){let a=o==="jit"?i.jit(n,{useAccessor:true}):i.compile(n,{useAccessor:true});return {value:a.bind(),artifact:a,isCondition:false}}let r=s?.handlers?.[t.compile];if(r){let a={allow:t.allow,deny:t.deny,transforms:s?.transforms,mode:s?.mode};return {value:r(n,i,a),artifact:null,isCondition:false}}throw new Error(`Unknown compile type: ${String(t.compile)}`)}function pn(e,n,t){let i={...e},s=o=>{let r={...e};for(let[a,l]of Object.entries(n)){let c=l.bind(o);r[a]=t.has(a)?c({}):c;}for(let[a,l]of Object.entries(e))a in n||l!=null&&typeof l.bind=="function"&&(r[a]=l.bind(o));return r.bind=s,r};return i.bind=s,i}var dt="3.0.0";exports.CompileCache=X;exports.ExpressionCompiler=L;exports.VERSION=dt;exports.assertValid=on;exports.builders=Ce;exports.compileDefinition=xe;exports.compilePath=H;exports.createBoundFn=j;exports.extractDeps=B;exports.get=Fe;exports.hasDeps=Ye;exports.hasWildcard=N;exports.isArrow=F;exports.isCompileField=D;exports.isCondition=b;exports.isConditionExpr=He;exports.isConditionGroup=T;exports.isConditional=I;exports.isExpression=Be;exports.isFn=E;exports.isHandlerCompile=W;exports.isLiteral=ze;exports.isNativeCompile=Re;exports.isPipe=$;exports.isPure=Qe;exports.isRef=w;exports.isSchemaField=O;exports.isValid=sn;exports.normalizePath=A;exports.validate=ee;exports.validateDefinition=G;
|
package/dist/index.d.cts
CHANGED
|
@@ -91,16 +91,73 @@ type Expression = Literal | RefExpr | ConditionalExpr | FnExpr | PipeExpr | Arro
|
|
|
91
91
|
*/
|
|
92
92
|
type CompiledFn<T = unknown, R = unknown> = (data: T) => R;
|
|
93
93
|
/**
|
|
94
|
-
*
|
|
94
|
+
* Callable function bound to a context, with metadata as properties.
|
|
95
|
+
* Call directly — no `.fn` needed.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const fn = artifact.bind({ scope, accessor });
|
|
99
|
+
* fn(data); // callable direct
|
|
100
|
+
* fn.deps; // ["hp", "armor"]
|
|
101
|
+
* fn.hash; // "a1b2c3"
|
|
102
|
+
* items.map(fn); // works as first-class function
|
|
103
|
+
*/
|
|
104
|
+
interface BoundFn<T = unknown, R = unknown> {
|
|
105
|
+
/** Execute the expression with the given data */
|
|
106
|
+
(data: T): R;
|
|
107
|
+
/** Paths this expression depends on */
|
|
108
|
+
readonly deps: string[];
|
|
109
|
+
/** Unique hash for caching */
|
|
110
|
+
readonly hash: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Create a BoundFn from a compiled function + metadata.
|
|
114
|
+
* Attaches deps and hash as readonly props.
|
|
95
115
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
116
|
+
declare function createBoundFn<T = unknown, R = unknown>(fn: CompiledFn<T, R>, deps: string[], hash: string): BoundFn<T, R>;
|
|
117
|
+
/**
|
|
118
|
+
* Context-free compile artifact — result of compilation without pre-prepared fn.
|
|
119
|
+
* Stored in shared compile caches. Use bind() to attach context and get fn.
|
|
120
|
+
*/
|
|
121
|
+
interface CompileArtifact<T = unknown, R = unknown> {
|
|
99
122
|
/** Paths this expression depends on */
|
|
100
123
|
deps: string[];
|
|
101
124
|
/** Unique hash for caching */
|
|
102
125
|
hash: string;
|
|
126
|
+
/**
|
|
127
|
+
* Bind to a context and get a callable BoundFn.
|
|
128
|
+
* @param ctx - Runtime context (scope, accessor, handlers). Uses defaults if omitted.
|
|
129
|
+
*/
|
|
130
|
+
bind(ctx?: BindContext): BoundFn<T, R>;
|
|
103
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Context for bind() — runtime dependencies injected after compilation.
|
|
134
|
+
*/
|
|
135
|
+
interface BindContext {
|
|
136
|
+
scope?: Scope;
|
|
137
|
+
accessor?: AccessorFn;
|
|
138
|
+
handlers?: WrappedHandlers;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Alias for CompileArtifact — the public name going forward.
|
|
142
|
+
*/
|
|
143
|
+
type Artifact<T = unknown, R = unknown> = CompileArtifact<T, R>;
|
|
144
|
+
/**
|
|
145
|
+
* Describes what each flat parameter of the compiled factory expects.
|
|
146
|
+
* Order matches the factory's parameter order.
|
|
147
|
+
*/
|
|
148
|
+
type ParamSlot = {
|
|
149
|
+
kind: "scope";
|
|
150
|
+
name: string;
|
|
151
|
+
} | {
|
|
152
|
+
kind: "accessor";
|
|
153
|
+
} | {
|
|
154
|
+
kind: "handler";
|
|
155
|
+
ns: string;
|
|
156
|
+
m: string;
|
|
157
|
+
} | {
|
|
158
|
+
kind: "boundary";
|
|
159
|
+
index: number;
|
|
160
|
+
};
|
|
104
161
|
/**
|
|
105
162
|
* Path getter function
|
|
106
163
|
*/
|
|
@@ -246,11 +303,62 @@ type TransformFn = (node: Record<string, unknown>) => Expression;
|
|
|
246
303
|
*/
|
|
247
304
|
type Transforms = Record<string, TransformFn>;
|
|
248
305
|
|
|
306
|
+
/**
|
|
307
|
+
* @statedelta-libs/expressions - JIT Compiler
|
|
308
|
+
*
|
|
309
|
+
* IR-based pipeline: receives IR from analyze(), emits optimized JS via AST.
|
|
310
|
+
* Factory with flat parameters — zero destructuring, zero property chains.
|
|
311
|
+
* No DSL knowledge — no guards, no type detection.
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/** Result when returnCode is true */
|
|
315
|
+
interface JITCodeResult {
|
|
316
|
+
code: string;
|
|
317
|
+
deps: string[];
|
|
318
|
+
hash: string;
|
|
319
|
+
dataRoots: string[];
|
|
320
|
+
scopeFns: string[];
|
|
321
|
+
paramNames: string[];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @statedelta-libs/expressions - Cache
|
|
326
|
+
*
|
|
327
|
+
* LRU cache for compiled expressions and compile artifacts.
|
|
328
|
+
* Performance: cache hit ~10M ops/sec
|
|
329
|
+
*/
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Shared compile cache — stores context-free CompileArtifacts.
|
|
333
|
+
* Can be shared between ExpressionCompiler instances for multi-tenant scenarios.
|
|
334
|
+
* Dual mode: separate closures and JIT caches.
|
|
335
|
+
*/
|
|
336
|
+
declare class CompileCache {
|
|
337
|
+
private closures;
|
|
338
|
+
private jit;
|
|
339
|
+
constructor(maxSize?: number);
|
|
340
|
+
/**
|
|
341
|
+
* Get or compile artifact by key and mode.
|
|
342
|
+
*/
|
|
343
|
+
getOrCompile<V>(key: string, mode: "closures" | "jit", factory: () => V): V;
|
|
344
|
+
/**
|
|
345
|
+
* Cache size per mode
|
|
346
|
+
*/
|
|
347
|
+
get size(): {
|
|
348
|
+
closures: number;
|
|
349
|
+
jit: number;
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* Clear both caches
|
|
353
|
+
*/
|
|
354
|
+
clear(): void;
|
|
355
|
+
}
|
|
356
|
+
|
|
249
357
|
/**
|
|
250
358
|
* @statedelta-libs/expressions - ExpressionCompiler
|
|
251
359
|
*
|
|
252
360
|
* Unified public API for expression compilation.
|
|
253
|
-
*
|
|
361
|
+
* Phase 4: two-level cache (instance + shared compile), DI-first pattern.
|
|
254
362
|
*/
|
|
255
363
|
|
|
256
364
|
interface ExpressionCompilerOptions<T = unknown> {
|
|
@@ -264,6 +372,8 @@ interface ExpressionCompilerOptions<T = unknown> {
|
|
|
264
372
|
boundaries?: BoundaryDef<T>[];
|
|
265
373
|
/** Handler namespaces — services with injected context */
|
|
266
374
|
handlers?: HandlersMap;
|
|
375
|
+
/** Shared compile cache — enables cross-instance cache sharing */
|
|
376
|
+
compileCache?: CompileCache;
|
|
267
377
|
}
|
|
268
378
|
interface JITOptions {
|
|
269
379
|
/** Use accessor to resolve paths instead of direct access */
|
|
@@ -271,6 +381,16 @@ interface JITOptions {
|
|
|
271
381
|
/** Prefix for paths that bypass accessor (direct access) */
|
|
272
382
|
lexicalPrefix?: string;
|
|
273
383
|
}
|
|
384
|
+
interface CompileConfig {
|
|
385
|
+
/** Use accessor to resolve paths instead of direct access */
|
|
386
|
+
useAccessor?: boolean;
|
|
387
|
+
}
|
|
388
|
+
interface EvalOptions {
|
|
389
|
+
/** Pipeline: "closures" (default) or "jit" */
|
|
390
|
+
mode?: "closures" | "jit";
|
|
391
|
+
/** Use accessor to resolve paths instead of direct access */
|
|
392
|
+
useAccessor?: boolean;
|
|
393
|
+
}
|
|
274
394
|
/**
|
|
275
395
|
* Context available via `this` inside every handler (bound via .bind()).
|
|
276
396
|
* Created once per ExpressionCompiler instance — zero per-call overhead.
|
|
@@ -290,13 +410,18 @@ declare class ExpressionCompiler<T = unknown> {
|
|
|
290
410
|
private readonly accessor?;
|
|
291
411
|
private readonly boundaries?;
|
|
292
412
|
private readonly wrappedHandlers?;
|
|
413
|
+
private readonly compileCache?;
|
|
293
414
|
private readonly cacheClosures;
|
|
294
415
|
private readonly cacheJIT;
|
|
295
416
|
constructor(options?: ExpressionCompilerOptions<T>);
|
|
296
|
-
/** Compile via closure composition
|
|
297
|
-
compile<R = unknown>(expr: Expression):
|
|
298
|
-
/** Compile via JS code generation (JIT) */
|
|
299
|
-
jit<R = unknown>(expr: Expression, opts?: JITOptions):
|
|
417
|
+
/** Compile via closure composition — DSL → IR → closures */
|
|
418
|
+
compile<R = unknown>(expr: Expression, opts?: CompileConfig): CompileArtifact<T, R>;
|
|
419
|
+
/** Compile via JS code generation (JIT) — DSL → IR → AST → JS */
|
|
420
|
+
jit<R = unknown>(expr: Expression, opts?: JITOptions): CompileArtifact<T, R>;
|
|
421
|
+
/** Return generated JS code without creating function — for inspection/testing */
|
|
422
|
+
jitCode(expr: Expression, opts?: JITOptions): JITCodeResult;
|
|
423
|
+
/** Compile, bind with default context, and execute in one step */
|
|
424
|
+
eval<R = unknown>(expr: Expression, data: T, opts?: EvalOptions): R;
|
|
300
425
|
/**
|
|
301
426
|
* Wrap raw handlers with ctx via .bind().
|
|
302
427
|
* Creates ctx once, binds each handler method once.
|
|
@@ -304,23 +429,36 @@ declare class ExpressionCompiler<T = unknown> {
|
|
|
304
429
|
* Handlers access ctx via `this`.
|
|
305
430
|
*/
|
|
306
431
|
private wrapHandlers;
|
|
307
|
-
/** Compile (closures) and execute in one step */
|
|
308
|
-
evaluate<R = unknown>(expr: Expression, data: T): R;
|
|
309
|
-
/** Compile (JIT) and execute in one step */
|
|
310
|
-
evaluateJIT<R = unknown>(expr: Expression, data: T, opts?: JITOptions): R;
|
|
311
432
|
/** Normalize custom expression to pure DSL */
|
|
312
433
|
normalize(expr: Expression, transforms: Transforms): Expression;
|
|
313
434
|
/** Extract dependencies without compiling */
|
|
314
435
|
extractDeps(expr: Expression): string[];
|
|
315
|
-
/**
|
|
436
|
+
/** Instance cache size per mode */
|
|
316
437
|
get cacheSize(): {
|
|
317
438
|
closures: number;
|
|
318
439
|
jit: number;
|
|
319
440
|
};
|
|
320
|
-
/** Clear
|
|
441
|
+
/** Clear instance caches */
|
|
321
442
|
clearCache(): void;
|
|
322
443
|
/** Read-only access to scope */
|
|
323
444
|
getScope(): Readonly<Scope>;
|
|
445
|
+
/**
|
|
446
|
+
* Get or create compile artifact.
|
|
447
|
+
* Two-level: compile cache (shared, optional) → fresh compile.
|
|
448
|
+
*/
|
|
449
|
+
private getArtifact;
|
|
450
|
+
/**
|
|
451
|
+
* Full compile: analyze → backend → CompileArtifact.
|
|
452
|
+
*/
|
|
453
|
+
private doCompile;
|
|
454
|
+
/**
|
|
455
|
+
* Default BindContext from constructor options.
|
|
456
|
+
*/
|
|
457
|
+
private defaultCtx;
|
|
458
|
+
/**
|
|
459
|
+
* Build compile cache key including compile-time flags.
|
|
460
|
+
*/
|
|
461
|
+
private compileCacheKey;
|
|
324
462
|
}
|
|
325
463
|
|
|
326
464
|
/**
|
|
@@ -521,6 +659,10 @@ declare const isConditionGroup: (v: unknown) => v is ConditionGroup;
|
|
|
521
659
|
* Check if value is any condition expression
|
|
522
660
|
*/
|
|
523
661
|
declare const isConditionExpr: (v: unknown) => v is ConditionExpr;
|
|
662
|
+
/**
|
|
663
|
+
* Check if value is any Expression DSL node
|
|
664
|
+
*/
|
|
665
|
+
declare const isExpression: (v: unknown) => boolean;
|
|
524
666
|
/**
|
|
525
667
|
* Check if value is a literal (not an expression object)
|
|
526
668
|
*/
|
|
@@ -608,6 +750,93 @@ declare function hasDeps(expr: Expression): boolean;
|
|
|
608
750
|
*/
|
|
609
751
|
declare function isPure(expr: Expression): boolean;
|
|
610
752
|
|
|
753
|
+
/**
|
|
754
|
+
* Template Compiler — Types
|
|
755
|
+
*
|
|
756
|
+
* Declarative template system for compiling arbitrary JSON structures
|
|
757
|
+
* using ExpressionCompiler as the engine.
|
|
758
|
+
*/
|
|
759
|
+
|
|
760
|
+
type SchemaType = "string" | "number" | "boolean" | "string[]" | "number[]" | "enum" | "schema" | "object" | "any";
|
|
761
|
+
interface SchemaFieldDef {
|
|
762
|
+
type: SchemaType;
|
|
763
|
+
required?: boolean;
|
|
764
|
+
default?: unknown;
|
|
765
|
+
values?: string[];
|
|
766
|
+
}
|
|
767
|
+
interface FieldHandlerOptions {
|
|
768
|
+
allow?: string[];
|
|
769
|
+
deny?: string[];
|
|
770
|
+
transforms?: Transforms;
|
|
771
|
+
mode?: "closures" | "jit";
|
|
772
|
+
}
|
|
773
|
+
type FieldHandler = (value: unknown, compiler: ExpressionCompiler, options: FieldHandlerOptions) => unknown;
|
|
774
|
+
type CompileFieldDef = {
|
|
775
|
+
compile: "condition";
|
|
776
|
+
} | {
|
|
777
|
+
compile: "expression";
|
|
778
|
+
} | {
|
|
779
|
+
compile: string;
|
|
780
|
+
allow?: string[];
|
|
781
|
+
deny?: string[];
|
|
782
|
+
} | {
|
|
783
|
+
compile: FieldHandler;
|
|
784
|
+
allow?: string[];
|
|
785
|
+
deny?: string[];
|
|
786
|
+
[key: string]: unknown;
|
|
787
|
+
};
|
|
788
|
+
type FieldDef = CompileFieldDef | SchemaFieldDef;
|
|
789
|
+
type DefinitionTemplate = Record<string, FieldDef>;
|
|
790
|
+
interface CompileDefinitionOptions {
|
|
791
|
+
transforms?: Transforms;
|
|
792
|
+
mode?: "closures" | "jit";
|
|
793
|
+
validate?: boolean;
|
|
794
|
+
handlers?: Record<string, FieldHandler>;
|
|
795
|
+
}
|
|
796
|
+
interface CompiledDefinition<T = unknown> {
|
|
797
|
+
[field: string]: unknown;
|
|
798
|
+
/** Bind to a context — re-binds all compiled fields */
|
|
799
|
+
bind(ctx: BindContext): CompiledDefinition<T>;
|
|
800
|
+
}
|
|
801
|
+
type DefinitionValidationErrorType = "unknown_field" | "missing_required" | "type_mismatch" | "invalid_enum";
|
|
802
|
+
interface DefinitionValidationError {
|
|
803
|
+
field: string;
|
|
804
|
+
message: string;
|
|
805
|
+
type: DefinitionValidationErrorType;
|
|
806
|
+
}
|
|
807
|
+
interface DefinitionValidationResult {
|
|
808
|
+
valid: boolean;
|
|
809
|
+
errors: DefinitionValidationError[];
|
|
810
|
+
}
|
|
811
|
+
declare function isCompileField(def: FieldDef): def is CompileFieldDef;
|
|
812
|
+
declare function isSchemaField(def: FieldDef): def is SchemaFieldDef;
|
|
813
|
+
declare function isNativeCompile(def: CompileFieldDef): def is {
|
|
814
|
+
compile: "condition";
|
|
815
|
+
} | {
|
|
816
|
+
compile: "expression";
|
|
817
|
+
};
|
|
818
|
+
declare function isHandlerCompile(def: CompileFieldDef): def is {
|
|
819
|
+
compile: FieldHandler;
|
|
820
|
+
[key: string]: unknown;
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Template Compiler — Core Compilation
|
|
825
|
+
*
|
|
826
|
+
* compileDefinition(): validates, normalizes, compiles, and assembles
|
|
827
|
+
* a JSON structure using a template + ExpressionCompiler.
|
|
828
|
+
*/
|
|
829
|
+
|
|
830
|
+
declare function compileDefinition<T = unknown>(dsl: Record<string, unknown>, template: DefinitionTemplate, compiler: ExpressionCompiler, options?: CompileDefinitionOptions): CompiledDefinition<T>;
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* Template Compiler — Validation
|
|
834
|
+
*
|
|
835
|
+
* Validates a DSL object against a DefinitionTemplate.
|
|
836
|
+
*/
|
|
837
|
+
|
|
838
|
+
declare function validateDefinition(dsl: Record<string, unknown>, template: DefinitionTemplate): DefinitionValidationResult;
|
|
839
|
+
|
|
611
840
|
/**
|
|
612
841
|
* @statedelta-libs/expressions
|
|
613
842
|
*
|
|
@@ -616,4 +845,4 @@ declare function isPure(expr: Expression): boolean;
|
|
|
616
845
|
*/
|
|
617
846
|
declare const VERSION = "3.0.0";
|
|
618
847
|
|
|
619
|
-
export { type AccessorFn, type ArrowExpr, type BoundaryDef, type CompileOptions, type
|
|
848
|
+
export { type AccessorFn, type ArrowExpr, type Artifact, type BindContext, type BoundFn, type BoundaryDef, type CompileArtifact, CompileCache, type CompileConfig, type CompileDefinitionOptions, type CompileFieldDef, type CompileOptions, type CompiledDefinition, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type DefinitionTemplate, type DefinitionValidationError, type DefinitionValidationErrorType, type DefinitionValidationResult, type EvalOptions, type Expression, ExpressionCompiler, type ExpressionCompilerOptions, type FieldDef, type FieldHandler, type FieldHandlerOptions, type FnExpr, type HandlerContext, type HandlerFn, type HandlersMap, type JITCodeResult, type JITOptions, type Literal, type ParamSlot, type PipeExpr, type RefExpr, type SchemaFieldDef, type SchemaType, type Scope, type TransformFn, type Transforms, VERSION, type ValidateOptions, type ValidationResult, type WrappedHandlers, assertValid, builders, compileDefinition, compilePath, createBoundFn, extractDeps, get, hasDeps, hasWildcard, isArrow, isCompileField, isCondition, isConditionExpr, isConditionGroup, isConditional, isExpression, isFn, isHandlerCompile, isLiteral, isNativeCompile, isPipe, isPure, isRef, isSchemaField, isValid, normalizePath, validate, validateDefinition };
|
package/dist/index.d.ts
CHANGED
|
@@ -91,16 +91,73 @@ type Expression = Literal | RefExpr | ConditionalExpr | FnExpr | PipeExpr | Arro
|
|
|
91
91
|
*/
|
|
92
92
|
type CompiledFn<T = unknown, R = unknown> = (data: T) => R;
|
|
93
93
|
/**
|
|
94
|
-
*
|
|
94
|
+
* Callable function bound to a context, with metadata as properties.
|
|
95
|
+
* Call directly — no `.fn` needed.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const fn = artifact.bind({ scope, accessor });
|
|
99
|
+
* fn(data); // callable direct
|
|
100
|
+
* fn.deps; // ["hp", "armor"]
|
|
101
|
+
* fn.hash; // "a1b2c3"
|
|
102
|
+
* items.map(fn); // works as first-class function
|
|
103
|
+
*/
|
|
104
|
+
interface BoundFn<T = unknown, R = unknown> {
|
|
105
|
+
/** Execute the expression with the given data */
|
|
106
|
+
(data: T): R;
|
|
107
|
+
/** Paths this expression depends on */
|
|
108
|
+
readonly deps: string[];
|
|
109
|
+
/** Unique hash for caching */
|
|
110
|
+
readonly hash: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Create a BoundFn from a compiled function + metadata.
|
|
114
|
+
* Attaches deps and hash as readonly props.
|
|
95
115
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
116
|
+
declare function createBoundFn<T = unknown, R = unknown>(fn: CompiledFn<T, R>, deps: string[], hash: string): BoundFn<T, R>;
|
|
117
|
+
/**
|
|
118
|
+
* Context-free compile artifact — result of compilation without pre-prepared fn.
|
|
119
|
+
* Stored in shared compile caches. Use bind() to attach context and get fn.
|
|
120
|
+
*/
|
|
121
|
+
interface CompileArtifact<T = unknown, R = unknown> {
|
|
99
122
|
/** Paths this expression depends on */
|
|
100
123
|
deps: string[];
|
|
101
124
|
/** Unique hash for caching */
|
|
102
125
|
hash: string;
|
|
126
|
+
/**
|
|
127
|
+
* Bind to a context and get a callable BoundFn.
|
|
128
|
+
* @param ctx - Runtime context (scope, accessor, handlers). Uses defaults if omitted.
|
|
129
|
+
*/
|
|
130
|
+
bind(ctx?: BindContext): BoundFn<T, R>;
|
|
103
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Context for bind() — runtime dependencies injected after compilation.
|
|
134
|
+
*/
|
|
135
|
+
interface BindContext {
|
|
136
|
+
scope?: Scope;
|
|
137
|
+
accessor?: AccessorFn;
|
|
138
|
+
handlers?: WrappedHandlers;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Alias for CompileArtifact — the public name going forward.
|
|
142
|
+
*/
|
|
143
|
+
type Artifact<T = unknown, R = unknown> = CompileArtifact<T, R>;
|
|
144
|
+
/**
|
|
145
|
+
* Describes what each flat parameter of the compiled factory expects.
|
|
146
|
+
* Order matches the factory's parameter order.
|
|
147
|
+
*/
|
|
148
|
+
type ParamSlot = {
|
|
149
|
+
kind: "scope";
|
|
150
|
+
name: string;
|
|
151
|
+
} | {
|
|
152
|
+
kind: "accessor";
|
|
153
|
+
} | {
|
|
154
|
+
kind: "handler";
|
|
155
|
+
ns: string;
|
|
156
|
+
m: string;
|
|
157
|
+
} | {
|
|
158
|
+
kind: "boundary";
|
|
159
|
+
index: number;
|
|
160
|
+
};
|
|
104
161
|
/**
|
|
105
162
|
* Path getter function
|
|
106
163
|
*/
|
|
@@ -246,11 +303,62 @@ type TransformFn = (node: Record<string, unknown>) => Expression;
|
|
|
246
303
|
*/
|
|
247
304
|
type Transforms = Record<string, TransformFn>;
|
|
248
305
|
|
|
306
|
+
/**
|
|
307
|
+
* @statedelta-libs/expressions - JIT Compiler
|
|
308
|
+
*
|
|
309
|
+
* IR-based pipeline: receives IR from analyze(), emits optimized JS via AST.
|
|
310
|
+
* Factory with flat parameters — zero destructuring, zero property chains.
|
|
311
|
+
* No DSL knowledge — no guards, no type detection.
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/** Result when returnCode is true */
|
|
315
|
+
interface JITCodeResult {
|
|
316
|
+
code: string;
|
|
317
|
+
deps: string[];
|
|
318
|
+
hash: string;
|
|
319
|
+
dataRoots: string[];
|
|
320
|
+
scopeFns: string[];
|
|
321
|
+
paramNames: string[];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @statedelta-libs/expressions - Cache
|
|
326
|
+
*
|
|
327
|
+
* LRU cache for compiled expressions and compile artifacts.
|
|
328
|
+
* Performance: cache hit ~10M ops/sec
|
|
329
|
+
*/
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Shared compile cache — stores context-free CompileArtifacts.
|
|
333
|
+
* Can be shared between ExpressionCompiler instances for multi-tenant scenarios.
|
|
334
|
+
* Dual mode: separate closures and JIT caches.
|
|
335
|
+
*/
|
|
336
|
+
declare class CompileCache {
|
|
337
|
+
private closures;
|
|
338
|
+
private jit;
|
|
339
|
+
constructor(maxSize?: number);
|
|
340
|
+
/**
|
|
341
|
+
* Get or compile artifact by key and mode.
|
|
342
|
+
*/
|
|
343
|
+
getOrCompile<V>(key: string, mode: "closures" | "jit", factory: () => V): V;
|
|
344
|
+
/**
|
|
345
|
+
* Cache size per mode
|
|
346
|
+
*/
|
|
347
|
+
get size(): {
|
|
348
|
+
closures: number;
|
|
349
|
+
jit: number;
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* Clear both caches
|
|
353
|
+
*/
|
|
354
|
+
clear(): void;
|
|
355
|
+
}
|
|
356
|
+
|
|
249
357
|
/**
|
|
250
358
|
* @statedelta-libs/expressions - ExpressionCompiler
|
|
251
359
|
*
|
|
252
360
|
* Unified public API for expression compilation.
|
|
253
|
-
*
|
|
361
|
+
* Phase 4: two-level cache (instance + shared compile), DI-first pattern.
|
|
254
362
|
*/
|
|
255
363
|
|
|
256
364
|
interface ExpressionCompilerOptions<T = unknown> {
|
|
@@ -264,6 +372,8 @@ interface ExpressionCompilerOptions<T = unknown> {
|
|
|
264
372
|
boundaries?: BoundaryDef<T>[];
|
|
265
373
|
/** Handler namespaces — services with injected context */
|
|
266
374
|
handlers?: HandlersMap;
|
|
375
|
+
/** Shared compile cache — enables cross-instance cache sharing */
|
|
376
|
+
compileCache?: CompileCache;
|
|
267
377
|
}
|
|
268
378
|
interface JITOptions {
|
|
269
379
|
/** Use accessor to resolve paths instead of direct access */
|
|
@@ -271,6 +381,16 @@ interface JITOptions {
|
|
|
271
381
|
/** Prefix for paths that bypass accessor (direct access) */
|
|
272
382
|
lexicalPrefix?: string;
|
|
273
383
|
}
|
|
384
|
+
interface CompileConfig {
|
|
385
|
+
/** Use accessor to resolve paths instead of direct access */
|
|
386
|
+
useAccessor?: boolean;
|
|
387
|
+
}
|
|
388
|
+
interface EvalOptions {
|
|
389
|
+
/** Pipeline: "closures" (default) or "jit" */
|
|
390
|
+
mode?: "closures" | "jit";
|
|
391
|
+
/** Use accessor to resolve paths instead of direct access */
|
|
392
|
+
useAccessor?: boolean;
|
|
393
|
+
}
|
|
274
394
|
/**
|
|
275
395
|
* Context available via `this` inside every handler (bound via .bind()).
|
|
276
396
|
* Created once per ExpressionCompiler instance — zero per-call overhead.
|
|
@@ -290,13 +410,18 @@ declare class ExpressionCompiler<T = unknown> {
|
|
|
290
410
|
private readonly accessor?;
|
|
291
411
|
private readonly boundaries?;
|
|
292
412
|
private readonly wrappedHandlers?;
|
|
413
|
+
private readonly compileCache?;
|
|
293
414
|
private readonly cacheClosures;
|
|
294
415
|
private readonly cacheJIT;
|
|
295
416
|
constructor(options?: ExpressionCompilerOptions<T>);
|
|
296
|
-
/** Compile via closure composition
|
|
297
|
-
compile<R = unknown>(expr: Expression):
|
|
298
|
-
/** Compile via JS code generation (JIT) */
|
|
299
|
-
jit<R = unknown>(expr: Expression, opts?: JITOptions):
|
|
417
|
+
/** Compile via closure composition — DSL → IR → closures */
|
|
418
|
+
compile<R = unknown>(expr: Expression, opts?: CompileConfig): CompileArtifact<T, R>;
|
|
419
|
+
/** Compile via JS code generation (JIT) — DSL → IR → AST → JS */
|
|
420
|
+
jit<R = unknown>(expr: Expression, opts?: JITOptions): CompileArtifact<T, R>;
|
|
421
|
+
/** Return generated JS code without creating function — for inspection/testing */
|
|
422
|
+
jitCode(expr: Expression, opts?: JITOptions): JITCodeResult;
|
|
423
|
+
/** Compile, bind with default context, and execute in one step */
|
|
424
|
+
eval<R = unknown>(expr: Expression, data: T, opts?: EvalOptions): R;
|
|
300
425
|
/**
|
|
301
426
|
* Wrap raw handlers with ctx via .bind().
|
|
302
427
|
* Creates ctx once, binds each handler method once.
|
|
@@ -304,23 +429,36 @@ declare class ExpressionCompiler<T = unknown> {
|
|
|
304
429
|
* Handlers access ctx via `this`.
|
|
305
430
|
*/
|
|
306
431
|
private wrapHandlers;
|
|
307
|
-
/** Compile (closures) and execute in one step */
|
|
308
|
-
evaluate<R = unknown>(expr: Expression, data: T): R;
|
|
309
|
-
/** Compile (JIT) and execute in one step */
|
|
310
|
-
evaluateJIT<R = unknown>(expr: Expression, data: T, opts?: JITOptions): R;
|
|
311
432
|
/** Normalize custom expression to pure DSL */
|
|
312
433
|
normalize(expr: Expression, transforms: Transforms): Expression;
|
|
313
434
|
/** Extract dependencies without compiling */
|
|
314
435
|
extractDeps(expr: Expression): string[];
|
|
315
|
-
/**
|
|
436
|
+
/** Instance cache size per mode */
|
|
316
437
|
get cacheSize(): {
|
|
317
438
|
closures: number;
|
|
318
439
|
jit: number;
|
|
319
440
|
};
|
|
320
|
-
/** Clear
|
|
441
|
+
/** Clear instance caches */
|
|
321
442
|
clearCache(): void;
|
|
322
443
|
/** Read-only access to scope */
|
|
323
444
|
getScope(): Readonly<Scope>;
|
|
445
|
+
/**
|
|
446
|
+
* Get or create compile artifact.
|
|
447
|
+
* Two-level: compile cache (shared, optional) → fresh compile.
|
|
448
|
+
*/
|
|
449
|
+
private getArtifact;
|
|
450
|
+
/**
|
|
451
|
+
* Full compile: analyze → backend → CompileArtifact.
|
|
452
|
+
*/
|
|
453
|
+
private doCompile;
|
|
454
|
+
/**
|
|
455
|
+
* Default BindContext from constructor options.
|
|
456
|
+
*/
|
|
457
|
+
private defaultCtx;
|
|
458
|
+
/**
|
|
459
|
+
* Build compile cache key including compile-time flags.
|
|
460
|
+
*/
|
|
461
|
+
private compileCacheKey;
|
|
324
462
|
}
|
|
325
463
|
|
|
326
464
|
/**
|
|
@@ -521,6 +659,10 @@ declare const isConditionGroup: (v: unknown) => v is ConditionGroup;
|
|
|
521
659
|
* Check if value is any condition expression
|
|
522
660
|
*/
|
|
523
661
|
declare const isConditionExpr: (v: unknown) => v is ConditionExpr;
|
|
662
|
+
/**
|
|
663
|
+
* Check if value is any Expression DSL node
|
|
664
|
+
*/
|
|
665
|
+
declare const isExpression: (v: unknown) => boolean;
|
|
524
666
|
/**
|
|
525
667
|
* Check if value is a literal (not an expression object)
|
|
526
668
|
*/
|
|
@@ -608,6 +750,93 @@ declare function hasDeps(expr: Expression): boolean;
|
|
|
608
750
|
*/
|
|
609
751
|
declare function isPure(expr: Expression): boolean;
|
|
610
752
|
|
|
753
|
+
/**
|
|
754
|
+
* Template Compiler — Types
|
|
755
|
+
*
|
|
756
|
+
* Declarative template system for compiling arbitrary JSON structures
|
|
757
|
+
* using ExpressionCompiler as the engine.
|
|
758
|
+
*/
|
|
759
|
+
|
|
760
|
+
type SchemaType = "string" | "number" | "boolean" | "string[]" | "number[]" | "enum" | "schema" | "object" | "any";
|
|
761
|
+
interface SchemaFieldDef {
|
|
762
|
+
type: SchemaType;
|
|
763
|
+
required?: boolean;
|
|
764
|
+
default?: unknown;
|
|
765
|
+
values?: string[];
|
|
766
|
+
}
|
|
767
|
+
interface FieldHandlerOptions {
|
|
768
|
+
allow?: string[];
|
|
769
|
+
deny?: string[];
|
|
770
|
+
transforms?: Transforms;
|
|
771
|
+
mode?: "closures" | "jit";
|
|
772
|
+
}
|
|
773
|
+
type FieldHandler = (value: unknown, compiler: ExpressionCompiler, options: FieldHandlerOptions) => unknown;
|
|
774
|
+
type CompileFieldDef = {
|
|
775
|
+
compile: "condition";
|
|
776
|
+
} | {
|
|
777
|
+
compile: "expression";
|
|
778
|
+
} | {
|
|
779
|
+
compile: string;
|
|
780
|
+
allow?: string[];
|
|
781
|
+
deny?: string[];
|
|
782
|
+
} | {
|
|
783
|
+
compile: FieldHandler;
|
|
784
|
+
allow?: string[];
|
|
785
|
+
deny?: string[];
|
|
786
|
+
[key: string]: unknown;
|
|
787
|
+
};
|
|
788
|
+
type FieldDef = CompileFieldDef | SchemaFieldDef;
|
|
789
|
+
type DefinitionTemplate = Record<string, FieldDef>;
|
|
790
|
+
interface CompileDefinitionOptions {
|
|
791
|
+
transforms?: Transforms;
|
|
792
|
+
mode?: "closures" | "jit";
|
|
793
|
+
validate?: boolean;
|
|
794
|
+
handlers?: Record<string, FieldHandler>;
|
|
795
|
+
}
|
|
796
|
+
interface CompiledDefinition<T = unknown> {
|
|
797
|
+
[field: string]: unknown;
|
|
798
|
+
/** Bind to a context — re-binds all compiled fields */
|
|
799
|
+
bind(ctx: BindContext): CompiledDefinition<T>;
|
|
800
|
+
}
|
|
801
|
+
type DefinitionValidationErrorType = "unknown_field" | "missing_required" | "type_mismatch" | "invalid_enum";
|
|
802
|
+
interface DefinitionValidationError {
|
|
803
|
+
field: string;
|
|
804
|
+
message: string;
|
|
805
|
+
type: DefinitionValidationErrorType;
|
|
806
|
+
}
|
|
807
|
+
interface DefinitionValidationResult {
|
|
808
|
+
valid: boolean;
|
|
809
|
+
errors: DefinitionValidationError[];
|
|
810
|
+
}
|
|
811
|
+
declare function isCompileField(def: FieldDef): def is CompileFieldDef;
|
|
812
|
+
declare function isSchemaField(def: FieldDef): def is SchemaFieldDef;
|
|
813
|
+
declare function isNativeCompile(def: CompileFieldDef): def is {
|
|
814
|
+
compile: "condition";
|
|
815
|
+
} | {
|
|
816
|
+
compile: "expression";
|
|
817
|
+
};
|
|
818
|
+
declare function isHandlerCompile(def: CompileFieldDef): def is {
|
|
819
|
+
compile: FieldHandler;
|
|
820
|
+
[key: string]: unknown;
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Template Compiler — Core Compilation
|
|
825
|
+
*
|
|
826
|
+
* compileDefinition(): validates, normalizes, compiles, and assembles
|
|
827
|
+
* a JSON structure using a template + ExpressionCompiler.
|
|
828
|
+
*/
|
|
829
|
+
|
|
830
|
+
declare function compileDefinition<T = unknown>(dsl: Record<string, unknown>, template: DefinitionTemplate, compiler: ExpressionCompiler, options?: CompileDefinitionOptions): CompiledDefinition<T>;
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* Template Compiler — Validation
|
|
834
|
+
*
|
|
835
|
+
* Validates a DSL object against a DefinitionTemplate.
|
|
836
|
+
*/
|
|
837
|
+
|
|
838
|
+
declare function validateDefinition(dsl: Record<string, unknown>, template: DefinitionTemplate): DefinitionValidationResult;
|
|
839
|
+
|
|
611
840
|
/**
|
|
612
841
|
* @statedelta-libs/expressions
|
|
613
842
|
*
|
|
@@ -616,4 +845,4 @@ declare function isPure(expr: Expression): boolean;
|
|
|
616
845
|
*/
|
|
617
846
|
declare const VERSION = "3.0.0";
|
|
618
847
|
|
|
619
|
-
export { type AccessorFn, type ArrowExpr, type BoundaryDef, type CompileOptions, type
|
|
848
|
+
export { type AccessorFn, type ArrowExpr, type Artifact, type BindContext, type BoundFn, type BoundaryDef, type CompileArtifact, CompileCache, type CompileConfig, type CompileDefinitionOptions, type CompileFieldDef, type CompileOptions, type CompiledDefinition, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type DefinitionTemplate, type DefinitionValidationError, type DefinitionValidationErrorType, type DefinitionValidationResult, type EvalOptions, type Expression, ExpressionCompiler, type ExpressionCompilerOptions, type FieldDef, type FieldHandler, type FieldHandlerOptions, type FnExpr, type HandlerContext, type HandlerFn, type HandlersMap, type JITCodeResult, type JITOptions, type Literal, type ParamSlot, type PipeExpr, type RefExpr, type SchemaFieldDef, type SchemaType, type Scope, type TransformFn, type Transforms, VERSION, type ValidateOptions, type ValidationResult, type WrappedHandlers, assertValid, builders, compileDefinition, compilePath, createBoundFn, extractDeps, get, hasDeps, hasWildcard, isArrow, isCompileField, isCondition, isConditionExpr, isConditionGroup, isConditional, isExpression, isFn, isHandlerCompile, isLiteral, isNativeCompile, isPipe, isPure, isRef, isSchemaField, isValid, normalizePath, validate, validateDefinition };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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};
|
|
1
|
+
import {generate,builders}from'omni-ast';var be=Object.defineProperty;var Te=(e,n)=>{for(var t in n)be(e,t,{get:n[t],enumerable:true});};function j(e,n,t){let i=e;return Object.defineProperty(i,"deps",{value:n,enumerable:true,configurable:true}),Object.defineProperty(i,"hash",{value:t,enumerable:true,configurable:true}),i}var K=Symbol("$");function P(e){return e!==null&&typeof e=="object"&&e[K]===true}function y(e,n){if(e===null||typeof e!="object")return n.rawPrimitive(e);if(P(e))return Ae(e,n);if(Array.isArray(e)){let o=e.map(r=>y(r,n));return n.rawArray(e,o)}let t=e,i=Object.keys(t),s=new Array(i.length);for(let o=0;o<i.length;o++){let r=i[o];s[o]=[r,y(t[r],n)];}return n.rawObject(t,s)}function Ae(e,n){switch(e.t){case "d":return n.data(e);case "a":return n.arg(e);case "x":return n.accessor(e);case "c":{let t=e,i=t.a.map(s=>y(s,n));return n.call(t,i)}case "r":return n.ref(e);case "ch":{let t=e,i=t.a.map(s=>y(s,n));return n.callH(t,i)}case "rh":return n.refH(e);case "?":{let t=e;return n.cond(t,y(t.test,n),y(t.then,n),y(t.else,n))}case "|":{let t=e;return n.pipe(t,y(t.head,n),t.steps.map(i=>y(i,n)))}case "=>":{let t=e;return n.arrow(t,y(t.body,n))}case "!":{let t=e;return n.not(t,y(t.v,n))}case "cmp":{let t=e;return n.compare(t,y(t.l,n),y(t.r,n))}case "lg":{let t=e;return n.logic(t,t.c.map(i=>y(i,n)))}case "v":return n.literal(e);case "s":return n.static_(e);case "b":return n.boundary(e);default:throw new Error(`Unknown IR node type: ${e.t}`)}}function ne(e,n){return {data:()=>e,arg:()=>e,accessor:()=>e,call:()=>e,ref:()=>e,callH:()=>e,refH:()=>e,cond:()=>e,pipe:()=>e,arrow:()=>e,not:()=>e,compare:()=>e,logic:()=>e,literal:()=>e,static_:()=>e,boundary:()=>e,rawPrimitive:()=>e,rawObject:()=>e,rawArray:()=>e,...n}}var te=new Map;function S(e){let n=[],t=e.length,i=0,s="";for(;i<t;){let o=e[i];if(o===".")s&&(n.push({type:"key",value:s}),s=""),i++;else if(o==="["){s&&(n.push({type:"key",value:s}),s=""),i++;let r=i;for(;i<t&&e[i]!=="]";)i++;let a=e.slice(r,i);if(i++,a==="*")n.push({type:"wildcard",value:"*"});else {let l=parseInt(a,10);n.push({type:"index",value:isNaN(l)?a:l});}}else s+=o,i++;}return s&&n.push({type:"key",value:s}),n}function N(e){return e.includes("[*]")}function H(e){let n=te.get(e);return n||(n=N(e)?Ie(e):we(e),te.set(e,n),n)}function we(e){if(!e.includes(".")&&!e.includes("["))return s=>s?.[e];let n=S(e),t=n.length;if(t===2){let[s,o]=n,r=s.value,a=o.value;return l=>l?.[r]?.[a]}if(t===3){let[s,o,r]=n,a=s.value,l=o.value,c=r.value;return u=>u?.[a]?.[l]?.[c]}let i=n.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 Ie(e){let n=S(e),t=[];for(let i=0;i<n.length;i++)n[i].type==="wildcard"&&t.push(i);return t.length===1?Ee(n,t[0]):$e(n,t)}function Ee(e,n){let t=e.slice(0,n).map(r=>r.value),i=e.slice(n+1).map(r=>r.value),s=t.length,o=i.length;if(o===0){if(s===1){let r=t[0];return a=>a?.[r]}return r=>{let a=r;for(let l=0;l<s&&a!=null;l++)a=a[t[l]];return a}}if(o===1){let r=i[0];if(s===1){let a=t[0];return l=>{let c=l?.[a];if(Array.isArray(c))return c.map(u=>u?.[r])}}return a=>{let l=a;for(let c=0;c<s&&l!=null;c++)l=l[t[c]];if(Array.isArray(l))return l.map(c=>c?.[r])}}return r=>{let a=r;for(let l=0;l<s&&a!=null;l++)a=a[t[l]];if(Array.isArray(a))return a.map(l=>{let c=l;for(let u=0;u<o&&c!=null;u++)c=c[i[u]];return c})}}function $e(e,n){let t=[],i=0;for(let o=0;o<n.length;o++){let r=n[o],a=o===n.length-1,l=e.slice(i,r).map(c=>c.value);l.length>0&&t.push({type:"access",keys:l}),t.push({type:a?"map":"flatMap",keys:[]}),i=r+1;}let s=e.slice(i).map(o=>o.value);return o=>{let r=o;for(let a of t){if(r==null)return;if(a.type==="access")for(let l of a.keys){if(r==null)return;r=r[l];}else if(a.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(l=>{let c=l;return Array.isArray(c)?c:[c]});}else if(a.type==="map"){if(!Array.isArray(r))return;s.length>0&&(r=r.map(l=>{let c=l;for(let u of s){if(c==null)return;c=c[u];}return c}));}}return r}}function Fe(e,n){return H(n)(e)}function A(e){let n=e.indexOf("[*]");return n===-1?e:e.slice(0,n)}function re(e){let{scope:n,accessor:t,handlers:i,boundaryFns:s}=e;return {data(o){return H(o.p)},arg(o){return H(o.p)},accessor(o){let r=t,a=o.p;return ()=>r(a)},call(o,r){let a=n[o.n];if(!a){let c=o.n;return ()=>{throw new Error(`Function not found in scope: ${c}`)}}let l=r.length;if(l===0)return ()=>a();if(l===1){let[c]=r;return u=>a(c(u))}if(l===2){let[c,u]=r;return f=>a(c(f),u(f))}if(l===3){let[c,u,f]=r;return d=>a(c(d),u(d),f(d))}return c=>a(...r.map(u=>u(c)))},ref(o){let r=n[o.n];if(!r){let a=o.n;return ()=>{throw new Error(`Function not found in scope: ${a}`)}}return ()=>r},callH(o,r){let a=i?.[o.ns]?.[o.m];if(!a)throw new Error(`Handler not found: ${o.ns}:${o.m}`);let l=r.length;if(l===0)return ()=>a();if(l===1){let[c]=r;return u=>a(c(u))}if(l===2){let[c,u]=r;return f=>a(c(f),u(f))}if(l===3){let[c,u,f]=r;return d=>a(c(d),u(d),f(d))}return c=>a(...r.map(u=>u(c)))},refH(o){let r=i?.[o.ns]?.[o.m];if(!r)throw new Error(`Handler not found: ${o.ns}:${o.m}`);return ()=>r},cond(o,r,a,l){return c=>r(c)?a(c):l(c)},pipe(o,r,a){let l=a.length;if(l===1){let[c]=a;return u=>{let f=r(u),d=c(u);return typeof d=="function"?d(f):d}}if(l===2){let[c,u]=a;return f=>{let d=r(f),m=c(f);return d=typeof m=="function"?m(d):m,m=u(f),typeof m=="function"?m(d):m}}if(l===3){let[c,u,f]=a;return d=>{let m=r(d),h=c(d);return m=typeof h=="function"?h(m):h,h=u(d),m=typeof h=="function"?h(m):h,h=f(d),typeof h=="function"?h(m):h}}return c=>{let u=r(c);for(let f=0;f<l;f++){let d=a[f](c);u=typeof d=="function"?d(u):d;}return u}},arrow(o,r){let a=o.params;if(a.length===0)return c=>()=>r(c);let l=a.length;return c=>(...u)=>{let f={...c};for(let d=0;d<l;d++)f[a[d]]=u[d];return r(f)}},not(o,r){return a=>!r(a)},compare(o,r,a){switch(o.op){case "eq":return l=>r(l)===a(l);case "neq":return l=>r(l)!==a(l);case "gt":return l=>r(l)>a(l);case "gte":return l=>r(l)>=a(l);case "lt":return l=>r(l)<a(l);case "lte":return l=>r(l)<=a(l);case "in":return l=>{let c=a(l);return Array.isArray(c)&&c.includes(r(l))};case "notIn":return l=>{let c=a(l);return !Array.isArray(c)||!c.includes(r(l))};case "contains":return l=>{let c=r(l);return Array.isArray(c)&&c.includes(a(l))};case "notContains":return l=>{let c=r(l);return !Array.isArray(c)||!c.includes(a(l))};case "exists":return l=>r(l)!==void 0;case "notExists":return l=>r(l)===void 0;case "matches":return l=>{let c=r(l),u=a(l);return typeof c!="string"||typeof u!="string"?false:new RegExp(u).test(c)};case "notMatches":return l=>{let c=r(l),u=a(l);return typeof c!="string"||typeof u!="string"?true:!new RegExp(u).test(c)};case "startsWith":return l=>{let c=r(l),u=a(l);return typeof c=="string"&&typeof u=="string"&&c.startsWith(u)};case "endsWith":return l=>{let c=r(l),u=a(l);return typeof c=="string"&&typeof u=="string"&&c.endsWith(u)};default:return l=>r(l)===a(l)}},logic(o,r){let a=r.length;if(a===0)return ()=>o.op==="AND";if(a===1)return l=>!!r[0](l);if(a===2){let[l,c]=r;return o.op==="AND"?u=>!!l(u)&&!!c(u):u=>!!l(u)||!!c(u)}if(a===3){let[l,c,u]=r;return o.op==="AND"?f=>!!l(f)&&!!c(f)&&!!u(f):f=>!!l(f)||!!c(f)||!!u(f)}return o.op==="AND"?l=>{for(let c=0;c<a;c++)if(!r[c](l))return false;return true}:l=>{for(let c=0;c<a;c++)if(r[c](l))return true;return false}},literal(o){let r=o.v;return ()=>r},static_(o){let r=o.v;return ()=>r},boundary(o){return s[o.i]},rawPrimitive(o){return ()=>o},rawObject(o,r){let a=r.length;return l=>{let c={};for(let u=0;u<a;u++)c[r[u][0]]=r[u][1](l);return c}},rawArray(o,r){return a=>r.map(l=>l(a))}}}function ie(e,n,t,i){return {deps:n,hash:i,bind:o=>{let r=re({scope:o?.scope??{},accessor:o?.accessor,handlers:o?.handlers,boundaryFns:t}),a=y(e,r);return j(a,n,i)}}}var Se="data",ke="accessor",Oe={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function q(e){let n=S(e);if(n.length===0)return builders.identifier("undefined");let t=n[0],i=builders.identifier(t.value);for(let s=1;s<n.length;s++){let o=n[s];o.type==="key"?i=builders.memberExpression(i,builders.identifier(o.value),false,true):i=builders.memberExpression(i,builders.literal(o.value),true,true);}return i}function De(e){let n=e.indexOf("[*]"),t=e.slice(0,n),i=e.slice(n+3),s;if(t)s=q(t);else return builders.identifier("undefined");if(!i||i==="")return s;if(i.includes("[*]"))return oe(s,i);let o="_i",r=i.startsWith(".")?i.slice(1):i,a=builders.identifier(o);if(r){let l=S(r);for(let c of l)c.type==="key"?a=builders.memberExpression(a,builders.identifier(c.value),false,true):a=builders.memberExpression(a,builders.literal(c.value),true,true);}return builders.callExpression(builders.memberExpression(s,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],a)])}function oe(e,n){let t=n.indexOf("[*]"),i=n.slice(0,t),s=n.slice(t+3),o="_i",r=i.startsWith(".")?i.slice(1):i,a=builders.identifier(o);if(r){let c=S(r);for(let u of c)u.type==="key"&&(a=builders.memberExpression(a,builders.identifier(u.value),false,true));}if(s.includes("[*]")){let c=oe(a,s);return builders.callExpression(builders.memberExpression(e,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],c)])}let l=s.startsWith(".")?s.slice(1):s;if(l){let c=S(l);for(let u of c)u.type==="key"&&(a=builders.memberExpression(a,builders.identifier(u.value),false,true));}return builders.callExpression(builders.memberExpression(e,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],a)])}function z(e){if(e===void 0)return builders.identifier("undefined");if(e===null)return builders.literal(null);if(typeof e!="object")return builders.literal(e);if(Array.isArray(e))return builders.arrayExpression(e.map(z));let t=Object.entries(e).map(([i,s])=>builders.property(builders.identifier(i),z(s)));return builders.objectExpression(t)}function se(){return {data(e){return e.w?De(e.p):q(e.p)},arg(e){return q(e.p)},accessor(e){return builders.callExpression(builders.identifier(ke),[builders.literal(e.p)])},call(e,n){let t=builders.identifier(e.n);return builders.callExpression(t,n)},ref(e){return builders.identifier(e.n)},callH(e,n){return builders.callExpression(builders.identifier(`h_${e.ns}_${e.m}`),n)},refH(e){return builders.identifier(`h_${e.ns}_${e.m}`)},cond(e,n,t,i){return builders.conditionalExpression(n,t,i)},pipe(e,n,t){let i=n;for(let s of t)i=builders.callExpression(s,[i]);return i},arrow(e,n){let t=e.params.map(i=>builders.identifier(i));return builders.arrowFunctionExpression(t,n)},not(e,n){return builders.unaryExpression("!",n)},compare(e,n,t){let i=Oe[e.op];if(i)return builders.binaryExpression(i,n,t);switch(e.op){case "in":return builders.callExpression(builders.memberExpression(t,builders.identifier("includes")),[n]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(t,builders.identifier("includes")),[n]));case "contains":return builders.callExpression(builders.memberExpression(n,builders.identifier("includes"),false,true),[t]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(n,builders.identifier("includes"),false,true),[t]));case "exists":return builders.binaryExpression("!=",n,builders.literal(null));case "notExists":return builders.binaryExpression("==",n,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[t]),builders.identifier("test")),[n]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[t]),builders.identifier("test")),[n]));case "startsWith":return builders.callExpression(builders.memberExpression(n,builders.identifier("startsWith"),false,true),[t]);case "endsWith":return builders.callExpression(builders.memberExpression(n,builders.identifier("endsWith"),false,true),[t]);default:return builders.binaryExpression("===",n,t)}},logic(e,n){let t=e.op==="AND"?"&&":"||";if(n.length===0)return builders.literal(e.op==="AND");if(n.length===1)return n[0];let i=n[0];for(let s=1;s<n.length;s++)i=builders.logicalExpression(t,i,n[s]);return i},literal(e){return z(e.v)},static_(e){return z(e.v)},boundary(e){return builders.callExpression(builders.identifier(`b${e.i}`),[builders.identifier(Se)])},rawPrimitive(e){return e===void 0?builders.identifier("undefined"):e===null?builders.literal(null):builders.literal(e)},rawObject(e,n){let t=n.map(([i,s])=>builders.property(builders.identifier(i),s));return builders.objectExpression(t)},rawArray(e,n){return builders.arrayExpression(n)}}}function je(e){let n=e.indexOf("."),t=e.indexOf("["),i=e.length;return n!==-1&&(i=Math.min(i,n)),t!==-1&&(i=Math.min(i,t)),e.slice(0,i)}function ae(e,n){let t=new Set,i=new Set,s=new Set,o=false;return y(e,ne(void 0,{data(r){let a=je(r.p);a&&i.add(a);},accessor(){o=true;},call(r){t.add(r.n);},ref(r){t.add(r.n);},callH(r){s.add(`${r.ns}:${r.m}`);},refH(r){s.add(`${r.ns}:${r.m}`);}})),{scopeFns:t,dataRoots:i,hasAccessor:o,handlerRefs:s,boundaryCount:n}}function le(e){let n=[],t=[];for(let i of e.scopeFns)n.push({kind:"scope",name:i}),t.push(i);e.hasAccessor&&(n.push({kind:"accessor"}),t.push("accessor"));for(let i of e.handlerRefs){let[s,o]=i.split(":");n.push({kind:"handler",ns:s,m:o}),t.push(`h_${s}_${o}`);}for(let i=0;i<e.boundaryCount;i++)n.push({kind:"boundary",index:i}),t.push(`b${i}`);return {params:n,paramNames:t}}function ce(e,n,t){let i=generate(e),s="";n.dataRoots.size>0&&(s=`const{${[...n.dataRoots].join(",")}}=data??{};`);let o=`${s}return ${i}`;return t.length>0?`(function(${t.join(",")}){return function(data){${o}}})`:`(function(){return function(data){${o}}})`}function Ne(e,n,t){let i=new Array(e.length);for(let s=0;s<e.length;s++){let o=e[s];switch(o.kind){case "scope":i[s]=n.scope?.[o.name];break;case "accessor":i[s]=n.accessor;break;case "handler":i[s]=n.handlers?.[o.ns]?.[o.m];break;case "boundary":i[s]=t[o.index];break}}return i}function U(e,n,t,i){let{hash:s,returnCode:o=false}=i,r=ae(e,t.length),a=y(e,se()),{params:l,paramNames:c}=le(r),u=ce(a,r,c);if(o)return {code:u,deps:n,hash:s,dataRoots:[...r.dataRoots],scopeFns:[...r.scopeFns],paramNames:c};let f;try{f=new Function(`return ${u}`)();}catch(m){throw new Error(`JIT compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${m instanceof Error?m.message:String(m)}`)}return {deps:n,hash:s,bind:m=>{let h=Ne(l,m??{},t),J=f(...h);return j(J,n,s)}}}var V=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),w=e=>e!==null&&typeof e=="object"&&"$"in e&&typeof e.$=="string"&&Object.keys(e).length===1,I=e=>e!==null&&typeof e=="object"&&"$if"in e&&"then"in e,E=e=>e!==null&&typeof e=="object"&&"$fn"in e&&typeof e.$fn=="string",$=e=>e!==null&&typeof e=="object"&&"$pipe"in e&&Array.isArray(e.$pipe),F=e=>e!==null&&typeof e=="object"&&"$arrow"in e,b=e=>e!==null&&typeof e=="object"&&"left"in e&&"op"in e&&V.has(e.op)&&!("$"in e)&&!("$if"in e)&&!("$fn"in e),T=e=>e!==null&&typeof e=="object"&&"logic"in e&&"conditions"in e,He=e=>b(e)||T(e),Be=e=>e!==null&&typeof e=="object"&&(w(e)||E(e)||I(e)||$(e)||F(e)||b(e)||T(e)),ze=e=>{if(e===null)return true;let n=typeof e;if(n==="string"||n==="number"||n==="boolean"||Array.isArray(e))return true;if(n==="object"&&e!==null){let t=e,i="left"in t&&"op"in t&&V.has(t.op);return !("$"in t)&&!("$if"in t)&&!("$fn"in t)&&!("$pipe"in t)&&!("$arrow"in t)&&!i&&!("logic"in t)}return false};function v(e,n){return _(e,n)}function _(e,n){if(e===null)return null;if(typeof e!="object")return e;if(Array.isArray(e))return e.map(o=>_(o,n));let t=e,i=Object.keys(t);for(let o of i)if(o in n){let r=n[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 _(r,n)}let s={};for(let o of i)s[o]=_(t[o],n);return s}function Ve(e){let n=e.indexOf("."),t=e.indexOf("["),i=e.length;return n!==-1&&(i=Math.min(i,n)),t!==-1&&(i=Math.min(i,t)),e.slice(0,i)}function g(e,n,t){return e[K]=true,t&&n&&(e.loc=n),e}function Q(e,n={}){let t=n.transforms?v(e,n.transforms):e,i=new Set,s=[],o=n.trackLocations===true;return {ir:C(t,n,new Set,i,s,o,o?"root":void 0),deps:Array.from(i),boundaryFns:s}}function C(e,n,t,i,s,o,r){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return Ue(e,n,t,i,s,o,r);let a=e;if(n.boundaries){for(let l of n.boundaries)if(l.check(a)){let c=l.handle(a),u=s.length;return s.push(c),g({t:"b",i:u},r,o)}}return w(e)?pe(e.$,n,t,i,o,r):I(e)?ve(e,n,t,i,s,o,r):$(e)?We(e,n,t,i,s,o,r):E(e)?Me(e,n,t,i,s,o,r):F(e)?Ge(e,n,t,i,s,o,r):b(e)?Je(e,n,t,i,s,o,r):T(e)?Ke(e,n,t,i,s,o,r):qe(a,n,t,i,s,o,r)}function _e(e,n){return n?e===n||e.startsWith(n+"."):false}function pe(e,n,t,i,s,o){let r=Ve(e);if(t.has(r))return g({t:"a",n:r,p:e},o,s);if(n.accessor){if(_e(e,n.lexicalPrefix)){i.add(A(e));let l={t:"d",p:e};return N(e)&&(l.w=true),g(l,o,s)}return i.add(A(e)),g({t:"x",p:e},o,s)}i.add(A(e));let a={t:"d",p:e};return N(e)&&(a.w=true),g(a,o,s)}function ve(e,n,t,i,s,o,r){let a;if(typeof e.$if=="string"){let u=e.$if.startsWith("!"),f=u?e.$if.slice(1):e.$if,d=pe(f,n,t,i,o,o?`${r}.$if`:void 0);a=u?g({t:"!",v:d},r,o):d;}else a=C(e.$if,n,t,i,s,o,o?`${r}.$if`:void 0);let l=C(e.then,n,t,i,s,o,o?`${r}.then`:void 0),c=e.else!==void 0?C(e.else,n,t,i,s,o,o?`${r}.else`:void 0):void 0;return g({t:"?",test:a,then:l,else:c},r,o)}function We(e,n,t,i,s,o,r){let a=e.$pipe;if(a.length===0)return;if(a.length===1)return C(a[0],n,t,i,s,o,o?`${r}.$pipe[0]`:void 0);let l=C(a[0],n,t,i,s,o,o?`${r}.$pipe[0]`:void 0),c=[];for(let u=1;u<a.length;u++)c.push(C(a[u],n,t,i,s,o,o?`${r}.$pipe[${u}]`:void 0));return g({t:"|",head:l,steps:c},r,o)}function Me(e,n,t,i,s,o,r){let a=e.$fn,l=a.indexOf(":"),c=e.args!==void 0;if(l!==-1){let f=a.slice(0,l),d=a.slice(l+1);if(!c)return g({t:"rh",ns:f,m:d},r,o);let m=e.args.map((h,J)=>C(h,n,t,i,s,o,o?`${r}.$fn.args[${J}]`:void 0));return g({t:"ch",ns:f,m:d,a:m},r,o)}if(!c)return g({t:"r",n:a},r,o);let u=e.args.map((f,d)=>C(f,n,t,i,s,o,o?`${r}.$fn.args[${d}]`:void 0));return g({t:"c",n:a,a:u},r,o)}function Ge(e,n,t,i,s,o,r){let a=e.args??[],l;if(a.length===0)l=t;else {l=new Set(t);for(let u of a)l.add(u);}let c=C(e.$arrow,n,l,i,s,o,o?`${r}.$arrow`:void 0);return g({t:"=>",params:a,body:c},r,o)}function Je(e,n,t,i,s,o,r){let a=C(e.left,n,t,i,s,o,o?`${r}.left`:void 0),l=e.right!==void 0?C(e.right,n,t,i,s,o,o?`${r}.right`:void 0):void 0;return g({t:"cmp",op:e.op,l:a,r:l},r,o)}function Ke(e,n,t,i,s,o,r){let a=e.conditions.map((l,c)=>C(l,n,t,i,s,o,o?`${r}.conditions[${c}]`:void 0));return g({t:"lg",op:e.logic,c:a},r,o)}function qe(e,n,t,i,s,o,r){let a=Object.keys(e);if(a.length===0)return g({t:"s",v:e},r,o);let l=false,c={};for(let u of a){let f=C(e[u],n,t,i,s,o,o?`${r}.${u}`:void 0);c[u]=f,l||(l=ue(f));}return l?c:g({t:"s",v:e},r,o)}function Ue(e,n,t,i,s,o,r){if(e.length===0)return g({t:"s",v:e},r,o);let a=false,l=[];for(let c=0;c<e.length;c++){let u=C(e[c],n,t,i,s,o,o?`${r}[${c}]`:void 0);l[c]=u,a||(a=ue(u));}return a?l:g({t:"s",v:e},r,o)}function ue(e){return e===null||typeof e!="object"?false:P(e)?true:Y(e)}function Y(e){if(P(e))return true;if(Array.isArray(e)){for(let n=0;n<e.length;n++)if(Y(e[n]))return true;return false}if(typeof e=="object"&&e!==null){let n=Object.keys(e);for(let t=0;t<n.length;t++)if(Y(e[n[t]]))return true}return false}function B(e){let n=new Set;return R(e,n),Array.from(n)}function R(e,n){if(e===null||typeof e!="object")return;if(Array.isArray(e)){for(let s=0;s<e.length;s++)R(e[s],n);return}if(w(e)){n.add(A(e.$));return}if(I(e)){if(typeof e.$if=="string"){let s=e.$if.startsWith("!")?e.$if.slice(1):e.$if;n.add(A(s));}else R(e.$if,n);R(e.then,n),e.else!==void 0&&R(e.else,n);return}if($(e)){for(let s=0;s<e.$pipe.length;s++)R(e.$pipe[s],n);return}if(E(e)){if(e.args)for(let s=0;s<e.args.length;s++)R(e.args[s],n);return}if(F(e)){let s=new Set;R(e.$arrow,s);let o=new Set(e.args??[]);for(let r of s){let a=r.split(".")[0].split("[")[0];o.has(a)||n.add(r);}return}if(b(e)){R(e.left,n),e.right!==void 0&&R(e.right,n);return}if(T(e)){for(let s=0;s<e.conditions.length;s++)R(e.conditions[s],n);return}let t=e,i=Object.keys(t);for(let s=0;s<i.length;s++)R(t[i[s]],n);}function Ye(e){return B(e).length>0}function Qe(e){return B(e).length===0}var k=class{constructor(n=1e3){this.cache=new Map,this._maxSize=n;}getOrCompile(n,t){let i=this.cache.get(n);if(i!==void 0)return this.cache.delete(n),this.cache.set(n,i),i;let s=t();if(this.cache.size>=this._maxSize){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}return this.cache.set(n,s),s}has(n){return this.cache.has(n)}delete(n){return this.cache.delete(n)}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(n){for(this._maxSize=n;this.cache.size>this._maxSize;){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}}},X=class{constructor(n=1e3){this.closures=new k(n),this.jit=new k(n);}getOrCompile(n,t,i){return (t==="closures"?this.closures:this.jit).getOrCompile(n,i)}get size(){return {closures:this.closures.size,jit:this.jit.size}}clear(){this.closures.clear(),this.jit.clear();}};function Z(e){return JSON.stringify(e)}var L=class{constructor(n={}){this.scope=n.scope??{},this.accessor=n.accessor,this.boundaries=n.boundaries,this.compileCache=n.compileCache,this.cacheClosures=new k(n.cacheSize??1e3),this.cacheJIT=new k(n.cacheSize??1e3),n.handlers&&(this.wrappedHandlers=this.wrapHandlers(n.handlers));}compile(n,t){let i=Z(n),s=t?.useAccessor??!!this.accessor,o=this.compileCacheKey(i,s);return this.cacheClosures.getOrCompile(o,()=>{let r=this.getArtifact(n,i,s,"closures"),a=this.defaultCtx(s);return {deps:r.deps,hash:r.hash,bind:l=>r.bind(l??a)}})}jit(n,t){let i=Z(n),s=t?.useAccessor??false,o=this.compileCacheKey(i,s,t?.lexicalPrefix);return this.cacheJIT.getOrCompile(o,()=>{let r=this.getArtifact(n,i,s,"jit",t?.lexicalPrefix),a=this.defaultCtx(s);return {deps:r.deps,hash:r.hash,bind:l=>r.bind(l??a)}})}jitCode(n,t){let s=t?.useAccessor??false?this.accessor??(()=>{}):void 0,{ir:o,deps:r,boundaryFns:a}=Q(n,{accessor:s,boundaries:this.boundaries,handlers:this.wrappedHandlers,lexicalPrefix:t?.lexicalPrefix});return U(o,r,a,{hash:JSON.stringify(n),returnCode:true})}eval(n,t,i){return i?.mode==="jit"?this.jit(n,{useAccessor:i?.useAccessor}).bind()(t):this.compile(n,{useAccessor:i?.useAccessor}).bind()(t)}wrapHandlers(n){let t={},i={};for(let s of Object.keys(n)){i[s]={};for(let o of Object.keys(n[s]))i[s][o]=n[s][o].bind(t);}return t.accessor=this.accessor,t.handlers=i,t.compiler=this,t.scope=this.scope,i}normalize(n,t){return v(n,t)}extractDeps(n){return B(n)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}getArtifact(n,t,i,s,o){let r=this.compileCacheKey(t,i,o);return this.compileCache?this.compileCache.getOrCompile(r,s,()=>this.doCompile(n,i,s,o)):this.doCompile(n,i,s,o)}doCompile(n,t,i,s){let o=t?this.accessor??(()=>{}):void 0,{ir:r,deps:a,boundaryFns:l}=Q(n,{accessor:o,boundaries:this.boundaries,handlers:this.wrappedHandlers,lexicalPrefix:s}),c=JSON.stringify(n);return i==="jit"?U(r,a,l,{hash:c}):ie(r,a,l,c)}defaultCtx(n){return {scope:this.scope,accessor:n?this.accessor:void 0,handlers:this.wrappedHandlers}}compileCacheKey(n,t,i){return i?`${t?"a":"n"}:${i}:${n}`:t?`a:${n}`:n}};var Ce={};Te(Ce,{$:()=>fe,$arrow:()=>ge,$call:()=>he,$cond:()=>ye,$fn:()=>de,$if:()=>Le,$pipe:()=>me,arrow:()=>tn,call:()=>rn,cond:()=>nn,fn:()=>Ze,pipe:()=>en,ref:()=>Xe});function fe(e){return {$:e}}var Xe=fe;function de(e,n){return n===void 0?{$fn:e}:{$fn:e,args:n}}var Ze=de;function Le(e,n,t){return t===void 0?{$if:e,then:n}:{$if:e,then:n,else:t}}function me(...e){return {$pipe:e}}var en=me;function ye(e,n,t){return t===void 0?{left:e,op:n}:{left:e,op:n,right:t}}var nn=ye;function ge(e,n){return n===void 0||n.length===0?{$arrow:e}:{$arrow:e,args:n}}var tn=ge;function he(e,n=[]){return {$fn:e,args:n}}var rn=he;function ee(e,n="root",t={}){let i=[];return x(e,n,i,t),{valid:i.length===0,errors:i}}function x(e,n,t,i){if(e===null||typeof e!="object")return;if(Array.isArray(e)){for(let r=0;r<e.length;r++)x(e[r],`${n}[${r}]`,t,i);return}if(w(e)){(!e.$||typeof e.$!="string")&&t.push(`${n}: invalid reference, $ must be non-empty string`);return}if(I(e)){typeof e.$if=="string"?(e.$if.startsWith("!")?e.$if.slice(1):e.$if)||t.push(`${n}.$if: empty path in string shorthand`):x(e.$if,`${n}.$if`,t,i),x(e.then,`${n}.then`,t,i),e.else!==void 0&&x(e.else,`${n}.else`,t,i);return}if($(e)){if(!Array.isArray(e.$pipe)){t.push(`${n}.$pipe: must be an array`);return}if(e.$pipe.length===0){t.push(`${n}.$pipe: must have at least one element`);return}for(let r=0;r<e.$pipe.length;r++)x(e.$pipe[r],`${n}.$pipe[${r}]`,t,i);return}if(E(e)){if(!e.$fn||typeof e.$fn!="string"){t.push(`${n}: invalid function, $fn must be non-empty string`);return}let r=e.$fn.indexOf(":");if(r!==-1){let a=e.$fn.slice(0,r),l=e.$fn.slice(r+1);!a||!l||l.includes(":")?t.push(`${n}: invalid handler format "${e.$fn}", expected "namespace:method"`):i.handlers&&!i.handlers[a]?.[l]&&t.push(`${n}: handler "${e.$fn}" not found`);}else i.scope&&!(e.$fn in i.scope)&&t.push(`${n}: function "${e.$fn}" not found in scope`);if(e.args!==void 0)if(!Array.isArray(e.args))t.push(`${n}.args: must be an array`);else for(let a=0;a<e.args.length;a++)x(e.args[a],`${n}.args[${a}]`,t,i);return}if(F(e)){if(x(e.$arrow,`${n}.$arrow`,t,i),e.args!==void 0)if(!Array.isArray(e.args))t.push(`${n}.args: must be an array of strings`);else for(let r of e.args)(typeof r!="string"||!r)&&t.push(`${n}.args: each arg must be a non-empty string`);return}if(b(e)){V.has(e.op)||t.push(`${n}: invalid operator "${e.op}"`),x(e.left,`${n}.left`,t,i),e.right!==void 0&&x(e.right,`${n}.right`,t,i);return}if(T(e)){if(e.logic!=="AND"&&e.logic!=="OR"&&t.push(`${n}: invalid logic "${e.logic}", must be "AND" or "OR"`),!Array.isArray(e.conditions)){t.push(`${n}.conditions: must be an array`);return}for(let r=0;r<e.conditions.length;r++)x(e.conditions[r],`${n}.conditions[${r}]`,t,i);return}if(i.boundaries?.length){let r=e;for(let a of i.boundaries)if(a.check(r))return}let s=e,o=Object.keys(s);for(let r=0;r<o.length;r++){let a=o[r];x(s[a],`${n}.${a}`,t,i);}}function on(e,n={}){let t=ee(e,"root",n);if(!t.valid)throw new Error(`Invalid expression: ${t.errors.join("; ")}`)}function sn(e,n={}){return ee(e,"root",n).valid}function D(e){return "compile"in e}function O(e){return "type"in e&&!("compile"in e)}function Re(e){return typeof e.compile=="string"}function W(e){return typeof e.compile=="function"}function G(e,n){let t=[];for(let i of Object.keys(e))i in n||t.push({field:i,message:`Unknown field "${i}"`,type:"unknown_field"});for(let[i,s]of Object.entries(n)){let o=e[i],r=i in e&&o!==void 0;if(O(s)){if(s.required&&!r&&s.default===void 0){t.push({field:i,message:`Missing required field "${i}"`,type:"missing_required"});continue}if(r){let a=an(i,o,s);a&&t.push(a);}}}return {valid:t.length===0,errors:t}}function an(e,n,t){switch(t.type){case "string":if(typeof n!="string")return {field:e,message:`Field "${e}" expected string, got ${typeof n}`,type:"type_mismatch"};break;case "number":if(typeof n!="number")return {field:e,message:`Field "${e}" expected number, got ${typeof n}`,type:"type_mismatch"};break;case "boolean":if(typeof n!="boolean")return {field:e,message:`Field "${e}" expected boolean, got ${typeof n}`,type:"type_mismatch"};break;case "string[]":if(!Array.isArray(n)||!n.every(i=>typeof i=="string"))return {field:e,message:`Field "${e}" expected string[], got ${M(n)}`,type:"type_mismatch"};break;case "number[]":if(!Array.isArray(n)||!n.every(i=>typeof i=="number"))return {field:e,message:`Field "${e}" expected number[], got ${M(n)}`,type:"type_mismatch"};break;case "enum":if(!t.values||!t.values.includes(n))return {field:e,message:`Field "${e}" expected one of [${t.values?.join(", ")}], got "${String(n)}"`,type:"invalid_enum"};break;case "object":if(typeof n!="object"||n===null||Array.isArray(n))return {field:e,message:`Field "${e}" expected object, got ${M(n)}`,type:"type_mismatch"};break;case "schema":if(typeof n!="object"||n===null||Array.isArray(n))return {field:e,message:`Field "${e}" expected schema object, got ${M(n)}`,type:"type_mismatch"};break;}return null}function M(e){return e===null?"null":Array.isArray(e)?"array":typeof e}function xe(e,n,t,i){if(i?.validate!==false){let l=G(e,n);if(!l.valid){let c=l.errors.map(u=>u.message).join("; ");throw new Error(`Template validation failed: ${c}`)}}let s=ln(e,n),o={},r={},a=new Set;for(let[l,c]of Object.entries(n)){let u=s[l];if(D(c)){if(u===void 0)continue;let f=cn(l,u,c,t,i);o[l]=f.value,f.artifact&&(r[l]=f.artifact),f.isCondition&&a.add(l);}else O(c)&&u!==void 0&&(o[l]=u);}return pn(o,r,a)}function ln(e,n){let t={...e};for(let[i,s]of Object.entries(n))O(s)&&!(i in t)&&s.default!==void 0&&(t[i]=s.default);return t}function cn(e,n,t,i,s){if(W(t)){let a={allow:t.allow,deny:t.deny,transforms:s?.transforms,mode:s?.mode};return {value:t.compile(n,i,a),artifact:null,isCondition:false}}let o=s?.mode??"closures";if(t.compile==="condition"){let l={$arrow:typeof n=="string"?{$if:n,then:true,else:false}:n},c=o==="jit"?i.jit(l,{useAccessor:true}):i.compile(l,{useAccessor:true});return {value:c.bind()({}),artifact:c,isCondition:true}}if(t.compile==="expression"){let a=o==="jit"?i.jit(n,{useAccessor:true}):i.compile(n,{useAccessor:true});return {value:a.bind(),artifact:a,isCondition:false}}let r=s?.handlers?.[t.compile];if(r){let a={allow:t.allow,deny:t.deny,transforms:s?.transforms,mode:s?.mode};return {value:r(n,i,a),artifact:null,isCondition:false}}throw new Error(`Unknown compile type: ${String(t.compile)}`)}function pn(e,n,t){let i={...e},s=o=>{let r={...e};for(let[a,l]of Object.entries(n)){let c=l.bind(o);r[a]=t.has(a)?c({}):c;}for(let[a,l]of Object.entries(e))a in n||l!=null&&typeof l.bind=="function"&&(r[a]=l.bind(o));return r.bind=s,r};return i.bind=s,i}var dt="3.0.0";export{X as CompileCache,L as ExpressionCompiler,dt as VERSION,on as assertValid,Ce as builders,xe as compileDefinition,H as compilePath,j as createBoundFn,B as extractDeps,Fe as get,Ye as hasDeps,N as hasWildcard,F as isArrow,D as isCompileField,b as isCondition,He as isConditionExpr,T as isConditionGroup,I as isConditional,Be as isExpression,E as isFn,W as isHandlerCompile,ze as isLiteral,Re as isNativeCompile,$ as isPipe,Qe as isPure,w as isRef,O as isSchemaField,sn as isValid,A as normalizePath,ee as validate,G as validateDefinition};
|