@statedelta-libs/expressions 2.3.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {builders,generate}from'omni-ast';var yn=Object.defineProperty;var En=(n,t)=>{for(var o in t)yn(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,x=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,S=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",R=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),k=n=>n!==null&&typeof n=="object"&&"$cb"in n&&typeof n.$cb=="string"&&"body"in n,I=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&&I.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),J=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&&I.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 W=new Map;function U(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 u=n.slice(e,r);if(r++,u==="*")t.push({type:"wildcard",value:"*"});else {let f=parseInt(u,10);t.push({type:"index",value:isNaN(f)?u:f});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function Z(n){return n.includes("[*]")}function j(n){let t=W.get(n);return t||(t=Z(n)?Cn(n):bn(n),W.set(n,t),t)}function bn(n){if(!n.includes(".")&&!n.includes("["))return i=>i?.[n];let t=U(n),o=t.length;if(o===2){let[i,s]=t,e=i.value,u=s.value;return f=>f?.[e]?.[u]}if(o===3){let[i,s,e]=t,u=i.value,f=s.value,l=e.value;return a=>a?.[u]?.[f]?.[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=U(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 u=>u?.[e]}return e=>{let u=e;for(let f=0;f<i&&u!=null;f++)u=u[o[f]];return u}}if(s===1){let e=r[0];if(i===1){let u=o[0];return f=>{let l=f?.[u];if(Array.isArray(l))return l.map(a=>a?.[e])}}return u=>{let f=u;for(let l=0;l<i&&f!=null;l++)f=f[o[l]];if(Array.isArray(f))return f.map(l=>l?.[e])}}return e=>{let u=e;for(let f=0;f<i&&u!=null;f++)u=u[o[f]];if(Array.isArray(u))return u.map(f=>{let l=f;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],u=s===t.length-1,f=n.slice(r,e).map(l=>l.value);f.length>0&&o.push({type:"access",keys:f}),o.push({type:u?"map":"flatMap",keys:[]}),r=e+1;}let i=n.slice(r).map(s=>s.value);return s=>{let e=s;for(let u of o){if(e==null)return;if(u.type==="access")for(let f of u.keys){if(e==null)return;e=e[f];}else if(u.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(f=>{let l=f;return Array.isArray(l)?l:[l]});}else if(u.type==="map"){if(!Array.isArray(e))return;i.length>0&&(e=e.map(f=>{let l=f;for(let a of i){if(l==null)return;l=l[a];}return l}));}}return e}}function An(n,t){return j(t)(n)}function z(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function xn(){W.clear();}function Sn(){return W.size}function O(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(z(n.$));return}if(x(n)){if(typeof n.$if=="string"){let i=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(z(i));}else C(n.$if,t);C(n.then,t),n.else!==void 0&&C(n.else,t);return}if(R(n)){for(let i=0;i<n.$pipe.length;i++)C(n.$pipe[i],t);return}if(S(n)){if(n.args)for(let i=0;i<n.args.length;i++)C(n.args[i],t);return}if(k(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 Rn(n){return O(n).length>0}function kn(n){return O(n).length===0}function wn(n){return JSON.stringify(n)}function N(n,t={}){let o=t.scope??{},r=t.accessor,i=t.context??{},s=y(n,o,r,i),e=O(n),u=wn(n);return {fn:s,deps:e,hash:u}}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 Fn(n,o);if(x(n))return On(n,t,o,r);if(R(n))return vn(n,t,o,r);if(S(n))return jn(n,t,o,r);if(k(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(J(n)){let i=n,s=Object.keys(i),e=s.map(u=>y(i[u],t,o,r));return u=>{let f={};for(let l=0;l<s.length;l++)f[s[l]]=e[l](u);return f}}return ()=>n}function nn(n,t){return t?o=>t(n,o):j(n)}function Fn(n,t){return nn(n.$,t)}function On(n,t,o,r){let i;if(typeof n.$if=="string"){let u=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=nn(u,o);i=n.$if.startsWith("!")?a=>!f(a):a=>!!f(a);}else {let u=y(n.$if,t,o,r);i=f=>!!u(f);}let s=y(n.then,t,o,r),e=n.else!==void 0?y(n.else,t,o,r):()=>{};return u=>i(u)?s(u):e(u)}function vn(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(f=>y(f,t,o,r)),u=e.length;if(u===1){let[f]=e;return l=>{let a=s(l),p=f(l);return typeof p=="function"?p(a):p}}if(u===2){let[f,l]=e;return a=>{let p=s(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(u===3){let[f,l,a]=e;return p=>{let d=s(p),g=f(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 f=>{let l=s(f);for(let a=0;a<u;a++){let p=e[a](f);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 f=t[i];if(!f)throw new Error(`Function not found in scope: ${i}`);return f};let e=s.map(f=>y(f,t,o,r)),u=e.length;if(u===0)return ()=>{let f=t[i];if(!f)throw new Error(`Function not found in scope: ${i}`);return f()};if(u===1){let[f]=e;return l=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(f(l))}}if(u===2){let[f,l]=e;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(f(a),l(a))}}if(u===3){let[f,l,a]=e;return p=>{let d=t[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(f(p),l(p),a(p))}}return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...e.map(a=>a(f)))}}function Nn(n,t,o,r){let i=n.$cb,s,e=(a,p)=>s?s(a):o?o(a,p):j(a)(p),u=y(n.body,t,e,r),f=n.params?y(n.params,t,o,r):void 0,l=a=>o?p=>o(p,a):p=>j(p)(a);return a=>{let p=r?.[i];if(!p)throw new Error(`Context function not found: ${i}`);let d=l(a),g={data:a,get:d},w=G=>{G.get!==d&&(s=G.get);try{return u(G.data)}finally{s=void 0;}},F=f?f(a):void 0;return p(w,g,F)}}function Gn(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 u=s(e);return Array.isArray(u)&&u.includes(i(e))};case "notIn":return e=>{let u=s(e);return !Array.isArray(u)||!u.includes(i(e))};case "contains":return e=>{let u=i(e);return Array.isArray(u)&&u.includes(s(e))};case "notContains":return e=>{let u=i(e);return !Array.isArray(u)||!u.includes(s(e))};case "exists":return e=>i(e)!==void 0;case "notExists":return e=>i(e)===void 0;case "matches":return e=>{let u=i(e),f=s(e);return typeof u!="string"||typeof f!="string"?false:new RegExp(f).test(u)};case "notMatches":return e=>{let u=i(e),f=s(e);return typeof u!="string"||typeof f!="string"?true:!new RegExp(f).test(u)};case "startsWith":return e=>{let u=i(e),f=s(e);return typeof u=="string"&&typeof f=="string"&&u.startsWith(f)};case "endsWith":return e=>{let u=i(e),f=s(e);return typeof u=="string"&&typeof f=="string"&&u.endsWith(f)}}}function In(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,u]=i;return n.logic==="AND"?f=>!!e(f)&&!!u(f):f=>!!e(f)||!!u(f)}if(s===3){let[e,u,f]=i;return n.logic==="AND"?l=>!!e(l)&&!!u(l)&&!!f(l):l=>!!e(l)||!!u(l)||!!f(l)}return n.logic==="AND"?e=>{for(let u=0;u<s;u++)if(!i[u](e))return false;return true}:e=>{for(let u=0;u<s;u++)if(i[u](e))return true;return false}}function Wn(n,t,o={}){return N(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=N(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);}}},tn=new D;function zn(n,t={}){return tn.get(n,t)}function K(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(x(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(R(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(S(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(k(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)){I.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 u=s[e];T(i[u],`${t}.${u}`,o,r);}}function Dn(n,t={}){let o=K(n,"root",t);if(!o.valid)throw new Error(`Invalid expression: ${o.errors.join("; ")}`)}function Mn(n,t={}){return K(n,"root",t).valid}var cn={};En(cn,{$:()=>en,$call:()=>fn,$cb:()=>un,$cond:()=>sn,$fn:()=>on,$if:()=>_n,$pipe:()=>rn,call:()=>Kn,cb:()=>Jn,cond:()=>qn,fn:()=>Vn,pipe:()=>Ln,ref:()=>Bn});function en(n){return {$:n}}var Bn=en;function on(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var Vn=on;function _n(n,t,o){return o===void 0?{$if:n,then:t}:{$if:n,then:t,else:o}}function rn(...n){return {$pipe:n}}var Ln=rn;function sn(n,t,o){return o===void 0?{left:n,op:t}:{left:n,op:t,right:o}}var qn=sn;function un(n,t,o){return o===void 0?{$cb:n,body:t}:{$cb:n,body:t,params:o}}var Jn=un;function fn(n,t=[]){return {$fn:n,args:t}}var Kn=fn;var P="data",an="scope",Pn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function B(n,t={}){let{dataParam:o=P,scopeParam:r=an,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 Xn(n.$,t,r,i,s);if(x(n))return Hn(n,t,o,r,i,s);if(R(n))return nt(n.$pipe,t,o,r,i,s);if(S(n))return Qn(n,t,o,r,i,s);if(k(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 u=Object.entries(n).map(([f,l])=>builders.property(builders.identifier(f),m(l,t,o,r,i,s)));return builders.objectExpression(u)}return builders.literal(null)}var X="accessor";function Y(n,t){return t?n===t||n.startsWith(t+"."):false}function Xn(n,t,o,r,i){return r?Y(n,i)?v(n,t,true):builders.callExpression(builders.identifier(X),[builders.literal(n),builders.identifier(t)]):n.includes("[*]")?Yn(n,t,o):v(n,t,o)}function v(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 u=r[e];u.type==="key"?i=builders.memberExpression(i,builders.identifier(u.value),false,true):i=builders.memberExpression(i,builders.literal(u.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 Yn(n,t,o){let r=n.indexOf("[*]"),i=n.slice(0,r),s=n.slice(r+3),e;if(i?e=v(i,t,o):e=o?builders.identifier("undefined"):builders.identifier(t),!s||s==="")return e;if(s.includes("[*]"))return pn(e,s);let u="_i",f=s.startsWith(".")?s.slice(1):s,l=builders.identifier(u);if(f){let a=M(f);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(u)],l)])}function pn(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,u=builders.identifier(s);if(e){let l=M(e);for(let a of l)a.type==="key"&&(u=builders.memberExpression(u,builders.identifier(a.value),false,true));}if(i.includes("[*]")){let l=pn(u,i);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],l)])}let f=i.startsWith(".")?i.slice(1):i;if(f){let l=M(f);for(let a of l)a.type==="key"&&(u=builders.memberExpression(u,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],u)])}function Hn(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=v(a,t,true):p=builders.callExpression(builders.identifier(X),[builders.literal(a),builders.identifier(t)]):p=v(a,t,r),e=l?builders.unaryExpression("!",p):p;}else e=m(n.$if,t,o,r,i,s);let u=m(n.then,t,o,r,i,s),f=n.else!==void 0?m(n.else,t,o,r,i,s):builders.identifier("undefined");return builders.conditionalExpression(e,u,f)}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 u=n.args.map(f=>m(f,t,o,r,i,s));return builders.callExpression(e,u)}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),u=m(n.body,t,o,r,i,s),l=[builders.arrowFunctionExpression([builders.identifier("ctx")],u),builders.identifier("ctx")];if(n.params!==void 0){let a=m(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 m(n[0],t,o,r,i,s);let e=m(n[0],t,o,r,i,s);for(let u=1;u<n.length;u++){let f=m(n[u],t,o,r,i,s);e=builders.callExpression(f,[e]);}return e}function ln(n,t,o,r,i,s){if(E(n)){let e=n.$;return i?Y(e,s)?v(e,t,true):builders.callExpression(builders.identifier(X),[builders.literal(e),builders.identifier(t)]):v(e,t,r)}return m(n,t,o,r,i,s)}function tt(n,t,o,r,i,s){let e=ln(n.left,t,o,r,i,s),u=n.right!==void 0?ln(n.right,t,o,r,i,s):builders.literal(null),f=Pn[n.op];if(f)return builders.binaryExpression(f,e,u);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(u,builders.identifier("includes")),[e]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(u,builders.identifier("includes")),[e]));case "contains":return builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[u]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[u]));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"),[u]),builders.identifier("test")),[e]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[u]),builders.identifier("test")),[e]));case "startsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("startsWith"),false,true),[u]);case "endsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("endsWith"),false,true),[u]);default:return builders.binaryExpression("===",e,u)}}function et(n,t,o,r,i,s){let{logic:e,conditions:u}=n,f=e==="AND"?"&&":"||";if(u.length===0)return builders.literal(e==="AND");if(u.length===1)return m(u[0],t,o,r,i,s);let l=m(u[0],t,o,r,i,s);for(let a=1;a<u.length;a++){let p=m(u[a],t,o,r,i,s);l=builders.logicalExpression(f,l,p);}return l}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 u=n.slice(e,r);if(r++,u!=="*"){let f=parseInt(u,10);t.push({type:"index",value:isNaN(f)?u:f});}}else i+=s,r++;}return i&&t.push({type:"key",value:i}),t}function dn(n,t=[P]){return builders.arrowFunctionExpression(t.map(o=>builders.identifier(o)),n)}function V(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(x(n)){$(n.$if,t),$(n.then,t),n.else!==void 0&&$(n.else,t);return}if(R(n)){for(let r of n.$pipe)$(r,t);return}if(S(n)){if(t.add(n.$fn),n.args)for(let r of n.args)$(r,t);return}if(k(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(x(n)){A(n.$if,t),A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(R(n)){for(let r of n.$pipe)A(r,t);return}if(S(n)){if(n.args)for(let r of n.args)A(r,t);return}if(k(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 ot(n){return JSON.stringify(n)}function rt(n,t,o,r,i,s){let e=B(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),u=generate(e),f="";i?s&&(f=`const{${s}}=data??{};`):t.size>0&&(f=`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);const ctx={data,get};":g="const get=(path)=>path.split('.').reduce((o,k)=>o?.[k],data);const ctx={data,get};");let w=l.size>0,F;if(w||d){let G=d?"scope,context":"scope",mn=`${f}${g}return ${u}`;F=`(function(${G}){${a}${p}return function(data){${mn}}})`;}else F=`(function(){return function(data){${f}return ${u}}})`;return F}function H(n,t={}){let{scope:o={},context:r={},returnCode:i=false,useAccessor:s=false,lexicalPrefix:e}=t,u=O(n),f=L(u),l=V(n),a=_(n),p=ot(n),d=rt(n,f,l,a,s,e);if(i)return {code:d,deps:u,hash:p,dataRoots:[...f],scopeFns:[...l],contextFns:[...a]};let g;try{let w=a.size>0,F=new Function(`return ${d}`)();g=w?F(o,r):F(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:u,hash:p}}function gn(n,t,o={}){let{fn:r}=H(n,o);return r(t)}function st(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 ut=new Set(["$","$if","$fn","$pipe","$cb"]);function ft(){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 u=new Set(e),f=[],l={compile:N,genId:i??ft(),scope:r,options:s},a=Q(n,u,o,l,f),p={...r};for(let d=0;d<f.length;d++){let[g,w]=f[d];p[g]=w;}return {expr:a,scope:p}}function Q(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]=Q(n[p],t,o,r,i);return a}let s=n,e=Object.keys(s),u=e.length;for(let l=0;l<u;l++){let a=e[l];if(a[0]==="$"&&!ut.has(a)&&t.has(a)){let p=o[a],d=p(s,r);return d.scopeEntry&&i.push(d.scopeEntry),d.expr}}let f={};for(let l=0;l<u;l++){let a=e[l];f[a]=Q(s[a],t,o,r,i);}return f}var Vt="2.0.0";export{D as ExpressionCache,Vt as VERSION,Dn as assertValid,cn as builders,tn as cache,zn as cached,xn as clearPathCache,N as compile,H as compileAST,j as compilePath,B as dslToAST,Wn as evaluate,gn as evaluateAST,_ as extractContextFns,L as extractDataRoots,O as extractDeps,V as extractScopeFns,An as get,Sn as getPathCacheSize,Rn as hasDeps,Z as hasWildcard,k as isCb,h as isCondition,hn as isConditionExpr,b as isConditionGroup,x as isConditional,S as isFn,J as isLiteral,R as isPipe,kn as isPure,E as isRef,Mn as isValid,st as normalize,z as normalizePath,ct as resolveBoundaries,K as validate,dn as wrapInFunction};
1
+ import {generate,builders}from'omni-ast';var jn=Object.defineProperty;var Nn=(n,e)=>{for(var t in e)jn(n,t,{get:e[t],enumerable:true});};var G=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),g=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,y=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,C=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",T=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),b=n=>n!==null&&typeof n=="object"&&"$arrow"in n,h=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&G.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),E=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,vn=n=>h(n)||E(n),L=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let t=n,i="left"in t&&"op"in t&&G.has(t.op);return !("$"in t)&&!("$if"in t)&&!("$fn"in t)&&!("$pipe"in t)&&!("$arrow"in t)&&!i&&!("logic"in t)}return false};var Z=new Map;function I(n){let e=[],t=n.length,i=0,s="";for(;i<t;){let o=n[i];if(o===".")s&&(e.push({type:"key",value:s}),s=""),i++;else if(o==="["){s&&(e.push({type:"key",value:s}),s=""),i++;let r=i;for(;i<t&&n[i]!=="]";)i++;let f=n.slice(r,i);if(i++,f==="*")e.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);e.push({type:"index",value:isNaN(l)?f:l});}}else s+=o,i++;}return s&&e.push({type:"key",value:s}),e}function P(n){return n.includes("[*]")}function _(n){let e=Z.get(n);return e||(e=P(n)?Dn(n):In(n),Z.set(n,e),e)}function In(n){if(!n.includes(".")&&!n.includes("["))return s=>s?.[n];let e=I(n),t=e.length;if(t===2){let[s,o]=e,r=s.value,f=o.value;return l=>l?.[r]?.[f]}if(t===3){let[s,o,r]=e,f=s.value,l=o.value,u=r.value;return a=>a?.[f]?.[l]?.[u]}let i=e.map(s=>s.value);return s=>{let o=s;for(let r=0;r<t&&o!=null;r++)o=o[i[r]];return o}}function Dn(n){let e=I(n),t=[];for(let i=0;i<e.length;i++)e[i].type==="wildcard"&&t.push(i);return t.length===1?Wn(e,t[0]):Mn(e,t)}function Wn(n,e){let t=n.slice(0,e).map(r=>r.value),i=n.slice(e+1).map(r=>r.value),s=t.length,o=i.length;if(o===0){if(s===1){let r=t[0];return f=>f?.[r]}return r=>{let f=r;for(let l=0;l<s&&f!=null;l++)f=f[t[l]];return f}}if(o===1){let r=i[0];if(s===1){let f=t[0];return l=>{let u=l?.[f];if(Array.isArray(u))return u.map(a=>a?.[r])}}return f=>{let l=f;for(let u=0;u<s&&l!=null;u++)l=l[t[u]];if(Array.isArray(l))return l.map(u=>u?.[r])}}return r=>{let f=r;for(let l=0;l<s&&f!=null;l++)f=f[t[l]];if(Array.isArray(f))return f.map(l=>{let u=l;for(let a=0;a<o&&u!=null;a++)u=u[i[a]];return u})}}function Mn(n,e){let t=[],i=0;for(let o=0;o<e.length;o++){let r=e[o],f=o===e.length-1,l=n.slice(i,r).map(u=>u.value);l.length>0&&t.push({type:"access",keys:l}),t.push({type:f?"map":"flatMap",keys:[]}),i=r+1;}let s=n.slice(i).map(o=>o.value);return o=>{let r=o;for(let f of t){if(r==null)return;if(f.type==="access")for(let l of f.keys){if(r==null)return;r=r[l];}else if(f.type==="flatMap"){if(!Array.isArray(r))return;r=r.flatMap(l=>{let u=l;return Array.isArray(u)?u:[u]});}else if(f.type==="map"){if(!Array.isArray(r))return;s.length>0&&(r=r.map(l=>{let u=l;for(let a of s){if(u==null)return;u=u[a];}return u}));}}return r}}function _n(n,e){return _(e)(n)}function J(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function v(n){let e=new Set;return S(n,e),Array.from(e)}function S(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)S(n[s],e);return}if(g(n)){e.add(J(n.$));return}if(y(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(J(s));}else S(n.$if,e);S(n.then,e),n.else!==void 0&&S(n.else,e);return}if(T(n)){for(let s=0;s<n.$pipe.length;s++)S(n.$pipe[s],e);return}if(C(n)){if(n.args)for(let s=0;s<n.args.length;s++)S(n.args[s],e);return}if(b(n)){let s=new Set;S(n.$arrow,s);let o=new Set(n.args??[]);for(let r of s){let f=r.split(".")[0].split("[")[0];o.has(f)||e.add(r);}return}if(h(n)){S(n.left,e),n.right!==void 0&&S(n.right,e);return}if(E(n)){for(let s=0;s<n.conditions.length;s++)S(n.conditions[s],e);return}let t=n,i=Object.keys(t);for(let s=0;s<i.length;s++)S(t[i[s]],e);}function zn(n){return v(n).length>0}function Hn(n){return v(n).length===0}var nn;function en(n){nn=n;}function F(n,e,t){return nn(n,e,t)}function Gn(n){let e=n.indexOf("."),t=n.indexOf("["),i=n.length;return e!==-1&&(i=Math.min(i,e)),t!==-1&&(i=Math.min(i,t)),n.slice(0,i)}function tn(n,e,t){return t&&t.has(Gn(n))?_(n):e?()=>e(n):_(n)}function on(n,e,t){return tn(n.$,e.accessor,t)}function rn(n,e,t){let i;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=tn(r,e.accessor,t);i=n.$if.startsWith("!")?u=>!f(u):u=>!!f(u);}else {let r=F(n.$if,e,t);i=f=>!!r(f);}let s=F(n.then,e,t),o=n.else!==void 0?F(n.else,e,t):()=>{};return r=>i(r)?s(r):o(r)}function sn(n,e,t){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return F(i[0],e,t);let s=F(i[0],e,t),o=i.slice(1).map(f=>F(f,e,t)),r=o.length;if(r===1){let[f]=o;return l=>{let u=s(l),a=f(l);return typeof a=="function"?a(u):a}}if(r===2){let[f,l]=o;return u=>{let a=s(u),p=f(u);return a=typeof p=="function"?p(a):p,p=l(u),typeof p=="function"?p(a):p}}if(r===3){let[f,l,u]=o;return a=>{let p=s(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=u(a),typeof d=="function"?d(p):d}}return f=>{let l=s(f);for(let u=0;u<r;u++){let a=o[u](f);l=typeof a=="function"?a(l):a;}return l}}function fn(n,e,t){let i=n.$fn,s=i.indexOf(":");if(s!==-1)return Jn(i,s,n.args,e,t);let{scope:o}=e,r=n.args;if(r===void 0)return ()=>{let u=o[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u};let f=r.map(u=>F(u,e,t)),l=f.length;if(l===0)return ()=>{let u=o[i];if(!u)throw new Error(`Function not found in scope: ${i}`);return u()};if(l===1){let[u]=f;return a=>{let p=o[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(u(a))}}if(l===2){let[u,a]=f;return p=>{let d=o[i];if(!d)throw new Error(`Function not found in scope: ${i}`);return d(u(p),a(p))}}if(l===3){let[u,a,p]=f;return d=>{let $=o[i];if(!$)throw new Error(`Function not found in scope: ${i}`);return $(u(d),a(d),p(d))}}return u=>{let a=o[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(...f.map(p=>p(u)))}}function Jn(n,e,t,i,s){let o=n.slice(0,e),r=n.slice(e+1),f=i.handlers?.[o]?.[r];if(!f)throw new Error(`Handler not found: ${n}`);if(t===void 0)return ()=>f;let l=t.map(a=>F(a,i,s)),u=l.length;if(u===0)return ()=>f();if(u===1){let[a]=l;return p=>f(a(p))}if(u===2){let[a,p]=l;return d=>f(a(d),p(d))}if(u===3){let[a,p,d]=l;return $=>f(a($),p($),d($))}return a=>f(...l.map(p=>p(a)))}function cn(n,e,t){let i=n.args??[];if(i.length===0){let r=F(n.$arrow,e,t);return f=>()=>r(f)}let s=new Set(t);for(let r of i)s.add(r);let o=F(n.$arrow,e,s);return r=>(...f)=>{let l={};for(let a=0;a<i.length;a++)l[i[a]]=f[a];let u={...r,...l};return o(u)}}var ln;function un(n){ln=n;}function V(n,e,t){return ln(n,e,t)}function an(n,e,t){let i=V(n.left,e,t),s=n.right!==void 0?V(n.right,e,t):()=>{};switch(n.op){case "eq":return o=>i(o)===s(o);case "neq":return o=>i(o)!==s(o);case "gt":return o=>i(o)>s(o);case "gte":return o=>i(o)>=s(o);case "lt":return o=>i(o)<s(o);case "lte":return o=>i(o)<=s(o);case "in":return o=>{let r=s(o);return Array.isArray(r)&&r.includes(i(o))};case "notIn":return o=>{let r=s(o);return !Array.isArray(r)||!r.includes(i(o))};case "contains":return o=>{let r=i(o);return Array.isArray(r)&&r.includes(s(o))};case "notContains":return o=>{let r=i(o);return !Array.isArray(r)||!r.includes(s(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let r=i(o),f=s(o);return typeof r!="string"||typeof f!="string"?false:new RegExp(f).test(r)};case "notMatches":return o=>{let r=i(o),f=s(o);return typeof r!="string"||typeof f!="string"?true:!new RegExp(f).test(r)};case "startsWith":return o=>{let r=i(o),f=s(o);return typeof r=="string"&&typeof f=="string"&&r.startsWith(f)};case "endsWith":return o=>{let r=i(o),f=s(o);return typeof r=="string"&&typeof f=="string"&&r.endsWith(f)}}}function pn(n,e,t){let i=n.conditions.map(o=>V(o,e,t)),s=i.length;if(s===1)return o=>!!i[0](o);if(s===2){let[o,r]=i;return n.logic==="AND"?f=>!!o(f)&&!!r(f):f=>!!o(f)||!!r(f)}if(s===3){let[o,r,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!r(l)&&!!f(l):l=>!!o(l)||!!r(l)||!!f(l)}return n.logic==="AND"?o=>{for(let r=0;r<s;r++)if(!i[r](o))return false;return true}:o=>{for(let r=0;r<s;r++)if(i[r](o))return true;return false}}function Bn(n){return JSON.stringify(n)}function dn(n,e={}){let t={scope:e.scope??{},accessor:e.accessor,boundaries:e.boundaries,handlers:e.handlers},i=z(n,t),s=v(n),o=Bn(n);return {fn:i,deps:s,hash:o}}function z(n,e,t){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(s=>z(s,e,t));return s=>i.map(o=>o(s))}if(g(n))return on(n,e,t);if(y(n))return rn(n,e,t);if(T(n))return sn(n,e,t);if(C(n))return fn(n,e,t);if(b(n))return cn(n,e,t);if(h(n))return an(n,e,t);if(E(n))return pn(n,e,t);if(e.boundaries?.length){let i=n;for(let s of e.boundaries)if(s.check(i))return s.handle(i)}if(L(n)){let i=n,s=Object.keys(i),o=s.map(r=>z(i[r],e,t));return r=>{let f={};for(let l=0;l<s.length;l++)f[s[l]]=o[l](r);return f}}return ()=>n}en(z);un(z);function mn(n){let e=new Set;return w(n,e),e}function w(n,e){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)w(i,e);return}if(g(n))return;if(y(n)){w(n.$if,e),w(n.then,e),n.else!==void 0&&w(n.else,e);return}if(T(n)){for(let i of n.$pipe)w(i,e);return}if(C(n)){if(n.$fn.includes(":")||e.add(n.$fn),n.args)for(let i of n.args)w(i,e);return}if(b(n)){w(n.$arrow,e);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&w(n.left,e),n.right!==void 0&&typeof n.right=="object"&&w(n.right,e);return}if(E(n)){for(let i of n.conditions)w(i,e);return}let t=n;for(let i of Object.keys(t))w(t[i],e);}function gn(n){return k(n)}function k(n){if(n===null||typeof n!="object")return false;if(Array.isArray(n)){for(let t of n)if(k(t))return true;return false}if(g(n))return false;if(y(n))return k(n.$if)||k(n.then)||n.else!==void 0&&k(n.else);if(T(n)){for(let t of n.$pipe)if(k(t))return true;return false}if(C(n)){if(n.$fn.includes(":"))return true;if(n.args){for(let t of n.args)if(k(t))return true}return false}if(b(n))return k(n.$arrow);if(h(n))return k(n.left)||n.right!==void 0&&k(n.right);if(E(n)){for(let t of n.conditions)if(k(t))return true;return false}let e=n;for(let t of Object.keys(e))if(k(e[t]))return true;return false}function hn(n){let e=new Set;for(let t of n){let i=t.indexOf("."),s=t.indexOf("["),o=t.length;i!==-1&&(o=Math.min(o,i)),s!==-1&&(o=Math.min(o,s));let r=t.slice(0,o);r&&e.add(r);}return e}var Ln="data",Vn="scope",W="__",qn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function yn(n,e={}){let{dataParam:t=Ln,scopeParam:i=Vn,noPrefixes:s=false,useAccessor:o=false,lexicalPrefix:r}=e;return m(n,t,i,s,o,r)}function m(n,e,t,i,s,o,r){if(n===null)return builders.literal(null);if(typeof n=="string")return builders.literal(n);if(typeof n=="number")return builders.literal(n);if(typeof n=="boolean")return builders.literal(n);if(Array.isArray(n))return builders.arrayExpression(n.map(f=>m(f,e,t,i,s,o,r)));if(g(n))return Kn(n.$,e,i,s,o,r);if(y(n))return Un(n,e,t,i,s,o,r);if(T(n))return Pn(n.$pipe,e,t,i,s,o,r);if(C(n))return Xn(n,e,t,i,s,o,r);if(b(n))return Qn(n,e,t,i,s,o,r);if(h(n))return ne(n,e,t,i,s,o,r);if(E(n))return ee(n,e,t,i,s,o,r);if(typeof n=="object"&&"$__b"in n){let f=n.$__b;return builders.callExpression(builders.memberExpression(builders.memberExpression(builders.identifier(W),builders.identifier("b")),builders.literal(f),true,false),[builders.identifier(e)])}if(typeof n=="object"){let l=Object.entries(n).map(([u,a])=>builders.property(builders.identifier(u),m(a,e,t,i,s,o,r)));return builders.objectExpression(l)}return builders.literal(null)}var q="accessor";function K(n,e){return e?n===e||n.startsWith(e+"."):false}function Y(n){let e=n.indexOf("."),t=n.indexOf("["),i=n.length;return e!==-1&&(i=Math.min(i,e)),t!==-1&&(i=Math.min(i,t)),n.slice(0,i)}function Kn(n,e,t,i,s,o){return o&&o.has(Y(n))?O(n,e,true):i?K(n,s)?O(n,e,true):builders.callExpression(builders.identifier(q),[builders.literal(n)]):n.includes("[*]")?Yn(n,e,t):O(n,e,t)}function O(n,e,t){let i=I(n);if(i.length===0)return t?builders.identifier("undefined"):builders.identifier(e);let s;if(t){let o=i[0];s=builders.identifier(o.value);for(let r=1;r<i.length;r++){let f=i[r];f.type==="key"?s=builders.memberExpression(s,builders.identifier(f.value),false,true):s=builders.memberExpression(s,builders.literal(f.value),true,true);}}else {s=builders.identifier(e);for(let o of i)o.type==="key"?s=builders.memberExpression(s,builders.identifier(o.value),false,true):s=builders.memberExpression(s,builders.literal(o.value),true,true);}return s}function Yn(n,e,t){let i=n.indexOf("[*]"),s=n.slice(0,i),o=n.slice(i+3),r;if(s?r=O(s,e,t):r=t?builders.identifier("undefined"):builders.identifier(e),!o||o==="")return r;if(o.includes("[*]"))return Cn(r,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,u=builders.identifier(f);if(l){let a=I(l);for(let p of a)p.type==="key"?u=builders.memberExpression(u,builders.identifier(p.value),false,true):u=builders.memberExpression(u,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(r,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],u)])}function Cn(n,e){let t=e.indexOf("[*]"),i=e.slice(0,t),s=e.slice(t+3),o="_i",r=i.startsWith(".")?i.slice(1):i,f=builders.identifier(o);if(r){let u=I(r);for(let a of u)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}if(s.includes("[*]")){let u=Cn(f,s);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],u)])}let l=s.startsWith(".")?s.slice(1):s;if(l){let u=I(l);for(let a of u)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],f)])}function Qn(n,e,t,i,s,o,r){let f=n.args??[],l=new Set(r);for(let p of f)l.add(p);let u=m(n.$arrow,e,t,i,s,o,l),a=f.map(p=>builders.identifier(p));return builders.arrowFunctionExpression(a,u)}function Un(n,e,t,i,s,o,r){let f;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;r&&r.has(Y(p))?d=O(p,e,true):s?K(p,o)?d=O(p,e,true):d=builders.callExpression(builders.identifier(q),[builders.literal(p)]):d=O(p,e,i),f=a?builders.unaryExpression("!",d):d;}else f=m(n.$if,e,t,i,s,o,r);let l=m(n.then,e,t,i,s,o,r),u=n.else!==void 0?m(n.else,e,t,i,s,o,r):builders.identifier("undefined");return builders.conditionalExpression(f,l,u)}function Xn(n,e,t,i,s,o,r){let f=n.$fn.indexOf(":");if(f!==-1)return Zn(n,f,e,t,i,s,o,r);let l=i?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(t),builders.identifier(n.$fn),false,false);if(n.args===void 0)return l;let u=n.args.map(a=>m(a,e,t,i,s,o,r));return builders.callExpression(l,u)}function Zn(n,e,t,i,s,o,r,f){let l=n.$fn.slice(0,e),u=n.$fn.slice(e+1),a=s?builders.memberExpression(builders.memberExpression(builders.identifier("h"),builders.identifier(l)),builders.identifier(u)):builders.memberExpression(builders.memberExpression(builders.memberExpression(builders.identifier(W),builders.identifier("h")),builders.identifier(l)),builders.identifier(u));if(n.args===void 0)return a;let p=n.args.map(d=>m(d,t,i,s,o,r,f));return builders.callExpression(a,p)}function Pn(n,e,t,i,s,o,r){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return m(n[0],e,t,i,s,o,r);let f=m(n[0],e,t,i,s,o,r);for(let l=1;l<n.length;l++){let u=m(n[l],e,t,i,s,o,r);f=builders.callExpression(u,[f]);}return f}function En(n,e,t,i,s,o,r){if(g(n)){let f=n.$;return r&&r.has(Y(f))?O(f,e,true):s?K(f,o)?O(f,e,true):builders.callExpression(builders.identifier(q),[builders.literal(f)]):O(f,e,i)}return m(n,e,t,i,s,o,r)}function ne(n,e,t,i,s,o,r){let f=En(n.left,e,t,i,s,o,r),l=n.right!==void 0?En(n.right,e,t,i,s,o,r):builders.literal(null),u=qn[n.op];if(u)return builders.binaryExpression(u,f,l);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(l,builders.identifier("includes")),[f]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(l,builders.identifier("includes")),[f]));case "contains":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes"),false,true),[l]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes"),false,true),[l]));case "exists":return builders.binaryExpression("!=",f,builders.literal(null));case "notExists":return builders.binaryExpression("==",f,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[l]),builders.identifier("test")),[f]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[l]),builders.identifier("test")),[f]));case "startsWith":return builders.callExpression(builders.memberExpression(f,builders.identifier("startsWith"),false,true),[l]);case "endsWith":return builders.callExpression(builders.memberExpression(f,builders.identifier("endsWith"),false,true),[l]);default:return builders.binaryExpression("===",f,l)}}function ee(n,e,t,i,s,o,r){let{logic:f,conditions:l}=n,u=f==="AND"?"&&":"||";if(l.length===0)return builders.literal(f==="AND");if(l.length===1)return m(l[0],e,t,i,s,o,r);let a=m(l[0],e,t,i,s,o,r);for(let p=1;p<l.length;p++){let d=m(l[p],e,t,i,s,o,r);a=builders.logicalExpression(u,a,d);}return a}function Tn(n,e,t,i,s,o=0,r=false){let f=yn(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:s}),l=generate(f),u="";i?s&&(u=`const{${s}}=data??{};`):e.size>0&&(u=`const{${[...e].join(",")}}=data??{};`);let a=t.size>0?`const{${[...t].join(",")}}=scope;`:"",p=i||o>0||r,d="";if(p){let x=[];i&&x.push("accessor"),o>0&&x.push("b"),r&&x.push("h"),d=`const{${x.join(",")}}=${W};`;}let $=t.size>0,j;$&&p?j=`scope,${W}`:$?j="scope":p?j=`_,${W}`:j="";let M=`${u}return ${l}`,D;return j?D=`(function(${j}){${a}${d}return function(data){${M}}})`:D=`(function(){return function(data){${M}}})`,D}function ie(n){return JSON.stringify(n)}function A(n,e,t){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>A(o,e,t));if(g(n))return n;if(y(n))return {$if:typeof n.$if=="string"?n.$if:A(n.$if,e,t),then:A(n.then,e,t),...n.else!==void 0?{else:A(n.else,e,t)}:{}};if(T(n))return {$pipe:n.$pipe.map(o=>A(o,e,t))};if(C(n))return {$fn:n.$fn,...n.args!==void 0?{args:n.args.map(o=>A(o,e,t))}:{}};if(b(n))return {$arrow:A(n.$arrow,e,t),...n.args?{args:n.args}:{},...n.schema?{schema:n.schema}:{}};if(h(n))return {left:A(n.left,e,t),op:n.op,...n.right!==void 0?{right:A(n.right,e,t)}:{}};if(E(n))return {logic:n.logic,conditions:n.conditions.map(o=>A(o,e,t))};let i=n;for(let o of e)if(o.check(i)){let r=t.length;return t.push(o.handle(i)),{$__b:r}}let s={};for(let o of Object.keys(i))s[o]=A(i[o],e,t);return s}function bn(n,e={}){let{scope:t={},accessor:i,returnCode:s=false,useAccessor:o=false,lexicalPrefix:r,boundaries:f,handlers:l}=e,u=n,a=[];if(f?.length){let N=[];u=A(n,f,N),a=N;}let p=v(u),d=hn(p),$=mn(u),j=l?gn(u):false,M=ie(n),D=Tn(u,d,$,o,r,a.length,j);if(s)return {code:D,deps:p,hash:M,dataRoots:[...d],scopeFns:[...$]};let x={};o&&i&&(x.accessor=i),a.length>0&&(x.b=a),j&&l&&(x.h=l);let On=Object.keys(x).length>0,X;try{let N=new Function(`return ${D}`)();X=On?N(t,x):N(t);}catch(N){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${N instanceof Error?N.message:String(N)}`)}return {fn:X,deps:p,hash:M}}function $n(n,e){return B(n,e)}function B(n,e){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>B(o,e));let t=n,i=Object.keys(t);for(let o of i)if(o in e){let r=e[o](t);if(typeof r=="object"&&r!==null&&o in r)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return B(r,e)}let s={};for(let o of i)s[o]=B(t[o],e);return s}var H=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}getOrCompile(e,t){let i=JSON.stringify(e),s=this.cache.get(i);if(s)return this.cache.delete(i),this.cache.set(i,s),s;let o=t();if(this.cache.size>=this._maxSize){let r=this.cache.keys().next().value;r&&this.cache.delete(r);}return this.cache.set(i,o),o}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}}};var Q=class{constructor(e={}){this.scope=e.scope??{},this.accessor=e.accessor,this.boundaries=e.boundaries,this.cacheClosures=new H(e.cacheSize??1e3),this.cacheJIT=new H(e.cacheSize??1e3),e.handlers&&(this.wrappedHandlers=this.wrapHandlers(e.handlers));}compile(e){return this.cacheClosures.getOrCompile(e,()=>dn(e,{scope:this.scope,accessor:this.accessor,boundaries:this.boundaries,handlers:this.wrappedHandlers}))}jit(e,t){return this.cacheJIT.getOrCompile(e,()=>bn(e,{scope:this.scope,accessor:this.accessor,useAccessor:t?.useAccessor,lexicalPrefix:t?.lexicalPrefix,boundaries:this.boundaries,handlers:this.wrappedHandlers}))}wrapHandlers(e){let t={},i={};for(let s of Object.keys(e)){i[s]={};for(let o of Object.keys(e[s]))i[s][o]=e[s][o].bind(t);}return t.accessor=this.accessor,t.handlers=i,t.compiler=this,t.scope=this.scope,i}evaluate(e,t){return this.compile(e).fn(t)}evaluateJIT(e,t,i){return this.jit(e,i).fn(t)}normalize(e,t){return $n(e,t)}extractDeps(e){return v(e)}get cacheSize(){return {closures:this.cacheClosures.size,jit:this.cacheJIT.size}}clearCache(){this.cacheClosures.clear(),this.cacheJIT.clear();}getScope(){return this.scope}};var Fn={};Nn(Fn,{$:()=>Sn,$arrow:()=>Rn,$call:()=>xn,$cond:()=>An,$fn:()=>wn,$if:()=>se,$pipe:()=>kn,arrow:()=>le,call:()=>ue,cond:()=>ce,fn:()=>re,pipe:()=>fe,ref:()=>oe});function Sn(n){return {$:n}}var oe=Sn;function wn(n,e){return e===void 0?{$fn:n}:{$fn:n,args:e}}var re=wn;function se(n,e,t){return t===void 0?{$if:n,then:e}:{$if:n,then:e,else:t}}function kn(...n){return {$pipe:n}}var fe=kn;function An(n,e,t){return t===void 0?{left:n,op:e}:{left:n,op:e,right:t}}var ce=An;function Rn(n,e){return e===void 0||e.length===0?{$arrow:n}:{$arrow:n,args:e}}var le=Rn;function xn(n,e=[]){return {$fn:n,args:e}}var ue=xn;function U(n,e="root",t={}){let i=[];return R(n,e,i,t),{valid:i.length===0,errors:i}}function R(n,e,t,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)R(n[r],`${e}[${r}]`,t,i);return}if(g(n)){(!n.$||typeof n.$!="string")&&t.push(`${e}: invalid reference, $ must be non-empty string`);return}if(y(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||t.push(`${e}.$if: empty path in string shorthand`):R(n.$if,`${e}.$if`,t,i),R(n.then,`${e}.then`,t,i),n.else!==void 0&&R(n.else,`${e}.else`,t,i);return}if(T(n)){if(!Array.isArray(n.$pipe)){t.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){t.push(`${e}.$pipe: must have at least one element`);return}for(let r=0;r<n.$pipe.length;r++)R(n.$pipe[r],`${e}.$pipe[${r}]`,t,i);return}if(C(n)){if(!n.$fn||typeof n.$fn!="string"){t.push(`${e}: invalid function, $fn must be non-empty string`);return}let r=n.$fn.indexOf(":");if(r!==-1){let f=n.$fn.slice(0,r),l=n.$fn.slice(r+1);!f||!l||l.includes(":")?t.push(`${e}: invalid handler format "${n.$fn}", expected "namespace:method"`):i.handlers&&!i.handlers[f]?.[l]&&t.push(`${e}: handler "${n.$fn}" not found`);}else i.scope&&!(n.$fn in i.scope)&&t.push(`${e}: function "${n.$fn}" not found in scope`);if(n.args!==void 0)if(!Array.isArray(n.args))t.push(`${e}.args: must be an array`);else for(let f=0;f<n.args.length;f++)R(n.args[f],`${e}.args[${f}]`,t,i);return}if(b(n)){if(R(n.$arrow,`${e}.$arrow`,t,i),n.args!==void 0)if(!Array.isArray(n.args))t.push(`${e}.args: must be an array of strings`);else for(let r of n.args)(typeof r!="string"||!r)&&t.push(`${e}.args: each arg must be a non-empty string`);return}if(h(n)){G.has(n.op)||t.push(`${e}: invalid operator "${n.op}"`),R(n.left,`${e}.left`,t,i),n.right!==void 0&&R(n.right,`${e}.right`,t,i);return}if(E(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&t.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){t.push(`${e}.conditions: must be an array`);return}for(let r=0;r<n.conditions.length;r++)R(n.conditions[r],`${e}.conditions[${r}]`,t,i);return}if(i.boundaries?.length){let r=n;for(let f of i.boundaries)if(f.check(r))return}let s=n,o=Object.keys(s);for(let r=0;r<o.length;r++){let f=o[r];R(s[f],`${e}.${f}`,t,i);}}function ae(n,e={}){let t=U(n,"root",e);if(!t.valid)throw new Error(`Invalid expression: ${t.errors.join("; ")}`)}function pe(n,e={}){return U(n,"root",e).valid}var Xe="3.0.0";export{Q as ExpressionCompiler,Xe as VERSION,ae as assertValid,Fn as builders,_ as compilePath,v as extractDeps,_n as get,zn as hasDeps,P as hasWildcard,b as isArrow,h as isCondition,vn as isConditionExpr,E as isConditionGroup,y as isConditional,C as isFn,L as isLiteral,T as isPipe,Hn as isPure,g as isRef,pe as isValid,J as normalizePath,U as validate};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statedelta-libs/expressions",
3
- "version": "2.3.0",
3
+ "version": "3.1.0",
4
4
  "description": "JSON DSL compiler for optimized functions - StateDelta expression engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -52,7 +52,7 @@
52
52
  "test": "vitest run",
53
53
  "test:watch": "vitest",
54
54
  "test:coverage": "vitest run --coverage",
55
- "typecheck": "tsc --noEmit",
55
+ "typecheck": "tsc --noEmit && eslint src/ tests/",
56
56
  "clean": "rm -rf dist",
57
57
  "format": "prettier --write \"src/**/*.ts\"",
58
58
  "format:check": "prettier --check \"src/**/*.ts\""