New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details โ†’ โ†’
Socket
Book a DemoSign in
Socket

@antv/expr

Package Overview
Dependencies
Maintainers
67
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@antv/expr - npm Package Compare versions

Comparing version
1.0.0
to
1.0.1
+1
-1
dist/index.cjs.js

@@ -1,1 +0,1 @@

"use strict";class e extends Error{constructor(e,t,n){super(e),this.position=t,this.token=n,this.name="ExpressionError"}}var t;!function(e){e[e.STRING=0]="STRING",e[e.NUMBER=1]="NUMBER",e[e.BOOLEAN=2]="BOOLEAN",e[e.NULL=3]="NULL",e[e.IDENTIFIER=4]="IDENTIFIER",e[e.OPERATOR=5]="OPERATOR",e[e.FUNCTION=6]="FUNCTION",e[e.DOT=7]="DOT",e[e.BRACKET_LEFT=8]="BRACKET_LEFT",e[e.BRACKET_RIGHT=9]="BRACKET_RIGHT",e[e.PAREN_LEFT=10]="PAREN_LEFT",e[e.PAREN_RIGHT=11]="PAREN_RIGHT",e[e.COMMA=12]="COMMA",e[e.QUESTION=13]="QUESTION",e[e.COLON=14]="COLON",e[e.DOLLAR=15]="DOLLAR"}(t||(t={}));const n=new Set([32,9,10,13]),r=new Set([43,45,42,47,37,33,38,124,61,60,62]),o=new Map([["true",t.BOOLEAN],["false",t.BOOLEAN],["null",t.NULL]]),s=new Map([["===",!0],["!==",!0],["<=",!0],[">=",!0],["&&",!0],["||",!0],["+",!0],["-",!0],["*",!0],["/",!0],["%",!0],["!",!0],["<",!0],[">",!0]]),a=new Map([[46,t.DOT],[91,t.BRACKET_LEFT],[93,t.BRACKET_RIGHT],[40,t.PAREN_LEFT],[41,t.PAREN_RIGHT],[44,t.COMMA],[63,t.QUESTION],[58,t.COLON],[36,t.DOLLAR]]),i=new Map;for(const[e,t]of a.entries())i.set(e,{type:t,value:String.fromCharCode(e)});function u(e){return e>=48&&e<=57}function c(e){return e>=97&&e<=122||e>=65&&e<=90||95===e}function p(e){return c(e)||u(e)}function l(e){return r.has(e)}var f;!function(e){e[e.Program=0]="Program",e[e.Literal=1]="Literal",e[e.Identifier=2]="Identifier",e[e.MemberExpression=3]="MemberExpression",e[e.CallExpression=4]="CallExpression",e[e.BinaryExpression=5]="BinaryExpression",e[e.UnaryExpression=6]="UnaryExpression",e[e.ConditionalExpression=7]="ConditionalExpression"}(f||(f={}));const E=new Map([["||",2],["&&",3],["===",4],["!==",4],[">",5],[">=",5],["<",5],["<=",5],["+",6],["-",6],["*",7],["/",7],["%",7],["!",8]]),h={type:f.Literal,value:null},w={type:f.Literal,value:!0},d={type:f.Literal,value:!1},y=n=>{let r=0;const o=n.length,s=()=>r>=o?null:n[r],a=()=>n[r++],i=e=>{const t=s();return null!==t&&t.type===e},u=e=>e.type===t.OPERATOR?E.get(e.value)||-1:e.type===t.DOT||e.type===t.BRACKET_LEFT?9:e.type===t.QUESTION?1:-1,c=n=>{let o,u;if(a().type===t.DOT){if(!i(t.IDENTIFIER)){const t=s();throw new e("Expected property name",r,t?t.value:"<end of input>")}const n=a();o={type:f.Identifier,name:n.value},u=!1}else{if(o=l(0),!i(t.BRACKET_RIGHT)){const t=s();throw new e("Expected closing bracket",r,t?t.value:"<end of input>")}a(),u=!0}return{type:f.MemberExpression,object:n,property:o,computed:u}},p=()=>{const n=s();if(!n)throw new e("Unexpected end of input",r,"<end of input>");if(n.type===t.OPERATOR&&("!"===n.value||"-"===n.value)){a();const e=p();return{type:f.UnaryExpression,operator:n.value,argument:e,prefix:!0}}switch(n.type){case t.NUMBER:return a(),{type:f.Literal,value:Number(n.value)};case t.STRING:return a(),{type:f.Literal,value:n.value};case t.BOOLEAN:return a(),"true"===n.value?w:d;case t.NULL:return a(),h;case t.IDENTIFIER:return a(),{type:f.Identifier,name:n.value};case t.FUNCTION:return(()=>{const n=a(),o=[];if(!i(t.PAREN_LEFT)){const t=s();throw new e("Expected opening parenthesis after function name",r,t?t.value:"<end of input>")}for(a();;){if(i(t.PAREN_RIGHT)){a();break}if(!s()){const t=s();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}if(o.length>0){if(!i(t.COMMA)){const t=s();throw new e("Expected comma between function arguments",r,t?t.value:"<end of input>")}a()}const n=l(0);o.push(n)}return{type:f.CallExpression,callee:{type:f.Identifier,name:n.value},arguments:o}})();case t.PAREN_LEFT:{a();const n=l(0);if(!i(t.PAREN_RIGHT)){const t=s();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}return a(),n}default:throw new e(`Unexpected token: ${n.type}`,r,n.value)}},l=(E=0)=>{let h=p();for(;r<o;){const o=n[r],p=u(o);if(p<=E)break;if(o.type!==t.QUESTION)if(o.type!==t.OPERATOR){if(o.type!==t.DOT&&o.type!==t.BRACKET_LEFT)break;h=c(h)}else{a();const e=l(p);h={type:f.BinaryExpression,operator:o.value,left:h,right:e}}else{a();const n=l(0);if(!i(t.COLON)){const t=s();throw new e("Expected : in conditional expression",r,t?t.value:"<end of input>")}a();const o=l(0);h={type:f.ConditionalExpression,test:h,consequent:n,alternate:o}}}return h},y=l();return{type:f.Program,body:y}},R=(t,n,r)=>{let o=n;r&&(o={...n,context:{...n.context,...r}});const s=t=>{try{switch(t.type){case f.Literal:return(e=>e.value)(t);case f.Identifier:return(t=>{if(!(t.name in o.context))throw new e(`Undefined variable: ${t.name}`);return o.context[t.name]})(t);case f.MemberExpression:return(t=>{const n=s(t.object);if(null==n)throw new e("Cannot access property of null or undefined");return n[t.computed?s(t.property):t.property.name]})(t);case f.CallExpression:return(t=>{const n=o.functions[t.callee.name];if(!n)throw new e(`Undefined function: ${t.callee.name}`);return n(...t.arguments.map((e=>s(e))))})(t);case f.BinaryExpression:return(t=>{const n=s(t.left),r=s(t.right);switch(t.operator){case"+":return"number"==typeof n&&"number"==typeof r?n+r:String(n)+String(r);case"-":return n-r;case"*":return n*r;case"/":return n/r;case"%":return n%r;case"===":return n===r;case"!==":return n!==r;case">":return n>r;case">=":return n>=r;case"<":return n<r;case"<=":return n<=r;case"&&":return n&&r;case"||":return n||r;default:throw new e(`Unknown operator: ${t.operator}`)}})(t);case f.UnaryExpression:return(t=>{const n=s(t.argument);if(t.prefix)switch(t.operator){case"!":return!n;case"-":if("number"!=typeof n)throw new e(`Cannot apply unary - to non-number: ${n}`);return-n;default:throw new e(`Unknown operator: ${t.operator}`)}throw new e(`Postfix operators are not supported: ${t.operator}`)})(t);case f.ConditionalExpression:return(e=>{const t=s(e.test);return s(t?e.consequent:e.alternate)})(t);default:throw new e(`Unsupported node type: ${t.type}`)}}catch(t){if(t instanceof e)throw new e(`Evaluation error: ${t.message}`);throw t}};return s(t.body)},T={};function O(e,t){T[e]=t}function x(r){const a=(r=>{const a=r,f=a.length,E=new Array(Math.ceil(f/3));let h=0,w=0;function d(n){const r=w+1;w++;let o="",s=!1;for(;w<f;){const e=a.charCodeAt(w);if(e===n)return s||(o=a.substring(r,w)),w++,{type:t.STRING,value:o};92===e?(s||(o=a.substring(r,w),s=!0),w++,o+=a[w]):s&&(o+=a[w]),w++}throw new e(`Unterminated string starting with ${String.fromCharCode(n)}`,w,a.substring(Math.max(0,w-10),w))}function y(){const e=w;for(45===a.charCodeAt(w)&&w++;w<f&&u(a.charCodeAt(w));)w++;if(w<f&&46===a.charCodeAt(w))for(w++;w<f&&u(a.charCodeAt(w));)w++;const n=a.slice(e,w);return{type:t.NUMBER,value:n}}function R(){w++;const e=w;if(w<f&&c(a.charCodeAt(w)))for(w++;w<f&&p(a.charCodeAt(w));)w++;const n=a.slice(e,w);return{type:t.FUNCTION,value:n}}function T(){const e=w++;for(;w<f&&p(a.charCodeAt(w));)w++;const n=a.slice(e,w),r=o.get(n);return r?{type:r,value:n}:{type:t.IDENTIFIER,value:n}}function O(){if(w+2<f){const e=a.substring(w,w+3);if(s.has(e))return w+=3,{type:t.OPERATOR,value:e}}if(w+1<f){const e=a.substring(w,w+2);if(s.has(e))return w+=2,{type:t.OPERATOR,value:e}}const n=a[w];if(s.has(n))return w++,{type:t.OPERATOR,value:n};throw new e(`Unknown operator at position ${w}: ${a.substring(w,w+1)}`,w,a.substring(Math.max(0,w-10),w))}for(;w<f;){const t=a.charCodeAt(w);if(x=t,n.has(x)){w++;continue}const r=i.get(t);if(r)E[h++]=r,w++;else if(34!==t&&39!==t)if(u(t)||45===t&&w+1<f&&u(a.charCodeAt(w+1)))E[h++]=y();else if(64!==t)if(c(t))E[h++]=T();else{if(!l(t))throw new e(`Unexpected character: ${a[w]}`,w,a.substring(Math.max(0,w-10),w));E[h++]=O()}else E[h++]=R();else E[h++]=d(t)}var x;return h===E.length?E:E.slice(0,h)})(r),f=y(a),E=((e={},t={})=>({context:e,functions:t}))({},T);return(t={})=>{try{return R(f,E,t)}catch(t){if(t instanceof e)throw t;if(t instanceof Error)throw new e(t.message);throw t}}}O("abs",Math.abs),O("ceil",Math.ceil),O("floor",Math.floor),O("max",Math.max),O("min",Math.min),O("round",Math.round),O("sqrt",Math.sqrt),O("pow",Math.pow),exports.ExpressionError=e,exports.compile=x,exports.evaluate=function(e,t={}){return x(e)(t)},exports.register=O;
"use strict";class e extends Error{constructor(e,t,n){super(e),this.position=t,this.token=n,this.name="ExpressionError"}}var t;!function(e){e[e.STRING=0]="STRING",e[e.NUMBER=1]="NUMBER",e[e.BOOLEAN=2]="BOOLEAN",e[e.NULL=3]="NULL",e[e.IDENTIFIER=4]="IDENTIFIER",e[e.OPERATOR=5]="OPERATOR",e[e.FUNCTION=6]="FUNCTION",e[e.DOT=7]="DOT",e[e.BRACKET_LEFT=8]="BRACKET_LEFT",e[e.BRACKET_RIGHT=9]="BRACKET_RIGHT",e[e.PAREN_LEFT=10]="PAREN_LEFT",e[e.PAREN_RIGHT=11]="PAREN_RIGHT",e[e.COMMA=12]="COMMA",e[e.QUESTION=13]="QUESTION",e[e.COLON=14]="COLON",e[e.DOLLAR=15]="DOLLAR"}(t||(t={}));const n=new Set([32,9,10,13]),r=new Set([43,45,42,47,37,33,38,124,61,60,62]),o=new Map([["true",t.BOOLEAN],["false",t.BOOLEAN],["null",t.NULL]]),s=new Map([["===",!0],["!==",!0],["<=",!0],[">=",!0],["&&",!0],["||",!0],["+",!0],["-",!0],["*",!0],["/",!0],["%",!0],["!",!0],["<",!0],[">",!0]]),a=new Map([[46,t.DOT],[91,t.BRACKET_LEFT],[93,t.BRACKET_RIGHT],[40,t.PAREN_LEFT],[41,t.PAREN_RIGHT],[44,t.COMMA],[63,t.QUESTION],[58,t.COLON],[36,t.DOLLAR]]),i=new Map;for(const[e,t]of a.entries())i.set(e,{type:t,value:String.fromCharCode(e)});function u(e){return e>=48&&e<=57}function c(e){return e>=97&&e<=122||e>=65&&e<=90||95===e}function p(e){return c(e)||u(e)}function l(e){return r.has(e)}var f;!function(e){e[e.Program=0]="Program",e[e.Literal=1]="Literal",e[e.Identifier=2]="Identifier",e[e.MemberExpression=3]="MemberExpression",e[e.CallExpression=4]="CallExpression",e[e.BinaryExpression=5]="BinaryExpression",e[e.UnaryExpression=6]="UnaryExpression",e[e.ConditionalExpression=7]="ConditionalExpression"}(f||(f={}));const E=new Map([["||",2],["&&",3],["===",4],["!==",4],[">",5],[">=",5],["<",5],["<=",5],["+",6],["-",6],["*",7],["/",7],["%",7],["!",8]]),h={type:f.Literal,value:null},d={type:f.Literal,value:!0},w={type:f.Literal,value:!1},R=n=>{let r=0;const o=n.length,s=()=>r>=o?null:n[r],a=()=>n[r++],i=e=>{const t=s();return null!==t&&t.type===e},u=e=>e.type===t.OPERATOR?E.get(e.value)||-1:e.type===t.DOT||e.type===t.BRACKET_LEFT?9:e.type===t.QUESTION?1:-1,c=n=>{let o,u;if(a().type===t.DOT){if(!i(t.IDENTIFIER)){const t=s();throw new e("Expected property name",r,t?t.value:"<end of input>")}const n=a();o={type:f.Identifier,name:n.value},u=!1}else{if(o=l(0),!i(t.BRACKET_RIGHT)){const t=s();throw new e("Expected closing bracket",r,t?t.value:"<end of input>")}a(),u=!0}return{type:f.MemberExpression,object:n,property:o,computed:u}},p=()=>{const n=s();if(!n)throw new e("Unexpected end of input",r,"<end of input>");if(n.type===t.OPERATOR&&("!"===n.value||"-"===n.value)){a();const e=p();return{type:f.UnaryExpression,operator:n.value,argument:e,prefix:!0}}switch(n.type){case t.NUMBER:return a(),{type:f.Literal,value:Number(n.value)};case t.STRING:return a(),{type:f.Literal,value:n.value};case t.BOOLEAN:return a(),"true"===n.value?d:w;case t.NULL:return a(),h;case t.IDENTIFIER:return a(),{type:f.Identifier,name:n.value};case t.FUNCTION:return(()=>{const n=a(),o=[];if(!i(t.PAREN_LEFT)){const t=s();throw new e("Expected opening parenthesis after function name",r,t?t.value:"<end of input>")}for(a();;){if(i(t.PAREN_RIGHT)){a();break}if(!s()){const t=s();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}if(o.length>0){if(!i(t.COMMA)){const t=s();throw new e("Expected comma between function arguments",r,t?t.value:"<end of input>")}a()}const n=l(0);o.push(n)}return{type:f.CallExpression,callee:{type:f.Identifier,name:n.value},arguments:o}})();case t.PAREN_LEFT:{a();const n=l(0);if(!i(t.PAREN_RIGHT)){const t=s();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}return a(),n}default:throw new e(`Unexpected token: ${n.type}`,r,n.value)}},l=(E=0)=>{let h=p();for(;r<o;){const o=n[r],p=u(o);if(p<=E)break;if(o.type!==t.QUESTION)if(o.type!==t.OPERATOR){if(o.type!==t.DOT&&o.type!==t.BRACKET_LEFT)break;h=c(h)}else{a();const e=l(p);h={type:f.BinaryExpression,operator:o.value,left:h,right:e}}else{a();const n=l(0);if(!i(t.COLON)){const t=s();throw new e("Expected : in conditional expression",r,t?t.value:"<end of input>")}a();const o=l(0);h={type:f.ConditionalExpression,test:h,consequent:n,alternate:o}}}return h},R=l();return{type:f.Program,body:R}},T=(t,n,r)=>{let o=n;r&&(o={...n,context:{...n.context,...r}});const s=t=>{switch(t.type){case f.Literal:return(e=>e.value)(t);case f.Identifier:return(t=>{if(!(t.name in o.context))throw new e(`Undefined variable: ${t.name}`);return o.context[t.name]})(t);case f.MemberExpression:return(t=>{const n=s(t.object);if(null==n)throw new e("Cannot access property of null or undefined");return n[t.computed?s(t.property):t.property.name]})(t);case f.CallExpression:return(t=>{const n=o.functions[t.callee.name];if(!n)throw new e(`Undefined function: ${t.callee.name}`);return n(...t.arguments.map((e=>s(e))))})(t);case f.BinaryExpression:return(t=>{const n=s(t.left),r=s(t.right);switch(t.operator){case"+":return n+r;case"-":return n-r;case"*":return n*r;case"/":return n/r;case"%":return n%r;case"===":return n===r;case"!==":return n!==r;case">":return n>r;case">=":return n>=r;case"<":return n<r;case"<=":return n<=r;case"&&":return n&&r;case"||":return n||r;default:throw new e(`Unknown operator: ${t.operator}`)}})(t);case f.UnaryExpression:return(t=>{const n=s(t.argument);if(t.prefix)switch(t.operator){case"!":return!n;case"-":if("number"!=typeof n)throw new e(`Cannot apply unary - to non-number: ${n}`);return-n;default:throw new e(`Unknown operator: ${t.operator}`)}throw new e(`Postfix operators are not supported: ${t.operator}`)})(t);case f.ConditionalExpression:return(e=>{const t=s(e.test);return s(t?e.consequent:e.alternate)})(t);default:throw new e(`Evaluation error: Unsupported node type: ${t.type}`)}};return s(t.body)},y={};function O(e,t){y[e]=t}function x(r){const a=(r=>{const a=r,f=a.length,E=new Array(Math.ceil(f/3));let h=0,d=0;function w(n){const r=d+1;d++;let o="",s=!1;for(;d<f;){const e=a.charCodeAt(d);if(e===n)return s||(o=a.substring(r,d)),d++,{type:t.STRING,value:o};92===e?(s||(o=a.substring(r,d),s=!0),d++,o+=a[d]):s&&(o+=a[d]),d++}throw new e(`Unterminated string starting with ${String.fromCharCode(n)}`,d,a.substring(Math.max(0,d-10),d))}function R(){const e=d;for(45===a.charCodeAt(d)&&d++;d<f&&u(a.charCodeAt(d));)d++;if(d<f&&46===a.charCodeAt(d))for(d++;d<f&&u(a.charCodeAt(d));)d++;const n=a.slice(e,d);return{type:t.NUMBER,value:n}}function T(){d++;const e=d;if(d<f&&c(a.charCodeAt(d)))for(d++;d<f&&p(a.charCodeAt(d));)d++;const n=a.slice(e,d);return{type:t.FUNCTION,value:n}}function y(){const e=d++;for(;d<f&&p(a.charCodeAt(d));)d++;const n=a.slice(e,d),r=o.get(n);return r?{type:r,value:n}:{type:t.IDENTIFIER,value:n}}function O(){if(d+2<f){const e=a.substring(d,d+3);if(s.has(e))return d+=3,{type:t.OPERATOR,value:e}}if(d+1<f){const e=a.substring(d,d+2);if(s.has(e))return d+=2,{type:t.OPERATOR,value:e}}const n=a[d];if(s.has(n))return d++,{type:t.OPERATOR,value:n};throw new e(`Unknown operator at position ${d}: ${a.substring(d,d+1)}`,d,a.substring(Math.max(0,d-10),d))}for(;d<f;){const t=a.charCodeAt(d);if(x=t,n.has(x)){d++;continue}const r=i.get(t);if(r)E[h++]=r,d++;else if(34!==t&&39!==t)if(u(t)||45===t&&d+1<f&&u(a.charCodeAt(d+1)))E[h++]=R();else if(64!==t)if(c(t))E[h++]=y();else{if(!l(t))throw new e(`Unexpected character: ${a[d]}`,d,a.substring(Math.max(0,d-10),d));E[h++]=O()}else E[h++]=T();else E[h++]=w(t)}var x;return h===E.length?E:E.slice(0,h)})(r),f=R(a),E=((e={},t={})=>({context:e,functions:t}))({},y);return(t={})=>{try{return T(f,E,t)}catch(t){if(t instanceof e)throw t;throw t}}}O("abs",Math.abs),O("ceil",Math.ceil),O("floor",Math.floor),O("max",Math.max),O("min",Math.min),O("round",Math.round),O("sqrt",Math.sqrt),O("pow",Math.pow),exports.ExpressionError=e,exports.compile=x,exports.evaluate=function(e,t={}){return x(e)(t)},exports.register=O;

