@statedelta-libs/expressions 2.1.0 → 2.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 +171 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +128 -2
- package/dist/index.d.ts +128 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
- **Dependency extraction** - Para dirty tracking/reatividade
|
|
20
20
|
- **Conditions nativo** - Condicionais integradas com expressions nos lados
|
|
21
21
|
- **normalize()** - Helper externo para custom transforms (`$query`, `$mapper`, etc.)
|
|
22
|
+
- **resolveBoundaries()** - Resolve boundaries customizados (`$simulate`, `$context`, etc.) com compilação independente
|
|
22
23
|
- **Type-safe** - TypeScript nativo com inferência
|
|
23
24
|
|
|
24
25
|
## Instalação
|
|
@@ -464,6 +465,156 @@ normalize({ $loop: "test" }, badTransforms);
|
|
|
464
465
|
// Error: Transform "$loop" returned object with same key — infinite loop
|
|
465
466
|
```
|
|
466
467
|
|
|
468
|
+
## Boundaries (resolveBoundaries)
|
|
469
|
+
|
|
470
|
+
Para DSL customizados que precisam de **compilação independente** dos slots internos, use `resolveBoundaries()`. Diferente de `normalize()` (que apenas transforma), boundaries param o fluxo de compilação e delegam para um resolver externo.
|
|
471
|
+
|
|
472
|
+
### Conceito
|
|
473
|
+
|
|
474
|
+
Um **boundary** é um "corpo estranho" no DSL que:
|
|
475
|
+
1. **Para** o fluxo normal de compilação
|
|
476
|
+
2. **Delega** para um resolver customizado
|
|
477
|
+
3. O resolver **compila slots internos** independentemente
|
|
478
|
+
4. **Substitui** por uma referência no scope
|
|
479
|
+
|
|
480
|
+
```
|
|
481
|
+
DSL "rico" DSL puro
|
|
482
|
+
{ $simulate: {...} } → { $fn: "__simulate_0", args: [] }
|
|
483
|
+
+ scope["__simulate_0"] = fn compilada
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
### Uso básico
|
|
487
|
+
|
|
488
|
+
```typescript
|
|
489
|
+
import { resolveBoundaries, compile, type BoundaryResolvers } from '@statedelta-libs/expressions';
|
|
490
|
+
|
|
491
|
+
const resolvers: BoundaryResolvers = {
|
|
492
|
+
$context: (node, { compile: compileFn, genId, scope }) => {
|
|
493
|
+
const id = `__context_${genId()}`;
|
|
494
|
+
const body = (node.$context as { body: Expression }).body;
|
|
495
|
+
|
|
496
|
+
// Compila o body internamente
|
|
497
|
+
const bodyFn = compileFn(body, { scope }).fn;
|
|
498
|
+
|
|
499
|
+
// Retorna função com lifecycle begin/end
|
|
500
|
+
const contextFn = () => (data: unknown) => {
|
|
501
|
+
console.log("begin");
|
|
502
|
+
try {
|
|
503
|
+
return bodyFn(data);
|
|
504
|
+
} finally {
|
|
505
|
+
console.log("end");
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
return {
|
|
510
|
+
expr: { $fn: id, args: [] },
|
|
511
|
+
scopeEntry: [id, contextFn],
|
|
512
|
+
};
|
|
513
|
+
},
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
// 1. Resolve boundaries (extrai e compila internamente)
|
|
517
|
+
const { expr, scope } = resolveBoundaries(
|
|
518
|
+
{ $context: { body: { $fn: "add", args: [{ $: "a" }, { $: "b" }] } } },
|
|
519
|
+
{ resolvers, scope: { add: (a, b) => a + b } }
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
// 2. Compila DSL puro resultante
|
|
523
|
+
const { fn } = compile(expr, { scope });
|
|
524
|
+
|
|
525
|
+
// 3. Executa
|
|
526
|
+
const contextFn = fn({}); // Retorna a função com lifecycle
|
|
527
|
+
const result = contextFn({ a: 1, b: 2 }); // "begin" → 3 → "end"
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
### Caso de uso: $simulate com múltiplos slots
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
interface SimulateNode {
|
|
534
|
+
$simulate: {
|
|
535
|
+
effects: Expression[];
|
|
536
|
+
query: Expression;
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const resolvers: BoundaryResolvers = {
|
|
541
|
+
$simulate: (node, { compile: compileFn, genId, scope }) => {
|
|
542
|
+
const id = `__simulate_${genId()}`;
|
|
543
|
+
const sim = (node as unknown as SimulateNode).$simulate;
|
|
544
|
+
|
|
545
|
+
// Compila cada slot independentemente
|
|
546
|
+
const effectFns = sim.effects.map((e) => compileFn(e, { scope }).fn);
|
|
547
|
+
const queryFn = compileFn(sim.query, { scope }).fn;
|
|
548
|
+
|
|
549
|
+
// Cria função que orquestra a execução
|
|
550
|
+
const simulateFn = () => (data: unknown) => {
|
|
551
|
+
const effects = effectFns.map((fn) => fn(data));
|
|
552
|
+
const query = queryFn(data);
|
|
553
|
+
return { effects, query };
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
return {
|
|
557
|
+
expr: { $fn: id, args: [] },
|
|
558
|
+
scopeEntry: [id, simulateFn],
|
|
559
|
+
};
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
const { expr, scope } = resolveBoundaries(
|
|
564
|
+
{
|
|
565
|
+
$simulate: {
|
|
566
|
+
effects: [
|
|
567
|
+
{ $fn: "increment", args: [{ $: "hp" }] },
|
|
568
|
+
{ $fn: "decrement", args: [{ $: "mp" }] },
|
|
569
|
+
],
|
|
570
|
+
query: { $fn: "isAlive", args: [{ $: "hp" }] },
|
|
571
|
+
},
|
|
572
|
+
},
|
|
573
|
+
{ resolvers, scope: myScope }
|
|
574
|
+
);
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
### Contexto do resolver
|
|
578
|
+
|
|
579
|
+
O resolver recebe um contexto com:
|
|
580
|
+
|
|
581
|
+
| Propriedade | Tipo | Descrição |
|
|
582
|
+
|-------------|------|-----------|
|
|
583
|
+
| `compile` | `typeof compile` | Mesmo compilador do fluxo principal |
|
|
584
|
+
| `genId` | `() => string` | Gerador de IDs únicos |
|
|
585
|
+
| `scope` | `Scope` | Scope atual (read-only) |
|
|
586
|
+
| `options` | `CompileOptions` | Opções de compilação |
|
|
587
|
+
|
|
588
|
+
### ID Generator customizado
|
|
589
|
+
|
|
590
|
+
Por padrão, IDs são gerados como `"0"`, `"1"`, `"2"`... Para IDs únicos globais, passe um `genId` customizado:
|
|
591
|
+
|
|
592
|
+
```typescript
|
|
593
|
+
import { nanoid } from 'nanoid';
|
|
594
|
+
|
|
595
|
+
const { expr, scope } = resolveBoundaries(expr, {
|
|
596
|
+
resolvers,
|
|
597
|
+
scope: baseScope,
|
|
598
|
+
genId: () => nanoid(), // IDs únicos globais
|
|
599
|
+
});
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
### Diferença entre normalize e resolveBoundaries
|
|
603
|
+
|
|
604
|
+
| | `normalize()` | `resolveBoundaries()` |
|
|
605
|
+
|---|---|---|
|
|
606
|
+
| **Propósito** | Transformar sintaxe | Compilação independente |
|
|
607
|
+
| **Retorno** | `Expression` | `{ expr, scope }` |
|
|
608
|
+
| **Compila slots** | Não | Sim (via `ctx.compile`) |
|
|
609
|
+
| **Acumula scope** | Não | Sim |
|
|
610
|
+
| **Uso típico** | `$query` → `$fn` | `$simulate`, `$context` |
|
|
611
|
+
|
|
612
|
+
Use `normalize()` para sugar syntax simples. Use `resolveBoundaries()` quando precisar:
|
|
613
|
+
- Compilar múltiplos slots independentemente
|
|
614
|
+
- Controlar ordem/momento de execução
|
|
615
|
+
- Adicionar lógica de lifecycle (begin/end, try/finally)
|
|
616
|
+
- DSL completamente diferente internamente
|
|
617
|
+
|
|
467
618
|
## Builders
|
|
468
619
|
|
|
469
620
|
Funções declarativas para construir expressões:
|
|
@@ -521,6 +672,14 @@ import type {
|
|
|
521
672
|
AccessorFn,
|
|
522
673
|
TransformFn,
|
|
523
674
|
Transforms,
|
|
675
|
+
// Boundaries
|
|
676
|
+
BoundaryResolver,
|
|
677
|
+
BoundaryResolvers,
|
|
678
|
+
ResolverContext,
|
|
679
|
+
ResolverResult,
|
|
680
|
+
ResolveBoundariesOptions,
|
|
681
|
+
ResolveBoundariesResult,
|
|
682
|
+
IdGenerator,
|
|
524
683
|
} from '@statedelta-libs/expressions';
|
|
525
684
|
|
|
526
685
|
// Type guards
|
|
@@ -608,6 +767,18 @@ Funções só são acessíveis se existirem no `scope` fornecido pelo desenvolve
|
|
|
608
767
|
|
|
609
768
|
6. **Novo extractor**: `extractContextFns()`
|
|
610
769
|
|
|
770
|
+
7. **Novo `resolveBoundaries()`**: Para DSL customizados com compilação independente de slots:
|
|
771
|
+
```typescript
|
|
772
|
+
const { expr, scope } = resolveBoundaries(richExpr, {
|
|
773
|
+
resolvers: { $simulate: (node, ctx) => ... },
|
|
774
|
+
scope: baseScope,
|
|
775
|
+
genId: () => nanoid(), // opcional
|
|
776
|
+
});
|
|
777
|
+
compile(expr, { scope });
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
8. **Novos tipos**: `BoundaryResolver`, `BoundaryResolvers`, `ResolverContext`, `ResolverResult`, `ResolveBoundariesOptions`, `ResolveBoundariesResult`, `IdGenerator`
|
|
781
|
+
|
|
611
782
|
## Licença
|
|
612
783
|
|
|
613
784
|
MIT © Anderson D. Rosa
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var omniAst=require('omni-ast');var gn=Object.defineProperty;var mn=(n,t)=>{for(var o in t)gn(n,o,{get:t[o],enumerable:true});};var E=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,S=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,w=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",k=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),x=n=>n!==null&&typeof n=="object"&&"$cb"in n&&typeof n.$cb=="string"&&"body"in n,v=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),h=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&v.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),b=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,yn=n=>h(n)||b(n),B=n=>{if(n===null)return true;let t=typeof n;if(t==="string"||t==="number"||t==="boolean"||Array.isArray(n))return true;if(t==="object"&&n!==null){let o=n,r="left"in o&&"op"in o&&v.has(o.op);return !("$"in o)&&!("$if"in o)&&!("$fn"in o)&&!("$pipe"in o)&&!("$cb"in o)&&!r&&!("logic"in o)}return false};var G=new Map;function Q(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f==="*")t.push({type:"wildcard",value:"*"});else {let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function U(n){return n.includes("[*]")}function j(n){let t=G.get(n);return t||(t=U(n)?hn(n):En(n),G.set(n,t),t)}function En(n){if(!n.includes(".")&&!n.includes("["))return i=>i?.[n];let t=Q(n),o=t.length;if(o===2){let[i,s]=t,e=i.value,f=s.value;return u=>u?.[e]?.[f]}if(o===3){let[i,s,e]=t,f=i.value,u=s.value,a=e.value;return l=>l?.[f]?.[u]?.[a]}let r=t.map(i=>i.value);return i=>{let s=i;for(let e=0;e<o&&s!=null;e++)s=s[r[e]];return s}}function hn(n){let t=Q(n),o=[];for(let r=0;r<t.length;r++)t[r].type==="wildcard"&&o.push(r);return o.length===1?bn(t,o[0]):Cn(t,o)}function bn(n,t){let o=n.slice(0,t).map(e=>e.value),r=n.slice(t+1).map(e=>e.value),i=o.length,s=r.length;if(s===0){if(i===1){let e=o[0];return f=>f?.[e]}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];return f}}if(s===1){let e=r[0];if(i===1){let f=o[0];return u=>{let a=u?.[f];if(Array.isArray(a))return a.map(l=>l?.[e])}}return f=>{let u=f;for(let a=0;a<i&&u!=null;a++)u=u[o[a]];if(Array.isArray(u))return u.map(a=>a?.[e])}}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];if(Array.isArray(f))return f.map(u=>{let a=u;for(let l=0;l<s&&a!=null;l++)a=a[r[l]];return a})}}function Cn(n,t){let o=[],r=0;for(let s=0;s<t.length;s++){let e=t[s],f=s===t.length-1,u=n.slice(r,e).map(a=>a.value);u.length>0&&o.push({type:"access",keys:u}),o.push({type:f?"map":"flatMap",keys:[]}),r=e+1;}let i=n.slice(r).map(s=>s.value);return s=>{let e=s;for(let f of o){if(e==null)return;if(f.type==="access")for(let u of f.keys){if(e==null)return;e=e[u];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(u=>{let a=u;return Array.isArray(a)?a:[a]});}else if(f.type==="map"){if(!Array.isArray(e))return;i.length>0&&(e=e.map(u=>{let a=u;for(let l of i){if(a==null)return;a=a[l];}return a}));}}return e}}function Tn(n,t){return j(t)(n)}function W(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function $n(){G.clear();}function An(){return G.size}function R(n){let t=new Set;return C(n,t),Array.from(t)}function C(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i=0;i<n.length;i++)C(n[i],t);return}if(E(n)){t.add(W(n.$));return}if(S(n)){if(typeof n.$if=="string"){let i=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(W(i));}else C(n.$if,t);C(n.then,t),n.else!==void 0&&C(n.else,t);return}if(k(n)){for(let i=0;i<n.$pipe.length;i++)C(n.$pipe[i],t);return}if(w(n)){if(n.args)for(let i=0;i<n.args.length;i++)C(n.args[i],t);return}if(x(n)){C(n.body,t),n.params!==void 0&&C(n.params,t);return}if(h(n)){C(n.left,t),n.right!==void 0&&C(n.right,t);return}if(b(n)){for(let i=0;i<n.conditions.length;i++)C(n.conditions[i],t);return}let o=n,r=Object.keys(o);for(let i=0;i<r.length;i++)C(o[r[i]],t);}function Sn(n){return R(n).length>0}function wn(n){return R(n).length===0}function kn(n){return JSON.stringify(n)}function z(n,t={}){let o=t.scope??{},r=t.accessor,i=t.context??{},s=y(n,o,r,i),e=R(n),f=kn(n);return {fn:s,deps:e,hash:f}}function y(n,t,o,r){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>y(s,t,o,r));return s=>i.map(e=>e(s))}if(E(n))return xn(n,o);if(S(n))return Fn(n,t,o,r);if(k(n))return Rn(n,t,o,r);if(w(n))return On(n,t,o,r);if(x(n))return Nn(n,t,o,r);if(h(n))return jn(n,t,o,r);if(b(n))return vn(n,t,o,r);if(B(n)){let i=n,s=Object.keys(i),e=s.map(f=>y(i[f],t,o,r));return f=>{let u={};for(let a=0;a<s.length;a++)u[s[a]]=e[a](f);return u}}return ()=>n}function Z(n,t){return t?o=>t(n,o):j(n)}function xn(n,t){return Z(n.$,t)}function Fn(n,t,o,r){let i;if(typeof n.$if=="string"){let f=n.$if.startsWith("!")?n.$if.slice(1):n.$if,u=Z(f,o);i=n.$if.startsWith("!")?l=>!u(l):l=>!!u(l);}else {let f=y(n.$if,t,o,r);i=u=>!!f(u);}let s=y(n.then,t,o,r),e=n.else!==void 0?y(n.else,t,o,r):()=>{};return f=>i(f)?s(f):e(f)}function Rn(n,t,o,r){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return y(i[0],t,o,r);let s=y(i[0],t,o,r),e=i.slice(1).map(u=>y(u,t,o,r)),f=e.length;if(f===1){let[u]=e;return a=>{let l=s(a),p=u(a);return typeof p=="function"?p(l):p}}if(f===2){let[u,a]=e;return l=>{let p=s(l),d=u(l);return p=typeof d=="function"?d(p):d,d=a(l),typeof d=="function"?d(p):d}}if(f===3){let[u,a,l]=e;return p=>{let d=s(p),g=u(p);return d=typeof g=="function"?g(d):g,g=a(p),d=typeof g=="function"?g(d):g,g=l(p),typeof g=="function"?g(d):g}}return u=>{let a=s(u);for(let l=0;l<f;l++){let p=e[l](u);a=typeof p=="function"?p(a):p;}return a}}function On(n,t,o,r){let i=n.$fn,s=n.args;if(s===void 0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let e=s.map(u=>y(u,t,o,r)),f=e.length;if(f===0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(f===1){let[u]=e;return a=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(u(a))}}if(f===2){let[u,a]=e;return l=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(l),a(l))}}if(f===3){let[u,a,l]=e;return p=>{let d=t[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),a(p),l(p))}}return u=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(...e.map(l=>l(u)))}}function Nn(n,t,o,r){let i=n.$cb,s=y(n.body,t,o,r),e=n.params?y(n.params,t,o,r):void 0,f=u=>o?a=>o(a,u):a=>j(a)(u);return u=>{let a=r?.[i];if(!a)throw new Error(`Context function not found: ${i}`);let l=(g,F)=>s(g),p=f(u),d=e?e(u):void 0;return a(l,u,p,d)}}function jn(n,t,o,r){let i=y(n.left,t,o,r),s=n.right!==void 0?y(n.right,t,o,r):()=>{};switch(n.op){case "eq":return e=>i(e)===s(e);case "neq":return e=>i(e)!==s(e);case "gt":return e=>i(e)>s(e);case "gte":return e=>i(e)>=s(e);case "lt":return e=>i(e)<s(e);case "lte":return e=>i(e)<=s(e);case "in":return e=>{let f=s(e);return Array.isArray(f)&&f.includes(i(e))};case "notIn":return e=>{let f=s(e);return !Array.isArray(f)||!f.includes(i(e))};case "contains":return e=>{let f=i(e);return Array.isArray(f)&&f.includes(s(e))};case "notContains":return e=>{let f=i(e);return !Array.isArray(f)||!f.includes(s(e))};case "exists":return e=>i(e)!==void 0;case "notExists":return e=>i(e)===void 0;case "matches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?false:new RegExp(u).test(f)};case "notMatches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?true:!new RegExp(u).test(f)};case "startsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.startsWith(u)};case "endsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.endsWith(u)}}}function vn(n,t,o,r){let i=n.conditions.map(e=>y(e,t,o,r)),s=i.length;if(s===1)return e=>!!i[0](e);if(s===2){let[e,f]=i;return n.logic==="AND"?u=>!!e(u)&&!!f(u):u=>!!e(u)||!!f(u)}if(s===3){let[e,f,u]=i;return n.logic==="AND"?a=>!!e(a)&&!!f(a)&&!!u(a):a=>!!e(a)||!!f(a)||!!u(a)}return n.logic==="AND"?e=>{for(let f=0;f<s;f++)if(!i[f](e))return false;return true}:e=>{for(let f=0;f<s;f++)if(i[f](e))return true;return false}}function Gn(n,t,o={}){return z(n,o).fn(t)}var D=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,o={}){let r=JSON.stringify(t),i=this.cache.get(r);if(i)return this.cache.delete(r),this.cache.set(r,i),i;let s=z(t,o);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(r,s),s}has(t){return this.cache.has(JSON.stringify(t))}delete(t){return this.cache.delete(JSON.stringify(t))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(t){for(this._maxSize=t;this.cache.size>this._maxSize;){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}}},P=new D;function Wn(n,t={}){return P.get(n,t)}function J(n,t="root",o={}){let r=[];return T(n,t,r,o),{valid:r.length===0,errors:r}}function T(n,t,o,r){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)T(n[e],`${t}[${e}]`,o,r);return}if(E(n)){(!n.$||typeof n.$!="string")&&o.push(`${t}: invalid reference, $ must be non-empty string`);return}if(S(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||o.push(`${t}.$if: empty path in string shorthand`):T(n.$if,`${t}.$if`,o,r),T(n.then,`${t}.then`,o,r),n.else!==void 0&&T(n.else,`${t}.else`,o,r);return}if(k(n)){if(!Array.isArray(n.$pipe)){o.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){o.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)T(n.$pipe[e],`${t}.$pipe[${e}]`,o,r);return}if(w(n)){if(!n.$fn||typeof n.$fn!="string"){o.push(`${t}: invalid function, $fn must be non-empty string`);return}if(r.scope&&!(n.$fn in r.scope)&&o.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))o.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)T(n.args[e],`${t}.args[${e}]`,o,r);return}if(x(n)){if(!n.$cb||typeof n.$cb!="string"){o.push(`${t}: invalid callback, $cb must be non-empty string`);return}r.context&&!(n.$cb in r.context)&&o.push(`${t}: context function "${n.$cb}" not found`),T(n.body,`${t}.body`,o,r),n.params!==void 0&&T(n.params,`${t}.params`,o,r);return}if(h(n)){v.has(n.op)||o.push(`${t}: invalid operator "${n.op}"`),T(n.left,`${t}.left`,o,r),n.right!==void 0&&T(n.right,`${t}.right`,o,r);return}if(b(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&o.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){o.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)T(n.conditions[e],`${t}.conditions[${e}]`,o,r);return}let i=n,s=Object.keys(i);for(let e=0;e<s.length;e++){let f=s[e];T(i[f],`${t}.${f}`,o,r);}}function zn(n,t={}){let o=J(n,"root",t);if(!o.valid)throw new Error(`Invalid expression: ${o.errors.join("; ")}`)}function Dn(n,t={}){return J(n,"root",t).valid}var sn={};mn(sn,{$:()=>nn,$cb:()=>rn,$cond:()=>on,$fn:()=>tn,$if:()=>_n,$pipe:()=>en,cb:()=>qn,cond:()=>Ln,fn:()=>In,pipe:()=>Vn,ref:()=>Mn});function nn(n){return {$:n}}var Mn=nn;function tn(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var In=tn;function _n(n,t,o){return o===void 0?{$if:n,then:t}:{$if:n,then:t,else:o}}function en(...n){return {$pipe:n}}var Vn=en;function on(n,t,o){return o===void 0?{left:n,op:t}:{left:n,op:t,right:o}}var Ln=on;function rn(n,t,o){return o===void 0?{$cb:n,body:t}:{$cb:n,body:t,params:o}}var qn=rn;var K="data",un="scope",Bn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function I(n,t={}){let{dataParam:o=K,scopeParam:r=un,noPrefixes:i=false,useAccessor:s=false,lexicalPrefix:e}=t;return m(n,o,r,i,s,e)}function m(n,t,o,r,i,s){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(e=>m(e,t,o,r,i,s)));if(E(n))return Jn(n.$,t,r,i,s);if(S(n))return Xn(n,t,o,r,i,s);if(k(n))return Un(n.$pipe,t,o,r,i,s);if(w(n))return Yn(n,t,o,r,i,s);if(x(n))return Qn(n,t,o,r,i,s);if(h(n))return Zn(n,t,o,r,i,s);if(b(n))return Pn(n,t,o,r,i,s);if(typeof n=="object"){let f=Object.entries(n).map(([u,a])=>omniAst.builders.property(omniAst.builders.identifier(u),m(a,t,o,r,i,s)));return omniAst.builders.objectExpression(f)}return omniAst.builders.literal(null)}var X="accessor";function Y(n,t){return t?n===t||n.startsWith(t+"."):false}function Jn(n,t,o,r,i){return r?Y(n,i)?O(n,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(X),[omniAst.builders.literal(n),omniAst.builders.identifier(t)]):n.includes("[*]")?Kn(n,t,o):O(n,t,o)}function O(n,t,o){let r=M(n);if(r.length===0)return o?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t);let i;if(o){let s=r[0];i=omniAst.builders.identifier(s.value);for(let e=1;e<r.length;e++){let f=r[e];f.type==="key"?i=omniAst.builders.memberExpression(i,omniAst.builders.identifier(f.value),false,true):i=omniAst.builders.memberExpression(i,omniAst.builders.literal(f.value),true,true);}}else {i=omniAst.builders.identifier(t);for(let s of r)s.type==="key"?i=omniAst.builders.memberExpression(i,omniAst.builders.identifier(s.value),false,true):i=omniAst.builders.memberExpression(i,omniAst.builders.literal(s.value),true,true);}return i}function Kn(n,t,o){let r=n.indexOf("[*]"),i=n.slice(0,r),s=n.slice(r+3),e;if(i?e=O(i,t,o):e=o?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t),!s||s==="")return e;if(s.includes("[*]"))return cn(e,s);let f="_i",u=s.startsWith(".")?s.slice(1):s,a=omniAst.builders.identifier(f);if(u){let l=M(u);for(let p of l)p.type==="key"?a=omniAst.builders.memberExpression(a,omniAst.builders.identifier(p.value),false,true):a=omniAst.builders.memberExpression(a,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],a)])}function cn(n,t){let o=t.indexOf("[*]"),r=t.slice(0,o),i=t.slice(o+3),s="_i",e=r.startsWith(".")?r.slice(1):r,f=omniAst.builders.identifier(s);if(e){let a=M(e);for(let l of a)l.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(l.value),false,true));}if(i.includes("[*]")){let a=cn(f,i);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],a)])}let u=i.startsWith(".")?i.slice(1):i;if(u){let a=M(u);for(let l of a)l.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(l.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],f)])}function Xn(n,t,o,r,i,s){let e;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),l=a?n.$if.slice(1):n.$if,p;i?Y(l,s)?p=O(l,t,true):p=omniAst.builders.callExpression(omniAst.builders.identifier(X),[omniAst.builders.literal(l),omniAst.builders.identifier(t)]):p=O(l,t,r),e=a?omniAst.builders.unaryExpression("!",p):p;}else e=m(n.$if,t,o,r,i,s);let f=m(n.then,t,o,r,i,s),u=n.else!==void 0?m(n.else,t,o,r,i,s):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(e,f,u)}function Yn(n,t,o,r,i,s){let e=r?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(o),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(u=>m(u,t,o,r,i,s));return omniAst.builders.callExpression(e,f)}var Hn="context";function Qn(n,t,o,r,i,s){let e=r?omniAst.builders.identifier(n.$cb):omniAst.builders.memberExpression(omniAst.builders.identifier(Hn),omniAst.builders.identifier(n.$cb),false,false),f=m(n.body,t,o,r,i,s),a=[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(t),omniAst.builders.identifier("get")],f),omniAst.builders.identifier(t),omniAst.builders.identifier("get")];if(n.params!==void 0){let l=m(n.params,t,o,r,i,s);a.push(l);}return omniAst.builders.callExpression(e,a)}function Un(n,t,o,r,i,s){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return m(n[0],t,o,r,i,s);let e=m(n[0],t,o,r,i,s);for(let f=1;f<n.length;f++){let u=m(n[f],t,o,r,i,s);e=omniAst.builders.callExpression(u,[e]);}return e}function fn(n,t,o,r,i,s){if(E(n)){let e=n.$;return i?Y(e,s)?O(e,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(X),[omniAst.builders.literal(e),omniAst.builders.identifier(t)]):O(e,t,r)}return m(n,t,o,r,i,s)}function Zn(n,t,o,r,i,s){let e=fn(n.left,t,o,r,i,s),f=n.right!==void 0?fn(n.right,t,o,r,i,s):omniAst.builders.literal(null),u=Bn[n.op];if(u)return omniAst.builders.binaryExpression(u,e,f);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]));case "exists":return omniAst.builders.binaryExpression("!=",e,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",e,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("startsWith"),false,true),[f]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("endsWith"),false,true),[f]);default:return omniAst.builders.binaryExpression("===",e,f)}}function Pn(n,t,o,r,i,s){let{logic:e,conditions:f}=n,u=e==="AND"?"&&":"||";if(f.length===0)return omniAst.builders.literal(e==="AND");if(f.length===1)return m(f[0],t,o,r,i,s);let a=m(f[0],t,o,r,i,s);for(let l=1;l<f.length;l++){let p=m(f[l],t,o,r,i,s);a=omniAst.builders.logicalExpression(u,a,p);}return a}function M(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f!=="*"){let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function an(n,t=[K]){return omniAst.builders.arrowFunctionExpression(t.map(o=>omniAst.builders.identifier(o)),n)}function _(n){let t=new Set;return $(n,t),t}function $(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)$(r,t);return}if(E(n))return;if(S(n)){$(n.$if,t),$(n.then,t),n.else!==void 0&&$(n.else,t);return}if(k(n)){for(let r of n.$pipe)$(r,t);return}if(w(n)){if(t.add(n.$fn),n.args)for(let r of n.args)$(r,t);return}if(x(n)){$(n.body,t),n.params!==void 0&&$(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&$(n.left,t),n.right!==void 0&&typeof n.right=="object"&&$(n.right,t);return}if(b(n)){for(let r of n.conditions)$(r,t);return}let o=n;for(let r of Object.keys(o))$(o[r],t);}function V(n){let t=new Set;return A(n,t),t}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)A(r,t);return}if(E(n))return;if(S(n)){A(n.$if,t),A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(k(n)){for(let r of n.$pipe)A(r,t);return}if(w(n)){if(n.args)for(let r of n.args)A(r,t);return}if(x(n)){t.add(n.$cb),A(n.body,t),n.params!==void 0&&A(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&A(n.left,t),n.right!==void 0&&typeof n.right=="object"&&A(n.right,t);return}if(b(n)){for(let r of n.conditions)A(r,t);return}let o=n;for(let r of Object.keys(o))A(o[r],t);}function L(n){let t=new Set;for(let o of n){let r=o.indexOf("."),i=o.indexOf("["),s=o.length;r!==-1&&(s=Math.min(s,r)),i!==-1&&(s=Math.min(s,i));let e=o.slice(0,s);e&&t.add(e);}return t}function tt(n){return JSON.stringify(n)}function et(n,t,o,r,i,s){let e=I(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),f=omniAst.generate(e),u="";i?s&&(u=`const{${s}}=data??{};`):t.size>0&&(u=`const{${[...t].join(",")}}=data??{};`);let a=i?new Set([...o,"accessor"]):o,l=a.size>0?`const{${[...a].join(",")}}=scope;`:"",p=r.size>0?`const{${[...r].join(",")}}=context;`:"",d=r.size>0,g="";d&&(i?g="const get=(path)=>accessor(path,data);":g="const get=(path)=>path.split('.').reduce((o,k)=>o?.[k],data);");let F=a.size>0,N;if(F||d){let pn=d?"scope,context":"scope",dn=`${u}${g}return ${f}`;N=`(function(${pn}){${l}${p}return function(data){${dn}}})`;}else N=`(function(){return function(data){${u}return ${f}}})`;return N}function H(n,t={}){let{scope:o={},context:r={},returnCode:i=false,useAccessor:s=false,lexicalPrefix:e}=t,f=R(n),u=L(f),a=_(n),l=V(n),p=tt(n),d=et(n,u,a,l,s,e);if(i)return {code:d,deps:f,hash:p,dataRoots:[...u],scopeFns:[...a],contextFns:[...l]};let g;try{let F=l.size>0,N=new Function(`return ${d}`)();g=F?N(o,r):N(o);}catch(F){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${F instanceof Error?F.message:String(F)}`)}return {fn:g,deps:f,hash:p}}function ln(n,t,o={}){let{fn:r}=H(n,o);return r(t)}function it(n,t){return q(n,t)}function q(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>q(s,t));let o=n,r=Object.keys(o);for(let s of r)if(s in t){let e=t[s](o);if(typeof e=="object"&&e!==null&&s in e)throw new Error(`Transform "${s}" returned object with same key \u2014 infinite loop`);return q(e,t)}let i={};for(let s of r)i[s]=q(o[s],t);return i}var vt="2.0.0";exports.ExpressionCache=D;exports.VERSION=vt;exports.assertValid=zn;exports.builders=sn;exports.cache=P;exports.cached=Wn;exports.clearPathCache=$n;exports.compile=z;exports.compileAST=H;exports.compilePath=j;exports.dslToAST=I;exports.evaluate=Gn;exports.evaluateAST=ln;exports.extractContextFns=V;exports.extractDataRoots=L;exports.extractDeps=R;exports.extractScopeFns=_;exports.get=Tn;exports.getPathCacheSize=An;exports.hasDeps=Sn;exports.hasWildcard=U;exports.isCb=x;exports.isCondition=h;exports.isConditionExpr=yn;exports.isConditionGroup=b;exports.isConditional=S;exports.isFn=w;exports.isLiteral=B;exports.isPipe=k;exports.isPure=wn;exports.isRef=E;exports.isValid=Dn;exports.normalize=it;exports.normalizePath=W;exports.validate=J;exports.wrapInFunction=an;
|
|
1
|
+
'use strict';var omniAst=require('omni-ast');var mn=Object.defineProperty;var En=(n,t)=>{for(var o in t)mn(n,o,{get:t[o],enumerable:true});};var E=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,S=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,k=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",x=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),R=n=>n!==null&&typeof n=="object"&&"$cb"in n&&typeof n.$cb=="string"&&"body"in n,G=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),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),b=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,hn=n=>h(n)||b(n),q=n=>{if(n===null)return true;let t=typeof n;if(t==="string"||t==="number"||t==="boolean"||Array.isArray(n))return true;if(t==="object"&&n!==null){let o=n,r="left"in o&&"op"in o&&G.has(o.op);return !("$"in o)&&!("$if"in o)&&!("$fn"in o)&&!("$pipe"in o)&&!("$cb"in o)&&!r&&!("logic"in o)}return false};var I=new Map;function Q(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f==="*")t.push({type:"wildcard",value:"*"});else {let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function U(n){return n.includes("[*]")}function N(n){let t=I.get(n);return t||(t=U(n)?Cn(n):bn(n),I.set(n,t),t)}function bn(n){if(!n.includes(".")&&!n.includes("["))return i=>i?.[n];let t=Q(n),o=t.length;if(o===2){let[i,s]=t,e=i.value,f=s.value;return u=>u?.[e]?.[f]}if(o===3){let[i,s,e]=t,f=i.value,u=s.value,l=e.value;return a=>a?.[f]?.[u]?.[l]}let r=t.map(i=>i.value);return i=>{let s=i;for(let e=0;e<o&&s!=null;e++)s=s[r[e]];return s}}function Cn(n){let t=Q(n),o=[];for(let r=0;r<t.length;r++)t[r].type==="wildcard"&&o.push(r);return o.length===1?Tn(t,o[0]):$n(t,o)}function Tn(n,t){let o=n.slice(0,t).map(e=>e.value),r=n.slice(t+1).map(e=>e.value),i=o.length,s=r.length;if(s===0){if(i===1){let e=o[0];return f=>f?.[e]}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];return f}}if(s===1){let e=r[0];if(i===1){let f=o[0];return u=>{let l=u?.[f];if(Array.isArray(l))return l.map(a=>a?.[e])}}return f=>{let u=f;for(let l=0;l<i&&u!=null;l++)u=u[o[l]];if(Array.isArray(u))return u.map(l=>l?.[e])}}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];if(Array.isArray(f))return f.map(u=>{let l=u;for(let a=0;a<s&&l!=null;a++)l=l[r[a]];return l})}}function $n(n,t){let o=[],r=0;for(let s=0;s<t.length;s++){let e=t[s],f=s===t.length-1,u=n.slice(r,e).map(l=>l.value);u.length>0&&o.push({type:"access",keys:u}),o.push({type:f?"map":"flatMap",keys:[]}),r=e+1;}let i=n.slice(r).map(s=>s.value);return s=>{let e=s;for(let f of o){if(e==null)return;if(f.type==="access")for(let u of f.keys){if(e==null)return;e=e[u];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(u=>{let l=u;return Array.isArray(l)?l:[l]});}else if(f.type==="map"){if(!Array.isArray(e))return;i.length>0&&(e=e.map(u=>{let l=u;for(let a of i){if(l==null)return;l=l[a];}return l}));}}return e}}function An(n,t){return N(t)(n)}function W(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function Sn(){I.clear();}function kn(){return I.size}function F(n){let t=new Set;return C(n,t),Array.from(t)}function C(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i=0;i<n.length;i++)C(n[i],t);return}if(E(n)){t.add(W(n.$));return}if(S(n)){if(typeof n.$if=="string"){let i=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(W(i));}else C(n.$if,t);C(n.then,t),n.else!==void 0&&C(n.else,t);return}if(x(n)){for(let i=0;i<n.$pipe.length;i++)C(n.$pipe[i],t);return}if(k(n)){if(n.args)for(let i=0;i<n.args.length;i++)C(n.args[i],t);return}if(R(n)){C(n.body,t),n.params!==void 0&&C(n.params,t);return}if(h(n)){C(n.left,t),n.right!==void 0&&C(n.right,t);return}if(b(n)){for(let i=0;i<n.conditions.length;i++)C(n.conditions[i],t);return}let o=n,r=Object.keys(o);for(let i=0;i<r.length;i++)C(o[r[i]],t);}function xn(n){return F(n).length>0}function Rn(n){return F(n).length===0}function wn(n){return JSON.stringify(n)}function j(n,t={}){let o=t.scope??{},r=t.accessor,i=t.context??{},s=m(n,o,r,i),e=F(n),f=wn(n);return {fn:s,deps:e,hash:f}}function m(n,t,o,r){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>m(s,t,o,r));return s=>i.map(e=>e(s))}if(E(n))return Fn(n,o);if(S(n))return On(n,t,o,r);if(x(n))return vn(n,t,o,r);if(k(n))return jn(n,t,o,r);if(R(n))return Nn(n,t,o,r);if(h(n))return Gn(n,t,o,r);if(b(n))return In(n,t,o,r);if(q(n)){let i=n,s=Object.keys(i),e=s.map(f=>m(i[f],t,o,r));return f=>{let u={};for(let l=0;l<s.length;l++)u[s[l]]=e[l](f);return u}}return ()=>n}function Z(n,t){return t?o=>t(n,o):N(n)}function Fn(n,t){return Z(n.$,t)}function On(n,t,o,r){let i;if(typeof n.$if=="string"){let f=n.$if.startsWith("!")?n.$if.slice(1):n.$if,u=Z(f,o);i=n.$if.startsWith("!")?a=>!u(a):a=>!!u(a);}else {let f=m(n.$if,t,o,r);i=u=>!!f(u);}let s=m(n.then,t,o,r),e=n.else!==void 0?m(n.else,t,o,r):()=>{};return f=>i(f)?s(f):e(f)}function vn(n,t,o,r){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return m(i[0],t,o,r);let s=m(i[0],t,o,r),e=i.slice(1).map(u=>m(u,t,o,r)),f=e.length;if(f===1){let[u]=e;return l=>{let a=s(l),p=u(l);return typeof p=="function"?p(a):p}}if(f===2){let[u,l]=e;return a=>{let p=s(a),d=u(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(f===3){let[u,l,a]=e;return p=>{let d=s(p),g=u(p);return d=typeof g=="function"?g(d):g,g=l(p),d=typeof g=="function"?g(d):g,g=a(p),typeof g=="function"?g(d):g}}return u=>{let l=s(u);for(let a=0;a<f;a++){let p=e[a](u);l=typeof p=="function"?p(l):p;}return l}}function jn(n,t,o,r){let i=n.$fn,s=n.args;if(s===void 0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let e=s.map(u=>m(u,t,o,r)),f=e.length;if(f===0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(f===1){let[u]=e;return l=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(u(l))}}if(f===2){let[u,l]=e;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(a),l(a))}}if(f===3){let[u,l,a]=e;return p=>{let d=t[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),l(p),a(p))}}return u=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...e.map(a=>a(u)))}}function Nn(n,t,o,r){let i=n.$cb,s=m(n.body,t,o,r),e=n.params?m(n.params,t,o,r):void 0,f=u=>o?l=>o(l,u):l=>N(l)(u);return u=>{let l=r?.[i];if(!l)throw new Error(`Context function not found: ${i}`);let a=(g,w)=>s(g),p=f(u),d=e?e(u):void 0;return l(a,u,p,d)}}function Gn(n,t,o,r){let i=m(n.left,t,o,r),s=n.right!==void 0?m(n.right,t,o,r):()=>{};switch(n.op){case "eq":return e=>i(e)===s(e);case "neq":return e=>i(e)!==s(e);case "gt":return e=>i(e)>s(e);case "gte":return e=>i(e)>=s(e);case "lt":return e=>i(e)<s(e);case "lte":return e=>i(e)<=s(e);case "in":return e=>{let f=s(e);return Array.isArray(f)&&f.includes(i(e))};case "notIn":return e=>{let f=s(e);return !Array.isArray(f)||!f.includes(i(e))};case "contains":return e=>{let f=i(e);return Array.isArray(f)&&f.includes(s(e))};case "notContains":return e=>{let f=i(e);return !Array.isArray(f)||!f.includes(s(e))};case "exists":return e=>i(e)!==void 0;case "notExists":return e=>i(e)===void 0;case "matches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?false:new RegExp(u).test(f)};case "notMatches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?true:!new RegExp(u).test(f)};case "startsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.startsWith(u)};case "endsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.endsWith(u)}}}function In(n,t,o,r){let i=n.conditions.map(e=>m(e,t,o,r)),s=i.length;if(s===1)return e=>!!i[0](e);if(s===2){let[e,f]=i;return n.logic==="AND"?u=>!!e(u)&&!!f(u):u=>!!e(u)||!!f(u)}if(s===3){let[e,f,u]=i;return n.logic==="AND"?l=>!!e(l)&&!!f(l)&&!!u(l):l=>!!e(l)||!!f(l)||!!u(l)}return n.logic==="AND"?e=>{for(let f=0;f<s;f++)if(!i[f](e))return false;return true}:e=>{for(let f=0;f<s;f++)if(i[f](e))return true;return false}}function Wn(n,t,o={}){return j(n,o).fn(t)}var z=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,o={}){let r=JSON.stringify(t),i=this.cache.get(r);if(i)return this.cache.delete(r),this.cache.set(r,i),i;let s=j(t,o);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(r,s),s}has(t){return this.cache.has(JSON.stringify(t))}delete(t){return this.cache.delete(JSON.stringify(t))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(t){for(this._maxSize=t;this.cache.size>this._maxSize;){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}}},nn=new z;function zn(n,t={}){return nn.get(n,t)}function J(n,t="root",o={}){let r=[];return T(n,t,r,o),{valid:r.length===0,errors:r}}function T(n,t,o,r){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)T(n[e],`${t}[${e}]`,o,r);return}if(E(n)){(!n.$||typeof n.$!="string")&&o.push(`${t}: invalid reference, $ must be non-empty string`);return}if(S(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||o.push(`${t}.$if: empty path in string shorthand`):T(n.$if,`${t}.$if`,o,r),T(n.then,`${t}.then`,o,r),n.else!==void 0&&T(n.else,`${t}.else`,o,r);return}if(x(n)){if(!Array.isArray(n.$pipe)){o.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){o.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)T(n.$pipe[e],`${t}.$pipe[${e}]`,o,r);return}if(k(n)){if(!n.$fn||typeof n.$fn!="string"){o.push(`${t}: invalid function, $fn must be non-empty string`);return}if(r.scope&&!(n.$fn in r.scope)&&o.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))o.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)T(n.args[e],`${t}.args[${e}]`,o,r);return}if(R(n)){if(!n.$cb||typeof n.$cb!="string"){o.push(`${t}: invalid callback, $cb must be non-empty string`);return}r.context&&!(n.$cb in r.context)&&o.push(`${t}: context function "${n.$cb}" not found`),T(n.body,`${t}.body`,o,r),n.params!==void 0&&T(n.params,`${t}.params`,o,r);return}if(h(n)){G.has(n.op)||o.push(`${t}: invalid operator "${n.op}"`),T(n.left,`${t}.left`,o,r),n.right!==void 0&&T(n.right,`${t}.right`,o,r);return}if(b(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&o.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){o.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)T(n.conditions[e],`${t}.conditions[${e}]`,o,r);return}let i=n,s=Object.keys(i);for(let e=0;e<s.length;e++){let f=s[e];T(i[f],`${t}.${f}`,o,r);}}function Dn(n,t={}){let o=J(n,"root",t);if(!o.valid)throw new Error(`Invalid expression: ${o.errors.join("; ")}`)}function Mn(n,t={}){return J(n,"root",t).valid}var un={};En(un,{$:()=>tn,$call:()=>fn,$cb:()=>sn,$cond:()=>rn,$fn:()=>en,$if:()=>Vn,$pipe:()=>on,call:()=>Kn,cb:()=>Jn,cond:()=>qn,fn:()=>_n,pipe:()=>Ln,ref:()=>Bn});function tn(n){return {$:n}}var Bn=tn;function en(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var _n=en;function Vn(n,t,o){return o===void 0?{$if:n,then:t}:{$if:n,then:t,else:o}}function on(...n){return {$pipe:n}}var Ln=on;function rn(n,t,o){return o===void 0?{left:n,op:t}:{left:n,op:t,right:o}}var qn=rn;function sn(n,t,o){return o===void 0?{$cb:n,body:t}:{$cb:n,body:t,params:o}}var Jn=sn;function fn(n,t=[]){return {$fn:n,args:t}}var Kn=fn;var K="data",ln="scope",Xn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function M(n,t={}){let{dataParam:o=K,scopeParam:r=ln,noPrefixes:i=false,useAccessor:s=false,lexicalPrefix:e}=t;return y(n,o,r,i,s,e)}function y(n,t,o,r,i,s){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(e=>y(e,t,o,r,i,s)));if(E(n))return Yn(n.$,t,r,i,s);if(S(n))return Pn(n,t,o,r,i,s);if(x(n))return nt(n.$pipe,t,o,r,i,s);if(k(n))return Qn(n,t,o,r,i,s);if(R(n))return Zn(n,t,o,r,i,s);if(h(n))return tt(n,t,o,r,i,s);if(b(n))return et(n,t,o,r,i,s);if(typeof n=="object"){let f=Object.entries(n).map(([u,l])=>omniAst.builders.property(omniAst.builders.identifier(u),y(l,t,o,r,i,s)));return omniAst.builders.objectExpression(f)}return omniAst.builders.literal(null)}var X="accessor";function Y(n,t){return t?n===t||n.startsWith(t+"."):false}function Yn(n,t,o,r,i){return r?Y(n,i)?O(n,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(X),[omniAst.builders.literal(n),omniAst.builders.identifier(t)]):n.includes("[*]")?Hn(n,t,o):O(n,t,o)}function O(n,t,o){let r=D(n);if(r.length===0)return o?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t);let i;if(o){let s=r[0];i=omniAst.builders.identifier(s.value);for(let e=1;e<r.length;e++){let f=r[e];f.type==="key"?i=omniAst.builders.memberExpression(i,omniAst.builders.identifier(f.value),false,true):i=omniAst.builders.memberExpression(i,omniAst.builders.literal(f.value),true,true);}}else {i=omniAst.builders.identifier(t);for(let s of r)s.type==="key"?i=omniAst.builders.memberExpression(i,omniAst.builders.identifier(s.value),false,true):i=omniAst.builders.memberExpression(i,omniAst.builders.literal(s.value),true,true);}return i}function Hn(n,t,o){let r=n.indexOf("[*]"),i=n.slice(0,r),s=n.slice(r+3),e;if(i?e=O(i,t,o):e=o?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t),!s||s==="")return e;if(s.includes("[*]"))return an(e,s);let f="_i",u=s.startsWith(".")?s.slice(1):s,l=omniAst.builders.identifier(f);if(u){let a=D(u);for(let p of a)p.type==="key"?l=omniAst.builders.memberExpression(l,omniAst.builders.identifier(p.value),false,true):l=omniAst.builders.memberExpression(l,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],l)])}function an(n,t){let o=t.indexOf("[*]"),r=t.slice(0,o),i=t.slice(o+3),s="_i",e=r.startsWith(".")?r.slice(1):r,f=omniAst.builders.identifier(s);if(e){let l=D(e);for(let a of l)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}if(i.includes("[*]")){let l=an(f,i);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],l)])}let u=i.startsWith(".")?i.slice(1):i;if(u){let l=D(u);for(let a of l)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],f)])}function Pn(n,t,o,r,i,s){let e;if(typeof n.$if=="string"){let l=n.$if.startsWith("!"),a=l?n.$if.slice(1):n.$if,p;i?Y(a,s)?p=O(a,t,true):p=omniAst.builders.callExpression(omniAst.builders.identifier(X),[omniAst.builders.literal(a),omniAst.builders.identifier(t)]):p=O(a,t,r),e=l?omniAst.builders.unaryExpression("!",p):p;}else e=y(n.$if,t,o,r,i,s);let f=y(n.then,t,o,r,i,s),u=n.else!==void 0?y(n.else,t,o,r,i,s):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(e,f,u)}function Qn(n,t,o,r,i,s){let e=r?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(o),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(u=>y(u,t,o,r,i,s));return omniAst.builders.callExpression(e,f)}var Un="context";function Zn(n,t,o,r,i,s){let e=r?omniAst.builders.identifier(n.$cb):omniAst.builders.memberExpression(omniAst.builders.identifier(Un),omniAst.builders.identifier(n.$cb),false,false),f=y(n.body,t,o,r,i,s),l=[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(t),omniAst.builders.identifier("get")],f),omniAst.builders.identifier(t),omniAst.builders.identifier("get")];if(n.params!==void 0){let a=y(n.params,t,o,r,i,s);l.push(a);}return omniAst.builders.callExpression(e,l)}function nt(n,t,o,r,i,s){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return y(n[0],t,o,r,i,s);let e=y(n[0],t,o,r,i,s);for(let f=1;f<n.length;f++){let u=y(n[f],t,o,r,i,s);e=omniAst.builders.callExpression(u,[e]);}return e}function cn(n,t,o,r,i,s){if(E(n)){let e=n.$;return i?Y(e,s)?O(e,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(X),[omniAst.builders.literal(e),omniAst.builders.identifier(t)]):O(e,t,r)}return y(n,t,o,r,i,s)}function tt(n,t,o,r,i,s){let e=cn(n.left,t,o,r,i,s),f=n.right!==void 0?cn(n.right,t,o,r,i,s):omniAst.builders.literal(null),u=Xn[n.op];if(u)return omniAst.builders.binaryExpression(u,e,f);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]));case "exists":return omniAst.builders.binaryExpression("!=",e,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",e,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("startsWith"),false,true),[f]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("endsWith"),false,true),[f]);default:return omniAst.builders.binaryExpression("===",e,f)}}function et(n,t,o,r,i,s){let{logic:e,conditions:f}=n,u=e==="AND"?"&&":"||";if(f.length===0)return omniAst.builders.literal(e==="AND");if(f.length===1)return y(f[0],t,o,r,i,s);let l=y(f[0],t,o,r,i,s);for(let a=1;a<f.length;a++){let p=y(f[a],t,o,r,i,s);l=omniAst.builders.logicalExpression(u,l,p);}return l}function D(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f!=="*"){let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function pn(n,t=[K]){return omniAst.builders.arrowFunctionExpression(t.map(o=>omniAst.builders.identifier(o)),n)}function B(n){let t=new Set;return $(n,t),t}function $(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)$(r,t);return}if(E(n))return;if(S(n)){$(n.$if,t),$(n.then,t),n.else!==void 0&&$(n.else,t);return}if(x(n)){for(let r of n.$pipe)$(r,t);return}if(k(n)){if(t.add(n.$fn),n.args)for(let r of n.args)$(r,t);return}if(R(n)){$(n.body,t),n.params!==void 0&&$(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&$(n.left,t),n.right!==void 0&&typeof n.right=="object"&&$(n.right,t);return}if(b(n)){for(let r of n.conditions)$(r,t);return}let o=n;for(let r of Object.keys(o))$(o[r],t);}function _(n){let t=new Set;return A(n,t),t}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)A(r,t);return}if(E(n))return;if(S(n)){A(n.$if,t),A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(x(n)){for(let r of n.$pipe)A(r,t);return}if(k(n)){if(n.args)for(let r of n.args)A(r,t);return}if(R(n)){t.add(n.$cb),A(n.body,t),n.params!==void 0&&A(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&A(n.left,t),n.right!==void 0&&typeof n.right=="object"&&A(n.right,t);return}if(b(n)){for(let r of n.conditions)A(r,t);return}let o=n;for(let r of Object.keys(o))A(o[r],t);}function V(n){let t=new Set;for(let o of n){let r=o.indexOf("."),i=o.indexOf("["),s=o.length;r!==-1&&(s=Math.min(s,r)),i!==-1&&(s=Math.min(s,i));let e=o.slice(0,s);e&&t.add(e);}return t}function ot(n){return JSON.stringify(n)}function rt(n,t,o,r,i,s){let e=M(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),f=omniAst.generate(e),u="";i?s&&(u=`const{${s}}=data??{};`):t.size>0&&(u=`const{${[...t].join(",")}}=data??{};`);let l=i?new Set([...o,"accessor"]):o,a=l.size>0?`const{${[...l].join(",")}}=scope;`:"",p=r.size>0?`const{${[...r].join(",")}}=context;`:"",d=r.size>0,g="";d&&(i?g="const get=(path)=>accessor(path,data);":g="const get=(path)=>path.split('.').reduce((o,k)=>o?.[k],data);");let w=l.size>0,v;if(w||d){let gn=d?"scope,context":"scope",yn=`${u}${g}return ${f}`;v=`(function(${gn}){${a}${p}return function(data){${yn}}})`;}else v=`(function(){return function(data){${u}return ${f}}})`;return v}function H(n,t={}){let{scope:o={},context:r={},returnCode:i=false,useAccessor:s=false,lexicalPrefix:e}=t,f=F(n),u=V(f),l=B(n),a=_(n),p=ot(n),d=rt(n,u,l,a,s,e);if(i)return {code:d,deps:f,hash:p,dataRoots:[...u],scopeFns:[...l],contextFns:[...a]};let g;try{let w=a.size>0,v=new Function(`return ${d}`)();g=w?v(o,r):v(o);}catch(w){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${w instanceof Error?w.message:String(w)}`)}return {fn:g,deps:f,hash:p}}function dn(n,t,o={}){let{fn:r}=H(n,o);return r(t)}function st(n,t){return L(n,t)}function L(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>L(s,t));let o=n,r=Object.keys(o);for(let s of r)if(s in t){let e=t[s](o);if(typeof e=="object"&&e!==null&&s in e)throw new Error(`Transform "${s}" returned object with same key \u2014 infinite loop`);return L(e,t)}let i={};for(let s of r)i[s]=L(o[s],t);return i}var ft=new Set(["$","$if","$fn","$pipe","$cb"]);function ut(){let n=0;return ()=>String(n++)}function ct(n,t){let{resolvers:o,scope:r={},genId:i,...s}=t,e=Object.keys(o);if(e.length===0)return {expr:n,scope:r};let f=new Set(e),u=[],l={compile:j,genId:i??ut(),scope:r,options:s},a=P(n,f,o,l,u),p={...r};for(let d=0;d<u.length;d++){let[g,w]=u[d];p[g]=w;}return {expr:a,scope:p}}function P(n,t,o,r,i){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n)){let l=n.length,a=new Array(l);for(let p=0;p<l;p++)a[p]=P(n[p],t,o,r,i);return a}let s=n,e=Object.keys(s),f=e.length;for(let l=0;l<f;l++){let a=e[l];if(a[0]==="$"&&!ft.has(a)&&t.has(a)){let p=o[a],d=p(s,r);return d.scopeEntry&&i.push(d.scopeEntry),d.expr}}let u={};for(let l=0;l<f;l++){let a=e[l];u[a]=P(s[a],t,o,r,i);}return u}var _t="2.0.0";exports.ExpressionCache=z;exports.VERSION=_t;exports.assertValid=Dn;exports.builders=un;exports.cache=nn;exports.cached=zn;exports.clearPathCache=Sn;exports.compile=j;exports.compileAST=H;exports.compilePath=N;exports.dslToAST=M;exports.evaluate=Wn;exports.evaluateAST=dn;exports.extractContextFns=_;exports.extractDataRoots=V;exports.extractDeps=F;exports.extractScopeFns=B;exports.get=An;exports.getPathCacheSize=kn;exports.hasDeps=xn;exports.hasWildcard=U;exports.isCb=R;exports.isCondition=h;exports.isConditionExpr=hn;exports.isConditionGroup=b;exports.isConditional=S;exports.isFn=k;exports.isLiteral=q;exports.isPipe=x;exports.isPure=Rn;exports.isRef=E;exports.isValid=Mn;exports.normalize=st;exports.normalizePath=W;exports.resolveBoundaries=ct;exports.validate=J;exports.wrapInFunction=pn;
|
package/dist/index.d.cts
CHANGED
|
@@ -574,20 +574,40 @@ declare function $cb(name: string, body: Expression, params?: Expression): CbExp
|
|
|
574
574
|
* Alias for $cb (callback builder)
|
|
575
575
|
*/
|
|
576
576
|
declare const cb: typeof $cb;
|
|
577
|
+
/**
|
|
578
|
+
* Create a function call expression that always calls the function.
|
|
579
|
+
* Unlike $fn which returns function reference when args is empty/undefined,
|
|
580
|
+
* $call always includes args (defaults to empty array).
|
|
581
|
+
*
|
|
582
|
+
* @param name - Function name (from scope)
|
|
583
|
+
* @param args - Arguments (can be expressions, defaults to [])
|
|
584
|
+
* @returns FnExpr with args always present
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* $call("now") // { $fn: "now", args: [] } - calls function
|
|
588
|
+
* $call("add", [$("a"), 1]) // { $fn: "add", args: [...] }
|
|
589
|
+
*/
|
|
590
|
+
declare function $call(name: string, args?: Expression[]): FnExpr;
|
|
591
|
+
/**
|
|
592
|
+
* Alias for $call (call builder)
|
|
593
|
+
*/
|
|
594
|
+
declare const call: typeof $call;
|
|
577
595
|
|
|
578
596
|
declare const builders_$: typeof $;
|
|
597
|
+
declare const builders_$call: typeof $call;
|
|
579
598
|
declare const builders_$cb: typeof $cb;
|
|
580
599
|
declare const builders_$cond: typeof $cond;
|
|
581
600
|
declare const builders_$fn: typeof $fn;
|
|
582
601
|
declare const builders_$if: typeof $if;
|
|
583
602
|
declare const builders_$pipe: typeof $pipe;
|
|
603
|
+
declare const builders_call: typeof call;
|
|
584
604
|
declare const builders_cb: typeof cb;
|
|
585
605
|
declare const builders_cond: typeof cond;
|
|
586
606
|
declare const builders_fn: typeof fn;
|
|
587
607
|
declare const builders_pipe: typeof pipe;
|
|
588
608
|
declare const builders_ref: typeof ref;
|
|
589
609
|
declare namespace builders {
|
|
590
|
-
export { builders_$ as $, builders_$cb as $cb, builders_$cond as $cond, builders_$fn as $fn, builders_$if as $if, builders_$pipe as $pipe, builders_cb as cb, builders_cond as cond, builders_fn as fn, builders_pipe as pipe, builders_ref as ref };
|
|
610
|
+
export { builders_$ as $, builders_$call as $call, builders_$cb as $cb, builders_$cond as $cond, builders_$fn as $fn, builders_$if as $if, builders_$pipe as $pipe, builders_call as call, builders_cb as cb, builders_cond as cond, builders_fn as fn, builders_pipe as pipe, builders_ref as ref };
|
|
591
611
|
}
|
|
592
612
|
|
|
593
613
|
/**
|
|
@@ -857,6 +877,112 @@ type Transforms = Record<string, TransformFn>;
|
|
|
857
877
|
*/
|
|
858
878
|
declare function normalize(expr: unknown, transforms: Transforms): Expression;
|
|
859
879
|
|
|
880
|
+
/**
|
|
881
|
+
* @statedelta-libs/expressions - Boundaries
|
|
882
|
+
*
|
|
883
|
+
* Resolve custom boundaries ($simulate, $query, etc.) before compilation.
|
|
884
|
+
* Boundaries are "foreign bodies" that stop the expression flow and delegate
|
|
885
|
+
* to external handlers. The handler can compile internal slots independently.
|
|
886
|
+
*
|
|
887
|
+
* @example
|
|
888
|
+
* ```ts
|
|
889
|
+
* const { expr, scope } = resolveBoundaries(
|
|
890
|
+
* { $simulate: { effects: [...], query: {...} } },
|
|
891
|
+
* {
|
|
892
|
+
* scope: baseScope,
|
|
893
|
+
* resolvers: {
|
|
894
|
+
* $simulate: (node, { compile, genId }) => {
|
|
895
|
+
* const id = `__simulate_${genId()}`;
|
|
896
|
+
* const fn = buildSimulateFn(node, compile);
|
|
897
|
+
* return {
|
|
898
|
+
* expr: { $fn: id },
|
|
899
|
+
* scopeEntry: [id, fn]
|
|
900
|
+
* };
|
|
901
|
+
* }
|
|
902
|
+
* }
|
|
903
|
+
* }
|
|
904
|
+
* );
|
|
905
|
+
*
|
|
906
|
+
* compile(expr, { scope });
|
|
907
|
+
* ```
|
|
908
|
+
*/
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Result returned by a boundary resolver
|
|
912
|
+
*/
|
|
913
|
+
interface ResolverResult {
|
|
914
|
+
/** Expression to replace the boundary with */
|
|
915
|
+
expr: Expression;
|
|
916
|
+
/** Optional entry to add to scope [key, value] */
|
|
917
|
+
scopeEntry?: [string, unknown];
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Context provided to boundary resolvers
|
|
921
|
+
*/
|
|
922
|
+
interface ResolverContext {
|
|
923
|
+
/** Compile function for compiling internal slots */
|
|
924
|
+
compile: typeof compile;
|
|
925
|
+
/** Generate unique ID for scope entries */
|
|
926
|
+
genId: () => string;
|
|
927
|
+
/** Current scope (read-only reference) */
|
|
928
|
+
scope: Readonly<Scope>;
|
|
929
|
+
/** Compile options passed to resolveBoundaries */
|
|
930
|
+
options: Readonly<CompileOptions>;
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Boundary resolver function.
|
|
934
|
+
* Receives the raw node and context, returns replacement expression.
|
|
935
|
+
*/
|
|
936
|
+
type BoundaryResolver = (node: Record<string, unknown>, ctx: ResolverContext) => ResolverResult;
|
|
937
|
+
/**
|
|
938
|
+
* Map of boundary resolvers keyed by marker (e.g. "$simulate", "$query")
|
|
939
|
+
*/
|
|
940
|
+
type BoundaryResolvers = Record<string, BoundaryResolver>;
|
|
941
|
+
/**
|
|
942
|
+
* ID generator function
|
|
943
|
+
*/
|
|
944
|
+
type IdGenerator = () => string;
|
|
945
|
+
/**
|
|
946
|
+
* Options for resolveBoundaries
|
|
947
|
+
*/
|
|
948
|
+
interface ResolveBoundariesOptions extends CompileOptions {
|
|
949
|
+
/** Map of boundary resolvers */
|
|
950
|
+
resolvers: BoundaryResolvers;
|
|
951
|
+
/** Custom ID generator. Default: simple counter ("0", "1", "2"...) */
|
|
952
|
+
genId?: IdGenerator;
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Result of resolveBoundaries
|
|
956
|
+
*/
|
|
957
|
+
interface ResolveBoundariesResult {
|
|
958
|
+
/** Pure expression with boundaries replaced */
|
|
959
|
+
expr: Expression;
|
|
960
|
+
/** Enriched scope with boundary handlers */
|
|
961
|
+
scope: Scope;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Resolve boundaries in an expression tree.
|
|
965
|
+
*
|
|
966
|
+
* Boundaries are custom DSL nodes ($simulate, $query, etc.) that stop the
|
|
967
|
+
* normal expression compilation flow. Each boundary is extracted, processed
|
|
968
|
+
* by its resolver, and replaced with a standard expression.
|
|
969
|
+
*
|
|
970
|
+
* The resolver receives:
|
|
971
|
+
* - `node`: The raw boundary object (e.g. { $simulate: {...}, effects: [...] })
|
|
972
|
+
* - `ctx.compile`: Compile function for internal slots
|
|
973
|
+
* - `ctx.genId`: Unique ID generator
|
|
974
|
+
* - `ctx.scope`: Current scope (read-only)
|
|
975
|
+
*
|
|
976
|
+
* The resolver returns:
|
|
977
|
+
* - `expr`: Replacement expression (typically { $fn: "__id" })
|
|
978
|
+
* - `scopeEntry`: Optional [key, fn] to add to scope
|
|
979
|
+
*
|
|
980
|
+
* @param expr - Expression (possibly with boundaries)
|
|
981
|
+
* @param options - Resolvers, scope, and compile options
|
|
982
|
+
* @returns Pure expression and enriched scope
|
|
983
|
+
*/
|
|
984
|
+
declare function resolveBoundaries(expr: unknown, options: ResolveBoundariesOptions): ResolveBoundariesResult;
|
|
985
|
+
|
|
860
986
|
/**
|
|
861
987
|
* @statedelta-libs/expressions
|
|
862
988
|
*
|
|
@@ -892,4 +1018,4 @@ declare function normalize(expr: unknown, transforms: Transforms): Expression;
|
|
|
892
1018
|
*/
|
|
893
1019
|
declare const VERSION = "2.0.0";
|
|
894
1020
|
|
|
895
|
-
export { type AccessorFn, type CbExpr, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Context, type ContextFn, type Expression, ExpressionCache, type FnExpr, type Literal, type PathGetter, type PathGetterFn, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractContextFns, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCb, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalize, normalizePath, validate, wrapInFunction };
|
|
1021
|
+
export { type AccessorFn, type BoundaryResolver, type BoundaryResolvers, type CbExpr, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Context, type ContextFn, type Expression, ExpressionCache, type FnExpr, type IdGenerator, type Literal, type PathGetter, type PathGetterFn, type PipeExpr, type RefExpr, type ResolveBoundariesOptions, type ResolveBoundariesResult, type ResolverContext, type ResolverResult, type Scope, type TransformFn, type TransformOptions, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractContextFns, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCb, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalize, normalizePath, resolveBoundaries, validate, wrapInFunction };
|
package/dist/index.d.ts
CHANGED
|
@@ -574,20 +574,40 @@ declare function $cb(name: string, body: Expression, params?: Expression): CbExp
|
|
|
574
574
|
* Alias for $cb (callback builder)
|
|
575
575
|
*/
|
|
576
576
|
declare const cb: typeof $cb;
|
|
577
|
+
/**
|
|
578
|
+
* Create a function call expression that always calls the function.
|
|
579
|
+
* Unlike $fn which returns function reference when args is empty/undefined,
|
|
580
|
+
* $call always includes args (defaults to empty array).
|
|
581
|
+
*
|
|
582
|
+
* @param name - Function name (from scope)
|
|
583
|
+
* @param args - Arguments (can be expressions, defaults to [])
|
|
584
|
+
* @returns FnExpr with args always present
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* $call("now") // { $fn: "now", args: [] } - calls function
|
|
588
|
+
* $call("add", [$("a"), 1]) // { $fn: "add", args: [...] }
|
|
589
|
+
*/
|
|
590
|
+
declare function $call(name: string, args?: Expression[]): FnExpr;
|
|
591
|
+
/**
|
|
592
|
+
* Alias for $call (call builder)
|
|
593
|
+
*/
|
|
594
|
+
declare const call: typeof $call;
|
|
577
595
|
|
|
578
596
|
declare const builders_$: typeof $;
|
|
597
|
+
declare const builders_$call: typeof $call;
|
|
579
598
|
declare const builders_$cb: typeof $cb;
|
|
580
599
|
declare const builders_$cond: typeof $cond;
|
|
581
600
|
declare const builders_$fn: typeof $fn;
|
|
582
601
|
declare const builders_$if: typeof $if;
|
|
583
602
|
declare const builders_$pipe: typeof $pipe;
|
|
603
|
+
declare const builders_call: typeof call;
|
|
584
604
|
declare const builders_cb: typeof cb;
|
|
585
605
|
declare const builders_cond: typeof cond;
|
|
586
606
|
declare const builders_fn: typeof fn;
|
|
587
607
|
declare const builders_pipe: typeof pipe;
|
|
588
608
|
declare const builders_ref: typeof ref;
|
|
589
609
|
declare namespace builders {
|
|
590
|
-
export { builders_$ as $, builders_$cb as $cb, builders_$cond as $cond, builders_$fn as $fn, builders_$if as $if, builders_$pipe as $pipe, builders_cb as cb, builders_cond as cond, builders_fn as fn, builders_pipe as pipe, builders_ref as ref };
|
|
610
|
+
export { builders_$ as $, builders_$call as $call, builders_$cb as $cb, builders_$cond as $cond, builders_$fn as $fn, builders_$if as $if, builders_$pipe as $pipe, builders_call as call, builders_cb as cb, builders_cond as cond, builders_fn as fn, builders_pipe as pipe, builders_ref as ref };
|
|
591
611
|
}
|
|
592
612
|
|
|
593
613
|
/**
|
|
@@ -857,6 +877,112 @@ type Transforms = Record<string, TransformFn>;
|
|
|
857
877
|
*/
|
|
858
878
|
declare function normalize(expr: unknown, transforms: Transforms): Expression;
|
|
859
879
|
|
|
880
|
+
/**
|
|
881
|
+
* @statedelta-libs/expressions - Boundaries
|
|
882
|
+
*
|
|
883
|
+
* Resolve custom boundaries ($simulate, $query, etc.) before compilation.
|
|
884
|
+
* Boundaries are "foreign bodies" that stop the expression flow and delegate
|
|
885
|
+
* to external handlers. The handler can compile internal slots independently.
|
|
886
|
+
*
|
|
887
|
+
* @example
|
|
888
|
+
* ```ts
|
|
889
|
+
* const { expr, scope } = resolveBoundaries(
|
|
890
|
+
* { $simulate: { effects: [...], query: {...} } },
|
|
891
|
+
* {
|
|
892
|
+
* scope: baseScope,
|
|
893
|
+
* resolvers: {
|
|
894
|
+
* $simulate: (node, { compile, genId }) => {
|
|
895
|
+
* const id = `__simulate_${genId()}`;
|
|
896
|
+
* const fn = buildSimulateFn(node, compile);
|
|
897
|
+
* return {
|
|
898
|
+
* expr: { $fn: id },
|
|
899
|
+
* scopeEntry: [id, fn]
|
|
900
|
+
* };
|
|
901
|
+
* }
|
|
902
|
+
* }
|
|
903
|
+
* }
|
|
904
|
+
* );
|
|
905
|
+
*
|
|
906
|
+
* compile(expr, { scope });
|
|
907
|
+
* ```
|
|
908
|
+
*/
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Result returned by a boundary resolver
|
|
912
|
+
*/
|
|
913
|
+
interface ResolverResult {
|
|
914
|
+
/** Expression to replace the boundary with */
|
|
915
|
+
expr: Expression;
|
|
916
|
+
/** Optional entry to add to scope [key, value] */
|
|
917
|
+
scopeEntry?: [string, unknown];
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Context provided to boundary resolvers
|
|
921
|
+
*/
|
|
922
|
+
interface ResolverContext {
|
|
923
|
+
/** Compile function for compiling internal slots */
|
|
924
|
+
compile: typeof compile;
|
|
925
|
+
/** Generate unique ID for scope entries */
|
|
926
|
+
genId: () => string;
|
|
927
|
+
/** Current scope (read-only reference) */
|
|
928
|
+
scope: Readonly<Scope>;
|
|
929
|
+
/** Compile options passed to resolveBoundaries */
|
|
930
|
+
options: Readonly<CompileOptions>;
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Boundary resolver function.
|
|
934
|
+
* Receives the raw node and context, returns replacement expression.
|
|
935
|
+
*/
|
|
936
|
+
type BoundaryResolver = (node: Record<string, unknown>, ctx: ResolverContext) => ResolverResult;
|
|
937
|
+
/**
|
|
938
|
+
* Map of boundary resolvers keyed by marker (e.g. "$simulate", "$query")
|
|
939
|
+
*/
|
|
940
|
+
type BoundaryResolvers = Record<string, BoundaryResolver>;
|
|
941
|
+
/**
|
|
942
|
+
* ID generator function
|
|
943
|
+
*/
|
|
944
|
+
type IdGenerator = () => string;
|
|
945
|
+
/**
|
|
946
|
+
* Options for resolveBoundaries
|
|
947
|
+
*/
|
|
948
|
+
interface ResolveBoundariesOptions extends CompileOptions {
|
|
949
|
+
/** Map of boundary resolvers */
|
|
950
|
+
resolvers: BoundaryResolvers;
|
|
951
|
+
/** Custom ID generator. Default: simple counter ("0", "1", "2"...) */
|
|
952
|
+
genId?: IdGenerator;
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Result of resolveBoundaries
|
|
956
|
+
*/
|
|
957
|
+
interface ResolveBoundariesResult {
|
|
958
|
+
/** Pure expression with boundaries replaced */
|
|
959
|
+
expr: Expression;
|
|
960
|
+
/** Enriched scope with boundary handlers */
|
|
961
|
+
scope: Scope;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Resolve boundaries in an expression tree.
|
|
965
|
+
*
|
|
966
|
+
* Boundaries are custom DSL nodes ($simulate, $query, etc.) that stop the
|
|
967
|
+
* normal expression compilation flow. Each boundary is extracted, processed
|
|
968
|
+
* by its resolver, and replaced with a standard expression.
|
|
969
|
+
*
|
|
970
|
+
* The resolver receives:
|
|
971
|
+
* - `node`: The raw boundary object (e.g. { $simulate: {...}, effects: [...] })
|
|
972
|
+
* - `ctx.compile`: Compile function for internal slots
|
|
973
|
+
* - `ctx.genId`: Unique ID generator
|
|
974
|
+
* - `ctx.scope`: Current scope (read-only)
|
|
975
|
+
*
|
|
976
|
+
* The resolver returns:
|
|
977
|
+
* - `expr`: Replacement expression (typically { $fn: "__id" })
|
|
978
|
+
* - `scopeEntry`: Optional [key, fn] to add to scope
|
|
979
|
+
*
|
|
980
|
+
* @param expr - Expression (possibly with boundaries)
|
|
981
|
+
* @param options - Resolvers, scope, and compile options
|
|
982
|
+
* @returns Pure expression and enriched scope
|
|
983
|
+
*/
|
|
984
|
+
declare function resolveBoundaries(expr: unknown, options: ResolveBoundariesOptions): ResolveBoundariesResult;
|
|
985
|
+
|
|
860
986
|
/**
|
|
861
987
|
* @statedelta-libs/expressions
|
|
862
988
|
*
|
|
@@ -892,4 +1018,4 @@ declare function normalize(expr: unknown, transforms: Transforms): Expression;
|
|
|
892
1018
|
*/
|
|
893
1019
|
declare const VERSION = "2.0.0";
|
|
894
1020
|
|
|
895
|
-
export { type AccessorFn, type CbExpr, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Context, type ContextFn, type Expression, ExpressionCache, type FnExpr, type Literal, type PathGetter, type PathGetterFn, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractContextFns, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCb, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalize, normalizePath, validate, wrapInFunction };
|
|
1021
|
+
export { type AccessorFn, type BoundaryResolver, type BoundaryResolvers, type CbExpr, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Context, type ContextFn, type Expression, ExpressionCache, type FnExpr, type IdGenerator, type Literal, type PathGetter, type PathGetterFn, type PipeExpr, type RefExpr, type ResolveBoundariesOptions, type ResolveBoundariesResult, type ResolverContext, type ResolverResult, type Scope, type TransformFn, type TransformOptions, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractContextFns, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCb, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalize, normalizePath, resolveBoundaries, validate, wrapInFunction };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {builders,generate}from'omni-ast';var gn=Object.defineProperty;var mn=(n,t)=>{for(var o in t)gn(n,o,{get:t[o],enumerable:true});};var E=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,S=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,w=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",k=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),x=n=>n!==null&&typeof n=="object"&&"$cb"in n&&typeof n.$cb=="string"&&"body"in n,v=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),h=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&v.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),b=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,yn=n=>h(n)||b(n),B=n=>{if(n===null)return true;let t=typeof n;if(t==="string"||t==="number"||t==="boolean"||Array.isArray(n))return true;if(t==="object"&&n!==null){let o=n,r="left"in o&&"op"in o&&v.has(o.op);return !("$"in o)&&!("$if"in o)&&!("$fn"in o)&&!("$pipe"in o)&&!("$cb"in o)&&!r&&!("logic"in o)}return false};var G=new Map;function Q(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f==="*")t.push({type:"wildcard",value:"*"});else {let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function U(n){return n.includes("[*]")}function j(n){let t=G.get(n);return t||(t=U(n)?hn(n):En(n),G.set(n,t),t)}function En(n){if(!n.includes(".")&&!n.includes("["))return i=>i?.[n];let t=Q(n),o=t.length;if(o===2){let[i,s]=t,e=i.value,f=s.value;return u=>u?.[e]?.[f]}if(o===3){let[i,s,e]=t,f=i.value,u=s.value,a=e.value;return l=>l?.[f]?.[u]?.[a]}let r=t.map(i=>i.value);return i=>{let s=i;for(let e=0;e<o&&s!=null;e++)s=s[r[e]];return s}}function hn(n){let t=Q(n),o=[];for(let r=0;r<t.length;r++)t[r].type==="wildcard"&&o.push(r);return o.length===1?bn(t,o[0]):Cn(t,o)}function bn(n,t){let o=n.slice(0,t).map(e=>e.value),r=n.slice(t+1).map(e=>e.value),i=o.length,s=r.length;if(s===0){if(i===1){let e=o[0];return f=>f?.[e]}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];return f}}if(s===1){let e=r[0];if(i===1){let f=o[0];return u=>{let a=u?.[f];if(Array.isArray(a))return a.map(l=>l?.[e])}}return f=>{let u=f;for(let a=0;a<i&&u!=null;a++)u=u[o[a]];if(Array.isArray(u))return u.map(a=>a?.[e])}}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];if(Array.isArray(f))return f.map(u=>{let a=u;for(let l=0;l<s&&a!=null;l++)a=a[r[l]];return a})}}function Cn(n,t){let o=[],r=0;for(let s=0;s<t.length;s++){let e=t[s],f=s===t.length-1,u=n.slice(r,e).map(a=>a.value);u.length>0&&o.push({type:"access",keys:u}),o.push({type:f?"map":"flatMap",keys:[]}),r=e+1;}let i=n.slice(r).map(s=>s.value);return s=>{let e=s;for(let f of o){if(e==null)return;if(f.type==="access")for(let u of f.keys){if(e==null)return;e=e[u];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(u=>{let a=u;return Array.isArray(a)?a:[a]});}else if(f.type==="map"){if(!Array.isArray(e))return;i.length>0&&(e=e.map(u=>{let a=u;for(let l of i){if(a==null)return;a=a[l];}return a}));}}return e}}function Tn(n,t){return j(t)(n)}function W(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function $n(){G.clear();}function An(){return G.size}function R(n){let t=new Set;return C(n,t),Array.from(t)}function C(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i=0;i<n.length;i++)C(n[i],t);return}if(E(n)){t.add(W(n.$));return}if(S(n)){if(typeof n.$if=="string"){let i=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(W(i));}else C(n.$if,t);C(n.then,t),n.else!==void 0&&C(n.else,t);return}if(k(n)){for(let i=0;i<n.$pipe.length;i++)C(n.$pipe[i],t);return}if(w(n)){if(n.args)for(let i=0;i<n.args.length;i++)C(n.args[i],t);return}if(x(n)){C(n.body,t),n.params!==void 0&&C(n.params,t);return}if(h(n)){C(n.left,t),n.right!==void 0&&C(n.right,t);return}if(b(n)){for(let i=0;i<n.conditions.length;i++)C(n.conditions[i],t);return}let o=n,r=Object.keys(o);for(let i=0;i<r.length;i++)C(o[r[i]],t);}function Sn(n){return R(n).length>0}function wn(n){return R(n).length===0}function kn(n){return JSON.stringify(n)}function z(n,t={}){let o=t.scope??{},r=t.accessor,i=t.context??{},s=y(n,o,r,i),e=R(n),f=kn(n);return {fn:s,deps:e,hash:f}}function y(n,t,o,r){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>y(s,t,o,r));return s=>i.map(e=>e(s))}if(E(n))return xn(n,o);if(S(n))return Fn(n,t,o,r);if(k(n))return Rn(n,t,o,r);if(w(n))return On(n,t,o,r);if(x(n))return Nn(n,t,o,r);if(h(n))return jn(n,t,o,r);if(b(n))return vn(n,t,o,r);if(B(n)){let i=n,s=Object.keys(i),e=s.map(f=>y(i[f],t,o,r));return f=>{let u={};for(let a=0;a<s.length;a++)u[s[a]]=e[a](f);return u}}return ()=>n}function Z(n,t){return t?o=>t(n,o):j(n)}function xn(n,t){return Z(n.$,t)}function Fn(n,t,o,r){let i;if(typeof n.$if=="string"){let f=n.$if.startsWith("!")?n.$if.slice(1):n.$if,u=Z(f,o);i=n.$if.startsWith("!")?l=>!u(l):l=>!!u(l);}else {let f=y(n.$if,t,o,r);i=u=>!!f(u);}let s=y(n.then,t,o,r),e=n.else!==void 0?y(n.else,t,o,r):()=>{};return f=>i(f)?s(f):e(f)}function Rn(n,t,o,r){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return y(i[0],t,o,r);let s=y(i[0],t,o,r),e=i.slice(1).map(u=>y(u,t,o,r)),f=e.length;if(f===1){let[u]=e;return a=>{let l=s(a),p=u(a);return typeof p=="function"?p(l):p}}if(f===2){let[u,a]=e;return l=>{let p=s(l),d=u(l);return p=typeof d=="function"?d(p):d,d=a(l),typeof d=="function"?d(p):d}}if(f===3){let[u,a,l]=e;return p=>{let d=s(p),g=u(p);return d=typeof g=="function"?g(d):g,g=a(p),d=typeof g=="function"?g(d):g,g=l(p),typeof g=="function"?g(d):g}}return u=>{let a=s(u);for(let l=0;l<f;l++){let p=e[l](u);a=typeof p=="function"?p(a):p;}return a}}function On(n,t,o,r){let i=n.$fn,s=n.args;if(s===void 0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let e=s.map(u=>y(u,t,o,r)),f=e.length;if(f===0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(f===1){let[u]=e;return a=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(u(a))}}if(f===2){let[u,a]=e;return l=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(l),a(l))}}if(f===3){let[u,a,l]=e;return p=>{let d=t[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),a(p),l(p))}}return u=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(...e.map(l=>l(u)))}}function Nn(n,t,o,r){let i=n.$cb,s=y(n.body,t,o,r),e=n.params?y(n.params,t,o,r):void 0,f=u=>o?a=>o(a,u):a=>j(a)(u);return u=>{let a=r?.[i];if(!a)throw new Error(`Context function not found: ${i}`);let l=(g,F)=>s(g),p=f(u),d=e?e(u):void 0;return a(l,u,p,d)}}function jn(n,t,o,r){let i=y(n.left,t,o,r),s=n.right!==void 0?y(n.right,t,o,r):()=>{};switch(n.op){case "eq":return e=>i(e)===s(e);case "neq":return e=>i(e)!==s(e);case "gt":return e=>i(e)>s(e);case "gte":return e=>i(e)>=s(e);case "lt":return e=>i(e)<s(e);case "lte":return e=>i(e)<=s(e);case "in":return e=>{let f=s(e);return Array.isArray(f)&&f.includes(i(e))};case "notIn":return e=>{let f=s(e);return !Array.isArray(f)||!f.includes(i(e))};case "contains":return e=>{let f=i(e);return Array.isArray(f)&&f.includes(s(e))};case "notContains":return e=>{let f=i(e);return !Array.isArray(f)||!f.includes(s(e))};case "exists":return e=>i(e)!==void 0;case "notExists":return e=>i(e)===void 0;case "matches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?false:new RegExp(u).test(f)};case "notMatches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?true:!new RegExp(u).test(f)};case "startsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.startsWith(u)};case "endsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.endsWith(u)}}}function vn(n,t,o,r){let i=n.conditions.map(e=>y(e,t,o,r)),s=i.length;if(s===1)return e=>!!i[0](e);if(s===2){let[e,f]=i;return n.logic==="AND"?u=>!!e(u)&&!!f(u):u=>!!e(u)||!!f(u)}if(s===3){let[e,f,u]=i;return n.logic==="AND"?a=>!!e(a)&&!!f(a)&&!!u(a):a=>!!e(a)||!!f(a)||!!u(a)}return n.logic==="AND"?e=>{for(let f=0;f<s;f++)if(!i[f](e))return false;return true}:e=>{for(let f=0;f<s;f++)if(i[f](e))return true;return false}}function Gn(n,t,o={}){return z(n,o).fn(t)}var D=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,o={}){let r=JSON.stringify(t),i=this.cache.get(r);if(i)return this.cache.delete(r),this.cache.set(r,i),i;let s=z(t,o);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(r,s),s}has(t){return this.cache.has(JSON.stringify(t))}delete(t){return this.cache.delete(JSON.stringify(t))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(t){for(this._maxSize=t;this.cache.size>this._maxSize;){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}}},P=new D;function Wn(n,t={}){return P.get(n,t)}function J(n,t="root",o={}){let r=[];return T(n,t,r,o),{valid:r.length===0,errors:r}}function T(n,t,o,r){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)T(n[e],`${t}[${e}]`,o,r);return}if(E(n)){(!n.$||typeof n.$!="string")&&o.push(`${t}: invalid reference, $ must be non-empty string`);return}if(S(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||o.push(`${t}.$if: empty path in string shorthand`):T(n.$if,`${t}.$if`,o,r),T(n.then,`${t}.then`,o,r),n.else!==void 0&&T(n.else,`${t}.else`,o,r);return}if(k(n)){if(!Array.isArray(n.$pipe)){o.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){o.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)T(n.$pipe[e],`${t}.$pipe[${e}]`,o,r);return}if(w(n)){if(!n.$fn||typeof n.$fn!="string"){o.push(`${t}: invalid function, $fn must be non-empty string`);return}if(r.scope&&!(n.$fn in r.scope)&&o.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))o.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)T(n.args[e],`${t}.args[${e}]`,o,r);return}if(x(n)){if(!n.$cb||typeof n.$cb!="string"){o.push(`${t}: invalid callback, $cb must be non-empty string`);return}r.context&&!(n.$cb in r.context)&&o.push(`${t}: context function "${n.$cb}" not found`),T(n.body,`${t}.body`,o,r),n.params!==void 0&&T(n.params,`${t}.params`,o,r);return}if(h(n)){v.has(n.op)||o.push(`${t}: invalid operator "${n.op}"`),T(n.left,`${t}.left`,o,r),n.right!==void 0&&T(n.right,`${t}.right`,o,r);return}if(b(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&o.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){o.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)T(n.conditions[e],`${t}.conditions[${e}]`,o,r);return}let i=n,s=Object.keys(i);for(let e=0;e<s.length;e++){let f=s[e];T(i[f],`${t}.${f}`,o,r);}}function zn(n,t={}){let o=J(n,"root",t);if(!o.valid)throw new Error(`Invalid expression: ${o.errors.join("; ")}`)}function Dn(n,t={}){return J(n,"root",t).valid}var sn={};mn(sn,{$:()=>nn,$cb:()=>rn,$cond:()=>on,$fn:()=>tn,$if:()=>_n,$pipe:()=>en,cb:()=>qn,cond:()=>Ln,fn:()=>In,pipe:()=>Vn,ref:()=>Mn});function nn(n){return {$:n}}var Mn=nn;function tn(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var In=tn;function _n(n,t,o){return o===void 0?{$if:n,then:t}:{$if:n,then:t,else:o}}function en(...n){return {$pipe:n}}var Vn=en;function on(n,t,o){return o===void 0?{left:n,op:t}:{left:n,op:t,right:o}}var Ln=on;function rn(n,t,o){return o===void 0?{$cb:n,body:t}:{$cb:n,body:t,params:o}}var qn=rn;var K="data",un="scope",Bn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function I(n,t={}){let{dataParam:o=K,scopeParam:r=un,noPrefixes:i=false,useAccessor:s=false,lexicalPrefix:e}=t;return m(n,o,r,i,s,e)}function m(n,t,o,r,i,s){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(e=>m(e,t,o,r,i,s)));if(E(n))return Jn(n.$,t,r,i,s);if(S(n))return Xn(n,t,o,r,i,s);if(k(n))return Un(n.$pipe,t,o,r,i,s);if(w(n))return Yn(n,t,o,r,i,s);if(x(n))return Qn(n,t,o,r,i,s);if(h(n))return Zn(n,t,o,r,i,s);if(b(n))return Pn(n,t,o,r,i,s);if(typeof n=="object"){let f=Object.entries(n).map(([u,a])=>builders.property(builders.identifier(u),m(a,t,o,r,i,s)));return builders.objectExpression(f)}return builders.literal(null)}var X="accessor";function Y(n,t){return t?n===t||n.startsWith(t+"."):false}function Jn(n,t,o,r,i){return r?Y(n,i)?O(n,t,true):builders.callExpression(builders.identifier(X),[builders.literal(n),builders.identifier(t)]):n.includes("[*]")?Kn(n,t,o):O(n,t,o)}function O(n,t,o){let r=M(n);if(r.length===0)return o?builders.identifier("undefined"):builders.identifier(t);let i;if(o){let s=r[0];i=builders.identifier(s.value);for(let e=1;e<r.length;e++){let f=r[e];f.type==="key"?i=builders.memberExpression(i,builders.identifier(f.value),false,true):i=builders.memberExpression(i,builders.literal(f.value),true,true);}}else {i=builders.identifier(t);for(let s of r)s.type==="key"?i=builders.memberExpression(i,builders.identifier(s.value),false,true):i=builders.memberExpression(i,builders.literal(s.value),true,true);}return i}function Kn(n,t,o){let r=n.indexOf("[*]"),i=n.slice(0,r),s=n.slice(r+3),e;if(i?e=O(i,t,o):e=o?builders.identifier("undefined"):builders.identifier(t),!s||s==="")return e;if(s.includes("[*]"))return cn(e,s);let f="_i",u=s.startsWith(".")?s.slice(1):s,a=builders.identifier(f);if(u){let l=M(u);for(let p of l)p.type==="key"?a=builders.memberExpression(a,builders.identifier(p.value),false,true):a=builders.memberExpression(a,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(e,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],a)])}function cn(n,t){let o=t.indexOf("[*]"),r=t.slice(0,o),i=t.slice(o+3),s="_i",e=r.startsWith(".")?r.slice(1):r,f=builders.identifier(s);if(e){let a=M(e);for(let l of a)l.type==="key"&&(f=builders.memberExpression(f,builders.identifier(l.value),false,true));}if(i.includes("[*]")){let a=cn(f,i);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],a)])}let u=i.startsWith(".")?i.slice(1):i;if(u){let a=M(u);for(let l of a)l.type==="key"&&(f=builders.memberExpression(f,builders.identifier(l.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],f)])}function Xn(n,t,o,r,i,s){let e;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),l=a?n.$if.slice(1):n.$if,p;i?Y(l,s)?p=O(l,t,true):p=builders.callExpression(builders.identifier(X),[builders.literal(l),builders.identifier(t)]):p=O(l,t,r),e=a?builders.unaryExpression("!",p):p;}else e=m(n.$if,t,o,r,i,s);let f=m(n.then,t,o,r,i,s),u=n.else!==void 0?m(n.else,t,o,r,i,s):builders.identifier("undefined");return builders.conditionalExpression(e,f,u)}function Yn(n,t,o,r,i,s){let e=r?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(o),builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(u=>m(u,t,o,r,i,s));return builders.callExpression(e,f)}var Hn="context";function Qn(n,t,o,r,i,s){let e=r?builders.identifier(n.$cb):builders.memberExpression(builders.identifier(Hn),builders.identifier(n.$cb),false,false),f=m(n.body,t,o,r,i,s),a=[builders.arrowFunctionExpression([builders.identifier(t),builders.identifier("get")],f),builders.identifier(t),builders.identifier("get")];if(n.params!==void 0){let l=m(n.params,t,o,r,i,s);a.push(l);}return builders.callExpression(e,a)}function Un(n,t,o,r,i,s){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return m(n[0],t,o,r,i,s);let e=m(n[0],t,o,r,i,s);for(let f=1;f<n.length;f++){let u=m(n[f],t,o,r,i,s);e=builders.callExpression(u,[e]);}return e}function fn(n,t,o,r,i,s){if(E(n)){let e=n.$;return i?Y(e,s)?O(e,t,true):builders.callExpression(builders.identifier(X),[builders.literal(e),builders.identifier(t)]):O(e,t,r)}return m(n,t,o,r,i,s)}function Zn(n,t,o,r,i,s){let e=fn(n.left,t,o,r,i,s),f=n.right!==void 0?fn(n.right,t,o,r,i,s):builders.literal(null),u=Bn[n.op];if(u)return builders.binaryExpression(u,e,f);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]));case "contains":return builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]));case "exists":return builders.binaryExpression("!=",e,builders.literal(null));case "notExists":return builders.binaryExpression("==",e,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]));case "startsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("startsWith"),false,true),[f]);case "endsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("endsWith"),false,true),[f]);default:return builders.binaryExpression("===",e,f)}}function Pn(n,t,o,r,i,s){let{logic:e,conditions:f}=n,u=e==="AND"?"&&":"||";if(f.length===0)return builders.literal(e==="AND");if(f.length===1)return m(f[0],t,o,r,i,s);let a=m(f[0],t,o,r,i,s);for(let l=1;l<f.length;l++){let p=m(f[l],t,o,r,i,s);a=builders.logicalExpression(u,a,p);}return a}function M(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f!=="*"){let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function an(n,t=[K]){return builders.arrowFunctionExpression(t.map(o=>builders.identifier(o)),n)}function _(n){let t=new Set;return $(n,t),t}function $(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)$(r,t);return}if(E(n))return;if(S(n)){$(n.$if,t),$(n.then,t),n.else!==void 0&&$(n.else,t);return}if(k(n)){for(let r of n.$pipe)$(r,t);return}if(w(n)){if(t.add(n.$fn),n.args)for(let r of n.args)$(r,t);return}if(x(n)){$(n.body,t),n.params!==void 0&&$(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&$(n.left,t),n.right!==void 0&&typeof n.right=="object"&&$(n.right,t);return}if(b(n)){for(let r of n.conditions)$(r,t);return}let o=n;for(let r of Object.keys(o))$(o[r],t);}function V(n){let t=new Set;return A(n,t),t}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)A(r,t);return}if(E(n))return;if(S(n)){A(n.$if,t),A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(k(n)){for(let r of n.$pipe)A(r,t);return}if(w(n)){if(n.args)for(let r of n.args)A(r,t);return}if(x(n)){t.add(n.$cb),A(n.body,t),n.params!==void 0&&A(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&A(n.left,t),n.right!==void 0&&typeof n.right=="object"&&A(n.right,t);return}if(b(n)){for(let r of n.conditions)A(r,t);return}let o=n;for(let r of Object.keys(o))A(o[r],t);}function L(n){let t=new Set;for(let o of n){let r=o.indexOf("."),i=o.indexOf("["),s=o.length;r!==-1&&(s=Math.min(s,r)),i!==-1&&(s=Math.min(s,i));let e=o.slice(0,s);e&&t.add(e);}return t}function tt(n){return JSON.stringify(n)}function et(n,t,o,r,i,s){let e=I(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),f=generate(e),u="";i?s&&(u=`const{${s}}=data??{};`):t.size>0&&(u=`const{${[...t].join(",")}}=data??{};`);let a=i?new Set([...o,"accessor"]):o,l=a.size>0?`const{${[...a].join(",")}}=scope;`:"",p=r.size>0?`const{${[...r].join(",")}}=context;`:"",d=r.size>0,g="";d&&(i?g="const get=(path)=>accessor(path,data);":g="const get=(path)=>path.split('.').reduce((o,k)=>o?.[k],data);");let F=a.size>0,N;if(F||d){let pn=d?"scope,context":"scope",dn=`${u}${g}return ${f}`;N=`(function(${pn}){${l}${p}return function(data){${dn}}})`;}else N=`(function(){return function(data){${u}return ${f}}})`;return N}function H(n,t={}){let{scope:o={},context:r={},returnCode:i=false,useAccessor:s=false,lexicalPrefix:e}=t,f=R(n),u=L(f),a=_(n),l=V(n),p=tt(n),d=et(n,u,a,l,s,e);if(i)return {code:d,deps:f,hash:p,dataRoots:[...u],scopeFns:[...a],contextFns:[...l]};let g;try{let F=l.size>0,N=new Function(`return ${d}`)();g=F?N(o,r):N(o);}catch(F){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${F instanceof Error?F.message:String(F)}`)}return {fn:g,deps:f,hash:p}}function ln(n,t,o={}){let{fn:r}=H(n,o);return r(t)}function it(n,t){return q(n,t)}function q(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>q(s,t));let o=n,r=Object.keys(o);for(let s of r)if(s in t){let e=t[s](o);if(typeof e=="object"&&e!==null&&s in e)throw new Error(`Transform "${s}" returned object with same key \u2014 infinite loop`);return q(e,t)}let i={};for(let s of r)i[s]=q(o[s],t);return i}var vt="2.0.0";export{D as ExpressionCache,vt as VERSION,zn as assertValid,sn as builders,P as cache,Wn as cached,$n as clearPathCache,z as compile,H as compileAST,j as compilePath,I as dslToAST,Gn as evaluate,ln as evaluateAST,V as extractContextFns,L as extractDataRoots,R as extractDeps,_ as extractScopeFns,Tn as get,An as getPathCacheSize,Sn as hasDeps,U as hasWildcard,x as isCb,h as isCondition,yn as isConditionExpr,b as isConditionGroup,S as isConditional,w as isFn,B as isLiteral,k as isPipe,wn as isPure,E as isRef,Dn as isValid,it as normalize,W as normalizePath,J as validate,an as wrapInFunction};
|
|
1
|
+
import {builders,generate}from'omni-ast';var mn=Object.defineProperty;var En=(n,t)=>{for(var o in t)mn(n,o,{get:t[o],enumerable:true});};var E=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,S=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,k=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",x=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),R=n=>n!==null&&typeof n=="object"&&"$cb"in n&&typeof n.$cb=="string"&&"body"in n,G=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),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),b=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,hn=n=>h(n)||b(n),q=n=>{if(n===null)return true;let t=typeof n;if(t==="string"||t==="number"||t==="boolean"||Array.isArray(n))return true;if(t==="object"&&n!==null){let o=n,r="left"in o&&"op"in o&&G.has(o.op);return !("$"in o)&&!("$if"in o)&&!("$fn"in o)&&!("$pipe"in o)&&!("$cb"in o)&&!r&&!("logic"in o)}return false};var I=new Map;function Q(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f==="*")t.push({type:"wildcard",value:"*"});else {let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function U(n){return n.includes("[*]")}function N(n){let t=I.get(n);return t||(t=U(n)?Cn(n):bn(n),I.set(n,t),t)}function bn(n){if(!n.includes(".")&&!n.includes("["))return i=>i?.[n];let t=Q(n),o=t.length;if(o===2){let[i,s]=t,e=i.value,f=s.value;return u=>u?.[e]?.[f]}if(o===3){let[i,s,e]=t,f=i.value,u=s.value,l=e.value;return a=>a?.[f]?.[u]?.[l]}let r=t.map(i=>i.value);return i=>{let s=i;for(let e=0;e<o&&s!=null;e++)s=s[r[e]];return s}}function Cn(n){let t=Q(n),o=[];for(let r=0;r<t.length;r++)t[r].type==="wildcard"&&o.push(r);return o.length===1?Tn(t,o[0]):$n(t,o)}function Tn(n,t){let o=n.slice(0,t).map(e=>e.value),r=n.slice(t+1).map(e=>e.value),i=o.length,s=r.length;if(s===0){if(i===1){let e=o[0];return f=>f?.[e]}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];return f}}if(s===1){let e=r[0];if(i===1){let f=o[0];return u=>{let l=u?.[f];if(Array.isArray(l))return l.map(a=>a?.[e])}}return f=>{let u=f;for(let l=0;l<i&&u!=null;l++)u=u[o[l]];if(Array.isArray(u))return u.map(l=>l?.[e])}}return e=>{let f=e;for(let u=0;u<i&&f!=null;u++)f=f[o[u]];if(Array.isArray(f))return f.map(u=>{let l=u;for(let a=0;a<s&&l!=null;a++)l=l[r[a]];return l})}}function $n(n,t){let o=[],r=0;for(let s=0;s<t.length;s++){let e=t[s],f=s===t.length-1,u=n.slice(r,e).map(l=>l.value);u.length>0&&o.push({type:"access",keys:u}),o.push({type:f?"map":"flatMap",keys:[]}),r=e+1;}let i=n.slice(r).map(s=>s.value);return s=>{let e=s;for(let f of o){if(e==null)return;if(f.type==="access")for(let u of f.keys){if(e==null)return;e=e[u];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(u=>{let l=u;return Array.isArray(l)?l:[l]});}else if(f.type==="map"){if(!Array.isArray(e))return;i.length>0&&(e=e.map(u=>{let l=u;for(let a of i){if(l==null)return;l=l[a];}return l}));}}return e}}function An(n,t){return N(t)(n)}function W(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function Sn(){I.clear();}function kn(){return I.size}function F(n){let t=new Set;return C(n,t),Array.from(t)}function C(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i=0;i<n.length;i++)C(n[i],t);return}if(E(n)){t.add(W(n.$));return}if(S(n)){if(typeof n.$if=="string"){let i=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(W(i));}else C(n.$if,t);C(n.then,t),n.else!==void 0&&C(n.else,t);return}if(x(n)){for(let i=0;i<n.$pipe.length;i++)C(n.$pipe[i],t);return}if(k(n)){if(n.args)for(let i=0;i<n.args.length;i++)C(n.args[i],t);return}if(R(n)){C(n.body,t),n.params!==void 0&&C(n.params,t);return}if(h(n)){C(n.left,t),n.right!==void 0&&C(n.right,t);return}if(b(n)){for(let i=0;i<n.conditions.length;i++)C(n.conditions[i],t);return}let o=n,r=Object.keys(o);for(let i=0;i<r.length;i++)C(o[r[i]],t);}function xn(n){return F(n).length>0}function Rn(n){return F(n).length===0}function wn(n){return JSON.stringify(n)}function j(n,t={}){let o=t.scope??{},r=t.accessor,i=t.context??{},s=m(n,o,r,i),e=F(n),f=wn(n);return {fn:s,deps:e,hash:f}}function m(n,t,o,r){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>m(s,t,o,r));return s=>i.map(e=>e(s))}if(E(n))return Fn(n,o);if(S(n))return On(n,t,o,r);if(x(n))return vn(n,t,o,r);if(k(n))return jn(n,t,o,r);if(R(n))return Nn(n,t,o,r);if(h(n))return Gn(n,t,o,r);if(b(n))return In(n,t,o,r);if(q(n)){let i=n,s=Object.keys(i),e=s.map(f=>m(i[f],t,o,r));return f=>{let u={};for(let l=0;l<s.length;l++)u[s[l]]=e[l](f);return u}}return ()=>n}function Z(n,t){return t?o=>t(n,o):N(n)}function Fn(n,t){return Z(n.$,t)}function On(n,t,o,r){let i;if(typeof n.$if=="string"){let f=n.$if.startsWith("!")?n.$if.slice(1):n.$if,u=Z(f,o);i=n.$if.startsWith("!")?a=>!u(a):a=>!!u(a);}else {let f=m(n.$if,t,o,r);i=u=>!!f(u);}let s=m(n.then,t,o,r),e=n.else!==void 0?m(n.else,t,o,r):()=>{};return f=>i(f)?s(f):e(f)}function vn(n,t,o,r){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return m(i[0],t,o,r);let s=m(i[0],t,o,r),e=i.slice(1).map(u=>m(u,t,o,r)),f=e.length;if(f===1){let[u]=e;return l=>{let a=s(l),p=u(l);return typeof p=="function"?p(a):p}}if(f===2){let[u,l]=e;return a=>{let p=s(a),d=u(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(f===3){let[u,l,a]=e;return p=>{let d=s(p),g=u(p);return d=typeof g=="function"?g(d):g,g=l(p),d=typeof g=="function"?g(d):g,g=a(p),typeof g=="function"?g(d):g}}return u=>{let l=s(u);for(let a=0;a<f;a++){let p=e[a](u);l=typeof p=="function"?p(l):p;}return l}}function jn(n,t,o,r){let i=n.$fn,s=n.args;if(s===void 0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let e=s.map(u=>m(u,t,o,r)),f=e.length;if(f===0)return ()=>{let u=t[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(f===1){let[u]=e;return l=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(u(l))}}if(f===2){let[u,l]=e;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(a),l(a))}}if(f===3){let[u,l,a]=e;return p=>{let d=t[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),l(p),a(p))}}return u=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...e.map(a=>a(u)))}}function Nn(n,t,o,r){let i=n.$cb,s=m(n.body,t,o,r),e=n.params?m(n.params,t,o,r):void 0,f=u=>o?l=>o(l,u):l=>N(l)(u);return u=>{let l=r?.[i];if(!l)throw new Error(`Context function not found: ${i}`);let a=(g,w)=>s(g),p=f(u),d=e?e(u):void 0;return l(a,u,p,d)}}function Gn(n,t,o,r){let i=m(n.left,t,o,r),s=n.right!==void 0?m(n.right,t,o,r):()=>{};switch(n.op){case "eq":return e=>i(e)===s(e);case "neq":return e=>i(e)!==s(e);case "gt":return e=>i(e)>s(e);case "gte":return e=>i(e)>=s(e);case "lt":return e=>i(e)<s(e);case "lte":return e=>i(e)<=s(e);case "in":return e=>{let f=s(e);return Array.isArray(f)&&f.includes(i(e))};case "notIn":return e=>{let f=s(e);return !Array.isArray(f)||!f.includes(i(e))};case "contains":return e=>{let f=i(e);return Array.isArray(f)&&f.includes(s(e))};case "notContains":return e=>{let f=i(e);return !Array.isArray(f)||!f.includes(s(e))};case "exists":return e=>i(e)!==void 0;case "notExists":return e=>i(e)===void 0;case "matches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?false:new RegExp(u).test(f)};case "notMatches":return e=>{let f=i(e),u=s(e);return typeof f!="string"||typeof u!="string"?true:!new RegExp(u).test(f)};case "startsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.startsWith(u)};case "endsWith":return e=>{let f=i(e),u=s(e);return typeof f=="string"&&typeof u=="string"&&f.endsWith(u)}}}function In(n,t,o,r){let i=n.conditions.map(e=>m(e,t,o,r)),s=i.length;if(s===1)return e=>!!i[0](e);if(s===2){let[e,f]=i;return n.logic==="AND"?u=>!!e(u)&&!!f(u):u=>!!e(u)||!!f(u)}if(s===3){let[e,f,u]=i;return n.logic==="AND"?l=>!!e(l)&&!!f(l)&&!!u(l):l=>!!e(l)||!!f(l)||!!u(l)}return n.logic==="AND"?e=>{for(let f=0;f<s;f++)if(!i[f](e))return false;return true}:e=>{for(let f=0;f<s;f++)if(i[f](e))return true;return false}}function Wn(n,t,o={}){return j(n,o).fn(t)}var z=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,o={}){let r=JSON.stringify(t),i=this.cache.get(r);if(i)return this.cache.delete(r),this.cache.set(r,i),i;let s=j(t,o);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(r,s),s}has(t){return this.cache.has(JSON.stringify(t))}delete(t){return this.cache.delete(JSON.stringify(t))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(t){for(this._maxSize=t;this.cache.size>this._maxSize;){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}}},nn=new z;function zn(n,t={}){return nn.get(n,t)}function J(n,t="root",o={}){let r=[];return T(n,t,r,o),{valid:r.length===0,errors:r}}function T(n,t,o,r){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)T(n[e],`${t}[${e}]`,o,r);return}if(E(n)){(!n.$||typeof n.$!="string")&&o.push(`${t}: invalid reference, $ must be non-empty string`);return}if(S(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||o.push(`${t}.$if: empty path in string shorthand`):T(n.$if,`${t}.$if`,o,r),T(n.then,`${t}.then`,o,r),n.else!==void 0&&T(n.else,`${t}.else`,o,r);return}if(x(n)){if(!Array.isArray(n.$pipe)){o.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){o.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)T(n.$pipe[e],`${t}.$pipe[${e}]`,o,r);return}if(k(n)){if(!n.$fn||typeof n.$fn!="string"){o.push(`${t}: invalid function, $fn must be non-empty string`);return}if(r.scope&&!(n.$fn in r.scope)&&o.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))o.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)T(n.args[e],`${t}.args[${e}]`,o,r);return}if(R(n)){if(!n.$cb||typeof n.$cb!="string"){o.push(`${t}: invalid callback, $cb must be non-empty string`);return}r.context&&!(n.$cb in r.context)&&o.push(`${t}: context function "${n.$cb}" not found`),T(n.body,`${t}.body`,o,r),n.params!==void 0&&T(n.params,`${t}.params`,o,r);return}if(h(n)){G.has(n.op)||o.push(`${t}: invalid operator "${n.op}"`),T(n.left,`${t}.left`,o,r),n.right!==void 0&&T(n.right,`${t}.right`,o,r);return}if(b(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&o.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){o.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)T(n.conditions[e],`${t}.conditions[${e}]`,o,r);return}let i=n,s=Object.keys(i);for(let e=0;e<s.length;e++){let f=s[e];T(i[f],`${t}.${f}`,o,r);}}function Dn(n,t={}){let o=J(n,"root",t);if(!o.valid)throw new Error(`Invalid expression: ${o.errors.join("; ")}`)}function Mn(n,t={}){return J(n,"root",t).valid}var un={};En(un,{$:()=>tn,$call:()=>fn,$cb:()=>sn,$cond:()=>rn,$fn:()=>en,$if:()=>Vn,$pipe:()=>on,call:()=>Kn,cb:()=>Jn,cond:()=>qn,fn:()=>_n,pipe:()=>Ln,ref:()=>Bn});function tn(n){return {$:n}}var Bn=tn;function en(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var _n=en;function Vn(n,t,o){return o===void 0?{$if:n,then:t}:{$if:n,then:t,else:o}}function on(...n){return {$pipe:n}}var Ln=on;function rn(n,t,o){return o===void 0?{left:n,op:t}:{left:n,op:t,right:o}}var qn=rn;function sn(n,t,o){return o===void 0?{$cb:n,body:t}:{$cb:n,body:t,params:o}}var Jn=sn;function fn(n,t=[]){return {$fn:n,args:t}}var Kn=fn;var K="data",ln="scope",Xn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function M(n,t={}){let{dataParam:o=K,scopeParam:r=ln,noPrefixes:i=false,useAccessor:s=false,lexicalPrefix:e}=t;return y(n,o,r,i,s,e)}function y(n,t,o,r,i,s){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(e=>y(e,t,o,r,i,s)));if(E(n))return Yn(n.$,t,r,i,s);if(S(n))return Pn(n,t,o,r,i,s);if(x(n))return nt(n.$pipe,t,o,r,i,s);if(k(n))return Qn(n,t,o,r,i,s);if(R(n))return Zn(n,t,o,r,i,s);if(h(n))return tt(n,t,o,r,i,s);if(b(n))return et(n,t,o,r,i,s);if(typeof n=="object"){let f=Object.entries(n).map(([u,l])=>builders.property(builders.identifier(u),y(l,t,o,r,i,s)));return builders.objectExpression(f)}return builders.literal(null)}var X="accessor";function Y(n,t){return t?n===t||n.startsWith(t+"."):false}function Yn(n,t,o,r,i){return r?Y(n,i)?O(n,t,true):builders.callExpression(builders.identifier(X),[builders.literal(n),builders.identifier(t)]):n.includes("[*]")?Hn(n,t,o):O(n,t,o)}function O(n,t,o){let r=D(n);if(r.length===0)return o?builders.identifier("undefined"):builders.identifier(t);let i;if(o){let s=r[0];i=builders.identifier(s.value);for(let e=1;e<r.length;e++){let f=r[e];f.type==="key"?i=builders.memberExpression(i,builders.identifier(f.value),false,true):i=builders.memberExpression(i,builders.literal(f.value),true,true);}}else {i=builders.identifier(t);for(let s of r)s.type==="key"?i=builders.memberExpression(i,builders.identifier(s.value),false,true):i=builders.memberExpression(i,builders.literal(s.value),true,true);}return i}function Hn(n,t,o){let r=n.indexOf("[*]"),i=n.slice(0,r),s=n.slice(r+3),e;if(i?e=O(i,t,o):e=o?builders.identifier("undefined"):builders.identifier(t),!s||s==="")return e;if(s.includes("[*]"))return an(e,s);let f="_i",u=s.startsWith(".")?s.slice(1):s,l=builders.identifier(f);if(u){let a=D(u);for(let p of a)p.type==="key"?l=builders.memberExpression(l,builders.identifier(p.value),false,true):l=builders.memberExpression(l,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(e,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],l)])}function an(n,t){let o=t.indexOf("[*]"),r=t.slice(0,o),i=t.slice(o+3),s="_i",e=r.startsWith(".")?r.slice(1):r,f=builders.identifier(s);if(e){let l=D(e);for(let a of l)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}if(i.includes("[*]")){let l=an(f,i);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],l)])}let u=i.startsWith(".")?i.slice(1):i;if(u){let l=D(u);for(let a of l)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],f)])}function Pn(n,t,o,r,i,s){let e;if(typeof n.$if=="string"){let l=n.$if.startsWith("!"),a=l?n.$if.slice(1):n.$if,p;i?Y(a,s)?p=O(a,t,true):p=builders.callExpression(builders.identifier(X),[builders.literal(a),builders.identifier(t)]):p=O(a,t,r),e=l?builders.unaryExpression("!",p):p;}else e=y(n.$if,t,o,r,i,s);let f=y(n.then,t,o,r,i,s),u=n.else!==void 0?y(n.else,t,o,r,i,s):builders.identifier("undefined");return builders.conditionalExpression(e,f,u)}function Qn(n,t,o,r,i,s){let e=r?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(o),builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(u=>y(u,t,o,r,i,s));return builders.callExpression(e,f)}var Un="context";function Zn(n,t,o,r,i,s){let e=r?builders.identifier(n.$cb):builders.memberExpression(builders.identifier(Un),builders.identifier(n.$cb),false,false),f=y(n.body,t,o,r,i,s),l=[builders.arrowFunctionExpression([builders.identifier(t),builders.identifier("get")],f),builders.identifier(t),builders.identifier("get")];if(n.params!==void 0){let a=y(n.params,t,o,r,i,s);l.push(a);}return builders.callExpression(e,l)}function nt(n,t,o,r,i,s){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return y(n[0],t,o,r,i,s);let e=y(n[0],t,o,r,i,s);for(let f=1;f<n.length;f++){let u=y(n[f],t,o,r,i,s);e=builders.callExpression(u,[e]);}return e}function cn(n,t,o,r,i,s){if(E(n)){let e=n.$;return i?Y(e,s)?O(e,t,true):builders.callExpression(builders.identifier(X),[builders.literal(e),builders.identifier(t)]):O(e,t,r)}return y(n,t,o,r,i,s)}function tt(n,t,o,r,i,s){let e=cn(n.left,t,o,r,i,s),f=n.right!==void 0?cn(n.right,t,o,r,i,s):builders.literal(null),u=Xn[n.op];if(u)return builders.binaryExpression(u,e,f);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]));case "contains":return builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]));case "exists":return builders.binaryExpression("!=",e,builders.literal(null));case "notExists":return builders.binaryExpression("==",e,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]));case "startsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("startsWith"),false,true),[f]);case "endsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("endsWith"),false,true),[f]);default:return builders.binaryExpression("===",e,f)}}function et(n,t,o,r,i,s){let{logic:e,conditions:f}=n,u=e==="AND"?"&&":"||";if(f.length===0)return builders.literal(e==="AND");if(f.length===1)return y(f[0],t,o,r,i,s);let l=y(f[0],t,o,r,i,s);for(let a=1;a<f.length;a++){let p=y(f[a],t,o,r,i,s);l=builders.logicalExpression(u,l,p);}return l}function D(n){let t=[],o=n.length,r=0,i="";for(;r<o;){let s=n[r];if(s===".")i&&(t.push({type:"key",value:i}),i=""),r++;else if(s==="["){i&&(t.push({type:"key",value:i}),i=""),r++;let e=r;for(;r<o&&n[r]!=="]";)r++;let f=n.slice(e,r);if(r++,f!=="*"){let u=parseInt(f,10);t.push({type:"index",value:isNaN(u)?f:u});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function pn(n,t=[K]){return builders.arrowFunctionExpression(t.map(o=>builders.identifier(o)),n)}function B(n){let t=new Set;return $(n,t),t}function $(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)$(r,t);return}if(E(n))return;if(S(n)){$(n.$if,t),$(n.then,t),n.else!==void 0&&$(n.else,t);return}if(x(n)){for(let r of n.$pipe)$(r,t);return}if(k(n)){if(t.add(n.$fn),n.args)for(let r of n.args)$(r,t);return}if(R(n)){$(n.body,t),n.params!==void 0&&$(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&$(n.left,t),n.right!==void 0&&typeof n.right=="object"&&$(n.right,t);return}if(b(n)){for(let r of n.conditions)$(r,t);return}let o=n;for(let r of Object.keys(o))$(o[r],t);}function _(n){let t=new Set;return A(n,t),t}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r of n)A(r,t);return}if(E(n))return;if(S(n)){A(n.$if,t),A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(x(n)){for(let r of n.$pipe)A(r,t);return}if(k(n)){if(n.args)for(let r of n.args)A(r,t);return}if(R(n)){t.add(n.$cb),A(n.body,t),n.params!==void 0&&A(n.params,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&A(n.left,t),n.right!==void 0&&typeof n.right=="object"&&A(n.right,t);return}if(b(n)){for(let r of n.conditions)A(r,t);return}let o=n;for(let r of Object.keys(o))A(o[r],t);}function V(n){let t=new Set;for(let o of n){let r=o.indexOf("."),i=o.indexOf("["),s=o.length;r!==-1&&(s=Math.min(s,r)),i!==-1&&(s=Math.min(s,i));let e=o.slice(0,s);e&&t.add(e);}return t}function ot(n){return JSON.stringify(n)}function rt(n,t,o,r,i,s){let e=M(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),f=generate(e),u="";i?s&&(u=`const{${s}}=data??{};`):t.size>0&&(u=`const{${[...t].join(",")}}=data??{};`);let l=i?new Set([...o,"accessor"]):o,a=l.size>0?`const{${[...l].join(",")}}=scope;`:"",p=r.size>0?`const{${[...r].join(",")}}=context;`:"",d=r.size>0,g="";d&&(i?g="const get=(path)=>accessor(path,data);":g="const get=(path)=>path.split('.').reduce((o,k)=>o?.[k],data);");let w=l.size>0,v;if(w||d){let gn=d?"scope,context":"scope",yn=`${u}${g}return ${f}`;v=`(function(${gn}){${a}${p}return function(data){${yn}}})`;}else v=`(function(){return function(data){${u}return ${f}}})`;return v}function H(n,t={}){let{scope:o={},context:r={},returnCode:i=false,useAccessor:s=false,lexicalPrefix:e}=t,f=F(n),u=V(f),l=B(n),a=_(n),p=ot(n),d=rt(n,u,l,a,s,e);if(i)return {code:d,deps:f,hash:p,dataRoots:[...u],scopeFns:[...l],contextFns:[...a]};let g;try{let w=a.size>0,v=new Function(`return ${d}`)();g=w?v(o,r):v(o);}catch(w){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${w instanceof Error?w.message:String(w)}`)}return {fn:g,deps:f,hash:p}}function dn(n,t,o={}){let{fn:r}=H(n,o);return r(t)}function st(n,t){return L(n,t)}function L(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>L(s,t));let o=n,r=Object.keys(o);for(let s of r)if(s in t){let e=t[s](o);if(typeof e=="object"&&e!==null&&s in e)throw new Error(`Transform "${s}" returned object with same key \u2014 infinite loop`);return L(e,t)}let i={};for(let s of r)i[s]=L(o[s],t);return i}var ft=new Set(["$","$if","$fn","$pipe","$cb"]);function ut(){let n=0;return ()=>String(n++)}function ct(n,t){let{resolvers:o,scope:r={},genId:i,...s}=t,e=Object.keys(o);if(e.length===0)return {expr:n,scope:r};let f=new Set(e),u=[],l={compile:j,genId:i??ut(),scope:r,options:s},a=P(n,f,o,l,u),p={...r};for(let d=0;d<u.length;d++){let[g,w]=u[d];p[g]=w;}return {expr:a,scope:p}}function P(n,t,o,r,i){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n)){let l=n.length,a=new Array(l);for(let p=0;p<l;p++)a[p]=P(n[p],t,o,r,i);return a}let s=n,e=Object.keys(s),f=e.length;for(let l=0;l<f;l++){let a=e[l];if(a[0]==="$"&&!ft.has(a)&&t.has(a)){let p=o[a],d=p(s,r);return d.scopeEntry&&i.push(d.scopeEntry),d.expr}}let u={};for(let l=0;l<f;l++){let a=e[l];u[a]=P(s[a],t,o,r,i);}return u}var _t="2.0.0";export{z as ExpressionCache,_t as VERSION,Dn as assertValid,un as builders,nn as cache,zn as cached,Sn as clearPathCache,j as compile,H as compileAST,N as compilePath,M as dslToAST,Wn as evaluate,dn as evaluateAST,_ as extractContextFns,V as extractDataRoots,F as extractDeps,B as extractScopeFns,An as get,kn as getPathCacheSize,xn as hasDeps,U as hasWildcard,R as isCb,h as isCondition,hn as isConditionExpr,b as isConditionGroup,S as isConditional,k as isFn,q as isLiteral,x as isPipe,Rn as isPure,E as isRef,Mn as isValid,st as normalize,W as normalizePath,ct as resolveBoundaries,J as validate,pn as wrapInFunction};
|