@statedelta-libs/expressions 2.0.0 → 2.0.1

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.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var omniAst=require('omni-ast');var rn=Object.defineProperty;var sn=(n,t)=>{for(var s in t)rn(n,s,{get:t[s],enumerable:true});};var y=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,T=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,b=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",C=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),x=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&&x.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),E=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,fn=n=>h(n)||E(n),I=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 s=n,i="left"in s&&"op"in s&&x.has(s.op);return !("$"in s)&&!("$if"in s)&&!("$fn"in s)&&!("$pipe"in s)&&!i&&!("logic"in s)}return false};var R=new Map;function J(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f==="*")t.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function B(n){return n.includes("[*]")}function O(n){let t=R.get(n);return t||(t=B(n)?ln(n):un(n),R.set(n,t),t)}function un(n){if(!n.includes(".")&&!n.includes("["))return r=>r?.[n];let t=J(n),s=t.length;if(s===2){let[r,o]=t,e=r.value,f=o.value;return l=>l?.[e]?.[f]}if(s===3){let[r,o,e]=t,f=r.value,l=o.value,c=e.value;return a=>a?.[f]?.[l]?.[c]}let i=t.map(r=>r.value);return r=>{let o=r;for(let e=0;e<s&&o!=null;e++)o=o[i[e]];return o}}function ln(n){let t=J(n),s=[];for(let i=0;i<t.length;i++)t[i].type==="wildcard"&&s.push(i);return s.length===1?cn(t,s[0]):an(t,s)}function cn(n,t){let s=n.slice(0,t).map(e=>e.value),i=n.slice(t+1).map(e=>e.value),r=s.length,o=i.length;if(o===0){if(r===1){let e=s[0];return f=>f?.[e]}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];return f}}if(o===1){let e=i[0];if(r===1){let f=s[0];return l=>{let c=l?.[f];if(Array.isArray(c))return c.map(a=>a?.[e])}}return f=>{let l=f;for(let c=0;c<r&&l!=null;c++)l=l[s[c]];if(Array.isArray(l))return l.map(c=>c?.[e])}}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];if(Array.isArray(f))return f.map(l=>{let c=l;for(let a=0;a<o&&c!=null;a++)c=c[i[a]];return c})}}function an(n,t){let s=[],i=0;for(let o=0;o<t.length;o++){let e=t[o],f=o===t.length-1,l=n.slice(i,e).map(c=>c.value);l.length>0&&s.push({type:"access",keys:l}),s.push({type:f?"map":"flatMap",keys:[]}),i=e+1;}let r=n.slice(i).map(o=>o.value);return o=>{let e=o;for(let f of s){if(e==null)return;if(f.type==="access")for(let l of f.keys){if(e==null)return;e=e[l];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(l=>{let c=l;return Array.isArray(c)?c:[c]});}else if(f.type==="map"){if(!Array.isArray(e))return;r.length>0&&(e=e.map(l=>{let c=l;for(let a of r){if(c==null)return;c=c[a];}return c}));}}return e}}function pn(n,t){return O(t)(n)}function F(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function dn(){R.clear();}function gn(){return R.size}function w(n){let t=new Set;return A(n,t),Array.from(t)}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)A(n[r],t);return}if(y(n)){t.add(F(n.$));return}if(T(n)){if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(F(r));}else A(n.$if,t);A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(C(n)){for(let r=0;r<n.$pipe.length;r++)A(n.$pipe[r],t);return}if(b(n)){if(n.args)for(let r=0;r<n.args.length;r++)A(n.args[r],t);return}if(h(n)){A(n.left,t),n.right!==void 0&&A(n.right,t);return}if(E(n)){for(let r=0;r<n.conditions.length;r++)A(n.conditions[r],t);return}let s=n,i=Object.keys(s);for(let r=0;r<i.length;r++)A(s[i[r]],t);}function mn(n){return w(n).length>0}function yn(n){return w(n).length===0}function hn(n){return JSON.stringify(n)}function N(n,t={}){let s=t.scope??{},i=t.accessor,r=m(n,s,i),o=w(n),e=hn(n);return {fn:r,deps:o,hash:e}}function m(n,t,s){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(r=>m(r,t,s));return r=>i.map(o=>o(r))}if(y(n))return En(n,s);if(T(n))return Tn(n,t,s);if(C(n))return bn(n,t,s);if(b(n))return Cn(n,t,s);if(h(n))return An(n,t,s);if(E(n))return $n(n,t,s);if(I(n)){let i=n,r=Object.keys(i),o=r.map(e=>m(i[e],t,s));return e=>{let f={};for(let l=0;l<r.length;l++)f[r[l]]=o[l](e);return f}}return ()=>n}function K(n,t){return t?s=>t(n,s):O(n)}function En(n,t){return K(n.$,t)}function Tn(n,t,s){let i;if(typeof n.$if=="string"){let e=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=K(e,s);i=n.$if.startsWith("!")?c=>!f(c):c=>!!f(c);}else {let e=m(n.$if,t,s);i=f=>!!e(f);}let r=m(n.then,t,s),o=n.else!==void 0?m(n.else,t,s):()=>{};return e=>i(e)?r(e):o(e)}function bn(n,t,s){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return m(i[0],t,s);let r=m(i[0],t,s),o=i.slice(1).map(f=>m(f,t,s)),e=o.length;if(e===1){let[f]=o;return l=>{let c=r(l),a=f(l);return typeof a=="function"?a(c):a}}if(e===2){let[f,l]=o;return c=>{let a=r(c),p=f(c);return a=typeof p=="function"?p(a):p,p=l(c),typeof p=="function"?p(a):p}}if(e===3){let[f,l,c]=o;return a=>{let p=r(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=c(a),typeof d=="function"?d(p):d}}return f=>{let l=r(f);for(let c=0;c<e;c++){let a=o[c](f);l=typeof a=="function"?a(l):a;}return l}}function Cn(n,t,s){let i=n.$fn,r=n.args;if(r===void 0)return ()=>{let f=t[i];if(!f)throw new Error(`Function not found in scope: ${i}`);return f};let o=r.map(f=>m(f,t,s)),e=o.length;if(e===0)return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l()};if(e===1){let[f]=o;return l=>{let c=t[i];if(!c)throw new Error(`Function not found in scope: ${i}`);return c(f(l))}}if(e===2){let[f,l]=o;return c=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(f(c),l(c))}}if(e===3){let[f,l,c]=o;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(f(a),l(a),c(a))}}return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...o.map(c=>c(f)))}}function An(n,t,s){let i=m(n.left,t,s),r=n.right!==void 0?m(n.right,t,s):()=>{};switch(n.op){case "eq":return o=>i(o)===r(o);case "neq":return o=>i(o)!==r(o);case "gt":return o=>i(o)>r(o);case "gte":return o=>i(o)>=r(o);case "lt":return o=>i(o)<r(o);case "lte":return o=>i(o)<=r(o);case "in":return o=>{let e=r(o);return Array.isArray(e)&&e.includes(i(o))};case "notIn":return o=>{let e=r(o);return !Array.isArray(e)||!e.includes(i(o))};case "contains":return o=>{let e=i(o);return Array.isArray(e)&&e.includes(r(o))};case "notContains":return o=>{let e=i(o);return !Array.isArray(e)||!e.includes(r(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?false:new RegExp(f).test(e)};case "notMatches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?true:!new RegExp(f).test(e)};case "startsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.startsWith(f)};case "endsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.endsWith(f)}}}function $n(n,t,s){let i=n.conditions.map(o=>m(o,t,s)),r=i.length;if(r===1)return o=>!!i[0](o);if(r===2){let[o,e]=i;return n.logic==="AND"?f=>!!o(f)&&!!e(f):f=>!!o(f)||!!e(f)}if(r===3){let[o,e,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!e(l)&&!!f(l):l=>!!o(l)||!!e(l)||!!f(l)}return n.logic==="AND"?o=>{for(let e=0;e<r;e++)if(!i[e](o))return false;return true}:o=>{for(let e=0;e<r;e++)if(i[e](o))return true;return false}}function Sn(n,t,s={}){return N(n,s).fn(t)}var j=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,s={}){let i=JSON.stringify(t),r=this.cache.get(i);if(r)return this.cache.delete(i),this.cache.set(i,r),r;let o=N(t,s);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(i,o),o}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 s=this.cache.keys().next().value;s&&this.cache.delete(s);}}},P=new j;function wn(n,t={}){return P.get(n,t)}function M(n,t="root",s={}){let i=[];return $(n,t,i,s),{valid:i.length===0,errors:i}}function $(n,t,s,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)$(n[e],`${t}[${e}]`,s,i);return}if(y(n)){(!n.$||typeof n.$!="string")&&s.push(`${t}: invalid reference, $ must be non-empty string`);return}if(T(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||s.push(`${t}.$if: empty path in string shorthand`):$(n.$if,`${t}.$if`,s,i),$(n.then,`${t}.then`,s,i),n.else!==void 0&&$(n.else,`${t}.else`,s,i);return}if(C(n)){if(!Array.isArray(n.$pipe)){s.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){s.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)$(n.$pipe[e],`${t}.$pipe[${e}]`,s,i);return}if(b(n)){if(!n.$fn||typeof n.$fn!="string"){s.push(`${t}: invalid function, $fn must be non-empty string`);return}if(i.scope&&!(n.$fn in i.scope)&&s.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))s.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)$(n.args[e],`${t}.args[${e}]`,s,i);return}if(h(n)){x.has(n.op)||s.push(`${t}: invalid operator "${n.op}"`),$(n.left,`${t}.left`,s,i),n.right!==void 0&&$(n.right,`${t}.right`,s,i);return}if(E(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&s.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){s.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)$(n.conditions[e],`${t}.conditions[${e}]`,s,i);return}let r=n,o=Object.keys(r);for(let e=0;e<o.length;e++){let f=o[e];$(r[f],`${t}.${f}`,s,i);}}function kn(n,t={}){let s=M(n,"root",t);if(!s.valid)throw new Error(`Invalid expression: ${s.errors.join("; ")}`)}function xn(n,t={}){return M(n,"root",t).valid}var X={};sn(X,{$:()=>Y,$cond:()=>U,$fn:()=>H,$if:()=>Fn,$pipe:()=>Q,cond:()=>jn,fn:()=>On,pipe:()=>Nn,ref:()=>Rn});function Y(n){return {$:n}}var Rn=Y;function H(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var On=H;function Fn(n,t,s){return s===void 0?{$if:n,then:t}:{$if:n,then:t,else:s}}function Q(...n){return {$pipe:n}}var Nn=Q;function U(n,t,s){return s===void 0?{left:n,op:t}:{left:n,op:t,right:s}}var jn=U;var L="data",nn="scope",vn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function G(n,t={}){let{dataParam:s=L,scopeParam:i=nn,noPrefixes:r=false,useAccessor:o=false,lexicalPrefix:e}=t;return g(n,s,i,r,o,e)}function g(n,t,s,i,r,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(e=>g(e,t,s,i,r,o)));if(y(n))return Gn(n.$,t,i,r,o);if(T(n))return Dn(n,t,s,i,r,o);if(C(n))return In(n.$pipe,t,s,i,r,o);if(b(n))return zn(n,t,s,i,r,o);if(h(n))return Mn(n,t,s,i,r,o);if(E(n))return Ln(n,t,s,i,r,o);if(typeof n=="object"){let f=Object.entries(n).map(([l,c])=>omniAst.builders.property(omniAst.builders.identifier(l),g(c,t,s,i,r,o)));return omniAst.builders.objectExpression(f)}return omniAst.builders.literal(null)}var V="accessor";function _(n,t){return t?n===t||n.startsWith(t+"."):false}function Gn(n,t,s,i,r){return i?_(n,r)?k(n,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(n),omniAst.builders.identifier(t)]):n.includes("[*]")?Wn(n,t,s):k(n,t,s)}function k(n,t,s){let i=v(n);if(i.length===0)return s?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t);let r;if(s){let o=i[0];r=omniAst.builders.identifier(o.value);for(let e=1;e<i.length;e++){let f=i[e];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);}}else {r=omniAst.builders.identifier(t);for(let o of i)o.type==="key"?r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(o.value),false,true):r=omniAst.builders.memberExpression(r,omniAst.builders.literal(o.value),true,true);}return r}function Wn(n,t,s){let i=n.indexOf("[*]"),r=n.slice(0,i),o=n.slice(i+3),e;if(r?e=k(r,t,s):e=s?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t),!o||o==="")return e;if(o.includes("[*]"))return tn(e,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,c=omniAst.builders.identifier(f);if(l){let a=v(l);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(e,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],c)])}function tn(n,t){let s=t.indexOf("[*]"),i=t.slice(0,s),r=t.slice(s+3),o="_i",e=i.startsWith(".")?i.slice(1):i,f=omniAst.builders.identifier(o);if(e){let c=v(e);for(let a of c)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}if(r.includes("[*]")){let c=tn(f,r);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],c)])}let l=r.startsWith(".")?r.slice(1):r;if(l){let c=v(l);for(let a of c)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],f)])}function Dn(n,t,s,i,r,o){let e;if(typeof n.$if=="string"){let c=n.$if.startsWith("!"),a=c?n.$if.slice(1):n.$if,p;r?_(a,o)?p=k(a,t,true):p=omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(a),omniAst.builders.identifier(t)]):p=k(a,t,i),e=c?omniAst.builders.unaryExpression("!",p):p;}else e=g(n.$if,t,s,i,r,o);let f=g(n.then,t,s,i,r,o),l=n.else!==void 0?g(n.else,t,s,i,r,o):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(e,f,l)}function zn(n,t,s,i,r,o){let e=i?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(s),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(l=>g(l,t,s,i,r,o));return omniAst.builders.callExpression(e,f)}function In(n,t,s,i,r,o){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return g(n[0],t,s,i,r,o);let e=g(n[0],t,s,i,r,o);for(let f=1;f<n.length;f++){let l=g(n[f],t,s,i,r,o);e=omniAst.builders.callExpression(l,[e]);}return e}function Z(n,t,s,i,r,o){if(y(n)){let e=n.$;return r?_(e,o)?k(e,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(e),omniAst.builders.identifier(t)]):k(e,t,i)}return g(n,t,s,i,r,o)}function Mn(n,t,s,i,r,o){let e=Z(n.left,t,s,i,r,o),f=n.right!==void 0?Z(n.right,t,s,i,r,o):omniAst.builders.literal(null),l=vn[n.op];if(l)return omniAst.builders.binaryExpression(l,e,f);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]));case "exists":return omniAst.builders.binaryExpression("!=",e,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",e,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("startsWith"),false,true),[f]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("endsWith"),false,true),[f]);default:return omniAst.builders.binaryExpression("===",e,f)}}function Ln(n,t,s,i,r,o){let{logic:e,conditions:f}=n,l=e==="AND"?"&&":"||";if(f.length===0)return omniAst.builders.literal(e==="AND");if(f.length===1)return g(f[0],t,s,i,r,o);let c=g(f[0],t,s,i,r,o);for(let a=1;a<f.length;a++){let p=g(f[a],t,s,i,r,o);c=omniAst.builders.logicalExpression(l,c,p);}return c}function v(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f!=="*"){let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function en(n,t=[L]){return omniAst.builders.arrowFunctionExpression(t.map(s=>omniAst.builders.identifier(s)),n)}function W(n){let t=new Set;return S(n,t),t}function S(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)S(i,t);return}if(y(n))return;if(T(n)){S(n.$if,t),S(n.then,t),n.else!==void 0&&S(n.else,t);return}if(C(n)){for(let i of n.$pipe)S(i,t);return}if(b(n)){if(t.add(n.$fn),n.args)for(let i of n.args)S(i,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&S(n.left,t),n.right!==void 0&&typeof n.right=="object"&&S(n.right,t);return}if(E(n)){for(let i of n.conditions)S(i,t);return}let s=n;for(let i of Object.keys(s))S(s[i],t);}function D(n){let t=new Set;for(let s of n){let i=s.indexOf("."),r=s.indexOf("["),o=s.length;i!==-1&&(o=Math.min(o,i)),r!==-1&&(o=Math.min(o,r));let e=s.slice(0,o);e&&t.add(e);}return t}function _n(n){return JSON.stringify(n)}function qn(n,t,s,i,r){let o=G(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:r}),e=omniAst.generate(o),f="";i?r&&(f=`const{${r}}=data??{};`):t.size>0&&(f=`const{${[...t].join(",")}}=data??{};`);let l=i?new Set([...s,"accessor"]):s,c=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return c?`(function(scope){${c}return function(data){${f}return ${e}}})`:`(function(){return function(data){${f}return ${e}}})`}function q(n,t={}){let{scope:s={},returnCode:i=false,useAccessor:r=false,lexicalPrefix:o}=t,e=w(n),f=D(e),l=W(n),c=_n(n),a=qn(n,f,l,r,o);if(i)return {code:a,deps:e,hash:c,dataRoots:[...f],scopeFns:[...l]};let p;try{p=new Function(`return ${a}`)()(s);}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:e,hash:c}}function on(n,t,s={}){let{fn:i}=q(n,s);return i(t)}function Jn(n,t){return z(n,t)}function z(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>z(o,t));let s=n,i=Object.keys(s);for(let o of i)if(o in t){let e=t[o](s);if(typeof e=="object"&&e!==null&&o in e)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return z(e,t)}let r={};for(let o of i)r[o]=z(s[o],t);return r}var bt="1.2.2";exports.ExpressionCache=j;exports.VERSION=bt;exports.assertValid=kn;exports.builders=X;exports.cache=P;exports.cached=wn;exports.clearPathCache=dn;exports.compile=N;exports.compileAST=q;exports.compilePath=O;exports.dslToAST=G;exports.evaluate=Sn;exports.evaluateAST=on;exports.extractDataRoots=D;exports.extractDeps=w;exports.extractScopeFns=W;exports.get=pn;exports.getPathCacheSize=gn;exports.hasDeps=mn;exports.hasWildcard=B;exports.isCondition=h;exports.isConditionExpr=fn;exports.isConditionGroup=E;exports.isConditional=T;exports.isFn=b;exports.isLiteral=I;exports.isPipe=C;exports.isPure=yn;exports.isRef=y;exports.isValid=xn;exports.normalize=Jn;exports.normalizePath=F;exports.validate=M;exports.wrapInFunction=en;
1
+ 'use strict';var omniAst=require('omni-ast');var rn=Object.defineProperty;var sn=(n,t)=>{for(var s in t)rn(n,s,{get:t[s],enumerable:true});};var y=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,T=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,b=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",C=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),x=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),E=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&x.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),h=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,fn=n=>E(n)||h(n),I=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 s=n,i="left"in s&&"op"in s&&x.has(s.op);return !("$"in s)&&!("$if"in s)&&!("$fn"in s)&&!("$pipe"in s)&&!i&&!("logic"in s)}return false};var R=new Map;function J(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f==="*")t.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function B(n){return n.includes("[*]")}function O(n){let t=R.get(n);return t||(t=B(n)?ln(n):un(n),R.set(n,t),t)}function un(n){if(!n.includes(".")&&!n.includes("["))return r=>r?.[n];let t=J(n),s=t.length;if(s===2){let[r,o]=t,e=r.value,f=o.value;return l=>l?.[e]?.[f]}if(s===3){let[r,o,e]=t,f=r.value,l=o.value,c=e.value;return a=>a?.[f]?.[l]?.[c]}let i=t.map(r=>r.value);return r=>{let o=r;for(let e=0;e<s&&o!=null;e++)o=o[i[e]];return o}}function ln(n){let t=J(n),s=[];for(let i=0;i<t.length;i++)t[i].type==="wildcard"&&s.push(i);return s.length===1?cn(t,s[0]):an(t,s)}function cn(n,t){let s=n.slice(0,t).map(e=>e.value),i=n.slice(t+1).map(e=>e.value),r=s.length,o=i.length;if(o===0){if(r===1){let e=s[0];return f=>f?.[e]}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];return f}}if(o===1){let e=i[0];if(r===1){let f=s[0];return l=>{let c=l?.[f];if(Array.isArray(c))return c.map(a=>a?.[e])}}return f=>{let l=f;for(let c=0;c<r&&l!=null;c++)l=l[s[c]];if(Array.isArray(l))return l.map(c=>c?.[e])}}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];if(Array.isArray(f))return f.map(l=>{let c=l;for(let a=0;a<o&&c!=null;a++)c=c[i[a]];return c})}}function an(n,t){let s=[],i=0;for(let o=0;o<t.length;o++){let e=t[o],f=o===t.length-1,l=n.slice(i,e).map(c=>c.value);l.length>0&&s.push({type:"access",keys:l}),s.push({type:f?"map":"flatMap",keys:[]}),i=e+1;}let r=n.slice(i).map(o=>o.value);return o=>{let e=o;for(let f of s){if(e==null)return;if(f.type==="access")for(let l of f.keys){if(e==null)return;e=e[l];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(l=>{let c=l;return Array.isArray(c)?c:[c]});}else if(f.type==="map"){if(!Array.isArray(e))return;r.length>0&&(e=e.map(l=>{let c=l;for(let a of r){if(c==null)return;c=c[a];}return c}));}}return e}}function pn(n,t){return O(t)(n)}function F(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function dn(){R.clear();}function gn(){return R.size}function w(n){let t=new Set;return A(n,t),Array.from(t)}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)A(n[r],t);return}if(y(n)){t.add(F(n.$));return}if(T(n)){if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(F(r));}else A(n.$if,t);A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(C(n)){for(let r=0;r<n.$pipe.length;r++)A(n.$pipe[r],t);return}if(b(n)){if(n.args)for(let r=0;r<n.args.length;r++)A(n.args[r],t);return}if(E(n)){A(n.left,t),n.right!==void 0&&A(n.right,t);return}if(h(n)){for(let r=0;r<n.conditions.length;r++)A(n.conditions[r],t);return}let s=n,i=Object.keys(s);for(let r=0;r<i.length;r++)A(s[i[r]],t);}function mn(n){return w(n).length>0}function yn(n){return w(n).length===0}function En(n){return JSON.stringify(n)}function N(n,t={}){let s=t.scope??{},i=t.accessor,r=m(n,s,i),o=w(n),e=En(n);return {fn:r,deps:o,hash:e}}function m(n,t,s){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(r=>m(r,t,s));return r=>i.map(o=>o(r))}if(y(n))return hn(n,s);if(T(n))return Tn(n,t,s);if(C(n))return bn(n,t,s);if(b(n))return Cn(n,t,s);if(E(n))return An(n,t,s);if(h(n))return $n(n,t,s);if(I(n)){let i=n,r=Object.keys(i),o=r.map(e=>m(i[e],t,s));return e=>{let f={};for(let l=0;l<r.length;l++)f[r[l]]=o[l](e);return f}}return ()=>n}function K(n,t){return t?s=>t(n,s):O(n)}function hn(n,t){return K(n.$,t)}function Tn(n,t,s){let i;if(typeof n.$if=="string"){let e=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=K(e,s);i=n.$if.startsWith("!")?c=>!f(c):c=>!!f(c);}else {let e=m(n.$if,t,s);i=f=>!!e(f);}let r=m(n.then,t,s),o=n.else!==void 0?m(n.else,t,s):()=>{};return e=>i(e)?r(e):o(e)}function bn(n,t,s){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return m(i[0],t,s);let r=m(i[0],t,s),o=i.slice(1).map(f=>m(f,t,s)),e=o.length;if(e===1){let[f]=o;return l=>{let c=r(l),a=f(l);return typeof a=="function"?a(c):a}}if(e===2){let[f,l]=o;return c=>{let a=r(c),p=f(c);return a=typeof p=="function"?p(a):p,p=l(c),typeof p=="function"?p(a):p}}if(e===3){let[f,l,c]=o;return a=>{let p=r(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=c(a),typeof d=="function"?d(p):d}}return f=>{let l=r(f);for(let c=0;c<e;c++){let a=o[c](f);l=typeof a=="function"?a(l):a;}return l}}function Cn(n,t,s){let i=n.$fn,r=n.args;if(r===void 0)return ()=>{let f=t[i];if(!f)throw new Error(`Function not found in scope: ${i}`);return f};let o=r.map(f=>m(f,t,s)),e=o.length;if(e===0)return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l()};if(e===1){let[f]=o;return l=>{let c=t[i];if(!c)throw new Error(`Function not found in scope: ${i}`);return c(f(l))}}if(e===2){let[f,l]=o;return c=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(f(c),l(c))}}if(e===3){let[f,l,c]=o;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(f(a),l(a),c(a))}}return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...o.map(c=>c(f)))}}function An(n,t,s){let i=m(n.left,t,s),r=n.right!==void 0?m(n.right,t,s):()=>{};switch(n.op){case "eq":return o=>i(o)===r(o);case "neq":return o=>i(o)!==r(o);case "gt":return o=>i(o)>r(o);case "gte":return o=>i(o)>=r(o);case "lt":return o=>i(o)<r(o);case "lte":return o=>i(o)<=r(o);case "in":return o=>{let e=r(o);return Array.isArray(e)&&e.includes(i(o))};case "notIn":return o=>{let e=r(o);return !Array.isArray(e)||!e.includes(i(o))};case "contains":return o=>{let e=i(o);return Array.isArray(e)&&e.includes(r(o))};case "notContains":return o=>{let e=i(o);return !Array.isArray(e)||!e.includes(r(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?false:new RegExp(f).test(e)};case "notMatches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?true:!new RegExp(f).test(e)};case "startsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.startsWith(f)};case "endsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.endsWith(f)}}}function $n(n,t,s){let i=n.conditions.map(o=>m(o,t,s)),r=i.length;if(r===1)return o=>!!i[0](o);if(r===2){let[o,e]=i;return n.logic==="AND"?f=>!!o(f)&&!!e(f):f=>!!o(f)||!!e(f)}if(r===3){let[o,e,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!e(l)&&!!f(l):l=>!!o(l)||!!e(l)||!!f(l)}return n.logic==="AND"?o=>{for(let e=0;e<r;e++)if(!i[e](o))return false;return true}:o=>{for(let e=0;e<r;e++)if(i[e](o))return true;return false}}function Sn(n,t,s={}){return N(n,s).fn(t)}var j=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,s={}){let i=JSON.stringify(t),r=this.cache.get(i);if(r)return this.cache.delete(i),this.cache.set(i,r),r;let o=N(t,s);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(i,o),o}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 s=this.cache.keys().next().value;s&&this.cache.delete(s);}}},P=new j;function wn(n,t={}){return P.get(n,t)}function M(n,t="root",s={}){let i=[];return $(n,t,i,s),{valid:i.length===0,errors:i}}function $(n,t,s,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)$(n[e],`${t}[${e}]`,s,i);return}if(y(n)){(!n.$||typeof n.$!="string")&&s.push(`${t}: invalid reference, $ must be non-empty string`);return}if(T(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||s.push(`${t}.$if: empty path in string shorthand`):$(n.$if,`${t}.$if`,s,i),$(n.then,`${t}.then`,s,i),n.else!==void 0&&$(n.else,`${t}.else`,s,i);return}if(C(n)){if(!Array.isArray(n.$pipe)){s.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){s.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)$(n.$pipe[e],`${t}.$pipe[${e}]`,s,i);return}if(b(n)){if(!n.$fn||typeof n.$fn!="string"){s.push(`${t}: invalid function, $fn must be non-empty string`);return}if(i.scope&&!(n.$fn in i.scope)&&s.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))s.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)$(n.args[e],`${t}.args[${e}]`,s,i);return}if(E(n)){x.has(n.op)||s.push(`${t}: invalid operator "${n.op}"`),$(n.left,`${t}.left`,s,i),n.right!==void 0&&$(n.right,`${t}.right`,s,i);return}if(h(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&s.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){s.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)$(n.conditions[e],`${t}.conditions[${e}]`,s,i);return}let r=n,o=Object.keys(r);for(let e=0;e<o.length;e++){let f=o[e];$(r[f],`${t}.${f}`,s,i);}}function kn(n,t={}){let s=M(n,"root",t);if(!s.valid)throw new Error(`Invalid expression: ${s.errors.join("; ")}`)}function xn(n,t={}){return M(n,"root",t).valid}var X={};sn(X,{$:()=>Y,$cond:()=>U,$fn:()=>H,$if:()=>Fn,$pipe:()=>Q,cond:()=>jn,fn:()=>On,pipe:()=>Nn,ref:()=>Rn});function Y(n){return {$:n}}var Rn=Y;function H(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var On=H;function Fn(n,t,s){return s===void 0?{$if:n,then:t}:{$if:n,then:t,else:s}}function Q(...n){return {$pipe:n}}var Nn=Q;function U(n,t,s){return s===void 0?{left:n,op:t}:{left:n,op:t,right:s}}var jn=U;var V="data",nn="scope",vn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function G(n,t={}){let{dataParam:s=V,scopeParam:i=nn,noPrefixes:r=false,useAccessor:o=false,lexicalPrefix:e}=t;return g(n,s,i,r,o,e)}function g(n,t,s,i,r,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(e=>g(e,t,s,i,r,o)));if(y(n))return Gn(n.$,t,i,r,o);if(T(n))return Dn(n,t,s,i,r,o);if(C(n))return In(n.$pipe,t,s,i,r,o);if(b(n))return zn(n,t,s,i,r,o);if(E(n))return Mn(n,t,s,i,r,o);if(h(n))return Vn(n,t,s,i,r,o);if(typeof n=="object"){let f=Object.entries(n).map(([l,c])=>omniAst.builders.property(omniAst.builders.identifier(l),g(c,t,s,i,r,o)));return omniAst.builders.objectExpression(f)}return omniAst.builders.literal(null)}var _="accessor";function L(n,t){return t?n===t||n.startsWith(t+"."):false}function Gn(n,t,s,i,r){return i?L(n,r)?k(n,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(_),[omniAst.builders.literal(n),omniAst.builders.identifier(t)]):n.includes("[*]")?Wn(n,t,s):k(n,t,s)}function k(n,t,s){let i=v(n);if(i.length===0)return s?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t);let r;if(s){let o=i[0];r=omniAst.builders.identifier(o.value);for(let e=1;e<i.length;e++){let f=i[e];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);}}else {r=omniAst.builders.identifier(t);for(let o of i)o.type==="key"?r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(o.value),false,true):r=omniAst.builders.memberExpression(r,omniAst.builders.literal(o.value),true,true);}return r}function Wn(n,t,s){let i=n.indexOf("[*]"),r=n.slice(0,i),o=n.slice(i+3),e;if(r?e=k(r,t,s):e=s?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(t),!o||o==="")return e;if(o.includes("[*]"))return tn(e,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,c=omniAst.builders.identifier(f);if(l){let a=v(l);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(e,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(f)],c)])}function tn(n,t){let s=t.indexOf("[*]"),i=t.slice(0,s),r=t.slice(s+3),o="_i",e=i.startsWith(".")?i.slice(1):i,f=omniAst.builders.identifier(o);if(e){let c=v(e);for(let a of c)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}if(r.includes("[*]")){let c=tn(f,r);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],c)])}let l=r.startsWith(".")?r.slice(1):r;if(l){let c=v(l);for(let a of c)a.type==="key"&&(f=omniAst.builders.memberExpression(f,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(o)],f)])}function Dn(n,t,s,i,r,o){let e;if(typeof n.$if=="string"){let c=n.$if.startsWith("!"),a=c?n.$if.slice(1):n.$if,p;r?L(a,o)?p=k(a,t,true):p=omniAst.builders.callExpression(omniAst.builders.identifier(_),[omniAst.builders.literal(a),omniAst.builders.identifier(t)]):p=k(a,t,i),e=c?omniAst.builders.unaryExpression("!",p):p;}else e=g(n.$if,t,s,i,r,o);let f=g(n.then,t,s,i,r,o),l=n.else!==void 0?g(n.else,t,s,i,r,o):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(e,f,l)}function zn(n,t,s,i,r,o){let e=i?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(s),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(l=>g(l,t,s,i,r,o));return omniAst.builders.callExpression(e,f)}function In(n,t,s,i,r,o){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return g(n[0],t,s,i,r,o);let e=g(n[0],t,s,i,r,o);for(let f=1;f<n.length;f++){let l=g(n[f],t,s,i,r,o);e=omniAst.builders.callExpression(l,[e]);}return e}function Z(n,t,s,i,r,o){if(y(n)){let e=n.$;return r?L(e,o)?k(e,t,true):omniAst.builders.callExpression(omniAst.builders.identifier(_),[omniAst.builders.literal(e),omniAst.builders.identifier(t)]):k(e,t,i)}return g(n,t,s,i,r,o)}function Mn(n,t,s,i,r,o){let e=Z(n.left,t,s,i,r,o),f=n.right!==void 0?Z(n.right,t,s,i,r,o):omniAst.builders.literal(null),l=vn[n.op];if(l)return omniAst.builders.binaryExpression(l,e,f);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(f,omniAst.builders.identifier("includes")),[e]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("includes"),false,true),[f]));case "exists":return omniAst.builders.binaryExpression("!=",e,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",e,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[f]),omniAst.builders.identifier("test")),[e]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("startsWith"),false,true),[f]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(e,omniAst.builders.identifier("endsWith"),false,true),[f]);default:return omniAst.builders.binaryExpression("===",e,f)}}function Vn(n,t,s,i,r,o){let{logic:e,conditions:f}=n,l=e==="AND"?"&&":"||";if(f.length===0)return omniAst.builders.literal(e==="AND");if(f.length===1)return g(f[0],t,s,i,r,o);let c=g(f[0],t,s,i,r,o);for(let a=1;a<f.length;a++){let p=g(f[a],t,s,i,r,o);c=omniAst.builders.logicalExpression(l,c,p);}return c}function v(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f!=="*"){let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function en(n,t=[V]){return omniAst.builders.arrowFunctionExpression(t.map(s=>omniAst.builders.identifier(s)),n)}function W(n){let t=new Set;return S(n,t),t}function S(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)S(i,t);return}if(y(n))return;if(T(n)){S(n.$if,t),S(n.then,t),n.else!==void 0&&S(n.else,t);return}if(C(n)){for(let i of n.$pipe)S(i,t);return}if(b(n)){if(t.add(n.$fn),n.args)for(let i of n.args)S(i,t);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&S(n.left,t),n.right!==void 0&&typeof n.right=="object"&&S(n.right,t);return}if(h(n)){for(let i of n.conditions)S(i,t);return}let s=n;for(let i of Object.keys(s))S(s[i],t);}function D(n){let t=new Set;for(let s of n){let i=s.indexOf("."),r=s.indexOf("["),o=s.length;i!==-1&&(o=Math.min(o,i)),r!==-1&&(o=Math.min(o,r));let e=s.slice(0,o);e&&t.add(e);}return t}function Ln(n){return JSON.stringify(n)}function qn(n,t,s,i,r){let o=G(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:r}),e=omniAst.generate(o),f="";i?r&&(f=`const{${r}}=data??{};`):t.size>0&&(f=`const{${[...t].join(",")}}=data??{};`);let l=i?new Set([...s,"accessor"]):s,c=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return c?`(function(scope){${c}return function(data){${f}return ${e}}})`:`(function(){return function(data){${f}return ${e}}})`}function q(n,t={}){let{scope:s={},returnCode:i=false,useAccessor:r=false,lexicalPrefix:o}=t,e=w(n),f=D(e),l=W(n),c=Ln(n),a=qn(n,f,l,r,o);if(i)return {code:a,deps:e,hash:c,dataRoots:[...f],scopeFns:[...l]};let p;try{p=new Function(`return ${a}`)()(s);}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:e,hash:c}}function on(n,t,s={}){let{fn:i}=q(n,s);return i(t)}function Jn(n,t){return z(n,t)}function z(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>z(o,t));let s=n,i=Object.keys(s);for(let o of i)if(o in t){let e=t[o](s);if(typeof e=="object"&&e!==null&&o in e)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return z(e,t)}let r={};for(let o of i)r[o]=z(s[o],t);return r}var bt="2.0.0";exports.ExpressionCache=j;exports.VERSION=bt;exports.assertValid=kn;exports.builders=X;exports.cache=P;exports.cached=wn;exports.clearPathCache=dn;exports.compile=N;exports.compileAST=q;exports.compilePath=O;exports.dslToAST=G;exports.evaluate=Sn;exports.evaluateAST=on;exports.extractDataRoots=D;exports.extractDeps=w;exports.extractScopeFns=W;exports.get=pn;exports.getPathCacheSize=gn;exports.hasDeps=mn;exports.hasWildcard=B;exports.isCondition=E;exports.isConditionExpr=fn;exports.isConditionGroup=h;exports.isConditional=T;exports.isFn=b;exports.isLiteral=I;exports.isPipe=C;exports.isPure=yn;exports.isRef=y;exports.isValid=xn;exports.normalize=Jn;exports.normalizePath=F;exports.validate=M;exports.wrapInFunction=en;
package/dist/index.d.cts CHANGED
@@ -47,8 +47,8 @@ interface PipeExpr {
47
47
  /**
48
48
  * Literal values (primitives, arrays, plain objects)
49
49
  */
50
- type Literal = string | number | boolean | null | Literal[] | {
51
- [key: string]: Literal;
50
+ type Literal = string | number | boolean | null | Expression[] | {
51
+ [key: string]: Expression;
52
52
  };
53
53
  /**
54
54
  * Condition expression - both sides accept any Expression
@@ -779,6 +779,6 @@ declare function normalize(expr: unknown, transforms: Transforms): Expression;
779
779
  * ); // 3
780
780
  * ```
781
781
  */
782
- declare const VERSION = "1.2.2";
782
+ declare const VERSION = "2.0.0";
783
783
 
784
784
  export { type AccessorFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCache, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalize, normalizePath, validate, wrapInFunction };
package/dist/index.d.ts CHANGED
@@ -47,8 +47,8 @@ interface PipeExpr {
47
47
  /**
48
48
  * Literal values (primitives, arrays, plain objects)
49
49
  */
50
- type Literal = string | number | boolean | null | Literal[] | {
51
- [key: string]: Literal;
50
+ type Literal = string | number | boolean | null | Expression[] | {
51
+ [key: string]: Expression;
52
52
  };
53
53
  /**
54
54
  * Condition expression - both sides accept any Expression
@@ -779,6 +779,6 @@ declare function normalize(expr: unknown, transforms: Transforms): Expression;
779
779
  * ); // 3
780
780
  * ```
781
781
  */
782
- declare const VERSION = "1.2.2";
782
+ declare const VERSION = "2.0.0";
783
783
 
784
784
  export { type AccessorFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCache, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, type Transforms, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalize, normalizePath, validate, wrapInFunction };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {builders,generate}from'omni-ast';var rn=Object.defineProperty;var sn=(n,t)=>{for(var s in t)rn(n,s,{get:t[s],enumerable:true});};var y=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,T=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,b=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",C=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),x=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&&x.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),E=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,fn=n=>h(n)||E(n),I=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 s=n,i="left"in s&&"op"in s&&x.has(s.op);return !("$"in s)&&!("$if"in s)&&!("$fn"in s)&&!("$pipe"in s)&&!i&&!("logic"in s)}return false};var R=new Map;function J(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f==="*")t.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function B(n){return n.includes("[*]")}function O(n){let t=R.get(n);return t||(t=B(n)?ln(n):un(n),R.set(n,t),t)}function un(n){if(!n.includes(".")&&!n.includes("["))return r=>r?.[n];let t=J(n),s=t.length;if(s===2){let[r,o]=t,e=r.value,f=o.value;return l=>l?.[e]?.[f]}if(s===3){let[r,o,e]=t,f=r.value,l=o.value,c=e.value;return a=>a?.[f]?.[l]?.[c]}let i=t.map(r=>r.value);return r=>{let o=r;for(let e=0;e<s&&o!=null;e++)o=o[i[e]];return o}}function ln(n){let t=J(n),s=[];for(let i=0;i<t.length;i++)t[i].type==="wildcard"&&s.push(i);return s.length===1?cn(t,s[0]):an(t,s)}function cn(n,t){let s=n.slice(0,t).map(e=>e.value),i=n.slice(t+1).map(e=>e.value),r=s.length,o=i.length;if(o===0){if(r===1){let e=s[0];return f=>f?.[e]}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];return f}}if(o===1){let e=i[0];if(r===1){let f=s[0];return l=>{let c=l?.[f];if(Array.isArray(c))return c.map(a=>a?.[e])}}return f=>{let l=f;for(let c=0;c<r&&l!=null;c++)l=l[s[c]];if(Array.isArray(l))return l.map(c=>c?.[e])}}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];if(Array.isArray(f))return f.map(l=>{let c=l;for(let a=0;a<o&&c!=null;a++)c=c[i[a]];return c})}}function an(n,t){let s=[],i=0;for(let o=0;o<t.length;o++){let e=t[o],f=o===t.length-1,l=n.slice(i,e).map(c=>c.value);l.length>0&&s.push({type:"access",keys:l}),s.push({type:f?"map":"flatMap",keys:[]}),i=e+1;}let r=n.slice(i).map(o=>o.value);return o=>{let e=o;for(let f of s){if(e==null)return;if(f.type==="access")for(let l of f.keys){if(e==null)return;e=e[l];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(l=>{let c=l;return Array.isArray(c)?c:[c]});}else if(f.type==="map"){if(!Array.isArray(e))return;r.length>0&&(e=e.map(l=>{let c=l;for(let a of r){if(c==null)return;c=c[a];}return c}));}}return e}}function pn(n,t){return O(t)(n)}function F(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function dn(){R.clear();}function gn(){return R.size}function w(n){let t=new Set;return A(n,t),Array.from(t)}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)A(n[r],t);return}if(y(n)){t.add(F(n.$));return}if(T(n)){if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(F(r));}else A(n.$if,t);A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(C(n)){for(let r=0;r<n.$pipe.length;r++)A(n.$pipe[r],t);return}if(b(n)){if(n.args)for(let r=0;r<n.args.length;r++)A(n.args[r],t);return}if(h(n)){A(n.left,t),n.right!==void 0&&A(n.right,t);return}if(E(n)){for(let r=0;r<n.conditions.length;r++)A(n.conditions[r],t);return}let s=n,i=Object.keys(s);for(let r=0;r<i.length;r++)A(s[i[r]],t);}function mn(n){return w(n).length>0}function yn(n){return w(n).length===0}function hn(n){return JSON.stringify(n)}function N(n,t={}){let s=t.scope??{},i=t.accessor,r=m(n,s,i),o=w(n),e=hn(n);return {fn:r,deps:o,hash:e}}function m(n,t,s){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(r=>m(r,t,s));return r=>i.map(o=>o(r))}if(y(n))return En(n,s);if(T(n))return Tn(n,t,s);if(C(n))return bn(n,t,s);if(b(n))return Cn(n,t,s);if(h(n))return An(n,t,s);if(E(n))return $n(n,t,s);if(I(n)){let i=n,r=Object.keys(i),o=r.map(e=>m(i[e],t,s));return e=>{let f={};for(let l=0;l<r.length;l++)f[r[l]]=o[l](e);return f}}return ()=>n}function K(n,t){return t?s=>t(n,s):O(n)}function En(n,t){return K(n.$,t)}function Tn(n,t,s){let i;if(typeof n.$if=="string"){let e=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=K(e,s);i=n.$if.startsWith("!")?c=>!f(c):c=>!!f(c);}else {let e=m(n.$if,t,s);i=f=>!!e(f);}let r=m(n.then,t,s),o=n.else!==void 0?m(n.else,t,s):()=>{};return e=>i(e)?r(e):o(e)}function bn(n,t,s){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return m(i[0],t,s);let r=m(i[0],t,s),o=i.slice(1).map(f=>m(f,t,s)),e=o.length;if(e===1){let[f]=o;return l=>{let c=r(l),a=f(l);return typeof a=="function"?a(c):a}}if(e===2){let[f,l]=o;return c=>{let a=r(c),p=f(c);return a=typeof p=="function"?p(a):p,p=l(c),typeof p=="function"?p(a):p}}if(e===3){let[f,l,c]=o;return a=>{let p=r(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=c(a),typeof d=="function"?d(p):d}}return f=>{let l=r(f);for(let c=0;c<e;c++){let a=o[c](f);l=typeof a=="function"?a(l):a;}return l}}function Cn(n,t,s){let i=n.$fn,r=n.args;if(r===void 0)return ()=>{let f=t[i];if(!f)throw new Error(`Function not found in scope: ${i}`);return f};let o=r.map(f=>m(f,t,s)),e=o.length;if(e===0)return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l()};if(e===1){let[f]=o;return l=>{let c=t[i];if(!c)throw new Error(`Function not found in scope: ${i}`);return c(f(l))}}if(e===2){let[f,l]=o;return c=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(f(c),l(c))}}if(e===3){let[f,l,c]=o;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(f(a),l(a),c(a))}}return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...o.map(c=>c(f)))}}function An(n,t,s){let i=m(n.left,t,s),r=n.right!==void 0?m(n.right,t,s):()=>{};switch(n.op){case "eq":return o=>i(o)===r(o);case "neq":return o=>i(o)!==r(o);case "gt":return o=>i(o)>r(o);case "gte":return o=>i(o)>=r(o);case "lt":return o=>i(o)<r(o);case "lte":return o=>i(o)<=r(o);case "in":return o=>{let e=r(o);return Array.isArray(e)&&e.includes(i(o))};case "notIn":return o=>{let e=r(o);return !Array.isArray(e)||!e.includes(i(o))};case "contains":return o=>{let e=i(o);return Array.isArray(e)&&e.includes(r(o))};case "notContains":return o=>{let e=i(o);return !Array.isArray(e)||!e.includes(r(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?false:new RegExp(f).test(e)};case "notMatches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?true:!new RegExp(f).test(e)};case "startsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.startsWith(f)};case "endsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.endsWith(f)}}}function $n(n,t,s){let i=n.conditions.map(o=>m(o,t,s)),r=i.length;if(r===1)return o=>!!i[0](o);if(r===2){let[o,e]=i;return n.logic==="AND"?f=>!!o(f)&&!!e(f):f=>!!o(f)||!!e(f)}if(r===3){let[o,e,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!e(l)&&!!f(l):l=>!!o(l)||!!e(l)||!!f(l)}return n.logic==="AND"?o=>{for(let e=0;e<r;e++)if(!i[e](o))return false;return true}:o=>{for(let e=0;e<r;e++)if(i[e](o))return true;return false}}function Sn(n,t,s={}){return N(n,s).fn(t)}var j=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,s={}){let i=JSON.stringify(t),r=this.cache.get(i);if(r)return this.cache.delete(i),this.cache.set(i,r),r;let o=N(t,s);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(i,o),o}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 s=this.cache.keys().next().value;s&&this.cache.delete(s);}}},P=new j;function wn(n,t={}){return P.get(n,t)}function M(n,t="root",s={}){let i=[];return $(n,t,i,s),{valid:i.length===0,errors:i}}function $(n,t,s,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)$(n[e],`${t}[${e}]`,s,i);return}if(y(n)){(!n.$||typeof n.$!="string")&&s.push(`${t}: invalid reference, $ must be non-empty string`);return}if(T(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||s.push(`${t}.$if: empty path in string shorthand`):$(n.$if,`${t}.$if`,s,i),$(n.then,`${t}.then`,s,i),n.else!==void 0&&$(n.else,`${t}.else`,s,i);return}if(C(n)){if(!Array.isArray(n.$pipe)){s.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){s.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)$(n.$pipe[e],`${t}.$pipe[${e}]`,s,i);return}if(b(n)){if(!n.$fn||typeof n.$fn!="string"){s.push(`${t}: invalid function, $fn must be non-empty string`);return}if(i.scope&&!(n.$fn in i.scope)&&s.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))s.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)$(n.args[e],`${t}.args[${e}]`,s,i);return}if(h(n)){x.has(n.op)||s.push(`${t}: invalid operator "${n.op}"`),$(n.left,`${t}.left`,s,i),n.right!==void 0&&$(n.right,`${t}.right`,s,i);return}if(E(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&s.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){s.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)$(n.conditions[e],`${t}.conditions[${e}]`,s,i);return}let r=n,o=Object.keys(r);for(let e=0;e<o.length;e++){let f=o[e];$(r[f],`${t}.${f}`,s,i);}}function kn(n,t={}){let s=M(n,"root",t);if(!s.valid)throw new Error(`Invalid expression: ${s.errors.join("; ")}`)}function xn(n,t={}){return M(n,"root",t).valid}var X={};sn(X,{$:()=>Y,$cond:()=>U,$fn:()=>H,$if:()=>Fn,$pipe:()=>Q,cond:()=>jn,fn:()=>On,pipe:()=>Nn,ref:()=>Rn});function Y(n){return {$:n}}var Rn=Y;function H(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var On=H;function Fn(n,t,s){return s===void 0?{$if:n,then:t}:{$if:n,then:t,else:s}}function Q(...n){return {$pipe:n}}var Nn=Q;function U(n,t,s){return s===void 0?{left:n,op:t}:{left:n,op:t,right:s}}var jn=U;var L="data",nn="scope",vn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function G(n,t={}){let{dataParam:s=L,scopeParam:i=nn,noPrefixes:r=false,useAccessor:o=false,lexicalPrefix:e}=t;return g(n,s,i,r,o,e)}function g(n,t,s,i,r,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(e=>g(e,t,s,i,r,o)));if(y(n))return Gn(n.$,t,i,r,o);if(T(n))return Dn(n,t,s,i,r,o);if(C(n))return In(n.$pipe,t,s,i,r,o);if(b(n))return zn(n,t,s,i,r,o);if(h(n))return Mn(n,t,s,i,r,o);if(E(n))return Ln(n,t,s,i,r,o);if(typeof n=="object"){let f=Object.entries(n).map(([l,c])=>builders.property(builders.identifier(l),g(c,t,s,i,r,o)));return builders.objectExpression(f)}return builders.literal(null)}var V="accessor";function _(n,t){return t?n===t||n.startsWith(t+"."):false}function Gn(n,t,s,i,r){return i?_(n,r)?k(n,t,true):builders.callExpression(builders.identifier(V),[builders.literal(n),builders.identifier(t)]):n.includes("[*]")?Wn(n,t,s):k(n,t,s)}function k(n,t,s){let i=v(n);if(i.length===0)return s?builders.identifier("undefined"):builders.identifier(t);let r;if(s){let o=i[0];r=builders.identifier(o.value);for(let e=1;e<i.length;e++){let f=i[e];f.type==="key"?r=builders.memberExpression(r,builders.identifier(f.value),false,true):r=builders.memberExpression(r,builders.literal(f.value),true,true);}}else {r=builders.identifier(t);for(let o of i)o.type==="key"?r=builders.memberExpression(r,builders.identifier(o.value),false,true):r=builders.memberExpression(r,builders.literal(o.value),true,true);}return r}function Wn(n,t,s){let i=n.indexOf("[*]"),r=n.slice(0,i),o=n.slice(i+3),e;if(r?e=k(r,t,s):e=s?builders.identifier("undefined"):builders.identifier(t),!o||o==="")return e;if(o.includes("[*]"))return tn(e,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,c=builders.identifier(f);if(l){let a=v(l);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(e,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],c)])}function tn(n,t){let s=t.indexOf("[*]"),i=t.slice(0,s),r=t.slice(s+3),o="_i",e=i.startsWith(".")?i.slice(1):i,f=builders.identifier(o);if(e){let c=v(e);for(let a of c)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}if(r.includes("[*]")){let c=tn(f,r);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],c)])}let l=r.startsWith(".")?r.slice(1):r;if(l){let c=v(l);for(let a of c)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 Dn(n,t,s,i,r,o){let e;if(typeof n.$if=="string"){let c=n.$if.startsWith("!"),a=c?n.$if.slice(1):n.$if,p;r?_(a,o)?p=k(a,t,true):p=builders.callExpression(builders.identifier(V),[builders.literal(a),builders.identifier(t)]):p=k(a,t,i),e=c?builders.unaryExpression("!",p):p;}else e=g(n.$if,t,s,i,r,o);let f=g(n.then,t,s,i,r,o),l=n.else!==void 0?g(n.else,t,s,i,r,o):builders.identifier("undefined");return builders.conditionalExpression(e,f,l)}function zn(n,t,s,i,r,o){let e=i?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(s),builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(l=>g(l,t,s,i,r,o));return builders.callExpression(e,f)}function In(n,t,s,i,r,o){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return g(n[0],t,s,i,r,o);let e=g(n[0],t,s,i,r,o);for(let f=1;f<n.length;f++){let l=g(n[f],t,s,i,r,o);e=builders.callExpression(l,[e]);}return e}function Z(n,t,s,i,r,o){if(y(n)){let e=n.$;return r?_(e,o)?k(e,t,true):builders.callExpression(builders.identifier(V),[builders.literal(e),builders.identifier(t)]):k(e,t,i)}return g(n,t,s,i,r,o)}function Mn(n,t,s,i,r,o){let e=Z(n.left,t,s,i,r,o),f=n.right!==void 0?Z(n.right,t,s,i,r,o):builders.literal(null),l=vn[n.op];if(l)return builders.binaryExpression(l,e,f);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]));case "contains":return builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]));case "exists":return builders.binaryExpression("!=",e,builders.literal(null));case "notExists":return builders.binaryExpression("==",e,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]));case "startsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("startsWith"),false,true),[f]);case "endsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("endsWith"),false,true),[f]);default:return builders.binaryExpression("===",e,f)}}function Ln(n,t,s,i,r,o){let{logic:e,conditions:f}=n,l=e==="AND"?"&&":"||";if(f.length===0)return builders.literal(e==="AND");if(f.length===1)return g(f[0],t,s,i,r,o);let c=g(f[0],t,s,i,r,o);for(let a=1;a<f.length;a++){let p=g(f[a],t,s,i,r,o);c=builders.logicalExpression(l,c,p);}return c}function v(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f!=="*"){let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function en(n,t=[L]){return builders.arrowFunctionExpression(t.map(s=>builders.identifier(s)),n)}function W(n){let t=new Set;return S(n,t),t}function S(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)S(i,t);return}if(y(n))return;if(T(n)){S(n.$if,t),S(n.then,t),n.else!==void 0&&S(n.else,t);return}if(C(n)){for(let i of n.$pipe)S(i,t);return}if(b(n)){if(t.add(n.$fn),n.args)for(let i of n.args)S(i,t);return}if(h(n)){n.left!==void 0&&typeof n.left=="object"&&S(n.left,t),n.right!==void 0&&typeof n.right=="object"&&S(n.right,t);return}if(E(n)){for(let i of n.conditions)S(i,t);return}let s=n;for(let i of Object.keys(s))S(s[i],t);}function D(n){let t=new Set;for(let s of n){let i=s.indexOf("."),r=s.indexOf("["),o=s.length;i!==-1&&(o=Math.min(o,i)),r!==-1&&(o=Math.min(o,r));let e=s.slice(0,o);e&&t.add(e);}return t}function _n(n){return JSON.stringify(n)}function qn(n,t,s,i,r){let o=G(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:r}),e=generate(o),f="";i?r&&(f=`const{${r}}=data??{};`):t.size>0&&(f=`const{${[...t].join(",")}}=data??{};`);let l=i?new Set([...s,"accessor"]):s,c=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return c?`(function(scope){${c}return function(data){${f}return ${e}}})`:`(function(){return function(data){${f}return ${e}}})`}function q(n,t={}){let{scope:s={},returnCode:i=false,useAccessor:r=false,lexicalPrefix:o}=t,e=w(n),f=D(e),l=W(n),c=_n(n),a=qn(n,f,l,r,o);if(i)return {code:a,deps:e,hash:c,dataRoots:[...f],scopeFns:[...l]};let p;try{p=new Function(`return ${a}`)()(s);}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:e,hash:c}}function on(n,t,s={}){let{fn:i}=q(n,s);return i(t)}function Jn(n,t){return z(n,t)}function z(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>z(o,t));let s=n,i=Object.keys(s);for(let o of i)if(o in t){let e=t[o](s);if(typeof e=="object"&&e!==null&&o in e)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return z(e,t)}let r={};for(let o of i)r[o]=z(s[o],t);return r}var bt="1.2.2";export{j as ExpressionCache,bt as VERSION,kn as assertValid,X as builders,P as cache,wn as cached,dn as clearPathCache,N as compile,q as compileAST,O as compilePath,G as dslToAST,Sn as evaluate,on as evaluateAST,D as extractDataRoots,w as extractDeps,W as extractScopeFns,pn as get,gn as getPathCacheSize,mn as hasDeps,B as hasWildcard,h as isCondition,fn as isConditionExpr,E as isConditionGroup,T as isConditional,b as isFn,I as isLiteral,C as isPipe,yn as isPure,y as isRef,xn as isValid,Jn as normalize,F as normalizePath,M as validate,en as wrapInFunction};
