@statedelta-libs/expressions 0.2.0 → 1.0.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 CHANGED
@@ -146,7 +146,7 @@ true
146
146
 
147
147
  ```typescript
148
148
  {
149
- $if: { path: "age", op: "gte", value: 18 },
149
+ $if: { left: { $: "age" }, op: "gte", right: 18 },
150
150
  then: "adult",
151
151
  else: "minor"
152
152
  }
@@ -193,7 +193,7 @@ Chama função do scope com argumentos compilados.
193
193
 
194
194
  ```typescript
195
195
  // Detectado automaticamente e delegado para @statedelta-libs/conditions
196
- { path: "user.age", op: "gte", value: 18 }
196
+ { left: { $: "user.age" }, op: "gte", right: 18 }
197
197
  ```
198
198
 
199
199
  > **Nota:** Apenas objetos com operadores de condition válidos (`eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `in`, `notIn`, `contains`, `notContains`, `exists`, `notExists`, `matches`, `notMatches`, `startsWith`, `endsWith`) são detectados como conditions. Objetos com `op: "set"` ou outros operadores não são conditions e são tratados como literals.
@@ -347,16 +347,16 @@ O compilador detecta automaticamente o tipo de expressão baseado na estrutura d
347
347
  | Conditional | `{ $if, then }` presentes | `{ $if: cond, then: x, else: y }` |
348
348
  | Function | `{ $fn }` presente | `{ $fn: "add", args: [...] }` |
349
349
  | Pipe | `{ $pipe }` presente | `{ $pipe: [...] }` |
350
- | Condition | `{ path, op }` com operador válido | `{ path: "age", op: "gte", value: 18 }` |
350
+ | Condition | `{ left, op }` com operador válido | `{ left: { $: "age" }, op: "gte", right: 18 }` |
351
351
  | Literal | Nenhum dos acima | `{ foo: "bar" }`, `42`, `"hello"` |
352
352
 
353
353
  ### Distinção entre Conditions e Effect Objects
354
354
 
355
- Objetos com `path` e `op` só são tratados como conditions se `op` for um operador válido:
355
+ Objetos com `left` e `op` só são tratados como conditions se `op` for um operador válido:
356
356
 