@@ -1,1 +0,1 @@

class e extends Error{constructor(e,t,n){super(e),this.position=t,this.token=n,this.name="ExpressionError"}}var t;!function(e){e[e.STRING=0]="STRING",e[e.NUMBER=1]="NUMBER",e[e.BOOLEAN=2]="BOOLEAN",e[e.NULL=3]="NULL",e[e.IDENTIFIER=4]="IDENTIFIER",e[e.OPERATOR=5]="OPERATOR",e[e.FUNCTION=6]="FUNCTION",e[e.DOT=7]="DOT",e[e.BRACKET_LEFT=8]="BRACKET_LEFT",e[e.BRACKET_RIGHT=9]="BRACKET_RIGHT",e[e.PAREN_LEFT=10]="PAREN_LEFT",e[e.PAREN_RIGHT=11]="PAREN_RIGHT",e[e.COMMA=12]="COMMA",e[e.QUESTION=13]="QUESTION",e[e.COLON=14]="COLON",e[e.DOLLAR=15]="DOLLAR"}(t||(t={}));const n=new Set([32,9,10,13]),r=new Set([43,45,42,47,37,33,38,124,61,60,62]),o=new Map([["true",t.BOOLEAN],["false",t.BOOLEAN],["null",t.NULL]]),a=new Map([["===",!0],["!==",!0],["<=",!0],[">=",!0],["&&",!0],["||",!0],["+",!0],["-",!0],["*",!0],["/",!0],["%",!0],["!",!0],["<",!0],[">",!0]]),s=new Map([[46,t.DOT],[91,t.BRACKET_LEFT],[93,t.BRACKET_RIGHT],[40,t.PAREN_LEFT],[41,t.PAREN_RIGHT],[44,t.COMMA],[63,t.QUESTION],[58,t.COLON],[36,t.DOLLAR]]),i=new Map;for(const[e,t]of s.entries())i.set(e,{type:t,value:String.fromCharCode(e)});function u(e){return e>=48&&e<=57}function c(e){return e>=97&&e<=122||e>=65&&e<=90||95===e}function p(e){return c(e)||u(e)}function l(e){return r.has(e)}var f;!function(e){e[e.Program=0]="Program",e[e.Literal=1]="Literal",e[e.Identifier=2]="Identifier",e[e.MemberExpression=3]="MemberExpression",e[e.CallExpression=4]="CallExpression",e[e.BinaryExpression=5]="BinaryExpression",e[e.UnaryExpression=6]="UnaryExpression",e[e.ConditionalExpression=7]="ConditionalExpression"}(f||(f={}));const E=new Map([["||",2],["&&",3],["===",4],["!==",4],[">",5],[">=",5],["<",5],["<=",5],["+",6],["-",6],["*",7],["/",7],["%",7],["!",8]]),h={type:f.Literal,value:null},w={type:f.Literal,value:!0},d={type:f.Literal,value:!1},y=n=>{let r=0;const o=n.length,a=()=>r>=o?null:n[r],s=()=>n[r++],i=e=>{const t=a();return null!==t&&t.type===e},u=e=>e.type===t.OPERATOR?E.get(e.value)||-1:e.type===t.DOT||e.type===t.BRACKET_LEFT?9:e.type===t.QUESTION?1:-1,c=n=>{let o,u;if(s().type===t.DOT){if(!i(t.IDENTIFIER)){const t=a();throw new e("Expected property name",r,t?t.value:"<end of input>")}const n=s();o={type:f.Identifier,name:n.value},u=!1}else{if(o=l(0),!i(t.BRACKET_RIGHT)){const t=a();throw new e("Expected closing bracket",r,t?t.value:"<end of input>")}s(),u=!0}return{type:f.MemberExpression,object:n,property:o,computed:u}},p=()=>{const n=a();if(!n)throw new e("Unexpected end of input",r,"<end of input>");if(n.type===t.OPERATOR&&("!"===n.value||"-"===n.value)){s();const e=p();return{type:f.UnaryExpression,operator:n.value,argument:e,prefix:!0}}switch(n.type){case t.NUMBER:return s(),{type:f.Literal,value:Number(n.value)};case t.STRING:return s(),{type:f.Literal,value:n.value};case t.BOOLEAN:return s(),"true"===n.value?w:d;case t.NULL:return s(),h;case t.IDENTIFIER:return s(),{type:f.Identifier,name:n.value};case t.FUNCTION:return(()=>{const n=s(),o=[];if(!i(t.PAREN_LEFT)){const t=a();throw new e("Expected opening parenthesis after function name",r,t?t.value:"<end of input>")}for(s();;){if(i(t.PAREN_RIGHT)){s();break}if(!a()){const t=a();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}if(o.length>0){if(!i(t.COMMA)){const t=a();throw new e("Expected comma between function arguments",r,t?t.value:"<end of input>")}s()}const n=l(0);o.push(n)}return{type:f.CallExpression,callee:{type:f.Identifier,name:n.value},arguments:o}})();case t.PAREN_LEFT:{s();const n=l(0);if(!i(t.PAREN_RIGHT)){const t=a();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}return s(),n}default:throw new e(`Unexpected token: ${n.type}`,r,n.value)}},l=(E=0)=>{let h=p();for(;r<o;){const o=n[r],p=u(o);if(p<=E)break;if(o.type!==t.QUESTION)if(o.type!==t.OPERATOR){if(o.type!==t.DOT&&o.type!==t.BRACKET_LEFT)break;h=c(h)}else{s();const e=l(p);h={type:f.BinaryExpression,operator:o.value,left:h,right:e}}else{s();const n=l(0);if(!i(t.COLON)){const t=a();throw new e("Expected : in conditional expression",r,t?t.value:"<end of input>")}s();const o=l(0);h={type:f.ConditionalExpression,test:h,consequent:n,alternate:o}}}return h},y=l();return{type:f.Program,body:y}},R=(t,n,r)=>{let o=n;r&&(o={...n,context:{...n.context,...r}});const a=t=>{try{switch(t.type){case f.Literal:return(e=>e.value)(t);case f.Identifier:return(t=>{if(!(t.name in o.context))throw new e(`Undefined variable: ${t.name}`);return o.context[t.name]})(t);case f.MemberExpression:return(t=>{const n=a(t.object);if(null==n)throw new e("Cannot access property of null or undefined");return n[t.computed?a(t.property):t.property.name]})(t);case f.CallExpression:return(t=>{const n=o.functions[t.callee.name];if(!n)throw new e(`Undefined function: ${t.callee.name}`);return n(...t.arguments.map((e=>a(e))))})(t);case f.BinaryExpression:return(t=>{const n=a(t.left),r=a(t.right);switch(t.operator){case"+":return"number"==typeof n&&"number"==typeof r?n+r:String(n)+String(r);case"-":return n-r;case"*":return n*r;case"/":return n/r;case"%":return n%r;case"===":return n===r;case"!==":return n!==r;case">":return n>r;case">=":return n>=r;case"<":return n<r;case"<=":return n<=r;case"&&":return n&&r;case"||":return n||r;default:throw new e(`Unknown operator: ${t.operator}`)}})(t);case f.UnaryExpression:return(t=>{const n=a(t.argument);if(t.prefix)switch(t.operator){case"!":return!n;case"-":if("number"!=typeof n)throw new e(`Cannot apply unary - to non-number: ${n}`);return-n;default:throw new e(`Unknown operator: ${t.operator}`)}throw new e(`Postfix operators are not supported: ${t.operator}`)})(t);case f.ConditionalExpression:return(e=>{const t=a(e.test);return a(t?e.consequent:e.alternate)})(t);default:throw new e(`Unsupported node type: ${t.type}`)}}catch(t){if(t instanceof e)throw new e(`Evaluation error: ${t.message}`);throw t}};return a(t.body)},T={};function O(e,t){T[e]=t}function A(r){const s=(r=>{const s=r,f=s.length,E=new Array(Math.ceil(f/3));let h=0,w=0;function d(n){const r=w+1;w++;let o="",a=!1;for(;w<f;){const e=s.charCodeAt(w);if(e===n)return a||(o=s.substring(r,w)),w++,{type:t.STRING,value:o};92===e?(a||(o=s.substring(r,w),a=!0),w++,o+=s[w]):a&&(o+=s[w]),w++}throw new e(`Unterminated string starting with ${String.fromCharCode(n)}`,w,s.substring(Math.max(0,w-10),w))}function y(){const e=w;for(45===s.charCodeAt(w)&&w++;w<f&&u(s.charCodeAt(w));)w++;if(w<f&&46===s.charCodeAt(w))for(w++;w<f&&u(s.charCodeAt(w));)w++;const n=s.slice(e,w);return{type:t.NUMBER,value:n}}function R(){w++;const e=w;if(w<f&&c(s.charCodeAt(w)))for(w++;w<f&&p(s.charCodeAt(w));)w++;const n=s.slice(e,w);return{type:t.FUNCTION,value:n}}function T(){const e=w++;for(;w<f&&p(s.charCodeAt(w));)w++;const n=s.slice(e,w),r=o.get(n);return r?{type:r,value:n}:{type:t.IDENTIFIER,value:n}}function O(){if(w+2<f){const e=s.substring(w,w+3);if(a.has(e))return w+=3,{type:t.OPERATOR,value:e}}if(w+1<f){const e=s.substring(w,w+2);if(a.has(e))return w+=2,{type:t.OPERATOR,value:e}}const n=s[w];if(a.has(n))return w++,{type:t.OPERATOR,value:n};throw new e(`Unknown operator at position ${w}: ${s.substring(w,w+1)}`,w,s.substring(Math.max(0,w-10),w))}for(;w<f;){const t=s.charCodeAt(w);if(A=t,n.has(A)){w++;continue}const r=i.get(t);if(r)E[h++]=r,w++;else if(34!==t&&39!==t)if(u(t)||45===t&&w+1<f&&u(s.charCodeAt(w+1)))E[h++]=y();else if(64!==t)if(c(t))E[h++]=T();else{if(!l(t))throw new e(`Unexpected character: ${s[w]}`,w,s.substring(Math.max(0,w-10),w));E[h++]=O()}else E[h++]=R();else E[h++]=d(t)}var A;return h===E.length?E:E.slice(0,h)})(r),f=y(s),E=((e={},t={})=>({context:e,functions:t}))({},T);return(t={})=>{try{return R(f,E,t)}catch(t){if(t instanceof e)throw t;if(t instanceof Error)throw new e(t.message);throw t}}}function N(e,t={}){return A(e)(t)}O("abs",Math.abs),O("ceil",Math.ceil),O("floor",Math.floor),O("max",Math.max),O("min",Math.min),O("round",Math.round),O("sqrt",Math.sqrt),O("pow",Math.pow);export{e as ExpressionError,A as compile,N as evaluate,O as register};
class e extends Error{constructor(e,t,n){super(e),this.position=t,this.token=n,this.name="ExpressionError"}}var t;!function(e){e[e.STRING=0]="STRING",e[e.NUMBER=1]="NUMBER",e[e.BOOLEAN=2]="BOOLEAN",e[e.NULL=3]="NULL",e[e.IDENTIFIER=4]="IDENTIFIER",e[e.OPERATOR=5]="OPERATOR",e[e.FUNCTION=6]="FUNCTION",e[e.DOT=7]="DOT",e[e.BRACKET_LEFT=8]="BRACKET_LEFT",e[e.BRACKET_RIGHT=9]="BRACKET_RIGHT",e[e.PAREN_LEFT=10]="PAREN_LEFT",e[e.PAREN_RIGHT=11]="PAREN_RIGHT",e[e.COMMA=12]="COMMA",e[e.QUESTION=13]="QUESTION",e[e.COLON=14]="COLON",e[e.DOLLAR=15]="DOLLAR"}(t||(t={}));const n=new Set([32,9,10,13]),r=new Set([43,45,42,47,37,33,38,124,61,60,62]),o=new Map([["true",t.BOOLEAN],["false",t.BOOLEAN],["null",t.NULL]]),a=new Map([["===",!0],["!==",!0],["<=",!0],[">=",!0],["&&",!0],["||",!0],["+",!0],["-",!0],["*",!0],["/",!0],["%",!0],["!",!0],["<",!0],[">",!0]]),s=new Map([[46,t.DOT],[91,t.BRACKET_LEFT],[93,t.BRACKET_RIGHT],[40,t.PAREN_LEFT],[41,t.PAREN_RIGHT],[44,t.COMMA],[63,t.QUESTION],[58,t.COLON],[36,t.DOLLAR]]),i=new Map;for(const[e,t]of s.entries())i.set(e,{type:t,value:String.fromCharCode(e)});function u(e){return e>=48&&e<=57}function c(e){return e>=97&&e<=122||e>=65&&e<=90||95===e}function p(e){return c(e)||u(e)}function l(e){return r.has(e)}var f;!function(e){e[e.Program=0]="Program",e[e.Literal=1]="Literal",e[e.Identifier=2]="Identifier",e[e.MemberExpression=3]="MemberExpression",e[e.CallExpression=4]="CallExpression",e[e.BinaryExpression=5]="BinaryExpression",e[e.UnaryExpression=6]="UnaryExpression",e[e.ConditionalExpression=7]="ConditionalExpression"}(f||(f={}));const E=new Map([["||",2],["&&",3],["===",4],["!==",4],[">",5],[">=",5],["<",5],["<=",5],["+",6],["-",6],["*",7],["/",7],["%",7],["!",8]]),h={type:f.Literal,value:null},d={type:f.Literal,value:!0},w={type:f.Literal,value:!1},R=n=>{let r=0;const o=n.length,a=()=>r>=o?null:n[r],s=()=>n[r++],i=e=>{const t=a();return null!==t&&t.type===e},u=e=>e.type===t.OPERATOR?E.get(e.value)||-1:e.type===t.DOT||e.type===t.BRACKET_LEFT?9:e.type===t.QUESTION?1:-1,c=n=>{let o,u;if(s().type===t.DOT){if(!i(t.IDENTIFIER)){const t=a();throw new e("Expected property name",r,t?t.value:"<end of input>")}const n=s();o={type:f.Identifier,name:n.value},u=!1}else{if(o=l(0),!i(t.BRACKET_RIGHT)){const t=a();throw new e("Expected closing bracket",r,t?t.value:"<end of input>")}s(),u=!0}return{type:f.MemberExpression,object:n,property:o,computed:u}},p=()=>{const n=a();if(!n)throw new e("Unexpected end of input",r,"<end of input>");if(n.type===t.OPERATOR&&("!"===n.value||"-"===n.value)){s();const e=p();return{type:f.UnaryExpression,operator:n.value,argument:e,prefix:!0}}switch(n.type){case t.NUMBER:return s(),{type:f.Literal,value:Number(n.value)};case t.STRING:return s(),{type:f.Literal,value:n.value};case t.BOOLEAN:return s(),"true"===n.value?d:w;case t.NULL:return s(),h;case t.IDENTIFIER:return s(),{type:f.Identifier,name:n.value};case t.FUNCTION:return(()=>{const n=s(),o=[];if(!i(t.PAREN_LEFT)){const t=a();throw new e("Expected opening parenthesis after function name",r,t?t.value:"<end of input>")}for(s();;){if(i(t.PAREN_RIGHT)){s();break}if(!a()){const t=a();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}if(o.length>0){if(!i(t.COMMA)){const t=a();throw new e("Expected comma between function arguments",r,t?t.value:"<end of input>")}s()}const n=l(0);o.push(n)}return{type:f.CallExpression,callee:{type:f.Identifier,name:n.value},arguments:o}})();case t.PAREN_LEFT:{s();const n=l(0);if(!i(t.PAREN_RIGHT)){const t=a();throw new e("Expected closing parenthesis",r,t?t.value:"<end of input>")}return s(),n}default:throw new e(`Unexpected token: ${n.type}`,r,n.value)}},l=(E=0)=>{let h=p();for(;r<o;){const o=n[r],p=u(o);if(p<=E)break;if(o.type!==t.QUESTION)if(o.type!==t.OPERATOR){if(o.type!==t.DOT&&o.type!==t.BRACKET_LEFT)break;h=c(h)}else{s();const e=l(p);h={type:f.BinaryExpression,operator:o.value,left:h,right:e}}else{s();const n=l(0);if(!i(t.COLON)){const t=a();throw new e("Expected : in conditional expression",r,t?t.value:"<end of input>")}s();const o=l(0);h={type:f.ConditionalExpression,test:h,consequent:n,alternate:o}}}return h},R=l();return{type:f.Program,body:R}},T=(t,n,r)=>{let o=n;r&&(o={...n,context:{...n.context,...r}});const a=t=>{switch(t.type){case f.Literal:return(e=>e.value)(t);case f.Identifier:return(t=>{if(!(t.name in o.context))throw new e(`Undefined variable: ${t.name}`);return o.context[t.name]})(t);case f.MemberExpression:return(t=>{const n=a(t.object);if(null==n)throw new e("Cannot access property of null or undefined");return n[t.computed?a(t.property):t.property.name]})(t);case f.CallExpression:return(t=>{const n=o.functions[t.callee.name];if(!n)throw new e(`Undefined function: ${t.callee.name}`);return n(...t.arguments.map((e=>a(e))))})(t);case f.BinaryExpression:return(t=>{const n=a(t.left),r=a(t.right);switch(t.operator){case"+":return n+r;case"-":return n-r;case"*":return n*r;case"/":return n/r;case"%":return n%r;case"===":return n===r;case"!==":return n!==r;case">":return n>r;case">=":return n>=r;case"<":return n<r;case"<=":return n<=r;case"&&":return n&&r;case"||":return n||r;default:throw new e(`Unknown operator: ${t.operator}`)}})(t);case f.UnaryExpression:return(t=>{const n=a(t.argument);if(t.prefix)switch(t.operator){case"!":return!n;case"-":if("number"!=typeof n)throw new e(`Cannot apply unary - to non-number: ${n}`);return-n;default:throw new e(`Unknown operator: ${t.operator}`)}throw new e(`Postfix operators are not supported: ${t.operator}`)})(t);case f.ConditionalExpression:return(e=>{const t=a(e.test);return a(t?e.consequent:e.alternate)})(t);default:throw new e(`Evaluation error: Unsupported node type: ${t.type}`)}};return a(t.body)},y={};function O(e,t){y[e]=t}function A(r){const s=(r=>{const s=r,f=s.length,E=new Array(Math.ceil(f/3));let h=0,d=0;function w(n){const r=d+1;d++;let o="",a=!1;for(;d<f;){const e=s.charCodeAt(d);if(e===n)return a||(o=s.substring(r,d)),d++,{type:t.STRING,value:o};92===e?(a||(o=s.substring(r,d),a=!0),d++,o+=s[d]):a&&(o+=s[d]),d++}throw new e(`Unterminated string starting with ${String.fromCharCode(n)}`,d,s.substring(Math.max(0,d-10),d))}function R(){const e=d;for(45===s.charCodeAt(d)&&d++;d<f&&u(s.charCodeAt(d));)d++;if(d<f&&46===s.charCodeAt(d))for(d++;d<f&&u(s.charCodeAt(d));)d++;const n=s.slice(e,d);return{type:t.NUMBER,value:n}}function T(){d++;const e=d;if(d<f&&c(s.charCodeAt(d)))for(d++;d<f&&p(s.charCodeAt(d));)d++;const n=s.slice(e,d);return{type:t.FUNCTION,value:n}}function y(){const e=d++;for(;d<f&&p(s.charCodeAt(d));)d++;const n=s.slice(e,d),r=o.get(n);return r?{type:r,value:n}:{type:t.IDENTIFIER,value:n}}function O(){if(d+2<f){const e=s.substring(d,d+3);if(a.has(e))return d+=3,{type:t.OPERATOR,value:e}}if(d+1<f){const e=s.substring(d,d+2);if(a.has(e))return d+=2,{type:t.OPERATOR,value:e}}const n=s[d];if(a.has(n))return d++,{type:t.OPERATOR,value:n};throw new e(`Unknown operator at position ${d}: ${s.substring(d,d+1)}`,d,s.substring(Math.max(0,d-10),d))}for(;d<f;){const t=s.charCodeAt(d);if(A=t,n.has(A)){d++;continue}const r=i.get(t);if(r)E[h++]=r,d++;else if(34!==t&&39!==t)if(u(t)||45===t&&d+1<f&&u(s.charCodeAt(d+1)))E[h++]=R();else if(64!==t)if(c(t))E[h++]=y();else{if(!l(t))throw new e(`Unexpected character: ${s[d]}`,d,s.substring(Math.max(0,d-10),d));E[h++]=O()}else E[h++]=T();else E[h++]=w(t)}var A;return h===E.length?E:E.slice(0,h)})(r),f=R(s),E=((e={},t={})=>({context:e,functions:t}))({},y);return(t={})=>{try{return T(f,E,t)}catch(t){if(t instanceof e)throw t;throw t}}}function N(e,t={}){return A(e)(t)}O("abs",Math.abs),O("ceil",Math.ceil),O("floor",Math.floor),O("max",Math.max),O("min",Math.min),O("round",Math.round),O("sqrt",Math.sqrt),O("pow",Math.pow);export{e as ExpressionError,A as compile,N as evaluate,O as register};
{
"name": "@antv/expr",
"version": "1.0.0",
"version": "1.0.1",
"description": "A secure, high-performance expression evaluator for dynamic chart rendering",

@@ -13,3 +13,3 @@ "main": "dist/index.cjs.js",

"benchmark": "vitest bench",
"prepublishOnly": "npm run build"
"prepublishOnly": "pnpm run test && pnpm run build"
},

@@ -16,0 +16,0 @@ "keywords": [

@@ -1,2 +0,2 @@

# @antv/expr
# @antv/expr ![gzip size](https://img.badgesize.io/https://unpkg.com/@antv/expr/dist/index.esm.js?compression=gzip)

@@ -12,3 +12,3 @@ Have you ever wanted to make your SSR charts more dynamic but worried about security risks?

- ๐Ÿ› ๏ธ **Extensible** - Register custom functions to easily extend functionality.
- ๐Ÿชฉ **Lightweight** - Zero dependencies, small footprint, only `7.8 Kb` before gzip.
- ๐Ÿชฉ **Lightweight** - Zero dependencies, small footprint, before gzip it was less than `8 Kb`.

@@ -15,0 +15,0 @@