@statedelta-libs/expressions 1.2.0 → 1.2.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/README.md +24 -6
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +53 -7
- package/dist/index.d.ts +53 -7
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -462,15 +462,20 @@ Mecanismo de extensão do walker para tipos de expressão customizados. Permite
|
|
|
462
462
|
|
|
463
463
|
### API
|
|
464
464
|
|
|
465
|
+
Transforms retornam `Expression` ou `TransformResult`. Use `TransformResult` quando os args contêm tipos domain-specific (não `FnArg`):
|
|
466
|
+
|
|
465
467
|
```typescript
|
|
466
|
-
|
|
468
|
+
import type { TransformFn, TransformResult } from '@statedelta-libs/expressions';
|
|
469
|
+
|
|
470
|
+
const transforms: Record<string, TransformFn> = {
|
|
471
|
+
// TransformResult.args aceita unknown[] — sem casts necessários
|
|
467
472
|
$query: (node) => ({
|
|
468
473
|
$fn: "__query",
|
|
469
474
|
args: [node.$query, node.params ?? {}]
|
|
470
475
|
}),
|
|
471
|
-
$
|
|
472
|
-
$fn: "
|
|
473
|
-
args: [node.$
|
|
476
|
+
$test: (node) => ({
|
|
477
|
+
$fn: "__test",
|
|
478
|
+
args: [node.$test] // TestSpec, QueryParams, etc. — qualquer tipo
|
|
474
479
|
}),
|
|
475
480
|
};
|
|
476
481
|
```
|
|
@@ -728,6 +733,7 @@ import type {
|
|
|
728
733
|
CompileOptions,
|
|
729
734
|
AccessorFn,
|
|
730
735
|
TransformFn,
|
|
736
|
+
TransformResult,
|
|
731
737
|
} from '@statedelta-libs/expressions';
|
|
732
738
|
|
|
733
739
|
// Type guards
|
|
@@ -738,6 +744,7 @@ import {
|
|
|
738
744
|
isPipe,
|
|
739
745
|
isCondition,
|
|
740
746
|
isLiteral,
|
|
747
|
+
isTransformResult,
|
|
741
748
|
} from '@statedelta-libs/expressions';
|
|
742
749
|
```
|
|
743
750
|
|
|
@@ -796,9 +803,20 @@ import {
|
|
|
796
803
|
extractDeps(expr, transforms);
|
|
797
804
|
```
|
|
798
805
|
|
|
799
|
-
3. **
|
|
806
|
+
3. **Novos tipos**: `TransformFn` e `TransformResult` exportados diretamente do expressions.
|
|
807
|
+
|
|
808
|
+
4. **TransformResult**: Transforms podem retornar `TransformResult` com `args: unknown[]` para tipos domain-specific:
|
|
809
|
+
```typescript
|
|
810
|
+
// TransformResult.args aceita unknown[] — sem casts necessários
|
|
811
|
+
const transforms: Record<string, TransformFn> = {
|
|
812
|
+
$test: (node) => ({
|
|
813
|
+
$fn: "__test",
|
|
814
|
+
args: [node.$test] // TestSpec, QueryParams, etc.
|
|
815
|
+
}),
|
|
816
|
+
};
|
|
817
|
+
```
|
|
800
818
|
|
|
801
|
-
|
|
819
|
+
5. **Zero breaking change**: O campo `transforms` é opcional. Sem ele, o comportamento é idêntico ao anterior.
|
|
802
820
|
|
|
803
821
|
## Licença
|
|
804
822
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var omniAst=require('omni-ast');var rn=Object.defineProperty;var sn=(n,e)=>{for(var i in e)rn(n,i,{get:e[i],enumerable:true});};var h=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,A=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,$=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",k=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),F=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&&F.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),T=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,fn=n=>E(n)||T(n),M=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,f="left"in i&&"op"in i&&F.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!f&&!("logic"in i)}return false};var x=new Map;function J(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r==="*")e.push({type:"wildcard",value:"*"});else {let c=parseInt(r,10);e.push({type:"index",value:isNaN(c)?r:c});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function B(n){return n.includes("[*]")}function O(n){let e=x.get(n);return e||(e=B(n)?un(n):cn(n),x.set(n,e),e)}function cn(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=J(n),i=e.length;if(i===2){let[o,s]=e,t=o.value,r=s.value;return c=>c?.[t]?.[r]}if(i===3){let[o,s,t]=e,r=o.value,c=s.value,l=t.value;return a=>a?.[r]?.[c]?.[l]}let f=e.map(o=>o.value);return o=>{let s=o;for(let t=0;t<i&&s!=null;t++)s=s[f[t]];return s}}function un(n){let e=J(n),i=[];for(let f=0;f<e.length;f++)e[f].type==="wildcard"&&i.push(f);return i.length===1?ln(e,i[0]):an(e,i)}function ln(n,e){let i=n.slice(0,e).map(t=>t.value),f=n.slice(e+1).map(t=>t.value),o=i.length,s=f.length;if(s===0){if(o===1){let t=i[0];return r=>r?.[t]}return t=>{let r=t;for(let c=0;c<o&&r!=null;c++)r=r[i[c]];return r}}if(s===1){let t=f[0];if(o===1){let r=i[0];return c=>{let l=c?.[r];if(Array.isArray(l))return l.map(a=>a?.[t])}}return r=>{let c=r;for(let l=0;l<o&&c!=null;l++)c=c[i[l]];if(Array.isArray(c))return c.map(l=>l?.[t])}}return t=>{let r=t;for(let c=0;c<o&&r!=null;c++)r=r[i[c]];if(Array.isArray(r))return r.map(c=>{let l=c;for(let a=0;a<s&&l!=null;a++)l=l[f[a]];return l})}}function an(n,e){let i=[],f=0;for(let s=0;s<e.length;s++){let t=e[s],r=s===e.length-1,c=n.slice(f,t).map(l=>l.value);c.length>0&&i.push({type:"access",keys:c}),i.push({type:r?"map":"flatMap",keys:[]}),f=t+1;}let o=n.slice(f).map(s=>s.value);return s=>{let t=s;for(let r of i){if(t==null)return;if(r.type==="access")for(let c of r.keys){if(t==null)return;t=t[c];}else if(r.type==="flatMap"){if(!Array.isArray(t))return;t=t.flatMap(c=>{let l=c;return Array.isArray(l)?l:[l]});}else if(r.type==="map"){if(!Array.isArray(t))return;o.length>0&&(t=t.map(c=>{let l=c;for(let a of o){if(l==null)return;l=l[a];}return l}));}}return t}}function pn(n,e){return O(e)(n)}function j(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function dn(){x.clear();}function gn(){return x.size}function w(n,e){let i=new Set;return b(n,i,e),Array.from(i)}function b(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)b(n[s],e,i);return}if(h(n)){e.add(j(n.$));return}if(A(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(j(s));}else b(n.$if,e,i);b(n.then,e,i),n.else!==void 0&&b(n.else,e,i);return}if(k(n)){for(let s=0;s<n.$pipe.length;s++)b(n.$pipe[s],e,i);return}if($(n)){if(n.args)for(let s=0;s<n.args.length;s++){let t=n.args[s];typeof t!="function"&&b(t,e,i);}return}if(E(n)){b(n.left,e,i),n.right!==void 0&&b(n.right,e,i);return}if(T(n)){for(let s=0;s<n.conditions.length;s++)b(n.conditions[s],e,i);return}if(i){let s=n,t=Object.keys(s);for(let r=0;r<t.length;r++){let c=t[r];if(c in i){let l=i[c](s);if(typeof l=="object"&&l!==null&&c in l)throw new Error(`Transform "${c}" returned object with same key \u2014 infinite loop`);b(l,e,i);return}}}let f=n,o=Object.keys(f);for(let s=0;s<o.length;s++)b(f[o[s]],e,i);}function yn(n){return w(n).length>0}function mn(n){return w(n).length===0}function hn(n){return JSON.stringify(n)}function N(n,e={}){let i=e.scope??{},f=e.accessor,o=e.transforms,s=m(n,i,f,o),t=w(n,o),r=hn(n);return {fn:s,deps:t,hash:r}}function m(n,e,i,f){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let o=n.map(s=>m(s,e,i,f));return s=>o.map(t=>t(s))}if(h(n))return En(n,i);if(A(n))return Tn(n,e,i,f);if(k(n))return bn(n,e,i,f);if($(n))return Cn(n,e,i,f);if(E(n))return An(n,e,i,f);if(T(n))return $n(n,e,i,f);if(f){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in f){let c=f[r](o);if(typeof c=="object"&&c!==null&&r in c)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);return m(c,e,i,f)}}}if(M(n)){let o=n,s=Object.keys(o),t=s.map(r=>m(o[r],e,i,f));return r=>{let c={};for(let l=0;l<s.length;l++)c[s[l]]=t[l](r);return c}}return ()=>n}function K(n,e){return e?i=>e(n,i):O(n)}function En(n,e){return K(n.$,e)}function Tn(n,e,i,f){let o;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,c=K(r,i);o=n.$if.startsWith("!")?a=>!c(a):a=>!!c(a);}else {let r=m(n.$if,e,i,f);o=c=>!!r(c);}let s=m(n.then,e,i,f),t=n.else!==void 0?m(n.else,e,i,f):()=>{};return r=>o(r)?s(r):t(r)}function bn(n,e,i,f){let o=n.$pipe;if(o.length===0)return ()=>{};if(o.length===1)return m(o[0],e,i,f);let s=m(o[0],e,i,f),t=o.slice(1).map(c=>m(c,e,i,f)),r=t.length;if(r===1){let[c]=t;return l=>{let a=s(l),p=c(l);return typeof p=="function"?p(a):p}}if(r===2){let[c,l]=t;return a=>{let p=s(a),d=c(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(r===3){let[c,l,a]=t;return p=>{let d=s(p),g=c(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 c=>{let l=s(c);for(let a=0;a<r;a++){let p=t[a](c);l=typeof p=="function"?p(l):p;}return l}}function Cn(n,e,i,f){let o=n.$fn,s=n.args;if(s===void 0)return ()=>{let c=e[o];if(!c)throw new Error(`Function not found in scope: ${o}`);return c};let t=s.map(c=>typeof c=="function"?()=>c:m(c,e,i,f)),r=t.length;if(r===0)return c=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l()};if(r===1){let[c]=t;return l=>{let a=e[o];if(!a)throw new Error(`Function not found in scope: ${o}`);return a(c(l))}}if(r===2){let[c,l]=t;return a=>{let p=e[o];if(!p)throw new Error(`Function not found in scope: ${o}`);return p(c(a),l(a))}}if(r===3){let[c,l,a]=t;return p=>{let d=e[o];if(!d)throw new Error(`Function not found in scope: ${o}`);return d(c(p),l(p),a(p))}}return c=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l(...t.map(a=>a(c)))}}function An(n,e,i,f){let o=m(n.left,e,i,f),s=n.right!==void 0?m(n.right,e,i,f):()=>{};switch(n.op){case "eq":return t=>o(t)===s(t);case "neq":return t=>o(t)!==s(t);case "gt":return t=>o(t)>s(t);case "gte":return t=>o(t)>=s(t);case "lt":return t=>o(t)<s(t);case "lte":return t=>o(t)<=s(t);case "in":return t=>{let r=s(t);return Array.isArray(r)&&r.includes(o(t))};case "notIn":return t=>{let r=s(t);return !Array.isArray(r)||!r.includes(o(t))};case "contains":return t=>{let r=o(t);return Array.isArray(r)&&r.includes(s(t))};case "notContains":return t=>{let r=o(t);return !Array.isArray(r)||!r.includes(s(t))};case "exists":return t=>o(t)!==void 0;case "notExists":return t=>o(t)===void 0;case "matches":return t=>{let r=o(t),c=s(t);return typeof r!="string"||typeof c!="string"?false:new RegExp(c).test(r)};case "notMatches":return t=>{let r=o(t),c=s(t);return typeof r!="string"||typeof c!="string"?true:!new RegExp(c).test(r)};case "startsWith":return t=>{let r=o(t),c=s(t);return typeof r=="string"&&typeof c=="string"&&r.startsWith(c)};case "endsWith":return t=>{let r=o(t),c=s(t);return typeof r=="string"&&typeof c=="string"&&r.endsWith(c)}}}function $n(n,e,i,f){let o=n.conditions.map(t=>m(t,e,i,f)),s=o.length;if(s===1)return t=>!!o[0](t);if(s===2){let[t,r]=o;return n.logic==="AND"?c=>!!t(c)&&!!r(c):c=>!!t(c)||!!r(c)}if(s===3){let[t,r,c]=o;return n.logic==="AND"?l=>!!t(l)&&!!r(l)&&!!c(l):l=>!!t(l)||!!r(l)||!!c(l)}return n.logic==="AND"?t=>{for(let r=0;r<s;r++)if(!o[r](t))return false;return true}:t=>{for(let r=0;r<s;r++)if(o[r](t))return true;return false}}function kn(n,e,i={}){return N(n,i).fn(e)}var v=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let f=JSON.stringify(e),o=this.cache.get(f);if(o)return this.cache.delete(f),this.cache.set(f,o),o;let s=N(e,i);if(this.cache.size>=this._maxSize){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}return this.cache.set(f,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},Y=new v;function Sn(n,e={}){return Y.get(n,e)}function z(n,e="root",i={}){let f=[];return S(n,e,f,i),{valid:f.length===0,errors:f}}function S(n,e,i,f){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t=0;t<n.length;t++)S(n[t],`${e}[${t}]`,i,f);return}if(h(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(A(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):S(n.$if,`${e}.$if`,i,f),S(n.then,`${e}.then`,i,f),n.else!==void 0&&S(n.else,`${e}.else`,i,f);return}if(k(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let t=0;t<n.$pipe.length;t++)S(n.$pipe[t],`${e}.$pipe[${t}]`,i,f);return}if($(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(f.scope&&!(n.$fn in f.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let t=0;t<n.args.length;t++){let r=n.args[t];typeof r!="function"&&S(r,`${e}.args[${t}]`,i,f);}return}if(E(n)){F.has(n.op)||i.push(`${e}: invalid operator "${n.op}"`),S(n.left,`${e}.left`,i,f),n.right!==void 0&&S(n.right,`${e}.right`,i,f);return}if(T(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&i.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){i.push(`${e}.conditions: must be an array`);return}for(let t=0;t<n.conditions.length;t++)S(n.conditions[t],`${e}.conditions[${t}]`,i,f);return}let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];S(o[r],`${e}.${r}`,i,f);}}function wn(n,e={}){let i=z(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function Rn(n,e={}){return z(n,"root",e).valid}var Z={};sn(Z,{$:()=>H,$cond:()=>X,$fn:()=>Q,$if:()=>On,$pipe:()=>U,cond:()=>Nn,fn:()=>xn,pipe:()=>jn,ref:()=>Fn});function H(n){return {$:n}}var Fn=H;function Q(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var xn=Q;function On(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function U(...n){return {$pipe:n}}var jn=U;function X(n,e,i){return i===void 0?{left:n,op:e}:{left:n,op:e,right:i}}var Nn=X;var L="data",nn="scope",vn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function W(n,e={}){let{dataParam:i=L,scopeParam:f=nn,noPrefixes:o=false,useAccessor:s=false,lexicalPrefix:t,transforms:r}=e;return y(n,i,f,o,s,t,r)}function y(n,e,i,f,o,s,t){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(r=>y(r,e,i,f,o,s,t)));if(h(n))return Gn(n.$,e,f,o,s);if(A(n))return Dn(n,e,i,f,o,s,t);if(k(n))return Mn(n.$pipe,e,i,f,o,s,t);if($(n))return In(n,e,i,f,o,s,t);if(E(n))return zn(n,e,i,f,o,s,t);if(T(n))return Ln(n,e,i,f,o,s,t);if(t&&typeof n=="object"){let r=n,c=Object.keys(r);for(let l=0;l<c.length;l++){let a=c[l];if(a in t){let p=t[a](r);if(typeof p=="object"&&p!==null&&a in p)throw new Error(`Transform "${a}" returned object with same key \u2014 infinite loop`);return y(p,e,i,f,o,s,t)}}}if(typeof n=="object"){let c=Object.entries(n).map(([l,a])=>omniAst.builders.property(omniAst.builders.identifier(l),y(a,e,i,f,o,s,t)));return omniAst.builders.objectExpression(c)}return omniAst.builders.literal(null)}var V="accessor";function _(n,e){return e?n===e||n.startsWith(e+"."):false}function Gn(n,e,i,f,o){return f?_(n,o)?R(n,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(n),omniAst.builders.identifier(e)]):n.includes("[*]")?Wn(n,e,i):R(n,e,i)}function R(n,e,i){let f=G(n);if(f.length===0)return i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e);let o;if(i){let s=f[0];o=omniAst.builders.identifier(s.value);for(let t=1;t<f.length;t++){let r=f[t];r.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(r.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(r.value),true,true);}}else {o=omniAst.builders.identifier(e);for(let s of f)s.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(s.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(s.value),true,true);}return o}function Wn(n,e,i){let f=n.indexOf("[*]"),o=n.slice(0,f),s=n.slice(f+3),t;if(o?t=R(o,e,i):t=i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e),!s||s==="")return t;if(s.includes("[*]"))return en(t,s);let r="_i",c=s.startsWith(".")?s.slice(1):s,l=omniAst.builders.identifier(r);if(c){let a=G(c);for(let p of a)p.type==="key"?l=omniAst.builders.memberExpression(l,omniAst.builders.identifier(p.value),false,true):l=omniAst.builders.memberExpression(l,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(t,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(r)],l)])}function en(n,e){let i=e.indexOf("[*]"),f=e.slice(0,i),o=e.slice(i+3),s="_i",t=f.startsWith(".")?f.slice(1):f,r=omniAst.builders.identifier(s);if(t){let l=G(t);for(let a of l)a.type==="key"&&(r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(a.value),false,true));}if(o.includes("[*]")){let l=en(r,o);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],l)])}let c=o.startsWith(".")?o.slice(1):o;if(c){let l=G(c);for(let a of l)a.type==="key"&&(r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],r)])}function Dn(n,e,i,f,o,s,t){let r;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;o?_(p,s)?d=R(p,e,true):d=omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(p),omniAst.builders.identifier(e)]):d=R(p,e,f),r=a?omniAst.builders.unaryExpression("!",d):d;}else r=y(n.$if,e,i,f,o,s,t);let c=y(n.then,e,i,f,o,s,t),l=n.else!==void 0?y(n.else,e,i,f,o,s,t):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(r,c,l)}function In(n,e,i,f,o,s,t){let r=f?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(i),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return r;let c=n.args.map(l=>typeof l=="function"?omniAst.builders.literal(null):y(l,e,i,f,o,s,t));return omniAst.builders.callExpression(r,c)}function Mn(n,e,i,f,o,s,t){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return y(n[0],e,i,f,o,s,t);let r=y(n[0],e,i,f,o,s,t);for(let c=1;c<n.length;c++){let l=y(n[c],e,i,f,o,s,t);r=omniAst.builders.callExpression(l,[r]);}return r}function P(n,e,i,f,o,s,t){if(h(n)){let r=n.$;return o?_(r,s)?R(r,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(r),omniAst.builders.identifier(e)]):R(r,e,f)}return y(n,e,i,f,o,s,t)}function zn(n,e,i,f,o,s,t){let r=P(n.left,e,i,f,o,s,t),c=n.right!==void 0?P(n.right,e,i,f,o,s,t):omniAst.builders.literal(null),l=vn[n.op];if(l)return omniAst.builders.binaryExpression(l,r,c);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(c,omniAst.builders.identifier("includes")),[r]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(c,omniAst.builders.identifier("includes")),[r]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("includes"),false,true),[c]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("includes"),false,true),[c]));case "exists":return omniAst.builders.binaryExpression("!=",r,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",r,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[c]),omniAst.builders.identifier("test")),[r]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[c]),omniAst.builders.identifier("test")),[r]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("startsWith"),false,true),[c]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("endsWith"),false,true),[c]);default:return omniAst.builders.binaryExpression("===",r,c)}}function Ln(n,e,i,f,o,s,t){let{logic:r,conditions:c}=n,l=r==="AND"?"&&":"||";if(c.length===0)return omniAst.builders.literal(r==="AND");if(c.length===1)return y(c[0],e,i,f,o,s,t);let a=y(c[0],e,i,f,o,s,t);for(let p=1;p<c.length;p++){let d=y(c[p],e,i,f,o,s,t);a=omniAst.builders.logicalExpression(l,a,d);}return a}function G(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r!=="*"){let c=parseInt(r,10);e.push({type:"index",value:isNaN(c)?r:c});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function tn(n,e=[L]){return omniAst.builders.arrowFunctionExpression(e.map(i=>omniAst.builders.identifier(i)),n)}function D(n,e){let i=new Set;return C(n,i,e),i}function C(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o of n)C(o,e,i);return}if(h(n))return;if(A(n)){C(n.$if,e,i),C(n.then,e,i),n.else!==void 0&&C(n.else,e,i);return}if(k(n)){for(let o of n.$pipe)C(o,e,i);return}if($(n)){if(e.add(n.$fn),n.args)for(let o of n.args)typeof o!="function"&&C(o,e,i);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&C(n.left,e,i),n.right!==void 0&&typeof n.right=="object"&&C(n.right,e,i);return}if(T(n)){for(let o of n.conditions)C(o,e,i);return}if(i){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in i){let c=i[r](o);if(typeof c=="object"&&c!==null&&r in c)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);C(c,e,i);return}}}let f=n;for(let o of Object.keys(f))C(f[o],e,i);}function I(n){let e=new Set;for(let i of n){let f=i.indexOf("."),o=i.indexOf("["),s=i.length;f!==-1&&(s=Math.min(s,f)),o!==-1&&(s=Math.min(s,o));let t=i.slice(0,s);t&&e.add(t);}return e}function _n(n){return JSON.stringify(n)}function qn(n,e,i,f,o,s){let t=W(n,{noPrefixes:true,useAccessor:f,lexicalPrefix:o,transforms:s}),r=omniAst.generate(t),c="";f?o&&(c=`const{${o}}=data??{};`):e.size>0&&(c=`const{${[...e].join(",")}}=data??{};`);let l=f?new Set([...i,"accessor"]):i,a=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return a?`(function(scope){${a}return function(data){${c}return ${r}}})`:`(function(){return function(data){${c}return ${r}}})`}function q(n,e={}){let{scope:i={},returnCode:f=false,useAccessor:o=false,lexicalPrefix:s,transforms:t}=e,r=w(n,t),c=I(r),l=D(n,t),a=_n(n),p=qn(n,c,l,o,s,t);if(f)return {code:p,deps:r,hash:a,dataRoots:[...c],scopeFns:[...l]};let d;try{d=new Function(`return ${p}`)()(i);}catch(g){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${g instanceof Error?g.message:String(g)}`)}return {fn:d,deps:r,hash:a}}function on(n,e,i={}){let{fn:f}=q(n,i);return f(e)}var Ee="0.1.1";exports.ExpressionCache=v;exports.VERSION=Ee;exports.assertValid=wn;exports.builders=Z;exports.cache=Y;exports.cached=Sn;exports.clearPathCache=dn;exports.compile=N;exports.compileAST=q;exports.compilePath=O;exports.dslToAST=W;exports.evaluate=kn;exports.evaluateAST=on;exports.extractDataRoots=I;exports.extractDeps=w;exports.extractScopeFns=D;exports.get=pn;exports.getPathCacheSize=gn;exports.hasDeps=yn;exports.hasWildcard=B;exports.isCondition=E;exports.isConditionExpr=fn;exports.isConditionGroup=T;exports.isConditional=A;exports.isFn=$;exports.isLiteral=M;exports.isPipe=k;exports.isPure=mn;exports.isRef=h;exports.isValid=Rn;exports.normalizePath=j;exports.validate=z;exports.wrapInFunction=tn;
|
|
1
|
+
'use strict';var omniAst=require('omni-ast');var rn=Object.defineProperty;var sn=(n,e)=>{for(var i in e)rn(n,i,{get:e[i],enumerable:true});};var h=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,A=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,$=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",fn=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",k=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),F=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&&F.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),T=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,un=n=>E(n)||T(n),M=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,f="left"in i&&"op"in i&&F.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!f&&!("logic"in i)}return false};var x=new Map;function J(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r==="*")e.push({type:"wildcard",value:"*"});else {let u=parseInt(r,10);e.push({type:"index",value:isNaN(u)?r:u});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function B(n){return n.includes("[*]")}function O(n){let e=x.get(n);return e||(e=B(n)?ln(n):cn(n),x.set(n,e),e)}function cn(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=J(n),i=e.length;if(i===2){let[o,s]=e,t=o.value,r=s.value;return u=>u?.[t]?.[r]}if(i===3){let[o,s,t]=e,r=o.value,u=s.value,l=t.value;return a=>a?.[r]?.[u]?.[l]}let f=e.map(o=>o.value);return o=>{let s=o;for(let t=0;t<i&&s!=null;t++)s=s[f[t]];return s}}function ln(n){let e=J(n),i=[];for(let f=0;f<e.length;f++)e[f].type==="wildcard"&&i.push(f);return i.length===1?an(e,i[0]):pn(e,i)}function an(n,e){let i=n.slice(0,e).map(t=>t.value),f=n.slice(e+1).map(t=>t.value),o=i.length,s=f.length;if(s===0){if(o===1){let t=i[0];return r=>r?.[t]}return t=>{let r=t;for(let u=0;u<o&&r!=null;u++)r=r[i[u]];return r}}if(s===1){let t=f[0];if(o===1){let r=i[0];return u=>{let l=u?.[r];if(Array.isArray(l))return l.map(a=>a?.[t])}}return r=>{let u=r;for(let l=0;l<o&&u!=null;l++)u=u[i[l]];if(Array.isArray(u))return u.map(l=>l?.[t])}}return t=>{let r=t;for(let u=0;u<o&&r!=null;u++)r=r[i[u]];if(Array.isArray(r))return r.map(u=>{let l=u;for(let a=0;a<s&&l!=null;a++)l=l[f[a]];return l})}}function pn(n,e){let i=[],f=0;for(let s=0;s<e.length;s++){let t=e[s],r=s===e.length-1,u=n.slice(f,t).map(l=>l.value);u.length>0&&i.push({type:"access",keys:u}),i.push({type:r?"map":"flatMap",keys:[]}),f=t+1;}let o=n.slice(f).map(s=>s.value);return s=>{let t=s;for(let r of i){if(t==null)return;if(r.type==="access")for(let u of r.keys){if(t==null)return;t=t[u];}else if(r.type==="flatMap"){if(!Array.isArray(t))return;t=t.flatMap(u=>{let l=u;return Array.isArray(l)?l:[l]});}else if(r.type==="map"){if(!Array.isArray(t))return;o.length>0&&(t=t.map(u=>{let l=u;for(let a of o){if(l==null)return;l=l[a];}return l}));}}return t}}function dn(n,e){return O(e)(n)}function j(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function gn(){x.clear();}function yn(){return x.size}function w(n,e){let i=new Set;return b(n,i,e),Array.from(i)}function b(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)b(n[s],e,i);return}if(h(n)){e.add(j(n.$));return}if(A(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(j(s));}else b(n.$if,e,i);b(n.then,e,i),n.else!==void 0&&b(n.else,e,i);return}if(k(n)){for(let s=0;s<n.$pipe.length;s++)b(n.$pipe[s],e,i);return}if($(n)){if(n.args)for(let s=0;s<n.args.length;s++){let t=n.args[s];typeof t!="function"&&b(t,e,i);}return}if(E(n)){b(n.left,e,i),n.right!==void 0&&b(n.right,e,i);return}if(T(n)){for(let s=0;s<n.conditions.length;s++)b(n.conditions[s],e,i);return}if(i){let s=n,t=Object.keys(s);for(let r=0;r<t.length;r++){let u=t[r];if(u in i){let l=i[u](s);if(typeof l=="object"&&l!==null&&u in l)throw new Error(`Transform "${u}" returned object with same key \u2014 infinite loop`);b(l,e,i);return}}}let f=n,o=Object.keys(f);for(let s=0;s<o.length;s++)b(f[o[s]],e,i);}function mn(n){return w(n).length>0}function hn(n){return w(n).length===0}function En(n){return JSON.stringify(n)}function N(n,e={}){let i=e.scope??{},f=e.accessor,o=e.transforms,s=m(n,i,f,o),t=w(n,o),r=En(n);return {fn:s,deps:t,hash:r}}function m(n,e,i,f){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let o=n.map(s=>m(s,e,i,f));return s=>o.map(t=>t(s))}if(h(n))return Tn(n,i);if(A(n))return bn(n,e,i,f);if(k(n))return Cn(n,e,i,f);if($(n))return An(n,e,i,f);if(E(n))return $n(n,e,i,f);if(T(n))return kn(n,e,i,f);if(f){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in f){let u=f[r](o);if(typeof u=="object"&&u!==null&&r in u)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);return m(u,e,i,f)}}}if(M(n)){let o=n,s=Object.keys(o),t=s.map(r=>m(o[r],e,i,f));return r=>{let u={};for(let l=0;l<s.length;l++)u[s[l]]=t[l](r);return u}}return ()=>n}function K(n,e){return e?i=>e(n,i):O(n)}function Tn(n,e){return K(n.$,e)}function bn(n,e,i,f){let o;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,u=K(r,i);o=n.$if.startsWith("!")?a=>!u(a):a=>!!u(a);}else {let r=m(n.$if,e,i,f);o=u=>!!r(u);}let s=m(n.then,e,i,f),t=n.else!==void 0?m(n.else,e,i,f):()=>{};return r=>o(r)?s(r):t(r)}function Cn(n,e,i,f){let o=n.$pipe;if(o.length===0)return ()=>{};if(o.length===1)return m(o[0],e,i,f);let s=m(o[0],e,i,f),t=o.slice(1).map(u=>m(u,e,i,f)),r=t.length;if(r===1){let[u]=t;return l=>{let a=s(l),p=u(l);return typeof p=="function"?p(a):p}}if(r===2){let[u,l]=t;return a=>{let p=s(a),d=u(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(r===3){let[u,l,a]=t;return p=>{let d=s(p),g=u(p);return d=typeof g=="function"?g(d):g,g=l(p),d=typeof g=="function"?g(d):g,g=a(p),typeof g=="function"?g(d):g}}return u=>{let l=s(u);for(let a=0;a<r;a++){let p=t[a](u);l=typeof p=="function"?p(l):p;}return l}}function An(n,e,i,f){let o=n.$fn,s=n.args;if(s===void 0)return ()=>{let u=e[o];if(!u)throw new Error(`Function not found in scope: ${o}`);return u};let t=s.map(u=>typeof u=="function"?()=>u:m(u,e,i,f)),r=t.length;if(r===0)return u=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l()};if(r===1){let[u]=t;return l=>{let a=e[o];if(!a)throw new Error(`Function not found in scope: ${o}`);return a(u(l))}}if(r===2){let[u,l]=t;return a=>{let p=e[o];if(!p)throw new Error(`Function not found in scope: ${o}`);return p(u(a),l(a))}}if(r===3){let[u,l,a]=t;return p=>{let d=e[o];if(!d)throw new Error(`Function not found in scope: ${o}`);return d(u(p),l(p),a(p))}}return u=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l(...t.map(a=>a(u)))}}function $n(n,e,i,f){let o=m(n.left,e,i,f),s=n.right!==void 0?m(n.right,e,i,f):()=>{};switch(n.op){case "eq":return t=>o(t)===s(t);case "neq":return t=>o(t)!==s(t);case "gt":return t=>o(t)>s(t);case "gte":return t=>o(t)>=s(t);case "lt":return t=>o(t)<s(t);case "lte":return t=>o(t)<=s(t);case "in":return t=>{let r=s(t);return Array.isArray(r)&&r.includes(o(t))};case "notIn":return t=>{let r=s(t);return !Array.isArray(r)||!r.includes(o(t))};case "contains":return t=>{let r=o(t);return Array.isArray(r)&&r.includes(s(t))};case "notContains":return t=>{let r=o(t);return !Array.isArray(r)||!r.includes(s(t))};case "exists":return t=>o(t)!==void 0;case "notExists":return t=>o(t)===void 0;case "matches":return t=>{let r=o(t),u=s(t);return typeof r!="string"||typeof u!="string"?false:new RegExp(u).test(r)};case "notMatches":return t=>{let r=o(t),u=s(t);return typeof r!="string"||typeof u!="string"?true:!new RegExp(u).test(r)};case "startsWith":return t=>{let r=o(t),u=s(t);return typeof r=="string"&&typeof u=="string"&&r.startsWith(u)};case "endsWith":return t=>{let r=o(t),u=s(t);return typeof r=="string"&&typeof u=="string"&&r.endsWith(u)}}}function kn(n,e,i,f){let o=n.conditions.map(t=>m(t,e,i,f)),s=o.length;if(s===1)return t=>!!o[0](t);if(s===2){let[t,r]=o;return n.logic==="AND"?u=>!!t(u)&&!!r(u):u=>!!t(u)||!!r(u)}if(s===3){let[t,r,u]=o;return n.logic==="AND"?l=>!!t(l)&&!!r(l)&&!!u(l):l=>!!t(l)||!!r(l)||!!u(l)}return n.logic==="AND"?t=>{for(let r=0;r<s;r++)if(!o[r](t))return false;return true}:t=>{for(let r=0;r<s;r++)if(o[r](t))return true;return false}}function Sn(n,e,i={}){return N(n,i).fn(e)}var v=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let f=JSON.stringify(e),o=this.cache.get(f);if(o)return this.cache.delete(f),this.cache.set(f,o),o;let s=N(e,i);if(this.cache.size>=this._maxSize){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}return this.cache.set(f,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},Y=new v;function wn(n,e={}){return Y.get(n,e)}function z(n,e="root",i={}){let f=[];return S(n,e,f,i),{valid:f.length===0,errors:f}}function S(n,e,i,f){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t=0;t<n.length;t++)S(n[t],`${e}[${t}]`,i,f);return}if(h(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(A(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):S(n.$if,`${e}.$if`,i,f),S(n.then,`${e}.then`,i,f),n.else!==void 0&&S(n.else,`${e}.else`,i,f);return}if(k(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let t=0;t<n.$pipe.length;t++)S(n.$pipe[t],`${e}.$pipe[${t}]`,i,f);return}if($(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(f.scope&&!(n.$fn in f.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let t=0;t<n.args.length;t++){let r=n.args[t];typeof r!="function"&&S(r,`${e}.args[${t}]`,i,f);}return}if(E(n)){F.has(n.op)||i.push(`${e}: invalid operator "${n.op}"`),S(n.left,`${e}.left`,i,f),n.right!==void 0&&S(n.right,`${e}.right`,i,f);return}if(T(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&i.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){i.push(`${e}.conditions: must be an array`);return}for(let t=0;t<n.conditions.length;t++)S(n.conditions[t],`${e}.conditions[${t}]`,i,f);return}let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];S(o[r],`${e}.${r}`,i,f);}}function Rn(n,e={}){let i=z(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function Fn(n,e={}){return z(n,"root",e).valid}var Z={};sn(Z,{$:()=>H,$cond:()=>X,$fn:()=>Q,$if:()=>jn,$pipe:()=>U,cond:()=>vn,fn:()=>On,pipe:()=>Nn,ref:()=>xn});function H(n){return {$:n}}var xn=H;function Q(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var On=Q;function jn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function U(...n){return {$pipe:n}}var Nn=U;function X(n,e,i){return i===void 0?{left:n,op:e}:{left:n,op:e,right:i}}var vn=X;var L="data",nn="scope",Gn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function W(n,e={}){let{dataParam:i=L,scopeParam:f=nn,noPrefixes:o=false,useAccessor:s=false,lexicalPrefix:t,transforms:r}=e;return y(n,i,f,o,s,t,r)}function y(n,e,i,f,o,s,t){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(r=>y(r,e,i,f,o,s,t)));if(h(n))return Wn(n.$,e,f,o,s);if(A(n))return In(n,e,i,f,o,s,t);if(k(n))return zn(n.$pipe,e,i,f,o,s,t);if($(n))return Mn(n,e,i,f,o,s,t);if(E(n))return Ln(n,e,i,f,o,s,t);if(T(n))return Vn(n,e,i,f,o,s,t);if(t&&typeof n=="object"){let r=n,u=Object.keys(r);for(let l=0;l<u.length;l++){let a=u[l];if(a in t){let p=t[a](r);if(typeof p=="object"&&p!==null&&a in p)throw new Error(`Transform "${a}" returned object with same key \u2014 infinite loop`);return y(p,e,i,f,o,s,t)}}}if(typeof n=="object"){let u=Object.entries(n).map(([l,a])=>omniAst.builders.property(omniAst.builders.identifier(l),y(a,e,i,f,o,s,t)));return omniAst.builders.objectExpression(u)}return omniAst.builders.literal(null)}var V="accessor";function _(n,e){return e?n===e||n.startsWith(e+"."):false}function Wn(n,e,i,f,o){return f?_(n,o)?R(n,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(n),omniAst.builders.identifier(e)]):n.includes("[*]")?Dn(n,e,i):R(n,e,i)}function R(n,e,i){let f=G(n);if(f.length===0)return i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e);let o;if(i){let s=f[0];o=omniAst.builders.identifier(s.value);for(let t=1;t<f.length;t++){let r=f[t];r.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(r.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(r.value),true,true);}}else {o=omniAst.builders.identifier(e);for(let s of f)s.type==="key"?o=omniAst.builders.memberExpression(o,omniAst.builders.identifier(s.value),false,true):o=omniAst.builders.memberExpression(o,omniAst.builders.literal(s.value),true,true);}return o}function Dn(n,e,i){let f=n.indexOf("[*]"),o=n.slice(0,f),s=n.slice(f+3),t;if(o?t=R(o,e,i):t=i?omniAst.builders.identifier("undefined"):omniAst.builders.identifier(e),!s||s==="")return t;if(s.includes("[*]"))return en(t,s);let r="_i",u=s.startsWith(".")?s.slice(1):s,l=omniAst.builders.identifier(r);if(u){let a=G(u);for(let p of a)p.type==="key"?l=omniAst.builders.memberExpression(l,omniAst.builders.identifier(p.value),false,true):l=omniAst.builders.memberExpression(l,omniAst.builders.literal(p.value),true,true);}return omniAst.builders.callExpression(omniAst.builders.memberExpression(t,omniAst.builders.identifier("map"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(r)],l)])}function en(n,e){let i=e.indexOf("[*]"),f=e.slice(0,i),o=e.slice(i+3),s="_i",t=f.startsWith(".")?f.slice(1):f,r=omniAst.builders.identifier(s);if(t){let l=G(t);for(let a of l)a.type==="key"&&(r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(a.value),false,true));}if(o.includes("[*]")){let l=en(r,o);return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],l)])}let u=o.startsWith(".")?o.slice(1):o;if(u){let l=G(u);for(let a of l)a.type==="key"&&(r=omniAst.builders.memberExpression(r,omniAst.builders.identifier(a.value),false,true));}return omniAst.builders.callExpression(omniAst.builders.memberExpression(n,omniAst.builders.identifier("flatMap"),false,true),[omniAst.builders.arrowFunctionExpression([omniAst.builders.identifier(s)],r)])}function In(n,e,i,f,o,s,t){let r;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;o?_(p,s)?d=R(p,e,true):d=omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(p),omniAst.builders.identifier(e)]):d=R(p,e,f),r=a?omniAst.builders.unaryExpression("!",d):d;}else r=y(n.$if,e,i,f,o,s,t);let u=y(n.then,e,i,f,o,s,t),l=n.else!==void 0?y(n.else,e,i,f,o,s,t):omniAst.builders.identifier("undefined");return omniAst.builders.conditionalExpression(r,u,l)}function Mn(n,e,i,f,o,s,t){let r=f?omniAst.builders.identifier(n.$fn):omniAst.builders.memberExpression(omniAst.builders.identifier(i),omniAst.builders.identifier(n.$fn),false,false);if(n.args===void 0)return r;let u=n.args.map(l=>typeof l=="function"?omniAst.builders.literal(null):y(l,e,i,f,o,s,t));return omniAst.builders.callExpression(r,u)}function zn(n,e,i,f,o,s,t){if(n.length===0)return omniAst.builders.identifier("undefined");if(n.length===1)return y(n[0],e,i,f,o,s,t);let r=y(n[0],e,i,f,o,s,t);for(let u=1;u<n.length;u++){let l=y(n[u],e,i,f,o,s,t);r=omniAst.builders.callExpression(l,[r]);}return r}function P(n,e,i,f,o,s,t){if(h(n)){let r=n.$;return o?_(r,s)?R(r,e,true):omniAst.builders.callExpression(omniAst.builders.identifier(V),[omniAst.builders.literal(r),omniAst.builders.identifier(e)]):R(r,e,f)}return y(n,e,i,f,o,s,t)}function Ln(n,e,i,f,o,s,t){let r=P(n.left,e,i,f,o,s,t),u=n.right!==void 0?P(n.right,e,i,f,o,s,t):omniAst.builders.literal(null),l=Gn[n.op];if(l)return omniAst.builders.binaryExpression(l,r,u);switch(n.op){case "in":return omniAst.builders.callExpression(omniAst.builders.memberExpression(u,omniAst.builders.identifier("includes")),[r]);case "notIn":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(u,omniAst.builders.identifier("includes")),[r]));case "contains":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("includes"),false,true),[u]);case "notContains":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("includes"),false,true),[u]));case "exists":return omniAst.builders.binaryExpression("!=",r,omniAst.builders.literal(null));case "notExists":return omniAst.builders.binaryExpression("==",r,omniAst.builders.literal(null));case "matches":return omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[u]),omniAst.builders.identifier("test")),[r]);case "notMatches":return omniAst.builders.unaryExpression("!",omniAst.builders.callExpression(omniAst.builders.memberExpression(omniAst.builders.newExpression(omniAst.builders.identifier("RegExp"),[u]),omniAst.builders.identifier("test")),[r]));case "startsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("startsWith"),false,true),[u]);case "endsWith":return omniAst.builders.callExpression(omniAst.builders.memberExpression(r,omniAst.builders.identifier("endsWith"),false,true),[u]);default:return omniAst.builders.binaryExpression("===",r,u)}}function Vn(n,e,i,f,o,s,t){let{logic:r,conditions:u}=n,l=r==="AND"?"&&":"||";if(u.length===0)return omniAst.builders.literal(r==="AND");if(u.length===1)return y(u[0],e,i,f,o,s,t);let a=y(u[0],e,i,f,o,s,t);for(let p=1;p<u.length;p++){let d=y(u[p],e,i,f,o,s,t);a=omniAst.builders.logicalExpression(l,a,d);}return a}function G(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r!=="*"){let u=parseInt(r,10);e.push({type:"index",value:isNaN(u)?r:u});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function tn(n,e=[L]){return omniAst.builders.arrowFunctionExpression(e.map(i=>omniAst.builders.identifier(i)),n)}function D(n,e){let i=new Set;return C(n,i,e),i}function C(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o of n)C(o,e,i);return}if(h(n))return;if(A(n)){C(n.$if,e,i),C(n.then,e,i),n.else!==void 0&&C(n.else,e,i);return}if(k(n)){for(let o of n.$pipe)C(o,e,i);return}if($(n)){if(e.add(n.$fn),n.args)for(let o of n.args)typeof o!="function"&&C(o,e,i);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&C(n.left,e,i),n.right!==void 0&&typeof n.right=="object"&&C(n.right,e,i);return}if(T(n)){for(let o of n.conditions)C(o,e,i);return}if(i){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in i){let u=i[r](o);if(typeof u=="object"&&u!==null&&r in u)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);C(u,e,i);return}}}let f=n;for(let o of Object.keys(f))C(f[o],e,i);}function I(n){let e=new Set;for(let i of n){let f=i.indexOf("."),o=i.indexOf("["),s=i.length;f!==-1&&(s=Math.min(s,f)),o!==-1&&(s=Math.min(s,o));let t=i.slice(0,s);t&&e.add(t);}return e}function qn(n){return JSON.stringify(n)}function Jn(n,e,i,f,o,s){let t=W(n,{noPrefixes:true,useAccessor:f,lexicalPrefix:o,transforms:s}),r=omniAst.generate(t),u="";f?o&&(u=`const{${o}}=data??{};`):e.size>0&&(u=`const{${[...e].join(",")}}=data??{};`);let l=f?new Set([...i,"accessor"]):i,a=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return a?`(function(scope){${a}return function(data){${u}return ${r}}})`:`(function(){return function(data){${u}return ${r}}})`}function q(n,e={}){let{scope:i={},returnCode:f=false,useAccessor:o=false,lexicalPrefix:s,transforms:t}=e,r=w(n,t),u=I(r),l=D(n,t),a=qn(n),p=Jn(n,u,l,o,s,t);if(f)return {code:p,deps:r,hash:a,dataRoots:[...u],scopeFns:[...l]};let d;try{d=new Function(`return ${p}`)()(i);}catch(g){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${g instanceof Error?g.message:String(g)}`)}return {fn:d,deps:r,hash:a}}function on(n,e,i={}){let{fn:f}=q(n,i);return f(e)}var Te="1.2.1";exports.ExpressionCache=v;exports.VERSION=Te;exports.assertValid=Rn;exports.builders=Z;exports.cache=Y;exports.cached=wn;exports.clearPathCache=gn;exports.compile=N;exports.compileAST=q;exports.compilePath=O;exports.dslToAST=W;exports.evaluate=Sn;exports.evaluateAST=on;exports.extractDataRoots=I;exports.extractDeps=w;exports.extractScopeFns=D;exports.get=dn;exports.getPathCacheSize=yn;exports.hasDeps=mn;exports.hasWildcard=B;exports.isCondition=E;exports.isConditionExpr=un;exports.isConditionGroup=T;exports.isConditional=A;exports.isFn=$;exports.isLiteral=M;exports.isPipe=k;exports.isPure=hn;exports.isRef=h;exports.isTransformResult=fn;exports.isValid=Fn;exports.normalizePath=j;exports.validate=z;exports.wrapInFunction=tn;
|
package/dist/index.d.cts
CHANGED
|
@@ -47,6 +47,29 @@ interface FnExpr {
|
|
|
47
47
|
$fn: string;
|
|
48
48
|
args?: FnArg[];
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Result of a transform function.
|
|
52
|
+
*
|
|
53
|
+
* Structurally similar to FnExpr but with relaxed args typing.
|
|
54
|
+
* Transforms can return domain-specific types in args (e.g., TestSpec, QueryParams)
|
|
55
|
+
* that will be passed to the scope function at runtime.
|
|
56
|
+
*
|
|
57
|
+
* Type safety for these args comes from the scope function implementation,
|
|
58
|
+
* not from the expression type system.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* // Transform for $test
|
|
63
|
+
* const transform: TransformFn = (node) => ({
|
|
64
|
+
* $fn: "__test",
|
|
65
|
+
* args: [node.$test] // TestSpec, not FnArg
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
interface TransformResult {
|
|
70
|
+
readonly $fn: string;
|
|
71
|
+
readonly args?: unknown[];
|
|
72
|
+
}
|
|
50
73
|
/**
|
|
51
74
|
* Pipe expression (DSL syntax for composition with initial value)
|
|
52
75
|
* First element is the initial value, rest are transformers
|
|
@@ -133,14 +156,31 @@ interface ValidationResult {
|
|
|
133
156
|
* @example { add, subtract, multiply, filter, map, sum }
|
|
134
157
|
*/
|
|
135
158
|
type Scope = Record<string, (...args: any[]) => any>;
|
|
136
|
-
/**
|
|
137
|
-
* Options for compilation
|
|
138
|
-
*/
|
|
139
159
|
/**
|
|
140
160
|
* Transform function for custom expression types.
|
|
141
|
-
*
|
|
161
|
+
*
|
|
162
|
+
* Receives the raw node (e.g., { $query: "name", params: {...} }) and returns:
|
|
163
|
+
* - Expression: for complex rewrites to standard expressions
|
|
164
|
+
* - TransformResult: for $fn calls with domain-specific args
|
|
165
|
+
*
|
|
166
|
+
* The returned value is compiled recursively. TransformResult args are
|
|
167
|
+
* compiled as literals and passed to the scope function at runtime.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* const transforms = {
|
|
172
|
+
* $query: (node) => ({
|
|
173
|
+
* $fn: "__query",
|
|
174
|
+
* args: [node.$query, node.params ?? {}]
|
|
175
|
+
* }),
|
|
176
|
+
* $test: (node) => ({
|
|
177
|
+
* $fn: "__test",
|
|
178
|
+
* args: [node.$test] // TestSpec passed as-is
|
|
179
|
+
* }),
|
|
180
|
+
* };
|
|
181
|
+
* ```
|
|
142
182
|
*/
|
|
143
|
-
type TransformFn = (node: Record<string, unknown>) => Expression;
|
|
183
|
+
type TransformFn = (node: Record<string, unknown>) => Expression | TransformResult;
|
|
144
184
|
/**
|
|
145
185
|
* Options for compilation
|
|
146
186
|
*/
|
|
@@ -176,6 +216,12 @@ declare const isConditional: (v: unknown) => v is ConditionalExpr;
|
|
|
176
216
|
* Check if value is a function call expression
|
|
177
217
|
*/
|
|
178
218
|
declare const isFn: (v: unknown) => v is FnExpr;
|
|
219
|
+
/**
|
|
220
|
+
* Check if value is a transform result.
|
|
221
|
+
* Note: TransformResult is structurally similar to FnExpr,
|
|
222
|
+
* so this guard is mainly for documentation/intent.
|
|
223
|
+
*/
|
|
224
|
+
declare const isTransformResult: (v: unknown) => v is TransformResult;
|
|
179
225
|
/**
|
|
180
226
|
* Check if value is a pipe expression
|
|
181
227
|
*/
|
|
@@ -750,6 +796,6 @@ declare function evaluateAST<T = unknown, R = unknown>(expr: Expression, data: T
|
|
|
750
796
|
* ); // 3
|
|
751
797
|
* ```
|
|
752
798
|
*/
|
|
753
|
-
declare const VERSION = "
|
|
799
|
+
declare const VERSION = "1.2.1";
|
|
754
800
|
|
|
755
|
-
export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCache, type FnArg, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate, wrapInFunction };
|
|
801
|
+
export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCache, type FnArg, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, type TransformResult, 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, isTransformResult, isValid, normalizePath, validate, wrapInFunction };
|
package/dist/index.d.ts
CHANGED
|
@@ -47,6 +47,29 @@ interface FnExpr {
|
|
|
47
47
|
$fn: string;
|
|
48
48
|
args?: FnArg[];
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Result of a transform function.
|
|
52
|
+
*
|
|
53
|
+
* Structurally similar to FnExpr but with relaxed args typing.
|
|
54
|
+
* Transforms can return domain-specific types in args (e.g., TestSpec, QueryParams)
|
|
55
|
+
* that will be passed to the scope function at runtime.
|
|
56
|
+
*
|
|
57
|
+
* Type safety for these args comes from the scope function implementation,
|
|
58
|
+
* not from the expression type system.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* // Transform for $test
|
|
63
|
+
* const transform: TransformFn = (node) => ({
|
|
64
|
+
* $fn: "__test",
|
|
65
|
+
* args: [node.$test] // TestSpec, not FnArg
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
interface TransformResult {
|
|
70
|
+
readonly $fn: string;
|
|
71
|
+
readonly args?: unknown[];
|
|
72
|
+
}
|
|
50
73
|
/**
|
|
51
74
|
* Pipe expression (DSL syntax for composition with initial value)
|
|
52
75
|
* First element is the initial value, rest are transformers
|
|
@@ -133,14 +156,31 @@ interface ValidationResult {
|
|
|
133
156
|
* @example { add, subtract, multiply, filter, map, sum }
|
|
134
157
|
*/
|
|
135
158
|
type Scope = Record<string, (...args: any[]) => any>;
|
|
136
|
-
/**
|
|
137
|
-
* Options for compilation
|
|
138
|
-
*/
|
|
139
159
|
/**
|
|
140
160
|
* Transform function for custom expression types.
|
|
141
|
-
*
|
|
161
|
+
*
|
|
162
|
+
* Receives the raw node (e.g., { $query: "name", params: {...} }) and returns:
|
|
163
|
+
* - Expression: for complex rewrites to standard expressions
|
|
164
|
+
* - TransformResult: for $fn calls with domain-specific args
|
|
165
|
+
*
|
|
166
|
+
* The returned value is compiled recursively. TransformResult args are
|
|
167
|
+
* compiled as literals and passed to the scope function at runtime.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* const transforms = {
|
|
172
|
+
* $query: (node) => ({
|
|
173
|
+
* $fn: "__query",
|
|
174
|
+
* args: [node.$query, node.params ?? {}]
|
|
175
|
+
* }),
|
|
176
|
+
* $test: (node) => ({
|
|
177
|
+
* $fn: "__test",
|
|
178
|
+
* args: [node.$test] // TestSpec passed as-is
|
|
179
|
+
* }),
|
|
180
|
+
* };
|
|
181
|
+
* ```
|
|
142
182
|
*/
|
|
143
|
-
type TransformFn = (node: Record<string, unknown>) => Expression;
|
|
183
|
+
type TransformFn = (node: Record<string, unknown>) => Expression | TransformResult;
|
|
144
184
|
/**
|
|
145
185
|
* Options for compilation
|
|
146
186
|
*/
|
|
@@ -176,6 +216,12 @@ declare const isConditional: (v: unknown) => v is ConditionalExpr;
|
|
|
176
216
|
* Check if value is a function call expression
|
|
177
217
|
*/
|
|
178
218
|
declare const isFn: (v: unknown) => v is FnExpr;
|
|
219
|
+
/**
|
|
220
|
+
* Check if value is a transform result.
|
|
221
|
+
* Note: TransformResult is structurally similar to FnExpr,
|
|
222
|
+
* so this guard is mainly for documentation/intent.
|
|
223
|
+
*/
|
|
224
|
+
declare const isTransformResult: (v: unknown) => v is TransformResult;
|
|
179
225
|
/**
|
|
180
226
|
* Check if value is a pipe expression
|
|
181
227
|
*/
|
|
@@ -750,6 +796,6 @@ declare function evaluateAST<T = unknown, R = unknown>(expr: Expression, data: T
|
|
|
750
796
|
* ); // 3
|
|
751
797
|
* ```
|
|
752
798
|
*/
|
|
753
|
-
declare const VERSION = "
|
|
799
|
+
declare const VERSION = "1.2.1";
|
|
754
800
|
|
|
755
|
-
export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCache, type FnArg, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, VERSION, type ValidateOptions, type ValidationResult, assertValid, builders, cache, cached, clearPathCache, compile, compileAST, compilePath, dslToAST, evaluate, evaluateAST, extractDataRoots, extractDeps, extractScopeFns, get, getPathCacheSize, hasDeps, hasWildcard, isCondition, isConditionExpr, isConditionGroup, isConditional, isFn, isLiteral, isPipe, isPure, isRef, isValid, normalizePath, validate, wrapInFunction };
|
|
801
|
+
export { type AccessorFn, type CallbackFn, type CompileASTCodeResult, type CompileASTOptions, type CompileOptions, type CompiledExpression, type CompiledFn, type Condition, type ConditionExpr, type ConditionGroup, type ConditionOp, type ConditionalExpr, type Expression, ExpressionCache, type FnArg, type FnExpr, type Literal, type PathGetter, type PipeExpr, type RefExpr, type Scope, type TransformFn, type TransformOptions, type TransformResult, 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, isTransformResult, isValid, normalizePath, validate, wrapInFunction };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {builders,generate}from'omni-ast';var rn=Object.defineProperty;var sn=(n,e)=>{for(var i in e)rn(n,i,{get:e[i],enumerable:true});};var h=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,A=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,$=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",k=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),F=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&&F.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),T=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,fn=n=>E(n)||T(n),M=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,f="left"in i&&"op"in i&&F.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!f&&!("logic"in i)}return false};var x=new Map;function J(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r==="*")e.push({type:"wildcard",value:"*"});else {let c=parseInt(r,10);e.push({type:"index",value:isNaN(c)?r:c});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function B(n){return n.includes("[*]")}function O(n){let e=x.get(n);return e||(e=B(n)?un(n):cn(n),x.set(n,e),e)}function cn(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=J(n),i=e.length;if(i===2){let[o,s]=e,t=o.value,r=s.value;return c=>c?.[t]?.[r]}if(i===3){let[o,s,t]=e,r=o.value,c=s.value,l=t.value;return a=>a?.[r]?.[c]?.[l]}let f=e.map(o=>o.value);return o=>{let s=o;for(let t=0;t<i&&s!=null;t++)s=s[f[t]];return s}}function un(n){let e=J(n),i=[];for(let f=0;f<e.length;f++)e[f].type==="wildcard"&&i.push(f);return i.length===1?ln(e,i[0]):an(e,i)}function ln(n,e){let i=n.slice(0,e).map(t=>t.value),f=n.slice(e+1).map(t=>t.value),o=i.length,s=f.length;if(s===0){if(o===1){let t=i[0];return r=>r?.[t]}return t=>{let r=t;for(let c=0;c<o&&r!=null;c++)r=r[i[c]];return r}}if(s===1){let t=f[0];if(o===1){let r=i[0];return c=>{let l=c?.[r];if(Array.isArray(l))return l.map(a=>a?.[t])}}return r=>{let c=r;for(let l=0;l<o&&c!=null;l++)c=c[i[l]];if(Array.isArray(c))return c.map(l=>l?.[t])}}return t=>{let r=t;for(let c=0;c<o&&r!=null;c++)r=r[i[c]];if(Array.isArray(r))return r.map(c=>{let l=c;for(let a=0;a<s&&l!=null;a++)l=l[f[a]];return l})}}function an(n,e){let i=[],f=0;for(let s=0;s<e.length;s++){let t=e[s],r=s===e.length-1,c=n.slice(f,t).map(l=>l.value);c.length>0&&i.push({type:"access",keys:c}),i.push({type:r?"map":"flatMap",keys:[]}),f=t+1;}let o=n.slice(f).map(s=>s.value);return s=>{let t=s;for(let r of i){if(t==null)return;if(r.type==="access")for(let c of r.keys){if(t==null)return;t=t[c];}else if(r.type==="flatMap"){if(!Array.isArray(t))return;t=t.flatMap(c=>{let l=c;return Array.isArray(l)?l:[l]});}else if(r.type==="map"){if(!Array.isArray(t))return;o.length>0&&(t=t.map(c=>{let l=c;for(let a of o){if(l==null)return;l=l[a];}return l}));}}return t}}function pn(n,e){return O(e)(n)}function j(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function dn(){x.clear();}function gn(){return x.size}function w(n,e){let i=new Set;return b(n,i,e),Array.from(i)}function b(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)b(n[s],e,i);return}if(h(n)){e.add(j(n.$));return}if(A(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(j(s));}else b(n.$if,e,i);b(n.then,e,i),n.else!==void 0&&b(n.else,e,i);return}if(k(n)){for(let s=0;s<n.$pipe.length;s++)b(n.$pipe[s],e,i);return}if($(n)){if(n.args)for(let s=0;s<n.args.length;s++){let t=n.args[s];typeof t!="function"&&b(t,e,i);}return}if(E(n)){b(n.left,e,i),n.right!==void 0&&b(n.right,e,i);return}if(T(n)){for(let s=0;s<n.conditions.length;s++)b(n.conditions[s],e,i);return}if(i){let s=n,t=Object.keys(s);for(let r=0;r<t.length;r++){let c=t[r];if(c in i){let l=i[c](s);if(typeof l=="object"&&l!==null&&c in l)throw new Error(`Transform "${c}" returned object with same key \u2014 infinite loop`);b(l,e,i);return}}}let f=n,o=Object.keys(f);for(let s=0;s<o.length;s++)b(f[o[s]],e,i);}function yn(n){return w(n).length>0}function mn(n){return w(n).length===0}function hn(n){return JSON.stringify(n)}function N(n,e={}){let i=e.scope??{},f=e.accessor,o=e.transforms,s=m(n,i,f,o),t=w(n,o),r=hn(n);return {fn:s,deps:t,hash:r}}function m(n,e,i,f){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let o=n.map(s=>m(s,e,i,f));return s=>o.map(t=>t(s))}if(h(n))return En(n,i);if(A(n))return Tn(n,e,i,f);if(k(n))return bn(n,e,i,f);if($(n))return Cn(n,e,i,f);if(E(n))return An(n,e,i,f);if(T(n))return $n(n,e,i,f);if(f){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in f){let c=f[r](o);if(typeof c=="object"&&c!==null&&r in c)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);return m(c,e,i,f)}}}if(M(n)){let o=n,s=Object.keys(o),t=s.map(r=>m(o[r],e,i,f));return r=>{let c={};for(let l=0;l<s.length;l++)c[s[l]]=t[l](r);return c}}return ()=>n}function K(n,e){return e?i=>e(n,i):O(n)}function En(n,e){return K(n.$,e)}function Tn(n,e,i,f){let o;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,c=K(r,i);o=n.$if.startsWith("!")?a=>!c(a):a=>!!c(a);}else {let r=m(n.$if,e,i,f);o=c=>!!r(c);}let s=m(n.then,e,i,f),t=n.else!==void 0?m(n.else,e,i,f):()=>{};return r=>o(r)?s(r):t(r)}function bn(n,e,i,f){let o=n.$pipe;if(o.length===0)return ()=>{};if(o.length===1)return m(o[0],e,i,f);let s=m(o[0],e,i,f),t=o.slice(1).map(c=>m(c,e,i,f)),r=t.length;if(r===1){let[c]=t;return l=>{let a=s(l),p=c(l);return typeof p=="function"?p(a):p}}if(r===2){let[c,l]=t;return a=>{let p=s(a),d=c(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(r===3){let[c,l,a]=t;return p=>{let d=s(p),g=c(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 c=>{let l=s(c);for(let a=0;a<r;a++){let p=t[a](c);l=typeof p=="function"?p(l):p;}return l}}function Cn(n,e,i,f){let o=n.$fn,s=n.args;if(s===void 0)return ()=>{let c=e[o];if(!c)throw new Error(`Function not found in scope: ${o}`);return c};let t=s.map(c=>typeof c=="function"?()=>c:m(c,e,i,f)),r=t.length;if(r===0)return c=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l()};if(r===1){let[c]=t;return l=>{let a=e[o];if(!a)throw new Error(`Function not found in scope: ${o}`);return a(c(l))}}if(r===2){let[c,l]=t;return a=>{let p=e[o];if(!p)throw new Error(`Function not found in scope: ${o}`);return p(c(a),l(a))}}if(r===3){let[c,l,a]=t;return p=>{let d=e[o];if(!d)throw new Error(`Function not found in scope: ${o}`);return d(c(p),l(p),a(p))}}return c=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l(...t.map(a=>a(c)))}}function An(n,e,i,f){let o=m(n.left,e,i,f),s=n.right!==void 0?m(n.right,e,i,f):()=>{};switch(n.op){case "eq":return t=>o(t)===s(t);case "neq":return t=>o(t)!==s(t);case "gt":return t=>o(t)>s(t);case "gte":return t=>o(t)>=s(t);case "lt":return t=>o(t)<s(t);case "lte":return t=>o(t)<=s(t);case "in":return t=>{let r=s(t);return Array.isArray(r)&&r.includes(o(t))};case "notIn":return t=>{let r=s(t);return !Array.isArray(r)||!r.includes(o(t))};case "contains":return t=>{let r=o(t);return Array.isArray(r)&&r.includes(s(t))};case "notContains":return t=>{let r=o(t);return !Array.isArray(r)||!r.includes(s(t))};case "exists":return t=>o(t)!==void 0;case "notExists":return t=>o(t)===void 0;case "matches":return t=>{let r=o(t),c=s(t);return typeof r!="string"||typeof c!="string"?false:new RegExp(c).test(r)};case "notMatches":return t=>{let r=o(t),c=s(t);return typeof r!="string"||typeof c!="string"?true:!new RegExp(c).test(r)};case "startsWith":return t=>{let r=o(t),c=s(t);return typeof r=="string"&&typeof c=="string"&&r.startsWith(c)};case "endsWith":return t=>{let r=o(t),c=s(t);return typeof r=="string"&&typeof c=="string"&&r.endsWith(c)}}}function $n(n,e,i,f){let o=n.conditions.map(t=>m(t,e,i,f)),s=o.length;if(s===1)return t=>!!o[0](t);if(s===2){let[t,r]=o;return n.logic==="AND"?c=>!!t(c)&&!!r(c):c=>!!t(c)||!!r(c)}if(s===3){let[t,r,c]=o;return n.logic==="AND"?l=>!!t(l)&&!!r(l)&&!!c(l):l=>!!t(l)||!!r(l)||!!c(l)}return n.logic==="AND"?t=>{for(let r=0;r<s;r++)if(!o[r](t))return false;return true}:t=>{for(let r=0;r<s;r++)if(o[r](t))return true;return false}}function kn(n,e,i={}){return N(n,i).fn(e)}var v=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let f=JSON.stringify(e),o=this.cache.get(f);if(o)return this.cache.delete(f),this.cache.set(f,o),o;let s=N(e,i);if(this.cache.size>=this._maxSize){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}return this.cache.set(f,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},Y=new v;function Sn(n,e={}){return Y.get(n,e)}function z(n,e="root",i={}){let f=[];return S(n,e,f,i),{valid:f.length===0,errors:f}}function S(n,e,i,f){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t=0;t<n.length;t++)S(n[t],`${e}[${t}]`,i,f);return}if(h(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(A(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):S(n.$if,`${e}.$if`,i,f),S(n.then,`${e}.then`,i,f),n.else!==void 0&&S(n.else,`${e}.else`,i,f);return}if(k(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let t=0;t<n.$pipe.length;t++)S(n.$pipe[t],`${e}.$pipe[${t}]`,i,f);return}if($(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(f.scope&&!(n.$fn in f.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let t=0;t<n.args.length;t++){let r=n.args[t];typeof r!="function"&&S(r,`${e}.args[${t}]`,i,f);}return}if(E(n)){F.has(n.op)||i.push(`${e}: invalid operator "${n.op}"`),S(n.left,`${e}.left`,i,f),n.right!==void 0&&S(n.right,`${e}.right`,i,f);return}if(T(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&i.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){i.push(`${e}.conditions: must be an array`);return}for(let t=0;t<n.conditions.length;t++)S(n.conditions[t],`${e}.conditions[${t}]`,i,f);return}let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];S(o[r],`${e}.${r}`,i,f);}}function wn(n,e={}){let i=z(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function Rn(n,e={}){return z(n,"root",e).valid}var Z={};sn(Z,{$:()=>H,$cond:()=>X,$fn:()=>Q,$if:()=>On,$pipe:()=>U,cond:()=>Nn,fn:()=>xn,pipe:()=>jn,ref:()=>Fn});function H(n){return {$:n}}var Fn=H;function Q(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var xn=Q;function On(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function U(...n){return {$pipe:n}}var jn=U;function X(n,e,i){return i===void 0?{left:n,op:e}:{left:n,op:e,right:i}}var Nn=X;var L="data",nn="scope",vn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function W(n,e={}){let{dataParam:i=L,scopeParam:f=nn,noPrefixes:o=false,useAccessor:s=false,lexicalPrefix:t,transforms:r}=e;return y(n,i,f,o,s,t,r)}function y(n,e,i,f,o,s,t){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(r=>y(r,e,i,f,o,s,t)));if(h(n))return Gn(n.$,e,f,o,s);if(A(n))return Dn(n,e,i,f,o,s,t);if(k(n))return Mn(n.$pipe,e,i,f,o,s,t);if($(n))return In(n,e,i,f,o,s,t);if(E(n))return zn(n,e,i,f,o,s,t);if(T(n))return Ln(n,e,i,f,o,s,t);if(t&&typeof n=="object"){let r=n,c=Object.keys(r);for(let l=0;l<c.length;l++){let a=c[l];if(a in t){let p=t[a](r);if(typeof p=="object"&&p!==null&&a in p)throw new Error(`Transform "${a}" returned object with same key \u2014 infinite loop`);return y(p,e,i,f,o,s,t)}}}if(typeof n=="object"){let c=Object.entries(n).map(([l,a])=>builders.property(builders.identifier(l),y(a,e,i,f,o,s,t)));return builders.objectExpression(c)}return builders.literal(null)}var V="accessor";function _(n,e){return e?n===e||n.startsWith(e+"."):false}function Gn(n,e,i,f,o){return f?_(n,o)?R(n,e,true):builders.callExpression(builders.identifier(V),[builders.literal(n),builders.identifier(e)]):n.includes("[*]")?Wn(n,e,i):R(n,e,i)}function R(n,e,i){let f=G(n);if(f.length===0)return i?builders.identifier("undefined"):builders.identifier(e);let o;if(i){let s=f[0];o=builders.identifier(s.value);for(let t=1;t<f.length;t++){let r=f[t];r.type==="key"?o=builders.memberExpression(o,builders.identifier(r.value),false,true):o=builders.memberExpression(o,builders.literal(r.value),true,true);}}else {o=builders.identifier(e);for(let s of f)s.type==="key"?o=builders.memberExpression(o,builders.identifier(s.value),false,true):o=builders.memberExpression(o,builders.literal(s.value),true,true);}return o}function Wn(n,e,i){let f=n.indexOf("[*]"),o=n.slice(0,f),s=n.slice(f+3),t;if(o?t=R(o,e,i):t=i?builders.identifier("undefined"):builders.identifier(e),!s||s==="")return t;if(s.includes("[*]"))return en(t,s);let r="_i",c=s.startsWith(".")?s.slice(1):s,l=builders.identifier(r);if(c){let a=G(c);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(t,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(r)],l)])}function en(n,e){let i=e.indexOf("[*]"),f=e.slice(0,i),o=e.slice(i+3),s="_i",t=f.startsWith(".")?f.slice(1):f,r=builders.identifier(s);if(t){let l=G(t);for(let a of l)a.type==="key"&&(r=builders.memberExpression(r,builders.identifier(a.value),false,true));}if(o.includes("[*]")){let l=en(r,o);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],l)])}let c=o.startsWith(".")?o.slice(1):o;if(c){let l=G(c);for(let a of l)a.type==="key"&&(r=builders.memberExpression(r,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],r)])}function Dn(n,e,i,f,o,s,t){let r;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;o?_(p,s)?d=R(p,e,true):d=builders.callExpression(builders.identifier(V),[builders.literal(p),builders.identifier(e)]):d=R(p,e,f),r=a?builders.unaryExpression("!",d):d;}else r=y(n.$if,e,i,f,o,s,t);let c=y(n.then,e,i,f,o,s,t),l=n.else!==void 0?y(n.else,e,i,f,o,s,t):builders.identifier("undefined");return builders.conditionalExpression(r,c,l)}function In(n,e,i,f,o,s,t){let r=f?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(i),builders.identifier(n.$fn),false,false);if(n.args===void 0)return r;let c=n.args.map(l=>typeof l=="function"?builders.literal(null):y(l,e,i,f,o,s,t));return builders.callExpression(r,c)}function Mn(n,e,i,f,o,s,t){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return y(n[0],e,i,f,o,s,t);let r=y(n[0],e,i,f,o,s,t);for(let c=1;c<n.length;c++){let l=y(n[c],e,i,f,o,s,t);r=builders.callExpression(l,[r]);}return r}function P(n,e,i,f,o,s,t){if(h(n)){let r=n.$;return o?_(r,s)?R(r,e,true):builders.callExpression(builders.identifier(V),[builders.literal(r),builders.identifier(e)]):R(r,e,f)}return y(n,e,i,f,o,s,t)}function zn(n,e,i,f,o,s,t){let r=P(n.left,e,i,f,o,s,t),c=n.right!==void 0?P(n.right,e,i,f,o,s,t):builders.literal(null),l=vn[n.op];if(l)return builders.binaryExpression(l,r,c);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(c,builders.identifier("includes")),[r]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(c,builders.identifier("includes")),[r]));case "contains":return builders.callExpression(builders.memberExpression(r,builders.identifier("includes"),false,true),[c]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(r,builders.identifier("includes"),false,true),[c]));case "exists":return builders.binaryExpression("!=",r,builders.literal(null));case "notExists":return builders.binaryExpression("==",r,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[c]),builders.identifier("test")),[r]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[c]),builders.identifier("test")),[r]));case "startsWith":return builders.callExpression(builders.memberExpression(r,builders.identifier("startsWith"),false,true),[c]);case "endsWith":return builders.callExpression(builders.memberExpression(r,builders.identifier("endsWith"),false,true),[c]);default:return builders.binaryExpression("===",r,c)}}function Ln(n,e,i,f,o,s,t){let{logic:r,conditions:c}=n,l=r==="AND"?"&&":"||";if(c.length===0)return builders.literal(r==="AND");if(c.length===1)return y(c[0],e,i,f,o,s,t);let a=y(c[0],e,i,f,o,s,t);for(let p=1;p<c.length;p++){let d=y(c[p],e,i,f,o,s,t);a=builders.logicalExpression(l,a,d);}return a}function G(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r!=="*"){let c=parseInt(r,10);e.push({type:"index",value:isNaN(c)?r:c});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function tn(n,e=[L]){return builders.arrowFunctionExpression(e.map(i=>builders.identifier(i)),n)}function D(n,e){let i=new Set;return C(n,i,e),i}function C(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o of n)C(o,e,i);return}if(h(n))return;if(A(n)){C(n.$if,e,i),C(n.then,e,i),n.else!==void 0&&C(n.else,e,i);return}if(k(n)){for(let o of n.$pipe)C(o,e,i);return}if($(n)){if(e.add(n.$fn),n.args)for(let o of n.args)typeof o!="function"&&C(o,e,i);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&C(n.left,e,i),n.right!==void 0&&typeof n.right=="object"&&C(n.right,e,i);return}if(T(n)){for(let o of n.conditions)C(o,e,i);return}if(i){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in i){let c=i[r](o);if(typeof c=="object"&&c!==null&&r in c)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);C(c,e,i);return}}}let f=n;for(let o of Object.keys(f))C(f[o],e,i);}function I(n){let e=new Set;for(let i of n){let f=i.indexOf("."),o=i.indexOf("["),s=i.length;f!==-1&&(s=Math.min(s,f)),o!==-1&&(s=Math.min(s,o));let t=i.slice(0,s);t&&e.add(t);}return e}function _n(n){return JSON.stringify(n)}function qn(n,e,i,f,o,s){let t=W(n,{noPrefixes:true,useAccessor:f,lexicalPrefix:o,transforms:s}),r=generate(t),c="";f?o&&(c=`const{${o}}=data??{};`):e.size>0&&(c=`const{${[...e].join(",")}}=data??{};`);let l=f?new Set([...i,"accessor"]):i,a=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return a?`(function(scope){${a}return function(data){${c}return ${r}}})`:`(function(){return function(data){${c}return ${r}}})`}function q(n,e={}){let{scope:i={},returnCode:f=false,useAccessor:o=false,lexicalPrefix:s,transforms:t}=e,r=w(n,t),c=I(r),l=D(n,t),a=_n(n),p=qn(n,c,l,o,s,t);if(f)return {code:p,deps:r,hash:a,dataRoots:[...c],scopeFns:[...l]};let d;try{d=new Function(`return ${p}`)()(i);}catch(g){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${g instanceof Error?g.message:String(g)}`)}return {fn:d,deps:r,hash:a}}function on(n,e,i={}){let{fn:f}=q(n,i);return f(e)}var Ee="0.1.1";export{v as ExpressionCache,Ee as VERSION,wn as assertValid,Z as builders,Y as cache,Sn as cached,dn as clearPathCache,N as compile,q as compileAST,O as compilePath,W as dslToAST,kn as evaluate,on as evaluateAST,I as extractDataRoots,w as extractDeps,D as extractScopeFns,pn as get,gn as getPathCacheSize,yn as hasDeps,B as hasWildcard,E as isCondition,fn as isConditionExpr,T as isConditionGroup,A as isConditional,$ as isFn,M as isLiteral,k as isPipe,mn as isPure,h as isRef,Rn as isValid,j as normalizePath,z as validate,tn as wrapInFunction};
|
|
1
|
+
import {builders,generate}from'omni-ast';var rn=Object.defineProperty;var sn=(n,e)=>{for(var i in e)rn(n,i,{get:e[i],enumerable:true});};var h=n=>n!==null&&typeof n=="object"&&"$"in n&&typeof n.$=="string"&&Object.keys(n).length===1,A=n=>n!==null&&typeof n=="object"&&"$if"in n&&"then"in n,$=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",fn=n=>n!==null&&typeof n=="object"&&"$fn"in n&&typeof n.$fn=="string",k=n=>n!==null&&typeof n=="object"&&"$pipe"in n&&Array.isArray(n.$pipe),F=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&&F.has(n.op)&&!("$"in n)&&!("$if"in n)&&!("$fn"in n),T=n=>n!==null&&typeof n=="object"&&"logic"in n&&"conditions"in n,un=n=>E(n)||T(n),M=n=>{if(n===null)return true;let e=typeof n;if(e==="string"||e==="number"||e==="boolean"||Array.isArray(n))return true;if(e==="object"&&n!==null){let i=n,f="left"in i&&"op"in i&&F.has(i.op);return !("$"in i)&&!("$if"in i)&&!("$fn"in i)&&!("$pipe"in i)&&!f&&!("logic"in i)}return false};var x=new Map;function J(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r==="*")e.push({type:"wildcard",value:"*"});else {let u=parseInt(r,10);e.push({type:"index",value:isNaN(u)?r:u});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function B(n){return n.includes("[*]")}function O(n){let e=x.get(n);return e||(e=B(n)?ln(n):cn(n),x.set(n,e),e)}function cn(n){if(!n.includes(".")&&!n.includes("["))return o=>o?.[n];let e=J(n),i=e.length;if(i===2){let[o,s]=e,t=o.value,r=s.value;return u=>u?.[t]?.[r]}if(i===3){let[o,s,t]=e,r=o.value,u=s.value,l=t.value;return a=>a?.[r]?.[u]?.[l]}let f=e.map(o=>o.value);return o=>{let s=o;for(let t=0;t<i&&s!=null;t++)s=s[f[t]];return s}}function ln(n){let e=J(n),i=[];for(let f=0;f<e.length;f++)e[f].type==="wildcard"&&i.push(f);return i.length===1?an(e,i[0]):pn(e,i)}function an(n,e){let i=n.slice(0,e).map(t=>t.value),f=n.slice(e+1).map(t=>t.value),o=i.length,s=f.length;if(s===0){if(o===1){let t=i[0];return r=>r?.[t]}return t=>{let r=t;for(let u=0;u<o&&r!=null;u++)r=r[i[u]];return r}}if(s===1){let t=f[0];if(o===1){let r=i[0];return u=>{let l=u?.[r];if(Array.isArray(l))return l.map(a=>a?.[t])}}return r=>{let u=r;for(let l=0;l<o&&u!=null;l++)u=u[i[l]];if(Array.isArray(u))return u.map(l=>l?.[t])}}return t=>{let r=t;for(let u=0;u<o&&r!=null;u++)r=r[i[u]];if(Array.isArray(r))return r.map(u=>{let l=u;for(let a=0;a<s&&l!=null;a++)l=l[f[a]];return l})}}function pn(n,e){let i=[],f=0;for(let s=0;s<e.length;s++){let t=e[s],r=s===e.length-1,u=n.slice(f,t).map(l=>l.value);u.length>0&&i.push({type:"access",keys:u}),i.push({type:r?"map":"flatMap",keys:[]}),f=t+1;}let o=n.slice(f).map(s=>s.value);return s=>{let t=s;for(let r of i){if(t==null)return;if(r.type==="access")for(let u of r.keys){if(t==null)return;t=t[u];}else if(r.type==="flatMap"){if(!Array.isArray(t))return;t=t.flatMap(u=>{let l=u;return Array.isArray(l)?l:[l]});}else if(r.type==="map"){if(!Array.isArray(t))return;o.length>0&&(t=t.map(u=>{let l=u;for(let a of o){if(l==null)return;l=l[a];}return l}));}}return t}}function dn(n,e){return O(e)(n)}function j(n){let e=n.indexOf("[*]");return e===-1?n:n.slice(0,e)}function gn(){x.clear();}function yn(){return x.size}function w(n,e){let i=new Set;return b(n,i,e),Array.from(i)}function b(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let s=0;s<n.length;s++)b(n[s],e,i);return}if(h(n)){e.add(j(n.$));return}if(A(n)){if(typeof n.$if=="string"){let s=n.$if.startsWith("!")?n.$if.slice(1):n.$if;e.add(j(s));}else b(n.$if,e,i);b(n.then,e,i),n.else!==void 0&&b(n.else,e,i);return}if(k(n)){for(let s=0;s<n.$pipe.length;s++)b(n.$pipe[s],e,i);return}if($(n)){if(n.args)for(let s=0;s<n.args.length;s++){let t=n.args[s];typeof t!="function"&&b(t,e,i);}return}if(E(n)){b(n.left,e,i),n.right!==void 0&&b(n.right,e,i);return}if(T(n)){for(let s=0;s<n.conditions.length;s++)b(n.conditions[s],e,i);return}if(i){let s=n,t=Object.keys(s);for(let r=0;r<t.length;r++){let u=t[r];if(u in i){let l=i[u](s);if(typeof l=="object"&&l!==null&&u in l)throw new Error(`Transform "${u}" returned object with same key \u2014 infinite loop`);b(l,e,i);return}}}let f=n,o=Object.keys(f);for(let s=0;s<o.length;s++)b(f[o[s]],e,i);}function mn(n){return w(n).length>0}function hn(n){return w(n).length===0}function En(n){return JSON.stringify(n)}function N(n,e={}){let i=e.scope??{},f=e.accessor,o=e.transforms,s=m(n,i,f,o),t=w(n,o),r=En(n);return {fn:s,deps:t,hash:r}}function m(n,e,i,f){if(n===null)return ()=>null;if(typeof n!="object")return ()=>n;if(Array.isArray(n)){let o=n.map(s=>m(s,e,i,f));return s=>o.map(t=>t(s))}if(h(n))return Tn(n,i);if(A(n))return bn(n,e,i,f);if(k(n))return Cn(n,e,i,f);if($(n))return An(n,e,i,f);if(E(n))return $n(n,e,i,f);if(T(n))return kn(n,e,i,f);if(f){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in f){let u=f[r](o);if(typeof u=="object"&&u!==null&&r in u)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);return m(u,e,i,f)}}}if(M(n)){let o=n,s=Object.keys(o),t=s.map(r=>m(o[r],e,i,f));return r=>{let u={};for(let l=0;l<s.length;l++)u[s[l]]=t[l](r);return u}}return ()=>n}function K(n,e){return e?i=>e(n,i):O(n)}function Tn(n,e){return K(n.$,e)}function bn(n,e,i,f){let o;if(typeof n.$if=="string"){let r=n.$if.startsWith("!")?n.$if.slice(1):n.$if,u=K(r,i);o=n.$if.startsWith("!")?a=>!u(a):a=>!!u(a);}else {let r=m(n.$if,e,i,f);o=u=>!!r(u);}let s=m(n.then,e,i,f),t=n.else!==void 0?m(n.else,e,i,f):()=>{};return r=>o(r)?s(r):t(r)}function Cn(n,e,i,f){let o=n.$pipe;if(o.length===0)return ()=>{};if(o.length===1)return m(o[0],e,i,f);let s=m(o[0],e,i,f),t=o.slice(1).map(u=>m(u,e,i,f)),r=t.length;if(r===1){let[u]=t;return l=>{let a=s(l),p=u(l);return typeof p=="function"?p(a):p}}if(r===2){let[u,l]=t;return a=>{let p=s(a),d=u(a);return p=typeof d=="function"?d(p):d,d=l(a),typeof d=="function"?d(p):d}}if(r===3){let[u,l,a]=t;return p=>{let d=s(p),g=u(p);return d=typeof g=="function"?g(d):g,g=l(p),d=typeof g=="function"?g(d):g,g=a(p),typeof g=="function"?g(d):g}}return u=>{let l=s(u);for(let a=0;a<r;a++){let p=t[a](u);l=typeof p=="function"?p(l):p;}return l}}function An(n,e,i,f){let o=n.$fn,s=n.args;if(s===void 0)return ()=>{let u=e[o];if(!u)throw new Error(`Function not found in scope: ${o}`);return u};let t=s.map(u=>typeof u=="function"?()=>u:m(u,e,i,f)),r=t.length;if(r===0)return u=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l()};if(r===1){let[u]=t;return l=>{let a=e[o];if(!a)throw new Error(`Function not found in scope: ${o}`);return a(u(l))}}if(r===2){let[u,l]=t;return a=>{let p=e[o];if(!p)throw new Error(`Function not found in scope: ${o}`);return p(u(a),l(a))}}if(r===3){let[u,l,a]=t;return p=>{let d=e[o];if(!d)throw new Error(`Function not found in scope: ${o}`);return d(u(p),l(p),a(p))}}return u=>{let l=e[o];if(!l)throw new Error(`Function not found in scope: ${o}`);return l(...t.map(a=>a(u)))}}function $n(n,e,i,f){let o=m(n.left,e,i,f),s=n.right!==void 0?m(n.right,e,i,f):()=>{};switch(n.op){case "eq":return t=>o(t)===s(t);case "neq":return t=>o(t)!==s(t);case "gt":return t=>o(t)>s(t);case "gte":return t=>o(t)>=s(t);case "lt":return t=>o(t)<s(t);case "lte":return t=>o(t)<=s(t);case "in":return t=>{let r=s(t);return Array.isArray(r)&&r.includes(o(t))};case "notIn":return t=>{let r=s(t);return !Array.isArray(r)||!r.includes(o(t))};case "contains":return t=>{let r=o(t);return Array.isArray(r)&&r.includes(s(t))};case "notContains":return t=>{let r=o(t);return !Array.isArray(r)||!r.includes(s(t))};case "exists":return t=>o(t)!==void 0;case "notExists":return t=>o(t)===void 0;case "matches":return t=>{let r=o(t),u=s(t);return typeof r!="string"||typeof u!="string"?false:new RegExp(u).test(r)};case "notMatches":return t=>{let r=o(t),u=s(t);return typeof r!="string"||typeof u!="string"?true:!new RegExp(u).test(r)};case "startsWith":return t=>{let r=o(t),u=s(t);return typeof r=="string"&&typeof u=="string"&&r.startsWith(u)};case "endsWith":return t=>{let r=o(t),u=s(t);return typeof r=="string"&&typeof u=="string"&&r.endsWith(u)}}}function kn(n,e,i,f){let o=n.conditions.map(t=>m(t,e,i,f)),s=o.length;if(s===1)return t=>!!o[0](t);if(s===2){let[t,r]=o;return n.logic==="AND"?u=>!!t(u)&&!!r(u):u=>!!t(u)||!!r(u)}if(s===3){let[t,r,u]=o;return n.logic==="AND"?l=>!!t(l)&&!!r(l)&&!!u(l):l=>!!t(l)||!!r(l)||!!u(l)}return n.logic==="AND"?t=>{for(let r=0;r<s;r++)if(!o[r](t))return false;return true}:t=>{for(let r=0;r<s;r++)if(o[r](t))return true;return false}}function Sn(n,e,i={}){return N(n,i).fn(e)}var v=class{constructor(e=1e3){this.cache=new Map,this._maxSize=e;}get(e,i={}){let f=JSON.stringify(e),o=this.cache.get(f);if(o)return this.cache.delete(f),this.cache.set(f,o),o;let s=N(e,i);if(this.cache.size>=this._maxSize){let t=this.cache.keys().next().value;t&&this.cache.delete(t);}return this.cache.set(f,s),s}has(e){return this.cache.has(JSON.stringify(e))}delete(e){return this.cache.delete(JSON.stringify(e))}clear(){this.cache.clear();}get size(){return this.cache.size}get maxSize(){return this._maxSize}set maxSize(e){for(this._maxSize=e;this.cache.size>this._maxSize;){let i=this.cache.keys().next().value;i&&this.cache.delete(i);}}},Y=new v;function wn(n,e={}){return Y.get(n,e)}function z(n,e="root",i={}){let f=[];return S(n,e,f,i),{valid:f.length===0,errors:f}}function S(n,e,i,f){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let t=0;t<n.length;t++)S(n[t],`${e}[${t}]`,i,f);return}if(h(n)){(!n.$||typeof n.$!="string")&&i.push(`${e}: invalid reference, $ must be non-empty string`);return}if(A(n)){typeof n.$if=="string"?(n.$if.startsWith("!")?n.$if.slice(1):n.$if)||i.push(`${e}.$if: empty path in string shorthand`):S(n.$if,`${e}.$if`,i,f),S(n.then,`${e}.then`,i,f),n.else!==void 0&&S(n.else,`${e}.else`,i,f);return}if(k(n)){if(!Array.isArray(n.$pipe)){i.push(`${e}.$pipe: must be an array`);return}if(n.$pipe.length===0){i.push(`${e}.$pipe: must have at least one element`);return}for(let t=0;t<n.$pipe.length;t++)S(n.$pipe[t],`${e}.$pipe[${t}]`,i,f);return}if($(n)){if(!n.$fn||typeof n.$fn!="string"){i.push(`${e}: invalid function, $fn must be non-empty string`);return}if(f.scope&&!(n.$fn in f.scope)&&i.push(`${e}: function "${n.$fn}" not found in scope`),n.args!==void 0)if(!Array.isArray(n.args))i.push(`${e}.args: must be an array`);else for(let t=0;t<n.args.length;t++){let r=n.args[t];typeof r!="function"&&S(r,`${e}.args[${t}]`,i,f);}return}if(E(n)){F.has(n.op)||i.push(`${e}: invalid operator "${n.op}"`),S(n.left,`${e}.left`,i,f),n.right!==void 0&&S(n.right,`${e}.right`,i,f);return}if(T(n)){if(n.logic!=="AND"&&n.logic!=="OR"&&i.push(`${e}: invalid logic "${n.logic}", must be "AND" or "OR"`),!Array.isArray(n.conditions)){i.push(`${e}.conditions: must be an array`);return}for(let t=0;t<n.conditions.length;t++)S(n.conditions[t],`${e}.conditions[${t}]`,i,f);return}let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];S(o[r],`${e}.${r}`,i,f);}}function Rn(n,e={}){let i=z(n,"root",e);if(!i.valid)throw new Error(`Invalid expression: ${i.errors.join("; ")}`)}function Fn(n,e={}){return z(n,"root",e).valid}var Z={};sn(Z,{$:()=>H,$cond:()=>X,$fn:()=>Q,$if:()=>jn,$pipe:()=>U,cond:()=>vn,fn:()=>On,pipe:()=>Nn,ref:()=>xn});function H(n){return {$:n}}var xn=H;function Q(n,e){return e===void 0||e.length===0?{$fn:n}:{$fn:n,args:e}}var On=Q;function jn(n,e,i){return i===void 0?{$if:n,then:e}:{$if:n,then:e,else:i}}function U(...n){return {$pipe:n}}var Nn=U;function X(n,e,i){return i===void 0?{left:n,op:e}:{left:n,op:e,right:i}}var vn=X;var L="data",nn="scope",Gn={eq:"===",neq:"!==",gt:">",gte:">=",lt:"<",lte:"<="};function W(n,e={}){let{dataParam:i=L,scopeParam:f=nn,noPrefixes:o=false,useAccessor:s=false,lexicalPrefix:t,transforms:r}=e;return y(n,i,f,o,s,t,r)}function y(n,e,i,f,o,s,t){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(r=>y(r,e,i,f,o,s,t)));if(h(n))return Wn(n.$,e,f,o,s);if(A(n))return In(n,e,i,f,o,s,t);if(k(n))return zn(n.$pipe,e,i,f,o,s,t);if($(n))return Mn(n,e,i,f,o,s,t);if(E(n))return Ln(n,e,i,f,o,s,t);if(T(n))return Vn(n,e,i,f,o,s,t);if(t&&typeof n=="object"){let r=n,u=Object.keys(r);for(let l=0;l<u.length;l++){let a=u[l];if(a in t){let p=t[a](r);if(typeof p=="object"&&p!==null&&a in p)throw new Error(`Transform "${a}" returned object with same key \u2014 infinite loop`);return y(p,e,i,f,o,s,t)}}}if(typeof n=="object"){let u=Object.entries(n).map(([l,a])=>builders.property(builders.identifier(l),y(a,e,i,f,o,s,t)));return builders.objectExpression(u)}return builders.literal(null)}var V="accessor";function _(n,e){return e?n===e||n.startsWith(e+"."):false}function Wn(n,e,i,f,o){return f?_(n,o)?R(n,e,true):builders.callExpression(builders.identifier(V),[builders.literal(n),builders.identifier(e)]):n.includes("[*]")?Dn(n,e,i):R(n,e,i)}function R(n,e,i){let f=G(n);if(f.length===0)return i?builders.identifier("undefined"):builders.identifier(e);let o;if(i){let s=f[0];o=builders.identifier(s.value);for(let t=1;t<f.length;t++){let r=f[t];r.type==="key"?o=builders.memberExpression(o,builders.identifier(r.value),false,true):o=builders.memberExpression(o,builders.literal(r.value),true,true);}}else {o=builders.identifier(e);for(let s of f)s.type==="key"?o=builders.memberExpression(o,builders.identifier(s.value),false,true):o=builders.memberExpression(o,builders.literal(s.value),true,true);}return o}function Dn(n,e,i){let f=n.indexOf("[*]"),o=n.slice(0,f),s=n.slice(f+3),t;if(o?t=R(o,e,i):t=i?builders.identifier("undefined"):builders.identifier(e),!s||s==="")return t;if(s.includes("[*]"))return en(t,s);let r="_i",u=s.startsWith(".")?s.slice(1):s,l=builders.identifier(r);if(u){let a=G(u);for(let p of a)p.type==="key"?l=builders.memberExpression(l,builders.identifier(p.value),false,true):l=builders.memberExpression(l,builders.literal(p.value),true,true);}return builders.callExpression(builders.memberExpression(t,builders.identifier("map"),false,true),[builders.arrowFunctionExpression([builders.identifier(r)],l)])}function en(n,e){let i=e.indexOf("[*]"),f=e.slice(0,i),o=e.slice(i+3),s="_i",t=f.startsWith(".")?f.slice(1):f,r=builders.identifier(s);if(t){let l=G(t);for(let a of l)a.type==="key"&&(r=builders.memberExpression(r,builders.identifier(a.value),false,true));}if(o.includes("[*]")){let l=en(r,o);return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],l)])}let u=o.startsWith(".")?o.slice(1):o;if(u){let l=G(u);for(let a of l)a.type==="key"&&(r=builders.memberExpression(r,builders.identifier(a.value),false,true));}return builders.callExpression(builders.memberExpression(n,builders.identifier("flatMap"),false,true),[builders.arrowFunctionExpression([builders.identifier(s)],r)])}function In(n,e,i,f,o,s,t){let r;if(typeof n.$if=="string"){let a=n.$if.startsWith("!"),p=a?n.$if.slice(1):n.$if,d;o?_(p,s)?d=R(p,e,true):d=builders.callExpression(builders.identifier(V),[builders.literal(p),builders.identifier(e)]):d=R(p,e,f),r=a?builders.unaryExpression("!",d):d;}else r=y(n.$if,e,i,f,o,s,t);let u=y(n.then,e,i,f,o,s,t),l=n.else!==void 0?y(n.else,e,i,f,o,s,t):builders.identifier("undefined");return builders.conditionalExpression(r,u,l)}function Mn(n,e,i,f,o,s,t){let r=f?builders.identifier(n.$fn):builders.memberExpression(builders.identifier(i),builders.identifier(n.$fn),false,false);if(n.args===void 0)return r;let u=n.args.map(l=>typeof l=="function"?builders.literal(null):y(l,e,i,f,o,s,t));return builders.callExpression(r,u)}function zn(n,e,i,f,o,s,t){if(n.length===0)return builders.identifier("undefined");if(n.length===1)return y(n[0],e,i,f,o,s,t);let r=y(n[0],e,i,f,o,s,t);for(let u=1;u<n.length;u++){let l=y(n[u],e,i,f,o,s,t);r=builders.callExpression(l,[r]);}return r}function P(n,e,i,f,o,s,t){if(h(n)){let r=n.$;return o?_(r,s)?R(r,e,true):builders.callExpression(builders.identifier(V),[builders.literal(r),builders.identifier(e)]):R(r,e,f)}return y(n,e,i,f,o,s,t)}function Ln(n,e,i,f,o,s,t){let r=P(n.left,e,i,f,o,s,t),u=n.right!==void 0?P(n.right,e,i,f,o,s,t):builders.literal(null),l=Gn[n.op];if(l)return builders.binaryExpression(l,r,u);switch(n.op){case "in":return builders.callExpression(builders.memberExpression(u,builders.identifier("includes")),[r]);case "notIn":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(u,builders.identifier("includes")),[r]));case "contains":return builders.callExpression(builders.memberExpression(r,builders.identifier("includes"),false,true),[u]);case "notContains":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(r,builders.identifier("includes"),false,true),[u]));case "exists":return builders.binaryExpression("!=",r,builders.literal(null));case "notExists":return builders.binaryExpression("==",r,builders.literal(null));case "matches":return builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[u]),builders.identifier("test")),[r]);case "notMatches":return builders.unaryExpression("!",builders.callExpression(builders.memberExpression(builders.newExpression(builders.identifier("RegExp"),[u]),builders.identifier("test")),[r]));case "startsWith":return builders.callExpression(builders.memberExpression(r,builders.identifier("startsWith"),false,true),[u]);case "endsWith":return builders.callExpression(builders.memberExpression(r,builders.identifier("endsWith"),false,true),[u]);default:return builders.binaryExpression("===",r,u)}}function Vn(n,e,i,f,o,s,t){let{logic:r,conditions:u}=n,l=r==="AND"?"&&":"||";if(u.length===0)return builders.literal(r==="AND");if(u.length===1)return y(u[0],e,i,f,o,s,t);let a=y(u[0],e,i,f,o,s,t);for(let p=1;p<u.length;p++){let d=y(u[p],e,i,f,o,s,t);a=builders.logicalExpression(l,a,d);}return a}function G(n){let e=[],i=n.length,f=0,o="";for(;f<i;){let s=n[f];if(s===".")o&&(e.push({type:"key",value:o}),o=""),f++;else if(s==="["){o&&(e.push({type:"key",value:o}),o=""),f++;let t=f;for(;f<i&&n[f]!=="]";)f++;let r=n.slice(t,f);if(f++,r!=="*"){let u=parseInt(r,10);e.push({type:"index",value:isNaN(u)?r:u});}}else o+=s,f++;}return o&&e.push({type:"key",value:o}),e}function tn(n,e=[L]){return builders.arrowFunctionExpression(e.map(i=>builders.identifier(i)),n)}function D(n,e){let i=new Set;return C(n,i,e),i}function C(n,e,i){if(n===null||typeof n!="object")return;if(Array.isArray(n)){for(let o of n)C(o,e,i);return}if(h(n))return;if(A(n)){C(n.$if,e,i),C(n.then,e,i),n.else!==void 0&&C(n.else,e,i);return}if(k(n)){for(let o of n.$pipe)C(o,e,i);return}if($(n)){if(e.add(n.$fn),n.args)for(let o of n.args)typeof o!="function"&&C(o,e,i);return}if(E(n)){n.left!==void 0&&typeof n.left=="object"&&C(n.left,e,i),n.right!==void 0&&typeof n.right=="object"&&C(n.right,e,i);return}if(T(n)){for(let o of n.conditions)C(o,e,i);return}if(i){let o=n,s=Object.keys(o);for(let t=0;t<s.length;t++){let r=s[t];if(r in i){let u=i[r](o);if(typeof u=="object"&&u!==null&&r in u)throw new Error(`Transform "${r}" returned object with same key \u2014 infinite loop`);C(u,e,i);return}}}let f=n;for(let o of Object.keys(f))C(f[o],e,i);}function I(n){let e=new Set;for(let i of n){let f=i.indexOf("."),o=i.indexOf("["),s=i.length;f!==-1&&(s=Math.min(s,f)),o!==-1&&(s=Math.min(s,o));let t=i.slice(0,s);t&&e.add(t);}return e}function qn(n){return JSON.stringify(n)}function Jn(n,e,i,f,o,s){let t=W(n,{noPrefixes:true,useAccessor:f,lexicalPrefix:o,transforms:s}),r=generate(t),u="";f?o&&(u=`const{${o}}=data??{};`):e.size>0&&(u=`const{${[...e].join(",")}}=data??{};`);let l=f?new Set([...i,"accessor"]):i,a=l.size>0?`const{${[...l].join(",")}}=scope;`:"";return a?`(function(scope){${a}return function(data){${u}return ${r}}})`:`(function(){return function(data){${u}return ${r}}})`}function q(n,e={}){let{scope:i={},returnCode:f=false,useAccessor:o=false,lexicalPrefix:s,transforms:t}=e,r=w(n,t),u=I(r),l=D(n,t),a=qn(n),p=Jn(n,u,l,o,s,t);if(f)return {code:p,deps:r,hash:a,dataRoots:[...u],scopeFns:[...l]};let d;try{d=new Function(`return ${p}`)()(i);}catch(g){throw new Error(`AST compilation failed. If this is due to CSP, use the standard compile() function instead. Error: ${g instanceof Error?g.message:String(g)}`)}return {fn:d,deps:r,hash:a}}function on(n,e,i={}){let{fn:f}=q(n,i);return f(e)}var Te="1.2.1";export{v as ExpressionCache,Te as VERSION,Rn as assertValid,Z as builders,Y as cache,wn as cached,gn as clearPathCache,N as compile,q as compileAST,O as compilePath,W as dslToAST,Sn as evaluate,on as evaluateAST,I as extractDataRoots,w as extractDeps,D as extractScopeFns,dn as get,yn as getPathCacheSize,mn as hasDeps,B as hasWildcard,E as isCondition,un as isConditionExpr,T as isConditionGroup,A as isConditional,$ as isFn,M as isLiteral,k as isPipe,hn as isPure,h as isRef,fn as isTransformResult,Fn as isValid,j as normalizePath,z as validate,tn as wrapInFunction};
|