1
+ import {builders,generate}from'omni-ast';var rn=Object.defineProperty;var sn=(n,t)=>{for(var s in t)rn(n,s,{get:t[s],enumerable:true});};var y=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,T=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,b=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",C=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),x=new Set(["eq","neq","gt","gte","lt","lte","in","notIn","contains","notContains","exists","notExists","matches","notMatches","startsWith","endsWith"]),E=n=>n!==null&&typeof n=="object"&&"left"in n&&"op"in n&&x.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),h=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,fn=n=>E(n)||h(n),I=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 s=n,i="left"in s&&"op"in s&&x.has(s.op);return !("$"in s)&&!("$if"in s)&&!("$fn"in s)&&!("$pipe"in s)&&!i&&!("logic"in s)}return false};var R=new Map;function J(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f==="*")t.push({type:"wildcard",value:"*"});else {let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function B(n){return n.includes("[*]")}function O(n){let t=R.get(n);return t||(t=B(n)?ln(n):un(n),R.set(n,t),t)}function un(n){if(!n.includes(".")&&!n.includes("["))return r=>r?.[n];let t=J(n),s=t.length;if(s===2){let[r,o]=t,e=r.value,f=o.value;return l=>l?.[e]?.[f]}if(s===3){let[r,o,e]=t,f=r.value,l=o.value,c=e.value;return a=>a?.[f]?.[l]?.[c]}let i=t.map(r=>r.value);return r=>{let o=r;for(let e=0;e<s&&o!=null;e++)o=o[i[e]];return o}}function ln(n){let t=J(n),s=[];for(let i=0;i<t.length;i++)t[i].type==="wildcard"&&s.push(i);return s.length===1?cn(t,s[0]):an(t,s)}function cn(n,t){let s=n.slice(0,t).map(e=>e.value),i=n.slice(t+1).map(e=>e.value),r=s.length,o=i.length;if(o===0){if(r===1){let e=s[0];return f=>f?.[e]}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];return f}}if(o===1){let e=i[0];if(r===1){let f=s[0];return l=>{let c=l?.[f];if(Array.isArray(c))return c.map(a=>a?.[e])}}return f=>{let l=f;for(let c=0;c<r&&l!=null;c++)l=l[s[c]];if(Array.isArray(l))return l.map(c=>c?.[e])}}return e=>{let f=e;for(let l=0;l<r&&f!=null;l++)f=f[s[l]];if(Array.isArray(f))return f.map(l=>{let c=l;for(let a=0;a<o&&c!=null;a++)c=c[i[a]];return c})}}function an(n,t){let s=[],i=0;for(let o=0;o<t.length;o++){let e=t[o],f=o===t.length-1,l=n.slice(i,e).map(c=>c.value);l.length>0&&s.push({type:"access",keys:l}),s.push({type:f?"map":"flatMap",keys:[]}),i=e+1;}let r=n.slice(i).map(o=>o.value);return o=>{let e=o;for(let f of s){if(e==null)return;if(f.type==="access")for(let l of f.keys){if(e==null)return;e=e[l];}else if(f.type==="flatMap"){if(!Array.isArray(e))return;e=e.flatMap(l=>{let c=l;return Array.isArray(c)?c:[c]});}else if(f.type==="map"){if(!Array.isArray(e))return;r.length>0&&(e=e.map(l=>{let c=l;for(let a of r){if(c==null)return;c=c[a];}return c}));}}return e}}function pn(n,t){return O(t)(n)}function F(n){let t=n.indexOf("[*]");return t===-1?n:n.slice(0,t)}function dn(){R.clear();}function gn(){return R.size}function w(n){let t=new Set;return A(n,t),Array.from(t)}function A(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let r=0;r<n.length;r++)A(n[r],t);return}if(y(n)){t.add(F(n.$));return}if(T(n)){if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if;t.add(F(r));}else A(n.$if,t);A(n.then,t),n.else!==void 0&&A(n.else,t);return}if(C(n)){for(let r=0;r<n.$pipe.length;r++)A(n.$pipe[r],t);return}if(b(n)){if(n.args)for(let r=0;r<n.args.length;r++)A(n.args[r],t);return}if(E(n)){A(n.left,t),n.right!==void 0&&A(n.right,t);return}if(h(n)){for(let r=0;r<n.conditions.length;r++)A(n.conditions[r],t);return}let s=n,i=Object.keys(s);for(let r=0;r<i.length;r++)A(s[i[r]],t);}function mn(n){return w(n).length>0}function yn(n){return w(n).length===0}function En(n){return JSON.stringify(n)}function N(n,t={}){let s=t.scope??{},i=t.accessor,r=m(n,s,i),o=w(n),e=En(n);return {fn:r,deps:o,hash:e}}function m(n,t,s){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let i=n.map(r=>m(r,t,s));return r=>i.map(o=>o(r))}if(y(n))return hn(n,s);if(T(n))return Tn(n,t,s);if(C(n))return bn(n,t,s);if(b(n))return Cn(n,t,s);if(E(n))return An(n,t,s);if(h(n))return $n(n,t,s);if(I(n)){let i=n,r=Object.keys(i),o=r.map(e=>m(i[e],t,s));return e=>{let f={};for(let l=0;l<r.length;l++)f[r[l]]=o[l](e);return f}}return ()=>n}function K(n,t){return t?s=>t(n,s):O(n)}function hn(n,t){return K(n.$,t)}function Tn(n,t,s){let i;if(typeof n.$if=="string"){let e=n.$if.startsWith("!")?n.$if.slice(1):n.$if,f=K(e,s);i=n.$if.startsWith("!")?c=>!f(c):c=>!!f(c);}else {let e=m(n.$if,t,s);i=f=>!!e(f);}let r=m(n.then,t,s),o=n.else!==void 0?m(n.else,t,s):()=>{};return e=>i(e)?r(e):o(e)}function bn(n,t,s){let i=n.$pipe;if(i.length===0)return ()=>{};if(i.length===1)return m(i[0],t,s);let r=m(i[0],t,s),o=i.slice(1).map(f=>m(f,t,s)),e=o.length;if(e===1){let[f]=o;return l=>{let c=r(l),a=f(l);return typeof a=="function"?a(c):a}}if(e===2){let[f,l]=o;return c=>{let a=r(c),p=f(c);return a=typeof p=="function"?p(a):p,p=l(c),typeof p=="function"?p(a):p}}if(e===3){let[f,l,c]=o;return a=>{let p=r(a),d=f(a);return p=typeof d=="function"?d(p):d,d=l(a),p=typeof d=="function"?d(p):d,d=c(a),typeof d=="function"?d(p):d}}return f=>{let l=r(f);for(let c=0;c<e;c++){let a=o[c](f);l=typeof a=="function"?a(l):a;}return l}}function Cn(n,t,s){let i=n.$fn,r=n.args;if(r===void 0)return ()=>{let f=t[i];if(!f)throw new Error(`Function not found in scope: ${i}`);return f};let o=r.map(f=>m(f,t,s)),e=o.length;if(e===0)return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l()};if(e===1){let[f]=o;return l=>{let c=t[i];if(!c)throw new Error(`Function not found in scope: ${i}`);return c(f(l))}}if(e===2){let[f,l]=o;return c=>{let a=t[i];if(!a)throw new Error(`Function not found in scope: ${i}`);return a(f(c),l(c))}}if(e===3){let[f,l,c]=o;return a=>{let p=t[i];if(!p)throw new Error(`Function not found in scope: ${i}`);return p(f(a),l(a),c(a))}}return f=>{let l=t[i];if(!l)throw new Error(`Function not found in scope: ${i}`);return l(...o.map(c=>c(f)))}}function An(n,t,s){let i=m(n.left,t,s),r=n.right!==void 0?m(n.right,t,s):()=>{};switch(n.op){case "eq":return o=>i(o)===r(o);case "neq":return o=>i(o)!==r(o);case "gt":return o=>i(o)>r(o);case "gte":return o=>i(o)>=r(o);case "lt":return o=>i(o)<r(o);case "lte":return o=>i(o)<=r(o);case "in":return o=>{let e=r(o);return Array.isArray(e)&&e.includes(i(o))};case "notIn":return o=>{let e=r(o);return !Array.isArray(e)||!e.includes(i(o))};case "contains":return o=>{let e=i(o);return Array.isArray(e)&&e.includes(r(o))};case "notContains":return o=>{let e=i(o);return !Array.isArray(e)||!e.includes(r(o))};case "exists":return o=>i(o)!==void 0;case "notExists":return o=>i(o)===void 0;case "matches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?false:new RegExp(f).test(e)};case "notMatches":return o=>{let e=i(o),f=r(o);return typeof e!="string"||typeof f!="string"?true:!new RegExp(f).test(e)};case "startsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.startsWith(f)};case "endsWith":return o=>{let e=i(o),f=r(o);return typeof e=="string"&&typeof f=="string"&&e.endsWith(f)}}}function $n(n,t,s){let i=n.conditions.map(o=>m(o,t,s)),r=i.length;if(r===1)return o=>!!i[0](o);if(r===2){let[o,e]=i;return n.logic==="AND"?f=>!!o(f)&&!!e(f):f=>!!o(f)||!!e(f)}if(r===3){let[o,e,f]=i;return n.logic==="AND"?l=>!!o(l)&&!!e(l)&&!!f(l):l=>!!o(l)||!!e(l)||!!f(l)}return n.logic==="AND"?o=>{for(let e=0;e<r;e++)if(!i[e](o))return false;return true}:o=>{for(let e=0;e<r;e++)if(i[e](o))return true;return false}}function Sn(n,t,s={}){return N(n,s).fn(t)}var j=class{constructor(t=1e3){this.cache=new Map,this._maxSize=t;}get(t,s={}){let i=JSON.stringify(t),r=this.cache.get(i);if(r)return this.cache.delete(i),this.cache.set(i,r),r;let o=N(t,s);if(this.cache.size>=this._maxSize){let e=this.cache.keys().next().value;e&&this.cache.delete(e);}return this.cache.set(i,o),o}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 s=this.cache.keys().next().value;s&&this.cache.delete(s);}}},P=new j;function wn(n,t={}){return P.get(n,t)}function M(n,t="root",s={}){let i=[];return $(n,t,i,s),{valid:i.length===0,errors:i}}function $(n,t,s,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let e=0;e<n.length;e++)$(n[e],`${t}[${e}]`,s,i);return}if(y(n)){(!n.$||typeof n.$!="string")&&s.push(`${t}: invalid reference, $ must be non-empty string`);return}if(T(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||s.push(`${t}.$if: empty path in string shorthand`):$(n.$if,`${t}.$if`,s,i),$(n.then,`${t}.then`,s,i),n.else!==void 0&&$(n.else,`${t}.else`,s,i);return}if(C(n)){if(!Array.isArray(n.$pipe)){s.push(`${t}.$pipe: must be an array`);return}if(n.$pipe.length===0){s.push(`${t}.$pipe: must have at least one element`);return}for(let e=0;e<n.$pipe.length;e++)$(n.$pipe[e],`${t}.$pipe[${e}]`,s,i);return}if(b(n)){if(!n.$fn||typeof n.$fn!="string"){s.push(`${t}: invalid function, $fn must be non-empty string`);return}if(i.scope&&!(n.$fn in i.scope)&&s.push(`${t}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))s.push(`${t}.args: must be an array`);else for(let e=0;e<n.args.length;e++)$(n.args[e],`${t}.args[${e}]`,s,i);return}if(E(n)){x.has(n.op)||s.push(`${t}: invalid operator "${n.op}"`),$(n.left,`${t}.left`,s,i),n.right!==void 0&&$(n.right,`${t}.right`,s,i);return}if(h(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&s.push(`${t}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){s.push(`${t}.conditions: must be an array`);return}for(let e=0;e<n.conditions.length;e++)$(n.conditions[e],`${t}.conditions[${e}]`,s,i);return}let r=n,o=Object.keys(r);for(let e=0;e<o.length;e++){let f=o[e];$(r[f],`${t}.${f}`,s,i);}}function kn(n,t={}){let s=M(n,"root",t);if(!s.valid)throw new Error(`Invalid expression: ${s.errors.join("; ")}`)}function xn(n,t={}){return M(n,"root",t).valid}var X={};sn(X,{$:()=>Y,$cond:()=>U,$fn:()=>H,$if:()=>Fn,$pipe:()=>Q,cond:()=>jn,fn:()=>On,pipe:()=>Nn,ref:()=>Rn});function Y(n){return {$:n}}var Rn=Y;function H(n,t){return t===void 0||t.length===0?{$fn:n}:{$fn:n,args:t}}var On=H;function Fn(n,t,s){return s===void 0?{$if:n,then:t}:{$if:n,then:t,else:s}}function Q(...n){return {$pipe:n}}var Nn=Q;function U(n,t,s){return s===void 0?{left:n,op:t}:{left:n,op:t,right:s}}var jn=U;var V="data",nn="scope",vn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function G(n,t={}){let{dataParam:s=V,scopeParam:i=nn,noPrefixes:r=false,useAccessor:o=false,lexicalPrefix:e}=t;return g(n,s,i,r,o,e)}function g(n,t,s,i,r,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(e=>g(e,t,s,i,r,o)));if(y(n))return Gn(n.$,t,i,r,o);if(T(n))return Dn(n,t,s,i,r,o);if(C(n))return In(n.$pipe,t,s,i,r,o);if(b(n))return zn(n,t,s,i,r,o);if(E(n))return Mn(n,t,s,i,r,o);if(h(n))return Vn(n,t,s,i,r,o);if(typeof n=="object"){let f=Object.entries(n).map(([l,c])=>builders.property(builders.identifier(l),g(c,t,s,i,r,o)));return builders.objectExpression(f)}return builders.literal(null)}var _="accessor";function L(n,t){return t?n===t||n.startsWith(t+"."):false}function Gn(n,t,s,i,r){return i?L(n,r)?k(n,t,true):builders.callExpression(builders.identifier(_),[builders.literal(n),builders.identifier(t)]):n.includes("[*]")?Wn(n,t,s):k(n,t,s)}function k(n,t,s){let i=v(n);if(i.length===0)return s?builders.identifier("undefined"):builders.identifier(t);let r;if(s){let o=i[0];r=builders.identifier(o.value);for(let e=1;e<i.length;e++){let f=i[e];f.type==="key"?r=builders.memberExpression(r,builders.identifier(f.value),false,true):r=builders.memberExpression(r,builders.literal(f.value),true,true);}}else {r=builders.identifier(t);for(let o of i)o.type==="key"?r=builders.memberExpression(r,builders.identifier(o.value),false,true):r=builders.memberExpression(r,builders.literal(o.value),true,true);}return r}function Wn(n,t,s){let i=n.indexOf("[*]"),r=n.slice(0,i),o=n.slice(i+3),e;if(r?e=k(r,t,s):e=s?builders.identifier("undefined"):builders.identifier(t),!o||o==="")return e;if(o.includes("[*]"))return tn(e,o);let f="_i",l=o.startsWith(".")?o.slice(1):o,c=builders.identifier(f);if(l){let a=v(l);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(e,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(f)],c)])}function tn(n,t){let s=t.indexOf("[*]"),i=t.slice(0,s),r=t.slice(s+3),o="_i",e=i.startsWith(".")?i.slice(1):i,f=builders.identifier(o);if(e){let c=v(e);for(let a of c)a.type==="key"&&(f=builders.memberExpression(f,builders.identifier(a.value),false,true));}if(r.includes("[*]")){let c=tn(f,r);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(o)],c)])}let l=r.startsWith(".")?r.slice(1):r;if(l){let c=v(l);for(let a of c)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 Dn(n,t,s,i,r,o){let e;if(typeof n.$if=="string"){let c=n.$if.startsWith("!"),a=c?n.$if.slice(1):n.$if,p;r?L(a,o)?p=k(a,t,true):p=builders.callExpression(builders.identifier(_),[builders.literal(a),builders.identifier(t)]):p=k(a,t,i),e=c?builders.unaryExpression("!",p):p;}else e=g(n.$if,t,s,i,r,o);let f=g(n.then,t,s,i,r,o),l=n.else!==void 0?g(n.else,t,s,i,r,o):builders.identifier("undefined");return builders.conditionalExpression(e,f,l)}function zn(n,t,s,i,r,o){let e=i?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(s),builders.identifier(n.$fn),false,false);if(n.args===void 0)return e;let f=n.args.map(l=>g(l,t,s,i,r,o));return builders.callExpression(e,f)}function In(n,t,s,i,r,o){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return g(n[0],t,s,i,r,o);let e=g(n[0],t,s,i,r,o);for(let f=1;f<n.length;f++){let l=g(n[f],t,s,i,r,o);e=builders.callExpression(l,[e]);}return e}function Z(n,t,s,i,r,o){if(y(n)){let e=n.$;return r?L(e,o)?k(e,t,true):builders.callExpression(builders.identifier(_),[builders.literal(e),builders.identifier(t)]):k(e,t,i)}return g(n,t,s,i,r,o)}function Mn(n,t,s,i,r,o){let e=Z(n.left,t,s,i,r,o),f=n.right!==void 0?Z(n.right,t,s,i,r,o):builders.literal(null),l=vn[n.op];if(l)return builders.binaryExpression(l,e,f);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(f,builders.identifier("includes")),[e]));case "contains":return builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(e,builders.identifier("includes"),false,true),[f]));case "exists":return builders.binaryExpression("!=",e,builders.literal(null));case "notExists":return builders.binaryExpression("==",e,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[f]),builders.identifier("test")),[e]));case "startsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("startsWith"),false,true),[f]);case "endsWith":return builders.callExpression(builders.memberExpression(e,builders.identifier("endsWith"),false,true),[f]);default:return builders.binaryExpression("===",e,f)}}function Vn(n,t,s,i,r,o){let{logic:e,conditions:f}=n,l=e==="AND"?"&&":"||";if(f.length===0)return builders.literal(e==="AND");if(f.length===1)return g(f[0],t,s,i,r,o);let c=g(f[0],t,s,i,r,o);for(let a=1;a<f.length;a++){let p=g(f[a],t,s,i,r,o);c=builders.logicalExpression(l,c,p);}return c}function v(n){let t=[],s=n.length,i=0,r="";for(;i<s;){let o=n[i];if(o===".")r&&(t.push({type:"key",value:r}),r=""),i++;else if(o==="["){r&&(t.push({type:"key",value:r}),r=""),i++;let e=i;for(;i<s&&n[i]!=="]";)i++;let f=n.slice(e,i);if(i++,f!=="*"){let l=parseInt(f,10);t.push({type:"index",value:isNaN(l)?f:l});}}else r+=o,i++;}return r&&t.push({type:"key",value:r}),t}function en(n,t=[V]){return builders.arrowFunctionExpression(t.map(s=>builders.identifier(s)),n)}function W(n){let t=new Set;return S(n,t),t}function S(n,t){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let i of n)S(i,t);return}if(y(n))return;if(T(n)){S(n.$if,t),S(n.then,t),n.else!==void 0&&S(n.else,t);return}if(C(n)){for(let i of n.$pipe)S(i,t);return}if(b(n)){if(t.add(n.$fn),n.args)for(let i of n.args)S(i,t);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&S(n.left,t),n.right!==void 0&&typeof n.right=="object"&&S(n.right,t);return}if(h(n)){for(let i of n.conditions)S(i,t);return}let s=n;for(let i of Object.keys(s))S(s[i],t);}function D(n){let t=new Set;for(let s of n){let i=s.indexOf("."),r=s.indexOf("["),o=s.length;i!==-1&&(o=Math.min(o,i)),r!==-1&&(o=Math.min(o,r));let e=s.slice(0,o);e&&t.add(e);}return t}function Ln(n){return JSON.stringify(n)}function qn(n,t,s,i,r){let o=G(n,{noPrefixes:true,useAccessor:i,lexicalPrefix:r}),e=generate(o),f="";i?r&&(f=`const{${r}}=data??{};`):t.size>0&&(f=`const{${[...t].join(",")}}=data??{};`);let l=i?new Set([...s,"accessor"]):s,c=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return c?`(function(scope){${c}return function(data){${f}return ${e}}})`:`(function(){return function(data){${f}return ${e}}})`}function q(n,t={}){let{scope:s={},returnCode:i=false,useAccessor:r=false,lexicalPrefix:o}=t,e=w(n),f=D(e),l=W(n),c=Ln(n),a=qn(n,f,l,r,o);if(i)return {code:a,deps:e,hash:c,dataRoots:[...f],scopeFns:[...l]};let p;try{p=new Function(`return ${a}`)()(s);}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:e,hash:c}}function on(n,t,s={}){let{fn:i}=q(n,s);return i(t)}function Jn(n,t){return z(n,t)}function z(n,t){if(n===null)return null;if(typeof n!="object")return n;if(Array.isArray(n))return n.map(o=>z(o,t));let s=n,i=Object.keys(s);for(let o of i)if(o in t){let e=t[o](s);if(typeof e=="object"&&e!==null&&o in e)throw new Error(`Transform "${o}" returned object with same key \u2014 infinite loop`);return z(e,t)}let r={};for(let o of i)r[o]=z(s[o],t);return r}var bt="2.0.0";export{j as ExpressionCache,bt as VERSION,kn as assertValid,X as builders,P as cache,wn as cached,dn as clearPathCache,N as compile,q as compileAST,O as compilePath,G as dslToAST,Sn as evaluate,on as evaluateAST,D as extractDataRoots,w as extractDeps,W as extractScopeFns,pn as get,gn as getPathCacheSize,mn as hasDeps,B as hasWildcard,E as isCondition,fn as isConditionExpr,h as isConditionGroup,T as isConditional,b as isFn,I as isLiteral,C as isPipe,yn as isPure,y as isRef,xn as isValid,Jn as normalize,F as normalizePath,M as validate,en as wrapInFunction};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statedelta-libs/expressions",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "JSON DSL compiler for optimized functions - StateDelta expression engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",