357
357
  ```typescript
358
358
  // ✓ Condition (op: "eq" é operador válido)
359
- { path: "user.age", op: "eq", value: 18 }
359
+ { left: { $: "user.age" }, op: "eq", right: 18 }
360
360
 
361
361
  // ✓ Literal object (op: "set" NÃO é operador de condition)
362
362
  { resource: "state", op: "set", path: "currentPlayer", value: "X" }
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var conditions=require('@statedelta-libs/conditions'),omniAst=require('omni-ast');var tn=Object.defineProperty;var on=(n,e)=>{for(var i in e)tn(n,i,{get:e[i],enumerable:true});};var m=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,h=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,T=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",b=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),V=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),g=n=>n!==null&&typeof n=="object"&&"path"in n&&"op"in n&&V.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),y=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,rn=n=>g(n)||y(n),M=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,t="path"in i&&"op"in i&&V.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!t&&!("logic"in i)}return false};var R=new Map;function L(n){let e=[],i=n.length,t=0,o="";for(;t<i;){let s=n[t];if(s===".")o&&(e.push({type:"key",value:o}),o=""),t++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),t++;let r=t;for(;t<i&&n[t]!=="]";)t++;let f=n.slice(r,t);if(t++,f==="*")e.push({type:"wildcard",value:"*"});else {let a=parseInt(f,10);e.push({type:"index",value:isNaN(a)?f:a});}}else o+=s,t++;}return o&&e.push({type:"key",value:o}),e}function _(n){return n.includes("[*]")}function O(n){let e=R.get(n);return e||(e=_(n)?ln(n):sn(n),R.set(n,e),e)}function sn(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=L(n),i=e.length;if(i===2){let[o,s]=e,r=o.value,f=s.value;return a=>a?.[r]?.[f]}if(i===3){let[o,s,r]=e,f=o.value,a=s.value,u=r.value;return c=>c?.[f]?.[a]?.[u]}let t=e.map(o=>o.value);return o=>{let s=o;for(let r=0;r<i&&s!=null;r++)s=s[t[r]];return s}}function ln(n){let e=L(n),i=[];for(let t=0;t<e.length;t++)e[t].type==="wildcard"&&i.push(t);return i.length===1?fn(e,i[0]):an(e,i)}function fn(n,e){let i=n.slice(0,e).map(r=>r.value),t=n.slice(e+1).map(r=>r.value),o=i.length,s=t.length;if(s===0){if(o===1){let r=i[0];return f=>f?.[r]}return r=>{let f=r;for(let a=0;a<o&&f!=null;a++)f=f[i[a]];return f}}if(s===1){let r=t[0];if(o===1){let f=i[0];return a=>{let u=a?.[f];if(Array.isArray(u))return u.map(c=>c?.[r])}}return f=>{let a=f;for(let u=0;u<o&&a!=null;u++)a=a[i[u]];if(Array.isArray(a))return a.map(u=>u?.[r])}}return r=>{let f=r;for(let a=0;a<o&&f!=null;a++)f=f[i[a]];if(Array.isArray(f))return f.map(a=>{let u=a;for(let c=0;c<s&&u!=null;c++)u=u[t[c]];return u})}}function an(n,e){let i=[],t=0;for(let s=0;s<e.length;s++){let r=e[s],f=s===e.length-1,a=n.slice(t,r).map(u=>u.value);a.length>0&&i.push({type:"access",keys:a}),i.push({type:f?"map":"flatMap",keys:[]}),t=r+1;}let o=n.slice(t).map(s=>s.value);return s=>{let r=s;for(let f of i){if(r==null)return;if(f.type==="access")for(let a of f.keys){if(r==null)return;r=r[a];}else if(f.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(a=>{let u=a;return Array.isArray(u)?u:[u]});}else if(f.type==="map"){if(!Array.isArray(r))return;o.length>0&&(r=r.map(a=>{let u=a;for(let c of o){if(u==null)return;u=u[c];}return u}));}}return r}}function un(n,e){return O(e)(n)}function k(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function cn(){R.clear();}function pn(){return R.size}function A(n){let e=new Set;return x(n,e),Array.from(e)}function x(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o=0;o<n.length;o++)x(n[o],e);return}if(m(n)){e.add(k(n.$));return}if(h(n)){if(typeof n.$if=="string"){let o=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(k(o));}else x(n.$if,e);x(n.then,e),n.else!==void 0&&x(n.else,e);return}if(b(n)){for(let o=0;o<n.$pipe.length;o++)x(n.$pipe[o],e);return}if(T(n)){if(n.args)for(let o=0;o<n.args.length;o++)x(n.args[o],e);return}if(g(n)){e.add(k(n.path)),n.value!==void 0&&conditions.isRef(n.value)&&e.add(k(n.value.$));return}if(y(n)){for(let o=0;o<n.conditions.length;o++)x(n.conditions[o],e);return}let i=n,t=Object.keys(i);for(let o=0;o<t.length;o++)x(i[t[o]],e);}function mn(n){return A(n).length>0}function gn(n){return A(n).length===0}function yn(n){return JSON.stringify(n)}function F(n,e={}){let i=e.scope??{},t=e.accessor,o=C(n,i,t),s=A(n),r=yn(n);return {fn:o,deps:s,hash:r}}function C(n,e,i){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let t=n.map(o=>C(o,e,i));return o=>t.map(s=>s(o))}if(m(n))return En(n,i);if(h(n))return hn(n,e,i);if(b(n))return Tn(n,e,i);if(T(n))return bn(n,e,i);if(g(n))return conditions.compile(n,i?{accessor:i}:void 0);if(y(n))return conditions.compile(n,i?{accessor:i}:void 0);if(M(n)){let t=n,o=Object.keys(t),s=o.map(r=>C(t[r],e,i));return r=>{let f={};for(let a=0;a<o.length;a++)f[o[a]]=s[a](r);return f}}return ()=>n}function J(n,e){return e?i=>e(n,i):O(n)}function En(n,e){return J(n.$,e)}function hn(n,e,i){let t;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=J(r,i);t=n.$if.startsWith("!")?u=>!f(u):u=>!!f(u);}else {let r=C(n.$if,e,i);t=f=>!!r(f);}let o=C(n.then,e,i),s=n.else!==void 0?C(n.else,e,i):()=>{};return r=>t(r)?o(r):s(r)}function Tn(n,e,i){let t=n.$pipe;if(t.length===0)return ()=>{};if(t.length===1)return C(t[0],e,i);let o=C(t[0],e,i),s=t.slice(1).map(f=>C(f,e,i)),r=s.length;if(r===1){let[f]=s;return a=>{let u=o(a),c=f(a);return typeof c=="function"?c(u):c}}if(r===2){let[f,a]=s;return u=>{let c=o(u),p=f(u);return c=typeof p=="function"?p(c):p,p=a(u),typeof p=="function"?p(c):p}}if(r===3){let[f,a,u]=s;return c=>{let p=o(c),E=f(c);return p=typeof E=="function"?E(p):E,E=a(c),p=typeof E=="function"?E(p):E,E=u(c),typeof E=="function"?E(p):E}}return f=>{let a=o(f);for(let u=0;u<r;u++){let c=s[u](f);a=typeof c=="function"?c(a):c;}return a}}function bn(n,e,i){let t=n.$fn,o=n.args;if(o===void 0)return ()=>{let f=e[t];if(!f)throw new Error(`Function not found in scope: ${t}`);return f};let s=o.map(f=>C(f,e,i)),r=s.length;if(r===0)return f=>{let a=e[t];if(!a)throw new Error(`Function not found in scope: ${t}`);return a()};if(r===1){let[f]=s;return a=>{let u=e[t];if(!u)throw new Error(`Function not found in scope: ${t}`);return u(f(a))}}if(r===2){let[f,a]=s;return u=>{let c=e[t];if(!c)throw new Error(`Function not found in scope: ${t}`);return c(f(u),a(u))}}if(r===3){let[f,a,u]=s;return c=>{let p=e[t];if(!p)throw new Error(`Function not found in scope: ${t}`);return p(f(c),a(c),u(c))}}return f=>{let a=e[t];if(!a)throw new Error(`Function not found in scope: ${t}`);return a(...s.map(u=>u(f)))}}function Cn(n,e,i={}){return F(n,i).fn(e)}var v=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let t=JSON.stringify(e),o=this.cache.get(t);if(o)return this.cache.delete(t),this.cache.set(t,o),o;let s=F(e,i);if(this.cache.size>=this._maxSize){let r=this.cache.keys().next().value;r&&this.cache.delete(r);}return this.cache.set(t,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},B=new v;function $n(n,e={}){return B.get(n,e)}function D(n,e="root",i={}){let t=[];return S(n,e,t,i),{valid:t.length===0,errors:t}}function S(n,e,i,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)S(n[r],`${e}[${r}]`,i,t);return}if(m(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(h(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):S(n.$if,`${e}.$if`,i,t),S(n.then,`${e}.then`,i,t),n.else!==void 0&&S(n.else,`${e}.else`,i,t);return}if(b(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let r=0;r<n.$pipe.length;r++)S(n.$pipe[r],`${e}.$pipe[${r}]`,i,t);return}if(T(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(t.scope&&!(n.$fn in t.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let r=0;r<n.args.length;r++)S(n.args[r],`${e}.args[${r}]`,i,t);return}if(g(n)){let r=conditions.validate(n,e);r.valid||i.push(...r.errors);return}if(y(n)){let r=conditions.validate(n,e);r.valid||i.push(...r.errors);return}let o=n,s=Object.keys(o);for(let r=0;r<s.length;r++){let f=s[r];S(o[f],`${e}.${f}`,i,t);}}function xn(n,e={}){let i=D(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function Sn(n,e={}){return D(n,"root",e).valid}var U={};on(U,{$:()=>Y,$fn:()=>H,$if:()=>wn,$pipe:()=>Q,fn:()=>kn,pipe:()=>Rn,ref:()=>An});function Y(n){return {$:n}}var An=Y;function H(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var kn=H;function wn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function Q(...n){return {$pipe:n}}var Rn=Q;var P="data",X="scope",On={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function G(n,e={}){let{dataParam:i=P,scopeParam:t=X,noPrefixes:o=false,useAccessor:s=false}=e;return d(n,i,t,o,s)}function d(n,e,i,t,o){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(s=>d(s,e,i,t,o)));if(m(n))return Fn(n.$,e,t,o);if(h(n))return Nn(n,e,i,t,o);if(b(n))return Gn(n.$pipe,e,i,t,o);if(T(n))return jn(n,e,i,t,o);if(g(n))return Wn(n,e,i,t,o);if(y(n))return zn(n,e,i,t,o);if(typeof n=="object"){let r=Object.entries(n).map(([f,a])=>omniAst.builders.property(omniAst.builders.identifier(f),d(a,e,i,t,o)));return omniAst.builders.objectExpression(r)}return omniAst.builders.literal(null)}var N="accessor";function Fn(n,e,i,t){return t?omniAst.builders.callExpression(omniAst.builders.identifier(N),[omniAst.builders.literal(n),omniAst.builders.identifier(e)]):n.includes("[*]")?vn(n,e,i):w(n,e,i)}function w(n,e,i){let t=j(n);if(t.length===0)return i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e);let o;if(i){let s=t[0];o=omniAst.builders.identifier(s.value);for(let r=1;r<t.length;r++){let f=t[r];f.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(f.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(f.value),true,true);}}else {o=omniAst.builders.identifier(e);for(let s of t)s.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(s.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(s.value),true,true);}return o}function vn(n,e,i){let t=n.indexOf("[*]"),o=n.slice(0,t),s=n.slice(t+3),r;if(o?r=w(o,e,i):r=i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e),!s||s==="")return r;if(s.includes("[*]"))return Z(r,s);let f="_i",a=s.startsWith(".")?s.slice(1):s,u=omniAst.builders.identifier(f);if(a){let c=j(a);for(let p of c)p.type==="key"?u=omniAst.builders.memberExpression(u,omniAst.builders.identifier(p.value),false,true):u=omniAst.builders.memberExpression(u,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],u)])}function Z(n,e){let i=e.indexOf("[*]"),t=e.slice(0,i),o=e.slice(i+3),s="_i",r=t.startsWith(".")?t.slice(1):t,f=omniAst.builders.identifier(s);if(r){let u=j(r);for(let c of u)c.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(c.value),false,true));}if(o.includes("[*]")){let u=Z(f,o);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],u)])}let a=o.startsWith(".")?o.slice(1):o;if(a){let u=j(a);for(let c of u)c.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(c.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 Nn(n,e,i,t,o){let s;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),u=a?n.$if.slice(1):n.$if,c=o?omniAst.builders.callExpression(omniAst.builders.identifier(N),[omniAst.builders.literal(u),omniAst.builders.identifier(e)]):w(u,e,t);s=a?omniAst.builders.unaryExpression("!",c):c;}else s=d(n.$if,e,i,t,o);let r=d(n.then,e,i,t,o),f=n.else!==void 0?d(n.else,e,i,t,o):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(s,r,f)}function jn(n,e,i,t,o){let s=t?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(i),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return s;let r=n.args.map(f=>d(f,e,i,t,o));return omniAst.builders.callExpression(s,r)}function Gn(n,e,i,t,o){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return d(n[0],e,i,t,o);let s=d(n[0],e,i,t,o);for(let r=1;r<n.length;r++){let f=d(n[r],e,i,t,o);s=omniAst.builders.callExpression(f,[s]);}return s}function Wn(n,e,i,t,o){let s=o?omniAst.builders.callExpression(omniAst.builders.identifier(N),[omniAst.builders.literal(n.path),omniAst.builders.identifier(e)]):w(n.path,e,t),r=n.value!==void 0?m(n.value)?o?omniAst.builders.callExpression(omniAst.builders.identifier(N),[omniAst.builders.literal(n.value.$),omniAst.builders.identifier(e)]):w(n.value.$,e,t):d(n.value,e,i,t,o):omniAst.builders.literal(null),f=On[n.op];if(f)return omniAst.builders.binaryExpression(f,s,r);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("includes")),[s]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("includes")),[s]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("includes"),false,true),[r]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("includes"),false,true),[r]));case "exists":return omniAst.builders.binaryExpression("!=",s,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",s,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[r]),omniAst.builders.identifier("test")),[s]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[r]),omniAst.builders.identifier("test")),[s]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("startsWith"),false,true),[r]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("endsWith"),false,true),[r]);default:return omniAst.builders.binaryExpression("===",s,r)}}function zn(n,e,i,t,o){let{logic:s,conditions:r}=n,f=s==="AND"?"&&":"||";if(r.length===0)return omniAst.builders.literal(s==="AND");if(r.length===1)return d(r[0],e,i,t,o);let a=d(r[0],e,i,t,o);for(let u=1;u<r.length;u++){let c=d(r[u],e,i,t,o);a=omniAst.builders.logicalExpression(f,a,c);}return a}function j(n){let e=[],i=n.length,t=0,o="";for(;t<i;){let s=n[t];if(s===".")o&&(e.push({type:"key",value:o}),o=""),t++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),t++;let r=t;for(;t<i&&n[t]!=="]";)t++;let f=n.slice(r,t);if(t++,f!=="*"){let a=parseInt(f,10);e.push({type:"index",value:isNaN(a)?f:a});}}else o+=s,t++;}return o&&e.push({type:"key",value:o}),e}function nn(n,e=[P]){return omniAst.builders.arrowFunctionExpression(e.map(i=>omniAst.builders.identifier(i)),n)}function W(n){let e=new Set;return $(n,e),e}function $(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t of n)$(t,e);return}if(m(n))return;if(h(n)){$(n.$if,e),$(n.then,e),n.else!==void 0&&$(n.else,e);return}if(b(n)){for(let t of n.$pipe)$(t,e);return}if(T(n)){if(e.add(n.$fn),n.args)for(let t of n.args)$(t,e);return}if(g(n)){n.value!==void 0&&typeof n.value=="object"&&$(n.value,e);return}if(y(n)){for(let t of n.conditions)$(t,e);return}let i=n;for(let t of Object.keys(i))$(i[t],e);}function z(n){let e=new Set;for(let i of n){let t=i.indexOf("."),o=i.indexOf("["),s=i.length;t!==-1&&(s=Math.min(s,t)),o!==-1&&(s=Math.min(s,o));let r=i.slice(0,s);r&&e.add(r);}return e}function Dn(n){return JSON.stringify(n)}function Pn(n,e,i,t){let o=G(n,{noPrefixes:true,useAccessor:t}),s=omniAst.generate(o),r=!t&&e.size>0?`const{${[...e].join(",")}}=data??{};`:"",f=t?new Set([...i,"accessor"]):i,a=f.size>0?`const{${[...f].join(",")}}=scope;`:"";return a?`(function(scope){${a}return function(data){${r}return ${s}}})`:`(function(){return function(data){${r}return ${s}}})`}function I(n,e={}){let{scope:i={},returnCode:t=false,useAccessor:o=false}=e,s=A(n),r=z(s),f=W(n),a=Dn(n),u=Pn(n,r,f,o);if(t)return {code:u,deps:s,hash:a,dataRoots:[...r],scopeFns:[...f]};let c;try{c=new Function(`return ${u}`)()(i);}catch(p){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${p instanceof Error?p.message:String(p)}`)}return {fn:c,deps:s,hash:a}}function en(n,e,i={}){let{fn:t}=I(n,i);return t(e)}var Ee="0.1.1";exports.ExpressionCache=v;exports.VERSION=Ee;exports.assertValid=xn;exports.builders=U;exports.cache=B;exports.cached=$n;exports.clearPathCache=cn;exports.compile=F;exports.compileAST=I;exports.compilePath=O;exports.dslToAST=G;exports.evaluate=Cn;exports.evaluateAST=en;exports.extractDataRoots=z;exports.extractDeps=A;exports.extractScopeFns=W;exports.get=un;exports.getPathCacheSize=pn;exports.hasDeps=mn;exports.hasWildcard=_;exports.isCondition=g;exports.isConditionExpr=rn;exports.isConditionGroup=y;exports.isConditional=h;exports.isFn=T;exports.isLiteral=M;exports.isPipe=b;exports.isPure=gn;exports.isRef=m;exports.isValid=Sn;exports.normalizePath=k;exports.validate=D;exports.wrapInFunction=nn;
1
+ 'use strict';var conditions=require('@statedelta-libs/conditions'),omniAst=require('omni-ast');var sn=Object.defineProperty;var fn=(n,e)=>{for(var i in e)sn(n,i,{get:e[i],enumerable:true});};var m=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,E=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,T=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",b=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),_=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),y=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&_.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),h=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,ln=n=>y(n)||h(n),z=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,t="left"in i&&"op"in i&&_.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!t&&!("logic"in i)}return false};var R=new Map;function q(n){let e=[],i=n.length,t=0,r="";for(;t<i;){let f=n[t];if(f===".")r&&(e.push({type:"key",value:r}),r=""),t++;else if(f==="["){r&&(e.push({type:"key",value:r}),r=""),t++;let o=t;for(;t<i&&n[t]!=="]";)t++;let s=n.slice(o,t);if(t++,s==="*")e.push({type:"wildcard",value:"*"});else {let u=parseInt(s,10);e.push({type:"index",value:isNaN(u)?s:u});}}else r+=f,t++;}return r&&e.push({type:"key",value:r}),e}function J(n){return n.includes("[*]")}function F(n){let e=R.get(n);return e||(e=J(n)?cn(n):un(n),R.set(n,e),e)}function un(n){if(!n.includes(".")&&!n.includes("["))return r=>r?.[n];let e=q(n),i=e.length;if(i===2){let[r,f]=e,o=r.value,s=f.value;return u=>u?.[o]?.[s]}if(i===3){let[r,f,o]=e,s=r.value,u=f.value,c=o.value;return a=>a?.[s]?.[u]?.[c]}let t=e.map(r=>r.value);return r=>{let f=r;for(let o=0;o<i&&f!=null;o++)f=f[t[o]];return f}}function cn(n){let e=q(n),i=[];for(let t=0;t<e.length;t++)e[t].type==="wildcard"&&i.push(t);return i.length===1?an(e,i[0]):pn(e,i)}function an(n,e){let i=n.slice(0,e).map(o=>o.value),t=n.slice(e+1).map(o=>o.value),r=i.length,f=t.length;if(f===0){if(r===1){let o=i[0];return s=>s?.[o]}return o=>{let s=o;for(let u=0;u<r&&s!=null;u++)s=s[i[u]];return s}}if(f===1){let o=t[0];if(r===1){let s=i[0];return u=>{let c=u?.[s];if(Array.isArray(c))return c.map(a=>a?.[o])}}return s=>{let u=s;for(let c=0;c<r&&u!=null;c++)u=u[i[c]];if(Array.isArray(u))return u.map(c=>c?.[o])}}return o=>{let s=o;for(let u=0;u<r&&s!=null;u++)s=s[i[u]];if(Array.isArray(s))return s.map(u=>{let c=u;for(let a=0;a<f&&c!=null;a++)c=c[t[a]];return c})}}function pn(n,e){let i=[],t=0;for(let f=0;f<e.length;f++){let o=e[f],s=f===e.length-1,u=n.slice(t,o).map(c=>c.value);u.length>0&&i.push({type:"access",keys:u}),i.push({type:s?"map":"flatMap",keys:[]}),t=o+1;}let r=n.slice(t).map(f=>f.value);return f=>{let o=f;for(let s of i){if(o==null)return;if(s.type==="access")for(let u of s.keys){if(o==null)return;o=o[u];}else if(s.type==="flatMap"){if(!Array.isArray(o))return;o=o.flatMap(u=>{let c=u;return Array.isArray(c)?c:[c]});}else if(s.type==="map"){if(!Array.isArray(o))return;r.length>0&&(o=o.map(u=>{let c=u;for(let a of r){if(c==null)return;c=c[a];}return c}));}}return o}}function dn(n,e){return F(e)(n)}function x(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function gn(){R.clear();}function mn(){return R.size}function k(n){let e=new Set;return $(n,e),Array.from(e)}function $(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)$(n[r],e);return}if(m(n)){e.add(x(n.$));return}if(E(n)){if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(x(r));}else $(n.$if,e);$(n.then,e),n.else!==void 0&&$(n.else,e);return}if(b(n)){for(let r=0;r<n.$pipe.length;r++)$(n.$pipe[r],e);return}if(T(n)){if(n.args)for(let r=0;r<n.args.length;r++){let f=n.args[r];typeof f!="function"&&$(f,e);}return}if(y(n)){conditions.isRef(n.left)&&e.add(x(n.left.$)),n.right!==void 0&&conditions.isRef(n.right)&&e.add(x(n.right.$));return}if(h(n)){for(let r=0;r<n.conditions.length;r++)$(n.conditions[r],e);return}let i=n,t=Object.keys(i);for(let r=0;r<t.length;r++)$(i[t[r]],e);}function yn(n){return k(n).length>0}function hn(n){return k(n).length===0}function En(n){return JSON.stringify(n)}function O(n,e={}){let i=e.scope??{},t=e.accessor,r=S(n,i,t),f=k(n),o=En(n);return {fn:r,deps:f,hash:o}}function S(n,e,i){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let t=n.map(r=>S(r,e,i));return r=>t.map(f=>f(r))}if(m(n))return Tn(n,i);if(E(n))return bn(n,e,i);if(b(n))return Cn(n,e,i);if(T(n))return Sn(n,e,i);if(y(n))return conditions.compile(n,i?{accessor:i}:void 0);if(h(n))return conditions.compile(n,i?{accessor:i}:void 0);if(z(n)){let t=n,r=Object.keys(t),f=r.map(o=>S(t[o],e,i));return o=>{let s={};for(let u=0;u<r.length;u++)s[r[u]]=f[u](o);return s}}return ()=>n}function Y(n,e){return e?i=>e(n,i):F(n)}function Tn(n,e){return Y(n.$,e)}function bn(n,e,i){let t;if(typeof n.$if=="string"){let o=n.$if.startsWith("!")?n.$if.slice(1):n.$if,s=Y(o,i);t=n.$if.startsWith("!")?c=>!s(c):c=>!!s(c);}else {let o=S(n.$if,e,i);t=s=>!!o(s);}let r=S(n.then,e,i),f=n.else!==void 0?S(n.else,e,i):()=>{};return o=>t(o)?r(o):f(o)}function Cn(n,e,i){let t=n.$pipe;if(t.length===0)return ()=>{};if(t.length===1)return S(t[0],e,i);let r=S(t[0],e,i),f=t.slice(1).map(s=>S(s,e,i)),o=f.length;if(o===1){let[s]=f;return u=>{let c=r(u),a=s(u);return typeof a=="function"?a(c):a}}if(o===2){let[s,u]=f;return c=>{let a=r(c),p=s(c);return a=typeof p=="function"?p(a):p,p=u(c),typeof p=="function"?p(a):p}}if(o===3){let[s,u,c]=f;return a=>{let p=r(a),d=s(a);return p=typeof d=="function"?d(p):d,d=u(a),p=typeof d=="function"?d(p):d,d=c(a),typeof d=="function"?d(p):d}}return s=>{let u=r(s);for(let c=0;c<o;c++){let a=f[c](s);u=typeof a=="function"?a(u):a;}return u}}function Sn(n,e,i){let t=n.$fn,r=n.args;if(r===void 0)return ()=>{let s=e[t];if(!s)throw new Error(`Function not found in scope: ${t}`);return s};let f=r.map(s=>typeof s=="function"?()=>s:S(s,e,i)),o=f.length;if(o===0)return s=>{let u=e[t];if(!u)throw new Error(`Function not found in scope: ${t}`);return u()};if(o===1){let[s]=f;return u=>{let c=e[t];if(!c)throw new Error(`Function not found in scope: ${t}`);return c(s(u))}}if(o===2){let[s,u]=f;return c=>{let a=e[t];if(!a)throw new Error(`Function not found in scope: ${t}`);return a(s(c),u(c))}}if(o===3){let[s,u,c]=f;return a=>{let p=e[t];if(!p)throw new Error(`Function not found in scope: ${t}`);return p(s(a),u(a),c(a))}}return s=>{let u=e[t];if(!u)throw new Error(`Function not found in scope: ${t}`);return u(...f.map(c=>c(s)))}}function $n(n,e,i={}){return O(n,i).fn(e)}var N=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let t=JSON.stringify(e),r=this.cache.get(t);if(r)return this.cache.delete(t),this.cache.set(t,r),r;let f=O(e,i);if(this.cache.size>=this._maxSize){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}return this.cache.set(t,f),f}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},H=new N;function An(n,e={}){return H.get(n,e)}function M(n,e="root",i={}){let t=[];return A(n,e,t,i),{valid:t.length===0,errors:t}}function A(n,e,i,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o=0;o<n.length;o++)A(n[o],`${e}[${o}]`,i,t);return}if(m(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(E(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):A(n.$if,`${e}.$if`,i,t),A(n.then,`${e}.then`,i,t),n.else!==void 0&&A(n.else,`${e}.else`,i,t);return}if(b(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let o=0;o<n.$pipe.length;o++)A(n.$pipe[o],`${e}.$pipe[${o}]`,i,t);return}if(T(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(t.scope&&!(n.$fn in t.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let o=0;o<n.args.length;o++){let s=n.args[o];typeof s!="function"&&A(s,`${e}.args[${o}]`,i,t);}return}if(y(n)){let o=conditions.validate(n,e);o.valid||i.push(...o.errors);return}if(h(n)){let o=conditions.validate(n,e);o.valid||i.push(...o.errors);return}let r=n,f=Object.keys(r);for(let o=0;o<f.length;o++){let s=f[o];A(r[s],`${e}.${s}`,i,t);}}function kn(n,e={}){let i=M(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function wn(n,e={}){return M(n,"root",e).valid}var Z={};fn(Z,{$:()=>Q,$fn:()=>U,$if:()=>Fn,$pipe:()=>X,fn:()=>Rn,pipe:()=>On,ref:()=>xn});function Q(n){return {$:n}}var xn=Q;function U(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var Rn=U;function Fn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function X(...n){return {$pipe:n}}var On=X;var D="data",en="scope",Nn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function v(n,e={}){let{dataParam:i=D,scopeParam:t=en,noPrefixes:r=false,useAccessor:f=false,lexicalPrefix:o}=e;return g(n,i,t,r,f,o)}function g(n,e,i,t,r,f){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(o=>g(o,e,i,t,r,f)));if(m(n))return jn(n.$,e,t,r,f);if(E(n))return Gn(n,e,i,t,r,f);if(b(n))return zn(n.$pipe,e,i,t,r,f);if(T(n))return Wn(n,e,i,t,r,f);if(y(n))return Mn(n,e,i,t,r,f);if(h(n))return Dn(n,e,i,t,r,f);if(typeof n=="object"){let s=Object.entries(n).map(([u,c])=>omniAst.builders.property(omniAst.builders.identifier(u),g(c,e,i,t,r,f)));return omniAst.builders.objectExpression(s)}return omniAst.builders.literal(null)}var I="accessor";function L(n,e){return e?n===e||n.startsWith(e+"."):false}function jn(n,e,i,t,r){return t?L(n,r)?w(n,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(I),[omniAst.builders.literal(n),omniAst.builders.identifier(e)]):n.includes("[*]")?vn(n,e,i):w(n,e,i)}function w(n,e,i){let t=j(n);if(t.length===0)return i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e);let r;if(i){let f=t[0];r=omniAst.builders.identifier(f.value);for(let o=1;o<t.length;o++){let s=t[o];s.type==="key"?r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(s.value),false,true):r=omniAst.builders.memberExpression(r,omniAst.builders.literal(s.value),true,true);}}else {r=omniAst.builders.identifier(e);for(let f of t)f.type==="key"?r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(f.value),false,true):r=omniAst.builders.memberExpression(r,omniAst.builders.literal(f.value),true,true);}return r}function vn(n,e,i){let t=n.indexOf("[*]"),r=n.slice(0,t),f=n.slice(t+3),o;if(r?o=w(r,e,i):o=i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e),!f||f==="")return o;if(f.includes("[*]"))return tn(o,f);let s="_i",u=f.startsWith(".")?f.slice(1):f,c=omniAst.builders.identifier(s);if(u){let a=j(u);for(let p of a)p.type==="key"?c=omniAst.builders.memberExpression(c,omniAst.builders.identifier(p.value),false,true):c=omniAst.builders.memberExpression(c,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(o,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],c)])}function tn(n,e){let i=e.indexOf("[*]"),t=e.slice(0,i),r=e.slice(i+3),f="_i",o=t.startsWith(".")?t.slice(1):t,s=omniAst.builders.identifier(f);if(o){let c=j(o);for(let a of c)a.type==="key"&&(s=omniAst.builders.memberExpression(s,omniAst.builders.identifier(a.value),false,true));}if(r.includes("[*]")){let c=tn(s,r);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],c)])}let u=r.startsWith(".")?r.slice(1):r;if(u){let c=j(u);for(let a of c)a.type==="key"&&(s=omniAst.builders.memberExpression(s,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(f)],s)])}function Gn(n,e,i,t,r,f){let o;if(typeof n.$if=="string"){let c=n.$if.startsWith("!"),a=c?n.$if.slice(1):n.$if,p;r?L(a,f)?p=w(a,e,true):p=omniAst.builders.callExpression(omniAst.builders.identifier(I),[omniAst.builders.literal(a),omniAst.builders.identifier(e)]):p=w(a,e,t),o=c?omniAst.builders.unaryExpression("!",p):p;}else o=g(n.$if,e,i,t,r,f);let s=g(n.then,e,i,t,r,f),u=n.else!==void 0?g(n.else,e,i,t,r,f):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(o,s,u)}function Wn(n,e,i,t,r,f){let o=t?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(i),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return o;let s=n.args.map(u=>typeof u=="function"?omniAst.builders.literal(null):g(u,e,i,t,r,f));return omniAst.builders.callExpression(o,s)}function zn(n,e,i,t,r,f){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return g(n[0],e,i,t,r,f);let o=g(n[0],e,i,t,r,f);for(let s=1;s<n.length;s++){let u=g(n[s],e,i,t,r,f);o=omniAst.builders.callExpression(u,[o]);}return o}function nn(n,e,i,t,r,f){if(m(n)){let o=n.$;return r?L(o,f)?w(o,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(I),[omniAst.builders.literal(o),omniAst.builders.identifier(e)]):w(o,e,t)}return g(n,e,i,t,r,f)}function Mn(n,e,i,t,r,f){let o=nn(n.left,e,i,t,r,f),s=n.right!==void 0?nn(n.right,e,i,t,r,f):omniAst.builders.literal(null),u=Nn[n.op];if(u)return omniAst.builders.binaryExpression(u,o,s);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("includes")),[o]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(s,omniAst.builders.identifier("includes")),[o]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(o,omniAst.builders.identifier("includes"),false,true),[s]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(o,omniAst.builders.identifier("includes"),false,true),[s]));case "exists":return omniAst.builders.binaryExpression("!=",o,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",o,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[s]),omniAst.builders.identifier("test")),[o]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[s]),omniAst.builders.identifier("test")),[o]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(o,omniAst.builders.identifier("startsWith"),false,true),[s]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(o,omniAst.builders.identifier("endsWith"),false,true),[s]);default:return omniAst.builders.binaryExpression("===",o,s)}}function Dn(n,e,i,t,r,f){let{logic:o,conditions:s}=n,u=o==="AND"?"&&":"||";if(s.length===0)return omniAst.builders.literal(o==="AND");if(s.length===1)return g(s[0],e,i,t,r,f);let c=g(s[0],e,i,t,r,f);for(let a=1;a<s.length;a++){let p=g(s[a],e,i,t,r,f);c=omniAst.builders.logicalExpression(u,c,p);}return c}function j(n){let e=[],i=n.length,t=0,r="";for(;t<i;){let f=n[t];if(f===".")r&&(e.push({type:"key",value:r}),r=""),t++;else if(f==="["){r&&(e.push({type:"key",value:r}),r=""),t++;let o=t;for(;t<i&&n[t]!=="]";)t++;let s=n.slice(o,t);if(t++,s!=="*"){let u=parseInt(s,10);e.push({type:"index",value:isNaN(u)?s:u});}}else r+=f,t++;}return r&&e.push({type:"key",value:r}),e}function on(n,e=[D]){return omniAst.builders.arrowFunctionExpression(e.map(i=>omniAst.builders.identifier(i)),n)}function G(n){let e=new Set;return C(n,e),e}function C(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t of n)C(t,e);return}if(m(n))return;if(E(n)){C(n.$if,e),C(n.then,e),n.else!==void 0&&C(n.else,e);return}if(b(n)){for(let t of n.$pipe)C(t,e);return}if(T(n)){if(e.add(n.$fn),n.args)for(let t of n.args)typeof t!="function"&&C(t,e);return}if(y(n)){n.left!==void 0&&typeof n.left=="object"&&C(n.left,e),n.right!==void 0&&typeof n.right=="object"&&C(n.right,e);return}if(h(n)){for(let t of n.conditions)C(t,e);return}let i=n;for(let t of Object.keys(i))C(i[t],e);}function W(n){let e=new Set;for(let i of n){let t=i.indexOf("."),r=i.indexOf("["),f=i.length;t!==-1&&(f=Math.min(f,t)),r!==-1&&(f=Math.min(f,r));let o=i.slice(0,f);o&&e.add(o);}return e}function Ln(n){return JSON.stringify(n)}function Vn(n,e,i,t,r){let f=v(n,{noPrefixes:true,useAccessor:t,lexicalPrefix:r}),o=omniAst.generate(f),s="";t?r&&(s=`const{${r}}=data??{};`):e.size>0&&(s=`const{${[...e].join(",")}}=data??{};`);let u=t?new Set([...i,"accessor"]):i,c=u.size>0?`const{${[...u].join(",")}}=scope;`:"";return c?`(function(scope){${c}return function(data){${s}return ${o}}})`:`(function(){return function(data){${s}return ${o}}})`}function V(n,e={}){let{scope:i={},returnCode:t=false,useAccessor:r=false,lexicalPrefix:f}=e,o=k(n),s=W(o),u=G(n),c=Ln(n),a=Vn(n,s,u,r,f);if(t)return {code:a,deps:o,hash:c,dataRoots:[...s],scopeFns:[...u]};let p;try{p=new Function(`return ${a}`)()(i);}catch(d){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${d instanceof Error?d.message:String(d)}`)}return {fn:p,deps:o,hash:c}}function rn(n,e,i={}){let{fn:t}=V(n,i);return t(e)}var Te="0.1.1";exports.ExpressionCache=N;exports.VERSION=Te;exports.assertValid=kn;exports.builders=Z;exports.cache=H;exports.cached=An;exports.clearPathCache=gn;exports.compile=O;exports.compileAST=V;exports.compilePath=F;exports.dslToAST=v;exports.evaluate=$n;exports.evaluateAST=rn;exports.extractDataRoots=W;exports.extractDeps=k;exports.extractScopeFns=G;exports.get=dn;exports.getPathCacheSize=mn;exports.hasDeps=yn;exports.hasWildcard=J;exports.isCondition=y;exports.isConditionExpr=ln;exports.isConditionGroup=h;exports.isConditional=E;exports.isFn=T;exports.isLiteral=z;exports.isPipe=b;exports.isPure=hn;exports.isRef=m;exports.isValid=wn;exports.normalizePath=x;exports.validate=M;exports.wrapInFunction=on;
package/dist/index.d.cts CHANGED
@@ -25,14 +25,26 @@ interface ConditionalExpr {
25
25
  then: Expression;
26
26
  else?: Expression;
27
27
  }
28
+ /**
29
+ * Callback function passed as argument to $fn (predicates, mappers, etc.)
30
+ * These are native JS functions, not DSL expressions.
31
+ * @example (item) => item.active
32
+ * @example (n) => n * 2
33
+ */
34
+ type CallbackFn = (...args: any[]) => any;
35
+ /**
36
+ * Argument to a $fn expression: can be a DSL expression or a native callback
37
+ */
38
+ type FnArg = Expression | CallbackFn;
28
39
  /**
29
40
  * Function call expression
30
41
  * Calls a function from the provided scope with compiled arguments
31
42
  * @example { "$fn": "add", "args": [{ "$": "a" }, { "$": "b" }] }
43
+ * @example { "$fn": "filter", "args": [(item) => item.active] }
32
44
  */
33
45
  interface FnExpr {
34
46
  $fn: string;
35
- args?: Expression[];
47
+ args?: FnArg[];
36
48
  }
37
49
  /**
38
50
  * Pipe expression (DSL syntax for composition with initial value)
@@ -48,17 +60,10 @@ interface PipeExpr {
48
60
  type Literal = string | number | boolean | null | Literal[] | {
49
61
  [key: string]: Literal;
50
62
  };
51
- /**
52
- * Callback function - used as argument in $fn (e.g., predicates, mappers)
53
- * Not serializable to JSON, but valid in code.
54
- * @example (item) => item.active
55
- * @example (a, b) => a + b
56
- */
57
- type CallbackFn = (...args: any[]) => unknown;
58
63
  /**
59
64
  * Union of all expression types
60
65
  */
61
- type Expression = Literal | RefExpr | ConditionalExpr | FnExpr | PipeExpr | ConditionExpr | CallbackFn;
66
+ type Expression = Literal | RefExpr | ConditionalExpr | FnExpr | PipeExpr | ConditionExpr;
62
67
  /**
63
68
  * Compiled expression function
64
69
  */
@@ -95,7 +100,13 @@ interface ValidationResult {
95
100
  errors: string[];
96
101
  }
97
102
  /**
98
- * Function scope - functions available to $fn expressions
103
+ * Function scope - functions available to $fn expressions.
104
+ *
105
+ * Uses `any` for function parameters because TypeScript's contravariance
106
+ * makes it impossible to assign typed functions like `(a: number, b: number) => number`
107
+ * to `(...args: unknown[]) => unknown`. This is the standard pattern for
108
+ * heterogeneous function registries (similar to Lodash, Ramda, etc.).
109
+ *
99
110
  * @example { add, subtract, multiply, filter, map, sum }
100
111
  */
101
112
  type Scope = Record<string, (...args: any[]) => any>;
@@ -135,9 +146,8 @@ declare const isPipe: (v: unknown) => v is PipeExpr;
135
146
  * Check if value is a condition (from @statedelta-libs/conditions)
136
147
  *
137
148
  * IMPORTANT: Must verify that `op` is a valid condition operator,
138
- * not just any string. Effect objects also have `path` and `op`
139
- * properties (e.g., { resource: "x", op: "set", path: "y", value: z })
140
- * but "set" is NOT a condition operator.
149
+ * not just any string. Effect objects also have `left` and `op`
150
+ * properties but with non-condition operators like "set".
141
151
  */
142
152
  declare const isCondition: (v: unknown) => v is Condition;
143
153
  /**
@@ -408,7 +418,7 @@ declare const ref: typeof $;
408
418
  * $fn("sum") // no args - returns function reference
409
419
  * $fn("now", []) // empty args - calls function
410
420
  */
411
- declare function $fn(name: string, args?: Expression[]): FnExpr;
421
+ declare function $fn(name: string, args?: FnArg[]): FnExpr;
412
422
  /**
413
423
  * Alias for $fn (function builder)
414
424
  */
@@ -484,11 +494,17 @@ interface TransformOptions {
484
494
  */
485
495
  noPrefixes?: boolean;
486
496
  /**
487
- * When true, generates accessor("path") instead of property access.
497
+ * When true, generates accessor("path", data) instead of property access.
488
498
  * Requires `accessor` function in scope.
489
- * - `accessor("user.name")` instead of `user?.name`
499
+ * - `accessor("user.name", data)` instead of `user?.name`
490
500
  */
491
501
  useAccessor?: boolean;
502
+ /**
503
+ * Path prefix that should use direct property access instead of accessor.
504
+ * Only used when useAccessor is true.
505
+ * - `lexicalPrefix: "params"` → `params?.foo` instead of `accessor("params.foo", data)`
506
+ */
507
+ lexicalPrefix?: string;
492
508
  }
493
509
  /**
494
510
  * Transform DSL expression to AST node
@@ -574,10 +590,16 @@ interface CompileASTOptions {
574
590
  /** Return generated code instead of function (for debugging) */
575
591
  returnCode?: boolean;
576
592
  /**
577
- * When true, generates accessor("path") instead of property access.
593
+ * When true, generates accessor("path", data) instead of property access.
578
594
  * Requires `accessor` function in scope.
579
595
  */
580
596
  useAccessor?: boolean;
597
+ /**
598
+ * Path prefix that should use direct property access instead of accessor.
599
+ * Only used when useAccessor is true.
600
+ * - `lexicalPrefix: "params"` → `params?.foo` instead of `accessor("params.foo", data)`
601
+ */
602
+ lexicalPrefix?: string;
581
603
  }
582
604
  /** Result when returnCode is true */
583
605
  interface CompileASTCodeResult {
@@ -662,4 +684,4 @@ declare function evaluateAST<T = unknown, R = unknown>(expr: Expression, data: T
662
684
  */
663
685
  declare const VERSION = "0.1.1";
664
686
 
665
- export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type ConditionalExpr, type Expression, ExpressionCache, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformOptions, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate, wrapInFunction };
687
+ export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type ConditionalExpr, type Expression, ExpressionCache, type FnArg, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformOptions, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate, wrapInFunction };
package/dist/index.d.ts CHANGED
@@ -25,14 +25,26 @@ interface ConditionalExpr {
25
25
  then: Expression;
26
26
  else?: Expression;
27
27
  }
28
+ /**
29
+ * Callback function passed as argument to $fn (predicates, mappers, etc.)
30
+ * These are native JS functions, not DSL expressions.
31
+ * @example (item) => item.active
32
+ * @example (n) => n * 2
33
+ */
34
+ type CallbackFn = (...args: any[]) => any;
35
+ /**
36
+ * Argument to a $fn expression: can be a DSL expression or a native callback
37
+ */
38
+ type FnArg = Expression | CallbackFn;
28
39
  /**
29
40
  * Function call expression
30
41
  * Calls a function from the provided scope with compiled arguments
31
42
  * @example { "$fn": "add", "args": [{ "$": "a" }, { "$": "b" }] }
43
+ * @example { "$fn": "filter", "args": [(item) => item.active] }
32
44
  */
33
45
  interface FnExpr {
34
46
  $fn: string;
35
- args?: Expression[];
47
+ args?: FnArg[];
36
48
  }
37
49
  /**
38
50
  * Pipe expression (DSL syntax for composition with initial value)
@@ -48,17 +60,10 @@ interface PipeExpr {
48
60
  type Literal = string | number | boolean | null | Literal[] | {
49
61
  [key: string]: Literal;
50
62
  };
51
- /**
52
- * Callback function - used as argument in $fn (e.g., predicates, mappers)
53
- * Not serializable to JSON, but valid in code.
54
- * @example (item) => item.active
55
- * @example (a, b) => a + b
56
- */
57
- type CallbackFn = (...args: any[]) => unknown;
58
63
  /**
59
64
  * Union of all expression types
60
65
  */
61
- type Expression = Literal | RefExpr | ConditionalExpr | FnExpr | PipeExpr | ConditionExpr | CallbackFn;
66
+ type Expression = Literal | RefExpr | ConditionalExpr | FnExpr | PipeExpr | ConditionExpr;
62
67
  /**
63
68
  * Compiled expression function
64
69
  */
@@ -95,7 +100,13 @@ interface ValidationResult {
95
100
  errors: string[];
96
101
  }
97
102
  /**
98
- * Function scope - functions available to $fn expressions
103
+ * Function scope - functions available to $fn expressions.
104
+ *
105
+ * Uses `any` for function parameters because TypeScript's contravariance
106
+ * makes it impossible to assign typed functions like `(a: number, b: number) => number`
107
+ * to `(...args: unknown[]) => unknown`. This is the standard pattern for
108
+ * heterogeneous function registries (similar to Lodash, Ramda, etc.).
109
+ *
99
110
  * @example { add, subtract, multiply, filter, map, sum }
100
111
  */
101
112
  type Scope = Record<string, (...args: any[]) => any>;
@@ -135,9 +146,8 @@ declare const isPipe: (v: unknown) => v is PipeExpr;
135
146
  * Check if value is a condition (from @statedelta-libs/conditions)
136
147
  *
137
148
  * IMPORTANT: Must verify that `op` is a valid condition operator,
138
- * not just any string. Effect objects also have `path` and `op`
139
- * properties (e.g., { resource: "x", op: "set", path: "y", value: z })
140
- * but "set" is NOT a condition operator.
149
+ * not just any string. Effect objects also have `left` and `op`
150
+ * properties but with non-condition operators like "set".
141
151
  */
142
152
  declare const isCondition: (v: unknown) => v is Condition;
143
153
  /**
@@ -408,7 +418,7 @@ declare const ref: typeof $;
408
418
  * $fn("sum") // no args - returns function reference
409
419
  * $fn("now", []) // empty args - calls function
410
420
  */
411
- declare function $fn(name: string, args?: Expression[]): FnExpr;
421
+ declare function $fn(name: string, args?: FnArg[]): FnExpr;
412
422
  /**
413
423
  * Alias for $fn (function builder)
414
424
  */
@@ -484,11 +494,17 @@ interface TransformOptions {
484
494
  */
485
495
  noPrefixes?: boolean;
486
496
  /**
487
- * When true, generates accessor("path") instead of property access.
497
+ * When true, generates accessor("path", data) instead of property access.
488
498
  * Requires `accessor` function in scope.
489
- * - `accessor("user.name")` instead of `user?.name`
499
+ * - `accessor("user.name", data)` instead of `user?.name`
490
500
  */
491
501
  useAccessor?: boolean;
502
+ /**
503
+ * Path prefix that should use direct property access instead of accessor.
504
+ * Only used when useAccessor is true.
505
+ * - `lexicalPrefix: "params"` → `params?.foo` instead of `accessor("params.foo", data)`
506
+ */
507
+ lexicalPrefix?: string;
492
508
  }
493
509
  /**
494
510
  * Transform DSL expression to AST node
@@ -574,10 +590,16 @@ interface CompileASTOptions {
574
590
  /** Return generated code instead of function (for debugging) */
575
591
  returnCode?: boolean;
576
592
  /**
577
- * When true, generates accessor("path") instead of property access.
593
+ * When true, generates accessor("path", data) instead of property access.
578
594
  * Requires `accessor` function in scope.
579
595
  */
580
596
  useAccessor?: boolean;
597
+ /**
598
+ * Path prefix that should use direct property access instead of accessor.
599
+ * Only used when useAccessor is true.
600
+ * - `lexicalPrefix: "params"` → `params?.foo` instead of `accessor("params.foo", data)`
601
+ */
602
+ lexicalPrefix?: string;
581
603
  }
582
604
  /** Result when returnCode is true */
583
605
  interface CompileASTCodeResult {
@@ -662,4 +684,4 @@ declare function evaluateAST<T = unknown, R = unknown>(expr: Expression, data: T
662
684
  */
663
685
  declare const VERSION = "0.1.1";
664
686
 
665
- export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type ConditionalExpr, type Expression, ExpressionCache, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformOptions, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate, wrapInFunction };
687
+ export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type ConditionalExpr, type Expression, ExpressionCache, type FnArg, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformOptions, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate, wrapInFunction };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {isRef,compile,validate}from'@statedelta-libs/conditions';import {builders,generate}from'omni-ast';var tn=Object.defineProperty;var on=(n,e)=>{for(var i in e)tn(n,i,{get:e[i],enumerable:true});};var m=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,h=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,T=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",b=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),V=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),g=n=>n!==null&&typeof n=="object"&&"path"in n&&"op"in n&&V.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),y=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,rn=n=>g(n)||y(n),M=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,t="path"in i&&"op"in i&&V.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!t&&!("logic"in i)}return false};var R=new Map;function L(n){let e=[],i=n.length,t=0,o="";for(;t<i;){let s=n[t];if(s===".")o&&(e.push({type:"key",value:o}),o=""),t++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),t++;let r=t;for(;t<i&&n[t]!=="]";)t++;let f=n.slice(r,t);if(t++,f==="*")e.push({type:"wildcard",value:"*"});else {let a=parseInt(f,10);e.push({type:"index",value:isNaN(a)?f:a});}}else o+=s,t++;}return o&&e.push({type:"key",value:o}),e}function _(n){return n.includes("[*]")}function O(n){let e=R.get(n);return e||(e=_(n)?ln(n):sn(n),R.set(n,e),e)}function sn(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=L(n),i=e.length;if(i===2){let[o,s]=e,r=o.value,f=s.value;return a=>a?.[r]?.[f]}if(i===3){let[o,s,r]=e,f=o.value,a=s.value,u=r.value;return c=>c?.[f]?.[a]?.[u]}let t=e.map(o=>o.value);return o=>{let s=o;for(let r=0;r<i&&s!=null;r++)s=s[t[r]];return s}}function ln(n){let e=L(n),i=[];for(let t=0;t<e.length;t++)e[t].type==="wildcard"&&i.push(t);return i.length===1?fn(e,i[0]):an(e,i)}function fn(n,e){let i=n.slice(0,e).map(r=>r.value),t=n.slice(e+1).map(r=>r.value),o=i.length,s=t.length;if(s===0){if(o===1){let r=i[0];return f=>f?.[r]}return r=>{let f=r;for(let a=0;a<o&&f!=null;a++)f=f[i[a]];return f}}if(s===1){let r=t[0];if(o===1){let f=i[0];return a=>{let u=a?.[f];if(Array.isArray(u))return u.map(c=>c?.[r])}}return f=>{let a=f;for(let u=0;u<o&&a!=null;u++)a=a[i[u]];if(Array.isArray(a))return a.map(u=>u?.[r])}}return r=>{let f=r;for(let a=0;a<o&&f!=null;a++)f=f[i[a]];if(Array.isArray(f))return f.map(a=>{let u=a;for(let c=0;c<s&&u!=null;c++)u=u[t[c]];return u})}}function an(n,e){let i=[],t=0;for(let s=0;s<e.length;s++){let r=e[s],f=s===e.length-1,a=n.slice(t,r).map(u=>u.value);a.length>0&&i.push({type:"access",keys:a}),i.push({type:f?"map":"flatMap",keys:[]}),t=r+1;}let o=n.slice(t).map(s=>s.value);return s=>{let r=s;for(let f of i){if(r==null)return;if(f.type==="access")for(let a of f.keys){if(r==null)return;r=r[a];}else if(f.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(a=>{let u=a;return Array.isArray(u)?u:[u]});}else if(f.type==="map"){if(!Array.isArray(r))return;o.length>0&&(r=r.map(a=>{let u=a;for(let c of o){if(u==null)return;u=u[c];}return u}));}}return r}}function un(n,e){return O(e)(n)}function k(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function cn(){R.clear();}function pn(){return R.size}function A(n){let e=new Set;return x(n,e),Array.from(e)}function x(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o=0;o<n.length;o++)x(n[o],e);return}if(m(n)){e.add(k(n.$));return}if(h(n)){if(typeof n.$if=="string"){let o=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(k(o));}else x(n.$if,e);x(n.then,e),n.else!==void 0&&x(n.else,e);return}if(b(n)){for(let o=0;o<n.$pipe.length;o++)x(n.$pipe[o],e);return}if(T(n)){if(n.args)for(let o=0;o<n.args.length;o++)x(n.args[o],e);return}if(g(n)){e.add(k(n.path)),n.value!==void 0&&isRef(n.value)&&e.add(k(n.value.$));return}if(y(n)){for(let o=0;o<n.conditions.length;o++)x(n.conditions[o],e);return}let i=n,t=Object.keys(i);for(let o=0;o<t.length;o++)x(i[t[o]],e);}function mn(n){return A(n).length>0}function gn(n){return A(n).length===0}function yn(n){return JSON.stringify(n)}function F(n,e={}){let i=e.scope??{},t=e.accessor,o=C(n,i,t),s=A(n),r=yn(n);return {fn:o,deps:s,hash:r}}function C(n,e,i){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let t=n.map(o=>C(o,e,i));return o=>t.map(s=>s(o))}if(m(n))return En(n,i);if(h(n))return hn(n,e,i);if(b(n))return Tn(n,e,i);if(T(n))return bn(n,e,i);if(g(n))return compile(n,i?{accessor:i}:void 0);if(y(n))return compile(n,i?{accessor:i}:void 0);if(M(n)){let t=n,o=Object.keys(t),s=o.map(r=>C(t[r],e,i));return r=>{let f={};for(let a=0;a<o.length;a++)f[o[a]]=s[a](r);return f}}return ()=>n}function J(n,e){return e?i=>e(n,i):O(n)}function En(n,e){return J(n.$,e)}function hn(n,e,i){let t;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=J(r,i);t=n.$if.startsWith("!")?u=>!f(u):u=>!!f(u);}else {let r=C(n.$if,e,i);t=f=>!!r(f);}let o=C(n.then,e,i),s=n.else!==void 0?C(n.else,e,i):()=>{};return r=>t(r)?o(r):s(r)}function Tn(n,e,i){let t=n.$pipe;if(t.length===0)return ()=>{};if(t.length===1)return C(t[0],e,i);let o=C(t[0],e,i),s=t.slice(1).map(f=>C(f,e,i)),r=s.length;if(r===1){let[f]=s;return a=>{let u=o(a),c=f(a);return typeof c=="function"?c(u):c}}if(r===2){let[f,a]=s;return u=>{let c=o(u),p=f(u);return c=typeof p=="function"?p(c):p,p=a(u),typeof p=="function"?p(c):p}}if(r===3){let[f,a,u]=s;return c=>{let p=o(c),E=f(c);return p=typeof E=="function"?E(p):E,E=a(c),p=typeof E=="function"?E(p):E,E=u(c),typeof E=="function"?E(p):E}}return f=>{let a=o(f);for(let u=0;u<r;u++){let c=s[u](f);a=typeof c=="function"?c(a):c;}return a}}function bn(n,e,i){let t=n.$fn,o=n.args;if(o===void 0)return ()=>{let f=e[t];if(!f)throw new Error(`Function not found in scope: ${t}`);return f};let s=o.map(f=>C(f,e,i)),r=s.length;if(r===0)return f=>{let a=e[t];if(!a)throw new Error(`Function not found in scope: ${t}`);return a()};if(r===1){let[f]=s;return a=>{let u=e[t];if(!u)throw new Error(`Function not found in scope: ${t}`);return u(f(a))}}if(r===2){let[f,a]=s;return u=>{let c=e[t];if(!c)throw new Error(`Function not found in scope: ${t}`);return c(f(u),a(u))}}if(r===3){let[f,a,u]=s;return c=>{let p=e[t];if(!p)throw new Error(`Function not found in scope: ${t}`);return p(f(c),a(c),u(c))}}return f=>{let a=e[t];if(!a)throw new Error(`Function not found in scope: ${t}`);return a(...s.map(u=>u(f)))}}function Cn(n,e,i={}){return F(n,i).fn(e)}var v=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let t=JSON.stringify(e),o=this.cache.get(t);if(o)return this.cache.delete(t),this.cache.set(t,o),o;let s=F(e,i);if(this.cache.size>=this._maxSize){let r=this.cache.keys().next().value;r&&this.cache.delete(r);}return this.cache.set(t,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},B=new v;function $n(n,e={}){return B.get(n,e)}function D(n,e="root",i={}){let t=[];return S(n,e,t,i),{valid:t.length===0,errors:t}}function S(n,e,i,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)S(n[r],`${e}[${r}]`,i,t);return}if(m(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(h(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):S(n.$if,`${e}.$if`,i,t),S(n.then,`${e}.then`,i,t),n.else!==void 0&&S(n.else,`${e}.else`,i,t);return}if(b(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let r=0;r<n.$pipe.length;r++)S(n.$pipe[r],`${e}.$pipe[${r}]`,i,t);return}if(T(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(t.scope&&!(n.$fn in t.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let r=0;r<n.args.length;r++)S(n.args[r],`${e}.args[${r}]`,i,t);return}if(g(n)){let r=validate(n,e);r.valid||i.push(...r.errors);return}if(y(n)){let r=validate(n,e);r.valid||i.push(...r.errors);return}let o=n,s=Object.keys(o);for(let r=0;r<s.length;r++){let f=s[r];S(o[f],`${e}.${f}`,i,t);}}function xn(n,e={}){let i=D(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function Sn(n,e={}){return D(n,"root",e).valid}var U={};on(U,{$:()=>Y,$fn:()=>H,$if:()=>wn,$pipe:()=>Q,fn:()=>kn,pipe:()=>Rn,ref:()=>An});function Y(n){return {$:n}}var An=Y;function H(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var kn=H;function wn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function Q(...n){return {$pipe:n}}var Rn=Q;var P="data",X="scope",On={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function G(n,e={}){let{dataParam:i=P,scopeParam:t=X,noPrefixes:o=false,useAccessor:s=false}=e;return d(n,i,t,o,s)}function d(n,e,i,t,o){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(s=>d(s,e,i,t,o)));if(m(n))return Fn(n.$,e,t,o);if(h(n))return Nn(n,e,i,t,o);if(b(n))return Gn(n.$pipe,e,i,t,o);if(T(n))return jn(n,e,i,t,o);if(g(n))return Wn(n,e,i,t,o);if(y(n))return zn(n,e,i,t,o);if(typeof n=="object"){let r=Object.entries(n).map(([f,a])=>builders.property(builders.identifier(f),d(a,e,i,t,o)));return builders.objectExpression(r)}return builders.literal(null)}var N="accessor";function Fn(n,e,i,t){return t?builders.callExpression(builders.identifier(N),[builders.literal(n),builders.identifier(e)]):n.includes("[*]")?vn(n,e,i):w(n,e,i)}function w(n,e,i){let t=j(n);if(t.length===0)return i?builders.identifier("undefined"):builders.identifier(e);let o;if(i){let s=t[0];o=builders.identifier(s.value);for(let r=1;r<t.length;r++){let f=t[r];f.type==="key"?o=builders.memberExpression(o,builders.identifier(f.value),false,true):o=builders.memberExpression(o,builders.literal(f.value),true,true);}}else {o=builders.identifier(e);for(let s of t)s.type==="key"?o=builders.memberExpression(o,builders.identifier(s.value),false,true):o=builders.memberExpression(o,builders.literal(s.value),true,true);}return o}function vn(n,e,i){let t=n.indexOf("[*]"),o=n.slice(0,t),s=n.slice(t+3),r;if(o?r=w(o,e,i):r=i?builders.identifier("undefined"):builders.identifier(e),!s||s==="")return r;if(s.includes("[*]"))return Z(r,s);let f="_i",a=s.startsWith(".")?s.slice(1):s,u=builders.identifier(f);if(a){let c=j(a);for(let p of c)p.type==="key"?u=builders.memberExpression(u,builders.identifier(p.value),false,true):u=builders.memberExpression(u,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(r,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],u)])}function Z(n,e){let i=e.indexOf("[*]"),t=e.slice(0,i),o=e.slice(i+3),s="_i",r=t.startsWith(".")?t.slice(1):t,f=builders.identifier(s);if(r){let u=j(r);for(let c of u)c.type==="key"&&(f=builders.memberExpression(f,builders.identifier(c.value),false,true));}if(o.includes("[*]")){let u=Z(f,o);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],u)])}let a=o.startsWith(".")?o.slice(1):o;if(a){let u=j(a);for(let c of u)c.type==="key"&&(f=builders.memberExpression(f,builders.identifier(c.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],f)])}function Nn(n,e,i,t,o){let s;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),u=a?n.$if.slice(1):n.$if,c=o?builders.callExpression(builders.identifier(N),[builders.literal(u),builders.identifier(e)]):w(u,e,t);s=a?builders.unaryExpression("!",c):c;}else s=d(n.$if,e,i,t,o);let r=d(n.then,e,i,t,o),f=n.else!==void 0?d(n.else,e,i,t,o):builders.identifier("undefined");return builders.conditionalExpression(s,r,f)}function jn(n,e,i,t,o){let s=t?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(i),builders.identifier(n.$fn),false,false);if(n.args===void 0)return s;let r=n.args.map(f=>d(f,e,i,t,o));return builders.callExpression(s,r)}function Gn(n,e,i,t,o){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return d(n[0],e,i,t,o);let s=d(n[0],e,i,t,o);for(let r=1;r<n.length;r++){let f=d(n[r],e,i,t,o);s=builders.callExpression(f,[s]);}return s}function Wn(n,e,i,t,o){let s=o?builders.callExpression(builders.identifier(N),[builders.literal(n.path),builders.identifier(e)]):w(n.path,e,t),r=n.value!==void 0?m(n.value)?o?builders.callExpression(builders.identifier(N),[builders.literal(n.value.$),builders.identifier(e)]):w(n.value.$,e,t):d(n.value,e,i,t,o):builders.literal(null),f=On[n.op];if(f)return builders.binaryExpression(f,s,r);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(r,builders.identifier("includes")),[s]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(r,builders.identifier("includes")),[s]));case "contains":return builders.callExpression(builders.memberExpression(s,builders.identifier("includes"),false,true),[r]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(s,builders.identifier("includes"),false,true),[r]));case "exists":return builders.binaryExpression("!=",s,builders.literal(null));case "notExists":return builders.binaryExpression("==",s,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[r]),builders.identifier("test")),[s]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[r]),builders.identifier("test")),[s]));case "startsWith":return builders.callExpression(builders.memberExpression(s,builders.identifier("startsWith"),false,true),[r]);case "endsWith":return builders.callExpression(builders.memberExpression(s,builders.identifier("endsWith"),false,true),[r]);default:return builders.binaryExpression("===",s,r)}}function zn(n,e,i,t,o){let{logic:s,conditions:r}=n,f=s==="AND"?"&&":"||";if(r.length===0)return builders.literal(s==="AND");if(r.length===1)return d(r[0],e,i,t,o);let a=d(r[0],e,i,t,o);for(let u=1;u<r.length;u++){let c=d(r[u],e,i,t,o);a=builders.logicalExpression(f,a,c);}return a}function j(n){let e=[],i=n.length,t=0,o="";for(;t<i;){let s=n[t];if(s===".")o&&(e.push({type:"key",value:o}),o=""),t++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),t++;let r=t;for(;t<i&&n[t]!=="]";)t++;let f=n.slice(r,t);if(t++,f!=="*"){let a=parseInt(f,10);e.push({type:"index",value:isNaN(a)?f:a});}}else o+=s,t++;}return o&&e.push({type:"key",value:o}),e}function nn(n,e=[P]){return builders.arrowFunctionExpression(e.map(i=>builders.identifier(i)),n)}function W(n){let e=new Set;return $(n,e),e}function $(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t of n)$(t,e);return}if(m(n))return;if(h(n)){$(n.$if,e),$(n.then,e),n.else!==void 0&&$(n.else,e);return}if(b(n)){for(let t of n.$pipe)$(t,e);return}if(T(n)){if(e.add(n.$fn),n.args)for(let t of n.args)$(t,e);return}if(g(n)){n.value!==void 0&&typeof n.value=="object"&&$(n.value,e);return}if(y(n)){for(let t of n.conditions)$(t,e);return}let i=n;for(let t of Object.keys(i))$(i[t],e);}function z(n){let e=new Set;for(let i of n){let t=i.indexOf("."),o=i.indexOf("["),s=i.length;t!==-1&&(s=Math.min(s,t)),o!==-1&&(s=Math.min(s,o));let r=i.slice(0,s);r&&e.add(r);}return e}function Dn(n){return JSON.stringify(n)}function Pn(n,e,i,t){let o=G(n,{noPrefixes:true,useAccessor:t}),s=generate(o),r=!t&&e.size>0?`const{${[...e].join(",")}}=data??{};`:"",f=t?new Set([...i,"accessor"]):i,a=f.size>0?`const{${[...f].join(",")}}=scope;`:"";return a?`(function(scope){${a}return function(data){${r}return ${s}}})`:`(function(){return function(data){${r}return ${s}}})`}function I(n,e={}){let{scope:i={},returnCode:t=false,useAccessor:o=false}=e,s=A(n),r=z(s),f=W(n),a=Dn(n),u=Pn(n,r,f,o);if(t)return {code:u,deps:s,hash:a,dataRoots:[...r],scopeFns:[...f]};let c;try{c=new Function(`return ${u}`)()(i);}catch(p){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${p instanceof Error?p.message:String(p)}`)}return {fn:c,deps:s,hash:a}}function en(n,e,i={}){let{fn:t}=I(n,i);return t(e)}var Ee="0.1.1";export{v as ExpressionCache,Ee as VERSION,xn as assertValid,U as builders,B as cache,$n as cached,cn as clearPathCache,F as compile,I as compileAST,O as compilePath,G as dslToAST,Cn as evaluate,en as evaluateAST,z as extractDataRoots,A as extractDeps,W as extractScopeFns,un as get,pn as getPathCacheSize,mn as hasDeps,_ as hasWildcard,g as isCondition,rn as isConditionExpr,y as isConditionGroup,h as isConditional,T as isFn,M as isLiteral,b as isPipe,gn as isPure,m as isRef,Sn as isValid,k as normalizePath,D as validate,nn as wrapInFunction};
1
+ import {isRef,compile,validate}from'@statedelta-libs/conditions';import {builders,generate}from'omni-ast';var sn=Object.defineProperty;var fn=(n,e)=>{for(var i in e)sn(n,i,{get:e[i],enumerable:true});};var m=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,E=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,T=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",b=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),_=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),y=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&_.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),h=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,ln=n=>y(n)||h(n),z=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,t="left"in i&&"op"in i&&_.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!t&&!("logic"in i)}return false};var R=new Map;function q(n){let e=[],i=n.length,t=0,r="";for(;t<i;){let f=n[t];if(f===".")r&&(e.push({type:"key",value:r}),r=""),t++;else if(f==="["){r&&(e.push({type:"key",value:r}),r=""),t++;let o=t;for(;t<i&&n[t]!=="]";)t++;let s=n.slice(o,t);if(t++,s==="*")e.push({type:"wildcard",value:"*"});else {let u=parseInt(s,10);e.push({type:"index",value:isNaN(u)?s:u});}}else r+=f,t++;}return r&&e.push({type:"key",value:r}),e}function J(n){return n.includes("[*]")}function F(n){let e=R.get(n);return e||(e=J(n)?cn(n):un(n),R.set(n,e),e)}function un(n){if(!n.includes(".")&&!n.includes("["))return r=>r?.[n];let e=q(n),i=e.length;if(i===2){let[r,f]=e,o=r.value,s=f.value;return u=>u?.[o]?.[s]}if(i===3){let[r,f,o]=e,s=r.value,u=f.value,c=o.value;return a=>a?.[s]?.[u]?.[c]}let t=e.map(r=>r.value);return r=>{let f=r;for(let o=0;o<i&&f!=null;o++)f=f[t[o]];return f}}function cn(n){let e=q(n),i=[];for(let t=0;t<e.length;t++)e[t].type==="wildcard"&&i.push(t);return i.length===1?an(e,i[0]):pn(e,i)}function an(n,e){let i=n.slice(0,e).map(o=>o.value),t=n.slice(e+1).map(o=>o.value),r=i.length,f=t.length;if(f===0){if(r===1){let o=i[0];return s=>s?.[o]}return o=>{let s=o;for(let u=0;u<r&&s!=null;u++)s=s[i[u]];return s}}if(f===1){let o=t[0];if(r===1){let s=i[0];return u=>{let c=u?.[s];if(Array.isArray(c))return c.map(a=>a?.[o])}}return s=>{let u=s;for(let c=0;c<r&&u!=null;c++)u=u[i[c]];if(Array.isArray(u))return u.map(c=>c?.[o])}}return o=>{let s=o;for(let u=0;u<r&&s!=null;u++)s=s[i[u]];if(Array.isArray(s))return s.map(u=>{let c=u;for(let a=0;a<f&&c!=null;a++)c=c[t[a]];return c})}}function pn(n,e){let i=[],t=0;for(let f=0;f<e.length;f++){let o=e[f],s=f===e.length-1,u=n.slice(t,o).map(c=>c.value);u.length>0&&i.push({type:"access",keys:u}),i.push({type:s?"map":"flatMap",keys:[]}),t=o+1;}let r=n.slice(t).map(f=>f.value);return f=>{let o=f;for(let s of i){if(o==null)return;if(s.type==="access")for(let u of s.keys){if(o==null)return;o=o[u];}else if(s.type==="flatMap"){if(!Array.isArray(o))return;o=o.flatMap(u=>{let c=u;return Array.isArray(c)?c:[c]});}else if(s.type==="map"){if(!Array.isArray(o))return;r.length>0&&(o=o.map(u=>{let c=u;for(let a of r){if(c==null)return;c=c[a];}return c}));}}return o}}function dn(n,e){return F(e)(n)}function x(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function gn(){R.clear();}function mn(){return R.size}function k(n){let e=new Set;return $(n,e),Array.from(e)}function $(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)$(n[r],e);return}if(m(n)){e.add(x(n.$));return}if(E(n)){if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(x(r));}else $(n.$if,e);$(n.then,e),n.else!==void 0&&$(n.else,e);return}if(b(n)){for(let r=0;r<n.$pipe.length;r++)$(n.$pipe[r],e);return}if(T(n)){if(n.args)for(let r=0;r<n.args.length;r++){let f=n.args[r];typeof f!="function"&&$(f,e);}return}if(y(n)){isRef(n.left)&&e.add(x(n.left.$)),n.right!==void 0&&isRef(n.right)&&e.add(x(n.right.$));return}if(h(n)){for(let r=0;r<n.conditions.length;r++)$(n.conditions[r],e);return}let i=n,t=Object.keys(i);for(let r=0;r<t.length;r++)$(i[t[r]],e);}function yn(n){return k(n).length>0}function hn(n){return k(n).length===0}function En(n){return JSON.stringify(n)}function O(n,e={}){let i=e.scope??{},t=e.accessor,r=S(n,i,t),f=k(n),o=En(n);return {fn:r,deps:f,hash:o}}function S(n,e,i){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let t=n.map(r=>S(r,e,i));return r=>t.map(f=>f(r))}if(m(n))return Tn(n,i);if(E(n))return bn(n,e,i);if(b(n))return Cn(n,e,i);if(T(n))return Sn(n,e,i);if(y(n))return compile(n,i?{accessor:i}:void 0);if(h(n))return compile(n,i?{accessor:i}:void 0);if(z(n)){let t=n,r=Object.keys(t),f=r.map(o=>S(t[o],e,i));return o=>{let s={};for(let u=0;u<r.length;u++)s[r[u]]=f[u](o);return s}}return ()=>n}function Y(n,e){return e?i=>e(n,i):F(n)}function Tn(n,e){return Y(n.$,e)}function bn(n,e,i){let t;if(typeof n.$if=="string"){let o=n.$if.startsWith("!")?n.$if.slice(1):n.$if,s=Y(o,i);t=n.$if.startsWith("!")?c=>!s(c):c=>!!s(c);}else {let o=S(n.$if,e,i);t=s=>!!o(s);}let r=S(n.then,e,i),f=n.else!==void 0?S(n.else,e,i):()=>{};return o=>t(o)?r(o):f(o)}function Cn(n,e,i){let t=n.$pipe;if(t.length===0)return ()=>{};if(t.length===1)return S(t[0],e,i);let r=S(t[0],e,i),f=t.slice(1).map(s=>S(s,e,i)),o=f.length;if(o===1){let[s]=f;return u=>{let c=r(u),a=s(u);return typeof a=="function"?a(c):a}}if(o===2){let[s,u]=f;return c=>{let a=r(c),p=s(c);return a=typeof p=="function"?p(a):p,p=u(c),typeof p=="function"?p(a):p}}if(o===3){let[s,u,c]=f;return a=>{let p=r(a),d=s(a);return p=typeof d=="function"?d(p):d,d=u(a),p=typeof d=="function"?d(p):d,d=c(a),typeof d=="function"?d(p):d}}return s=>{let u=r(s);for(let c=0;c<o;c++){let a=f[c](s);u=typeof a=="function"?a(u):a;}return u}}function Sn(n,e,i){let t=n.$fn,r=n.args;if(r===void 0)return ()=>{let s=e[t];if(!s)throw new Error(`Function not found in scope: ${t}`);return s};let f=r.map(s=>typeof s=="function"?()=>s:S(s,e,i)),o=f.length;if(o===0)return s=>{let u=e[t];if(!u)throw new Error(`Function not found in scope: ${t}`);return u()};if(o===1){let[s]=f;return u=>{let c=e[t];if(!c)throw new Error(`Function not found in scope: ${t}`);return c(s(u))}}if(o===2){let[s,u]=f;return c=>{let a=e[t];if(!a)throw new Error(`Function not found in scope: ${t}`);return a(s(c),u(c))}}if(o===3){let[s,u,c]=f;return a=>{let p=e[t];if(!p)throw new Error(`Function not found in scope: ${t}`);return p(s(a),u(a),c(a))}}return s=>{let u=e[t];if(!u)throw new Error(`Function not found in scope: ${t}`);return u(...f.map(c=>c(s)))}}function $n(n,e,i={}){return O(n,i).fn(e)}var N=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let t=JSON.stringify(e),r=this.cache.get(t);if(r)return this.cache.delete(t),this.cache.set(t,r),r;let f=O(e,i);if(this.cache.size>=this._maxSize){let o=this.cache.keys().next().value;o&&this.cache.delete(o);}return this.cache.set(t,f),f}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},H=new N;function An(n,e={}){return H.get(n,e)}function M(n,e="root",i={}){let t=[];return A(n,e,t,i),{valid:t.length===0,errors:t}}function A(n,e,i,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o=0;o<n.length;o++)A(n[o],`${e}[${o}]`,i,t);return}if(m(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(E(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):A(n.$if,`${e}.$if`,i,t),A(n.then,`${e}.then`,i,t),n.else!==void 0&&A(n.else,`${e}.else`,i,t);return}if(b(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let o=0;o<n.$pipe.length;o++)A(n.$pipe[o],`${e}.$pipe[${o}]`,i,t);return}if(T(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(t.scope&&!(n.$fn in t.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let o=0;o<n.args.length;o++){let s=n.args[o];typeof s!="function"&&A(s,`${e}.args[${o}]`,i,t);}return}if(y(n)){let o=validate(n,e);o.valid||i.push(...o.errors);return}if(h(n)){let o=validate(n,e);o.valid||i.push(...o.errors);return}let r=n,f=Object.keys(r);for(let o=0;o<f.length;o++){let s=f[o];A(r[s],`${e}.${s}`,i,t);}}function kn(n,e={}){let i=M(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function wn(n,e={}){return M(n,"root",e).valid}var Z={};fn(Z,{$:()=>Q,$fn:()=>U,$if:()=>Fn,$pipe:()=>X,fn:()=>Rn,pipe:()=>On,ref:()=>xn});function Q(n){return {$:n}}var xn=Q;function U(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var Rn=U;function Fn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function X(...n){return {$pipe:n}}var On=X;var D="data",en="scope",Nn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function v(n,e={}){let{dataParam:i=D,scopeParam:t=en,noPrefixes:r=false,useAccessor:f=false,lexicalPrefix:o}=e;return g(n,i,t,r,f,o)}function g(n,e,i,t,r,f){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(o=>g(o,e,i,t,r,f)));if(m(n))return jn(n.$,e,t,r,f);if(E(n))return Gn(n,e,i,t,r,f);if(b(n))return zn(n.$pipe,e,i,t,r,f);if(T(n))return Wn(n,e,i,t,r,f);if(y(n))return Mn(n,e,i,t,r,f);if(h(n))return Dn(n,e,i,t,r,f);if(typeof n=="object"){let s=Object.entries(n).map(([u,c])=>builders.property(builders.identifier(u),g(c,e,i,t,r,f)));return builders.objectExpression(s)}return builders.literal(null)}var I="accessor";function L(n,e){return e?n===e||n.startsWith(e+"."):false}function jn(n,e,i,t,r){return t?L(n,r)?w(n,e,true):builders.callExpression(builders.identifier(I),[builders.literal(n),builders.identifier(e)]):n.includes("[*]")?vn(n,e,i):w(n,e,i)}function w(n,e,i){let t=j(n);if(t.length===0)return i?builders.identifier("undefined"):builders.identifier(e);let r;if(i){let f=t[0];r=builders.identifier(f.value);for(let o=1;o<t.length;o++){let s=t[o];s.type==="key"?r=builders.memberExpression(r,builders.identifier(s.value),false,true):r=builders.memberExpression(r,builders.literal(s.value),true,true);}}else {r=builders.identifier(e);for(let f of t)f.type==="key"?r=builders.memberExpression(r,builders.identifier(f.value),false,true):r=builders.memberExpression(r,builders.literal(f.value),true,true);}return r}function vn(n,e,i){let t=n.indexOf("[*]"),r=n.slice(0,t),f=n.slice(t+3),o;if(r?o=w(r,e,i):o=i?builders.identifier("undefined"):builders.identifier(e),!f||f==="")return o;if(f.includes("[*]"))return tn(o,f);let s="_i",u=f.startsWith(".")?f.slice(1):f,c=builders.identifier(s);if(u){let a=j(u);for(let p of a)p.type==="key"?c=builders.memberExpression(c,builders.identifier(p.value),false,true):c=builders.memberExpression(c,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(o,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],c)])}function tn(n,e){let i=e.indexOf("[*]"),t=e.slice(0,i),r=e.slice(i+3),f="_i",o=t.startsWith(".")?t.slice(1):t,s=builders.identifier(f);if(o){let c=j(o);for(let a of c)a.type==="key"&&(s=builders.memberExpression(s,builders.identifier(a.value),false,true));}if(r.includes("[*]")){let c=tn(s,r);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],c)])}let u=r.startsWith(".")?r.slice(1):r;if(u){let c=j(u);for(let a of c)a.type==="key"&&(s=builders.memberExpression(s,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],s)])}function Gn(n,e,i,t,r,f){let o;if(typeof n.$if=="string"){let c=n.$if.startsWith("!"),a=c?n.$if.slice(1):n.$if,p;r?L(a,f)?p=w(a,e,true):p=builders.callExpression(builders.identifier(I),[builders.literal(a),builders.identifier(e)]):p=w(a,e,t),o=c?builders.unaryExpression("!",p):p;}else o=g(n.$if,e,i,t,r,f);let s=g(n.then,e,i,t,r,f),u=n.else!==void 0?g(n.else,e,i,t,r,f):builders.identifier("undefined");return builders.conditionalExpression(o,s,u)}function Wn(n,e,i,t,r,f){let o=t?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(i),builders.identifier(n.$fn),false,false);if(n.args===void 0)return o;let s=n.args.map(u=>typeof u=="function"?builders.literal(null):g(u,e,i,t,r,f));return builders.callExpression(o,s)}function zn(n,e,i,t,r,f){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return g(n[0],e,i,t,r,f);let o=g(n[0],e,i,t,r,f);for(let s=1;s<n.length;s++){let u=g(n[s],e,i,t,r,f);o=builders.callExpression(u,[o]);}return o}function nn(n,e,i,t,r,f){if(m(n)){let o=n.$;return r?L(o,f)?w(o,e,true):builders.callExpression(builders.identifier(I),[builders.literal(o),builders.identifier(e)]):w(o,e,t)}return g(n,e,i,t,r,f)}function Mn(n,e,i,t,r,f){let o=nn(n.left,e,i,t,r,f),s=n.right!==void 0?nn(n.right,e,i,t,r,f):builders.literal(null),u=Nn[n.op];if(u)return builders.binaryExpression(u,o,s);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(s,builders.identifier("includes")),[o]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(s,builders.identifier("includes")),[o]));case "contains":return builders.callExpression(builders.memberExpression(o,builders.identifier("includes"),false,true),[s]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(o,builders.identifier("includes"),false,true),[s]));case "exists":return builders.binaryExpression("!=",o,builders.literal(null));case "notExists":return builders.binaryExpression("==",o,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[s]),builders.identifier("test")),[o]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[s]),builders.identifier("test")),[o]));case "startsWith":return builders.callExpression(builders.memberExpression(o,builders.identifier("startsWith"),false,true),[s]);case "endsWith":return builders.callExpression(builders.memberExpression(o,builders.identifier("endsWith"),false,true),[s]);default:return builders.binaryExpression("===",o,s)}}function Dn(n,e,i,t,r,f){let{logic:o,conditions:s}=n,u=o==="AND"?"&&":"||";if(s.length===0)return builders.literal(o==="AND");if(s.length===1)return g(s[0],e,i,t,r,f);let c=g(s[0],e,i,t,r,f);for(let a=1;a<s.length;a++){let p=g(s[a],e,i,t,r,f);c=builders.logicalExpression(u,c,p);}return c}function j(n){let e=[],i=n.length,t=0,r="";for(;t<i;){let f=n[t];if(f===".")r&&(e.push({type:"key",value:r}),r=""),t++;else if(f==="["){r&&(e.push({type:"key",value:r}),r=""),t++;let o=t;for(;t<i&&n[t]!=="]";)t++;let s=n.slice(o,t);if(t++,s!=="*"){let u=parseInt(s,10);e.push({type:"index",value:isNaN(u)?s:u});}}else r+=f,t++;}return r&&e.push({type:"key",value:r}),e}function on(n,e=[D]){return builders.arrowFunctionExpression(e.map(i=>builders.identifier(i)),n)}function G(n){let e=new Set;return C(n,e),e}function C(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t of n)C(t,e);return}if(m(n))return;if(E(n)){C(n.$if,e),C(n.then,e),n.else!==void 0&&C(n.else,e);return}if(b(n)){for(let t of n.$pipe)C(t,e);return}if(T(n)){if(e.add(n.$fn),n.args)for(let t of n.args)typeof t!="function"&&C(t,e);return}if(y(n)){n.left!==void 0&&typeof n.left=="object"&&C(n.left,e),n.right!==void 0&&typeof n.right=="object"&&C(n.right,e);return}if(h(n)){for(let t of n.conditions)C(t,e);return}let i=n;for(let t of Object.keys(i))C(i[t],e);}function W(n){let e=new Set;for(let i of n){let t=i.indexOf("."),r=i.indexOf("["),f=i.length;t!==-1&&(f=Math.min(f,t)),r!==-1&&(f=Math.min(f,r));let o=i.slice(0,f);o&&e.add(o);}return e}function Ln(n){return JSON.stringify(n)}function Vn(n,e,i,t,r){let f=v(n,{noPrefixes:true,useAccessor:t,lexicalPrefix:r}),o=generate(f),s="";t?r&&(s=`const{${r}}=data??{};`):e.size>0&&(s=`const{${[...e].join(",")}}=data??{};`);let u=t?new Set([...i,"accessor"]):i,c=u.size>0?`const{${[...u].join(",")}}=scope;`:"";return c?`(function(scope){${c}return function(data){${s}return ${o}}})`:`(function(){return function(data){${s}return ${o}}})`}function V(n,e={}){let{scope:i={},returnCode:t=false,useAccessor:r=false,lexicalPrefix:f}=e,o=k(n),s=W(o),u=G(n),c=Ln(n),a=Vn(n,s,u,r,f);if(t)return {code:a,deps:o,hash:c,dataRoots:[...s],scopeFns:[...u]};let p;try{p=new Function(`return ${a}`)()(i);}catch(d){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${d instanceof Error?d.message:String(d)}`)}return {fn:p,deps:o,hash:c}}function rn(n,e,i={}){let{fn:t}=V(n,i);return t(e)}var Te="0.1.1";export{N as ExpressionCache,Te as VERSION,kn as assertValid,Z as builders,H as cache,An as cached,gn as clearPathCache,O as compile,V as compileAST,F as compilePath,v as dslToAST,$n as evaluate,rn as evaluateAST,W as extractDataRoots,k as extractDeps,G as extractScopeFns,dn as get,mn as getPathCacheSize,yn as hasDeps,J as hasWildcard,y as isCondition,ln as isConditionExpr,h as isConditionGroup,E as isConditional,T as isFn,z as isLiteral,b as isPipe,hn as isPure,m as isRef,wn as isValid,x as normalizePath,M as validate,on as wrapInFunction};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statedelta-libs/expressions",
3
- "version": "0.2.0",
3
+ "version": "1.0.0",
4
4
  "description": "JSON DSL compiler for optimized functions - StateDelta expression engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -14,7 +14,7 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "@statedelta-libs/conditions": "^0.1.1",
17
+ "@statedelta-libs/conditions": "^1.0.0",
18
18
  "omni-ast": "^2.0.0"
19
19
  },
20
20
  "files": [