Socket
Socket
Sign inDemoInstall

expr-eval

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.0.1

27

dist/bundle.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.exprEval = factory());
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.exprEval = factory());
}(this, (function () { 'use strict';

@@ -975,27 +975,12 @@

var unaryInstructionCache = {};
function unaryInstruction(value) {
var inst = unaryInstructionCache[value];
if (!inst) {
inst = unaryInstructionCache[value] = new Instruction(IOP1, value);
}
return inst;
return new Instruction(IOP1, value);
}
var binaryInstructionCache = {};
function binaryInstruction(value) {
var inst = binaryInstructionCache[value];
if (!inst) {
inst = binaryInstructionCache[value] = new Instruction(IOP2, value);
}
return inst;
return new Instruction(IOP2, value);
}
var ternaryInstructionCache = {};
function ternaryInstruction(value) {
var inst = ternaryInstructionCache[value];
if (!inst) {
inst = ternaryInstructionCache[value] = new Instruction(IOP3, value);
}
return inst;
return new Instruction(IOP3, value);
}

@@ -1002,0 +987,0 @@

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

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.exprEval=e()}(this,function(){"use strict";function t(t,e){for(var s=0;s<t.length;s++)if(t[s]===e)return s;return-1}function e(t,e){this.type=t,this.value=void 0!==e&&null!==e?e:0}function s(t,e){this.tokens=t,this.parser=e,this.unaryOps=e.unaryOps,this.binaryOps=e.binaryOps,this.ternaryOps=e.ternaryOps,this.functions=e.functions}function n(t){return"string"==typeof t?JSON.stringify(t).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029"):t}function r(t,s,n,i,o){for(var h,a,p,u,c=[],l=[],f=0,v=t.length;f<v;f++){var x=t[f],y=x.type;if(y===z)c.push(x);else if(y===Q&&o.hasOwnProperty(x.value))x=new e(z,o[x.value]),c.push(x);else if(y===H&&c.length>1)a=c.pop(),h=c.pop(),u=n[x.value],x=new e(z,u(h.value,a.value)),c.push(x);else if(y===K&&c.length>2)p=c.pop(),a=c.pop(),h=c.pop(),"?"===x.value?c.push(h.value?a.value:p.value):(u=i[x.value],x=new e(z,u(h.value,a.value,p.value)),c.push(x));else if(y===D&&c.length>0)h=c.pop(),u=s[x.value],x=new e(z,u(h.value)),c.push(x);else if(y===Z){for(;c.length>0;)l.push(c.shift());l.push(new e(Z,r(x.value,s,n,i,o)))}else if(y===tt&&c.length>0)h=c.pop(),c.push(new e(z,h.value[x.value]));else{for(;c.length>0;)l.push(c.shift());l.push(x)}}for(;c.length>0;)l.push(c.shift());return l}function i(t,s,n){for(var r=[],o=0,h=t.length;o<h;o++){var a=t[o],p=a.type;if(p===Q&&a.value===s)for(var u=0;u<n.tokens.length;u++){var c,l=n.tokens[u];c=l.type===D?J(l.value):l.type===H?W(l.value):l.type===K?V(l.value):new e(l.type,l.value),r.push(c)}else p===Z?r.push(new e(Z,i(a.value,s,n))):r.push(a)}return r}function o(t,e,s){for(var n,r,i,h,a=[],p=0,u=t.length;p<u;p++){var c=t[p],l=c.type;if(l===z)a.push(c.value);else if(l===H)r=a.pop(),n=a.pop(),h=e.binaryOps[c.value],a.push(h(n,r));else if(l===K)i=a.pop(),r=a.pop(),n=a.pop(),"?"===c.value?a.push(o(n?r:i,e,s)):(h=e.ternaryOps[c.value],a.push(h(n,r,i)));else if(l===Q)if(c.value in e.functions)a.push(e.functions[c.value]);else{var f=s[c.value];if(void 0===f)throw new Error("undefined variable: "+c.value);a.push(f)}else if(l===D)n=a.pop(),h=e.unaryOps[c.value],a.push(h(n));else if(l===Y){for(var v=c.value,x=[];v-- >0;)x.unshift(a.pop());if(h=a.pop(),!h.apply||!h.call)throw new Error(h+" is not a function");a.push(h.apply(void 0,x))}else if(l===Z)a.push(c.value);else{if(l!==tt)throw new Error("invalid Expression");n=a.pop(),a.push(n[c.value])}}if(a.length>1)throw new Error("invalid Expression (parity)");return a[0]}function h(t,e){for(var s,r,i,o,a=[],p=0,u=t.length;p<u;p++){var c=t[p],l=c.type;if(l===z)"number"==typeof c.value&&c.value<0?a.push("("+c.value+")"):a.push(n(c.value));else if(l===H)r=a.pop(),s=a.pop(),o=c.value,e?"^"===o?a.push("Math.pow("+s+", "+r+")"):"and"===o?a.push("(!!"+s+" && !!"+r+")"):"or"===o?a.push("(!!"+s+" || !!"+r+")"):"||"===o?a.push("(String("+s+") + String("+r+"))"):"=="===o?a.push("("+s+" === "+r+")"):"!="===o?a.push("("+s+" !== "+r+")"):a.push("("+s+" "+o+" "+r+")"):a.push("("+s+" "+o+" "+r+")");else if(l===K){if(i=a.pop(),r=a.pop(),s=a.pop(),o=c.value,"?"!==o)throw new Error("invalid Expression");a.push("("+s+" ? "+r+" : "+i+")")}else if(l===Q)a.push(c.value);else if(l===D)s=a.pop(),o=c.value,"-"===o||"+"===o?a.push("("+o+s+")"):e?"not"===o?a.push("(!"+s+")"):"!"===o?a.push("fac("+s+")"):a.push(o+"("+s+")"):"!"===o?a.push("("+s+"!)"):a.push("("+o+" "+s+")");else if(l===Y){for(var f=c.value,v=[];f-- >0;)v.unshift(a.pop());o=a.pop(),a.push(o+"("+v.join(", ")+")")}else if(l===tt)s=a.pop(),a.push(s+"."+c.value);else{if(l!==Z)throw new Error("invalid Expression");a.push("("+h(c.value,e)+")")}}if(a.length>1)throw new Error("invalid Expression (parity)");return a[0]}function a(e,s){for(var n=0,r=e.length;n<r;n++){var i=e[n];i.type===Q&&t(s,i.value)===-1?s.push(i.value):i.type===Z&&a(i.value,s)}}function p(t,e){return Number(t)+Number(e)}function u(t,e){return t-e}function c(t,e){return t*e}function l(t,e){return t/e}function f(t,e){return t%e}function v(t,e){return""+t+e}function x(t,e){return t===e}function y(t,e){return t!==e}function w(t,e){return t>e}function d(t,e){return t<e}function g(t,e){return t>=e}function m(t,e){return t<=e}function M(t,e){return Boolean(t&&e)}function k(t,e){return Boolean(t||e)}function E(t){return(Math.exp(t)-Math.exp(-t))/2}function O(t){return(Math.exp(t)+Math.exp(-t))/2}function b(t){return t===1/0?1:t===-(1/0)?-1:(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t))}function T(t){return t===-(1/0)?t:Math.log(t+Math.sqrt(t*t+1))}function A(t){return Math.log(t+Math.sqrt(t*t-1))}function C(t){return Math.log((1+t)/(1-t))/2}function N(t){return Math.log(t)*Math.LOG10E}function P(t){return-t}function I(t){return!t}function F(t){return t<0?Math.ceil(t):Math.floor(t)}function S(t){return Math.random()*(t||1)}function L(t){return _(t+1)}function q(t){return String(t).length}function R(){for(var t=0,e=0,s=0,n=arguments.length;s<n;s++){var r,i=Math.abs(arguments[s]);e<i?(r=e/i,t=t*r*r+1,e=i):i>0?(r=i/e,t+=r*r):t+=i}return e===1/0?1/0:e*Math.sqrt(t)}function U(t,e,s){return t?e:s}function B(t){return isFinite(t)&&t===Math.round(t)}function _(t){var e,s;if(B(t)){if(t<=0)return isFinite(t)?1/0:NaN;if(t>171)return 1/0;for(var n=t-2,r=t-1;n>1;)r*=n,n--;return 0===r&&(r=1),r}if(t<.5)return Math.PI/(Math.sin(Math.PI*t)*_(1-t));if(t>=171.35)return 1/0;if(t>85){var i=t*t,o=i*t,h=o*t,a=h*t;return Math.sqrt(2*Math.PI/t)*Math.pow(t/Math.E,t)*(1+1/(12*t)+1/(288*i)-139/(51840*o)-571/(2488320*h)+163879/(209018880*a)+5246819/(75246796800*a*t))}--t,s=st[0];for(var p=1;p<st.length;++p)s+=st[p]/(t+p);return e=t+et+.5,Math.sqrt(2*Math.PI)*Math.pow(e,t+.5)*Math.exp(-e)*s}function j(t,e,s,n){this.type=t,this.value=e,this.line=s,this.column=n}function G(t,e,s,n,r){this.pos=0,this.line=0,this.column=0,this.current=null,this.unaryOps=e,this.binaryOps=s,this.ternaryOps=n,this.consts=r,this.expression=t,this.savedPosition=0,this.savedCurrent=null,this.savedLine=0,this.savedColumn=0}function J(t){var s=ct[t];return s||(s=ct[t]=new e(D,t)),s}function W(t){var s=lt[t];return s||(s=lt[t]=new e(H,t)),s}function V(t){var s=ft[t];return s||(s=ft[t]=new e(K,t)),s}function X(t,e){this.parser=t,this.tokens=e,this.current=null,this.nextToken=null,this.next(),this.savedCurrent=null,this.savedNextToken=null}function $(){this.unaryOps={sin:Math.sin,cos:Math.cos,tan:Math.tan,asin:Math.asin,acos:Math.acos,atan:Math.atan,sinh:Math.sinh||E,cosh:Math.cosh||O,tanh:Math.tanh||b,asinh:Math.asinh||T,acosh:Math.acosh||A,atanh:Math.atanh||C,sqrt:Math.sqrt,log:Math.log,ln:Math.log,lg:Math.log10||N,log10:Math.log10||N,abs:Math.abs,ceil:Math.ceil,floor:Math.floor,round:Math.round,trunc:Math.trunc||F,"-":P,"+":Number,exp:Math.exp,not:I,length:q,"!":L},this.binaryOps={"+":p,"-":u,"*":c,"/":l,"%":f,"^":Math.pow,"||":v,"==":x,"!=":y,">":w,"<":d,">=":g,"<=":m,and:M,or:k},this.ternaryOps={"?":U},this.functions={random:S,fac:L,min:Math.min,max:Math.max,hypot:Math.hypot||R,pyt:Math.hypot||R,pow:Math.pow,atan2:Math.atan2,if:U,gamma:_},this.consts={E:Math.E,PI:Math.PI,true:!0,false:!1}}var z="INUMBER",D="IOP1",H="IOP2",K="IOP3",Q="IVAR",Y="IFUNCALL",Z="IEXPR",tt="IMEMBER";e.prototype.toString=function(){switch(this.type){case z:case D:case H:case K:case Q:return this.value;case Y:return"CALL "+this.value;case tt:return"."+this.value;default:return"Invalid Instruction"}},s.prototype.simplify=function(t){return t=t||{},new s(r(this.tokens,this.unaryOps,this.binaryOps,this.ternaryOps,t),this.parser)},s.prototype.substitute=function(t,e){return e instanceof s||(e=this.parser.parse(String(e))),new s(i(this.tokens,t,e),this.parser)},s.prototype.evaluate=function(t){return t=t||{},o(this.tokens,this,t)},s.prototype.toString=function(){return h(this.tokens,!1)},s.prototype.symbols=function(){var t=[];return a(this.tokens,t),t},s.prototype.variables=function(){var t=[];a(this.tokens,t);var e=this.functions;return t.filter(function(t){return!(t in e)})},s.prototype.toJSFunction=function(t,e){var s=this,n=new Function(t,"with(this.functions) with (this.ternaryOps) with (this.binaryOps) with (this.unaryOps) { return "+h(this.simplify(e).tokens,!0)+"; }");return function(){return n.apply(s,arguments)}};var et=4.7421875,st=[.9999999999999971,57.15623566586292,-59.59796035547549,14.136097974741746,-.4919138160976202,3399464998481189e-20,4652362892704858e-20,-9837447530487956e-20,.0001580887032249125,-.00021026444172410488,.00021743961811521265,-.0001643181065367639,8441822398385275e-20,-26190838401581408e-21,36899182659531625e-22],nt="TEOF",rt="TOP",it="TNUMBER",ot="TSTRING",ht="TPAREN",at="TCOMMA",pt="TNAME";j.prototype.toString=function(){return this.type+": "+this.value},G.prototype.newToken=function(t,e,s,n){return new j(t,e,null!=s?s:this.line,null!=n?n:this.column)},G.prototype.save=function(){this.savedPosition=this.pos,this.savedCurrent=this.current,this.savedLine=this.line,this.savedColumn=this.column},G.prototype.restore=function(){this.pos=this.savedPosition,this.current=this.savedCurrent,this.line=this.savedLine,this.column=this.savedColumn},G.prototype.next=function(){return this.pos>=this.expression.length?this.newToken(nt,"EOF"):this.isWhitespace()||this.isComment()?this.next():this.isNumber()||this.isOperator()||this.isString()||this.isParen()||this.isComma()||this.isNamedOp()||this.isConst()||this.isName()?this.current:void this.parseError('Unknown character "'+this.expression.charAt(this.pos)+'"')},G.prototype.isString=function(){var t=!1,e=this.line,s=this.column,n=this.pos,r=this.expression.charAt(n);if("'"===r||'"'===r){this.pos++,this.column++;for(var i=this.expression.indexOf(r,n+1);i>=0&&this.pos<this.expression.length;){if(this.pos=i+1,"\\"!==this.expression.charAt(i-1)){var o=this.expression.substring(n+1,i);this.current=this.newToken(ot,this.unescape(o),e,s);for(var h=o.indexOf("\n"),a=-1;h>=0;)this.line++,this.column=0,a=h,h=o.indexOf("\n",h+1);this.column+=o.length-a,t=!0;break}i=this.expression.indexOf(r,i+1)}}return t},G.prototype.isParen=function(){var t=this.expression.charAt(this.pos);return("("===t||")"===t)&&(this.current=this.newToken(ht,t),this.pos++,this.column++,!0)},G.prototype.isComma=function(){var t=this.expression.charAt(this.pos);return","===t&&(this.current=this.newToken(at,","),this.pos++,this.column++,!0)},G.prototype.isConst=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase()&&(e===this.pos||"_"!==s&&"."!==s&&(s<"0"||s>"9")))break}if(e>t){var n=this.expression.substring(t,e);if(n in this.consts)return this.current=this.newToken(it,this.consts[n]),this.pos+=n.length,this.column+=n.length,!0}return!1},G.prototype.isNamedOp=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase()&&(e===this.pos||"_"!==s&&(s<"0"||s>"9")))break}if(e>t){var n=this.expression.substring(t,e);if(n in this.binaryOps||n in this.unaryOps||n in this.ternaryOps)return this.current=this.newToken(rt,n),this.pos+=n.length,this.column+=n.length,!0}return!1},G.prototype.isName=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase()&&(e===this.pos||"_"!==s&&(s<"0"||s>"9")))break}if(e>t){var n=this.expression.substring(t,e);return this.current=this.newToken(pt,n),this.pos+=n.length,this.column+=n.length,!0}return!1},G.prototype.isWhitespace=function(){for(var t=!1,e=this.expression.charAt(this.pos);!(" "!==e&&"\t"!==e&&"\n"!==e&&"\r"!==e||(t=!0,this.pos++,this.column++,"\n"===e&&(this.line++,this.column=0),this.pos>=this.expression.length));)e=this.expression.charAt(this.pos);return t};var ut=/^[0-9a-f]{4}$/i;G.prototype.unescape=function(t){var e=t.indexOf("\\");if(e<0)return t;for(var s=t.substring(0,e);e>=0;){var n=t.charAt(++e);switch(n){case"'":s+="'";break;case'"':s+='"';break;case"\\":s+="\\";break;case"/":s+="/";break;case"b":s+="\b";break;case"f":s+="\f";break;case"n":s+="\n";break;case"r":s+="\r";break;case"t":s+="\t";break;case"u":var r=t.substring(e+1,e+5);ut.test(r)||this.parseError("Illegal escape sequence: \\u"+r),s+=String.fromCharCode(parseInt(r,16)),e+=4;break;default:throw this.parseError('Illegal escape sequence: "\\'+n+'"')}++e;var i=t.indexOf("\\",e);s+=t.substring(e,i<0?t.length:i),e=i}return s},G.prototype.isComment=function(){var t=this.expression.charAt(this.pos);if("/"===t&&"*"===this.expression.charAt(this.pos+1)){var e=this.pos;this.pos=this.expression.indexOf("*/",this.pos)+2,1===this.pos&&(this.pos=this.expression.length);for(var s=this.expression.substring(e,this.pos),n=s.indexOf("\n");n>=0;)this.line++,this.column=s.length-n,n=s.indexOf("\n",n+1);return!0}return!1},G.prototype.isNumber=function(){for(var t,e=!1,s=this.pos,n=s,r=s,i=this.column,o=i,h=!1,a=!1;s<this.expression.length&&(t=this.expression.charAt(s),t>="0"&&t<="9"||!h&&"."===t);)"."===t?h=!0:a=!0,s++,i++,e=a;if(e&&(r=s,o=i),"e"===t||"E"===t){s++,i++;for(var p=!0,u=!1;s<this.expression.length;){if(t=this.expression.charAt(s),!p||"+"!==t&&"-"!==t){if(!(t>="0"&&t<="9"))break;u=!0,p=!1}else p=!1;s++,i++}u||(s=r,i=o)}return e?(this.current=this.newToken(it,parseFloat(this.expression.substring(n,s))),this.pos=s,this.column=i):(this.pos=r,this.column=o),e},G.prototype.isOperator=function(){var t=this.expression.charAt(this.pos);if("+"===t||"-"===t||"*"===t||"/"===t||"%"===t||"^"===t||"?"===t||":"===t||"."===t)this.current=this.newToken(rt,t);else if("∙"===t||"•"===t)this.current=this.newToken(rt,"*");else if(">"===t)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(rt,">="),this.pos++,this.column++):this.current=this.newToken(rt,">");else if("<"===t)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(rt,"<="),this.pos++,this.column++):this.current=this.newToken(rt,"<");else if("|"===t){if("|"!==this.expression.charAt(this.pos+1))return!1;this.current=this.newToken(rt,"||"),this.pos++,this.column++}else if("="===t){if("="!==this.expression.charAt(this.pos+1))return!1;this.current=this.newToken(rt,"=="),this.pos++,this.column++}else{if("!"!==t)return!1;"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(rt,"!="),this.pos++,this.column++):this.current=this.newToken(rt,t)}return this.pos++,this.column++,!0},G.prototype.parseError=function(t){throw new Error("parse error ["+(this.line+1)+":"+(this.column+1)+"]: "+t)};var ct={},lt={},ft={};X.prototype.next=function(){return this.current=this.nextToken,this.nextToken=this.tokens.next()},X.prototype.tokenMatches=function(e,s){return"undefined"==typeof s||(Array.isArray(s)?t(s,e.value)>=0:"function"==typeof s?s(e):e.value===s)},X.prototype.save=function(){this.savedCurrent=this.current,this.savedNextToken=this.nextToken,this.tokens.save()},X.prototype.restore=function(){this.tokens.restore(),this.current=this.savedCurrent,this.nextToken=this.savedNextToken},X.prototype.accept=function(t,e){return!(this.nextToken.type!==t||!this.tokenMatches(this.nextToken,e))&&(this.next(),!0)},X.prototype.expect=function(t,e){if(!this.accept(t,e))throw new Error("parse error ["+this.tokens.line+":"+this.tokens.column+"]: Expected "+(e||t))},X.prototype.parseAtom=function(t){if(this.accept(pt))t.push(new e(Q,this.current.value));else if(this.accept(it))t.push(new e(z,this.current.value));else if(this.accept(ot))t.push(new e(z,this.current.value));else{if(!this.accept(ht,"("))throw new Error("unexpected "+this.nextToken);this.parseExpression(t),this.expect(ht,")")}},X.prototype.parseExpression=function(t){this.parseConditionalExpression(t)},X.prototype.parseConditionalExpression=function(t){for(this.parseOrExpression(t);this.accept(rt,"?");){var s=[],n=[];this.parseConditionalExpression(s),this.expect(rt,":"),this.parseConditionalExpression(n),t.push(new e(Z,s)),t.push(new e(Z,n)),t.push(V("?"))}},X.prototype.parseOrExpression=function(t){for(this.parseAndExpression(t);this.accept(rt,"or");)this.parseAndExpression(t),t.push(W("or"))},X.prototype.parseAndExpression=function(t){for(this.parseComparison(t);this.accept(rt,"and");)this.parseComparison(t),t.push(W("and"))},X.prototype.parseComparison=function(t){for(this.parseAddSub(t);this.accept(rt,["==","!=","<","<=",">=",">"]);){var e=this.current;this.parseAddSub(t),t.push(W(e.value))}},X.prototype.parseAddSub=function(t){for(this.parseTerm(t);this.accept(rt,["+","-","||"]);){var e=this.current;this.parseTerm(t),t.push(W(e.value))}},X.prototype.parseTerm=function(t){for(this.parseFactor(t);this.accept(rt,["*","/","%"]);){var e=this.current;this.parseFactor(t),t.push(W(e.value))}},X.prototype.parseFactor=function(t){function e(t){return t.value in s}var s=this.tokens.unaryOps;if(this.save(),this.accept(rt,e))if("-"!==this.current.value&&"+"!==this.current.value&&this.nextToken.type===ht&&"("===this.nextToken.value)this.restore(),this.parseExponential(t);else{var n=this.current;this.parseFactor(t),t.push(J(n.value))}else this.parseExponential(t)},X.prototype.parseExponential=function(t){for(this.parsePostfixExpression(t);this.accept(rt,"^");)this.parseFactor(t),t.push(W("^"))},X.prototype.parsePostfixExpression=function(t){for(this.parseFunctionCall(t);this.accept(rt,"!");)t.push(J("!"))},X.prototype.parseFunctionCall=function(t){function s(t){return t.value in n}var n=this.tokens.unaryOps;if(this.accept(rt,s)){var r=this.current;this.parseAtom(t),t.push(J(r.value))}else for(this.parseMemberExpression(t);this.accept(ht,"(");)if(this.accept(ht,")"))t.push(new e(Y,0));else{var i=this.parseArgumentList(t);t.push(new e(Y,i))}},X.prototype.parseArgumentList=function(t){for(var e=0;!this.accept(ht,")");)for(this.parseExpression(t),++e;this.accept(at);)this.parseExpression(t),++e;return e},X.prototype.parseMemberExpression=function(t){for(this.parseAtom(t);this.accept(rt,".");)this.expect(pt),t.push(new e(tt,this.current.value))},$.parse=function(t){return(new $).parse(t)},$.evaluate=function(t,e){return $.parse(t).evaluate(e)},$.prototype={parse:function(t){var e=[],n=new X(this,new G(t,this.unaryOps,this.binaryOps,this.ternaryOps,this.consts));return n.parseExpression(e),n.expect(nt,"EOF"),new s(e,this)},evaluate:function(t,e){return this.parse(t).evaluate(e)}};var vt={Parser:$,Expression:s};return vt});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.exprEval=e()}(this,function(){"use strict";function t(t,e){for(var s=0;s<t.length;s++)if(t[s]===e)return s;return-1}function e(t,e){this.type=t,this.value=void 0!==e&&null!==e?e:0}function s(t,e){this.tokens=t,this.parser=e,this.unaryOps=e.unaryOps,this.binaryOps=e.binaryOps,this.ternaryOps=e.ternaryOps,this.functions=e.functions}function n(t){return"string"==typeof t?JSON.stringify(t).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029"):t}function r(t,s,n,i,o){for(var h,p,a,u,c=[],l=[],f=0,v=t.length;f<v;f++){var x=t[f],y=x.type;if(y===z)c.push(x);else if(y===Q&&o.hasOwnProperty(x.value))x=new e(z,o[x.value]),c.push(x);else if(y===H&&c.length>1)p=c.pop(),h=c.pop(),u=n[x.value],x=new e(z,u(h.value,p.value)),c.push(x);else if(y===K&&c.length>2)a=c.pop(),p=c.pop(),h=c.pop(),"?"===x.value?c.push(h.value?p.value:a.value):(u=i[x.value],x=new e(z,u(h.value,p.value,a.value)),c.push(x));else if(y===D&&c.length>0)h=c.pop(),u=s[x.value],x=new e(z,u(h.value)),c.push(x);else if(y===Z){for(;c.length>0;)l.push(c.shift());l.push(new e(Z,r(x.value,s,n,i,o)))}else if(y===tt&&c.length>0)h=c.pop(),c.push(new e(z,h.value[x.value]));else{for(;c.length>0;)l.push(c.shift());l.push(x)}}for(;c.length>0;)l.push(c.shift());return l}function i(t,s,n){for(var r=[],o=0,h=t.length;o<h;o++){var p=t[o],a=p.type;if(a===Q&&p.value===s)for(var u=0;u<n.tokens.length;u++){var c,l=n.tokens[u];c=l.type===D?J(l.value):l.type===H?W(l.value):l.type===K?V(l.value):new e(l.type,l.value),r.push(c)}else a===Z?r.push(new e(Z,i(p.value,s,n))):r.push(p)}return r}function o(t,e,s){for(var n,r,i,h,p=[],a=0,u=t.length;a<u;a++){var c=t[a],l=c.type;if(l===z)p.push(c.value);else if(l===H)r=p.pop(),n=p.pop(),h=e.binaryOps[c.value],p.push(h(n,r));else if(l===K)i=p.pop(),r=p.pop(),n=p.pop(),"?"===c.value?p.push(o(n?r:i,e,s)):(h=e.ternaryOps[c.value],p.push(h(n,r,i)));else if(l===Q)if(c.value in e.functions)p.push(e.functions[c.value]);else{var f=s[c.value];if(void 0===f)throw new Error("undefined variable: "+c.value);p.push(f)}else if(l===D)n=p.pop(),h=e.unaryOps[c.value],p.push(h(n));else if(l===Y){for(var v=c.value,x=[];v-- >0;)x.unshift(p.pop());if(!(h=p.pop()).apply||!h.call)throw new Error(h+" is not a function");p.push(h.apply(void 0,x))}else if(l===Z)p.push(c.value);else{if(l!==tt)throw new Error("invalid Expression");n=p.pop(),p.push(n[c.value])}}if(p.length>1)throw new Error("invalid Expression (parity)");return p[0]}function h(t,e){for(var s,r,i,o,p=[],a=0,u=t.length;a<u;a++){var c=t[a],l=c.type;if(l===z)"number"==typeof c.value&&c.value<0?p.push("("+c.value+")"):p.push(n(c.value));else if(l===H)r=p.pop(),s=p.pop(),o=c.value,e?"^"===o?p.push("Math.pow("+s+", "+r+")"):"and"===o?p.push("(!!"+s+" && !!"+r+")"):"or"===o?p.push("(!!"+s+" || !!"+r+")"):"||"===o?p.push("(String("+s+") + String("+r+"))"):"=="===o?p.push("("+s+" === "+r+")"):"!="===o?p.push("("+s+" !== "+r+")"):p.push("("+s+" "+o+" "+r+")"):p.push("("+s+" "+o+" "+r+")");else if(l===K){if(i=p.pop(),r=p.pop(),s=p.pop(),"?"!==(o=c.value))throw new Error("invalid Expression");p.push("("+s+" ? "+r+" : "+i+")")}else if(l===Q)p.push(c.value);else if(l===D)s=p.pop(),"-"===(o=c.value)||"+"===o?p.push("("+o+s+")"):e?"not"===o?p.push("(!"+s+")"):"!"===o?p.push("fac("+s+")"):p.push(o+"("+s+")"):"!"===o?p.push("("+s+"!)"):p.push("("+o+" "+s+")");else if(l===Y){for(var f=c.value,v=[];f-- >0;)v.unshift(p.pop());o=p.pop(),p.push(o+"("+v.join(", ")+")")}else if(l===tt)s=p.pop(),p.push(s+"."+c.value);else{if(l!==Z)throw new Error("invalid Expression");p.push("("+h(c.value,e)+")")}}if(p.length>1)throw new Error("invalid Expression (parity)");return p[0]}function p(e,s){for(var n=0,r=e.length;n<r;n++){var i=e[n];i.type===Q&&-1===t(s,i.value)?s.push(i.value):i.type===Z&&p(i.value,s)}}function a(t,e){return Number(t)+Number(e)}function u(t,e){return t-e}function c(t,e){return t*e}function l(t,e){return t/e}function f(t,e){return t%e}function v(t,e){return""+t+e}function x(t,e){return t===e}function y(t,e){return t!==e}function w(t,e){return t>e}function M(t,e){return t<e}function d(t,e){return t>=e}function g(t,e){return t<=e}function m(t,e){return Boolean(t&&e)}function E(t,e){return Boolean(t||e)}function k(t){return(Math.exp(t)-Math.exp(-t))/2}function O(t){return(Math.exp(t)+Math.exp(-t))/2}function T(t){return t===1/0?1:t===-1/0?-1:(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t))}function b(t){return t===-1/0?t:Math.log(t+Math.sqrt(t*t+1))}function A(t){return Math.log(t+Math.sqrt(t*t-1))}function C(t){return Math.log((1+t)/(1-t))/2}function N(t){return Math.log(t)*Math.LOG10E}function P(t){return-t}function I(t){return!t}function F(t){return t<0?Math.ceil(t):Math.floor(t)}function S(t){return Math.random()*(t||1)}function R(t){return G(t+1)}function L(t){return String(t).length}function q(){for(var t=0,e=0,s=0,n=arguments.length;s<n;s++){var r,i=Math.abs(arguments[s]);e<i?(t=t*(r=e/i)*r+1,e=i):t+=i>0?(r=i/e)*r:i}return e===1/0?1/0:e*Math.sqrt(t)}function U(t,e,s){return t?e:s}function B(t){return isFinite(t)&&t===Math.round(t)}function G(t){var e,s;if(B(t)){if(t<=0)return isFinite(t)?1/0:NaN;if(t>171)return 1/0;for(var n=t-2,r=t-1;n>1;)r*=n,n--;return 0===r&&(r=1),r}if(t<.5)return Math.PI/(Math.sin(Math.PI*t)*G(1-t));if(t>=171.35)return 1/0;if(t>85){var i=t*t,o=i*t,h=o*t,p=h*t;return Math.sqrt(2*Math.PI/t)*Math.pow(t/Math.E,t)*(1+1/(12*t)+1/(288*i)-139/(51840*o)-571/(2488320*h)+163879/(209018880*p)+5246819/(75246796800*p*t))}--t,s=st[0];for(var a=1;a<st.length;++a)s+=st[a]/(t+a);return e=t+et+.5,Math.sqrt(2*Math.PI)*Math.pow(e,t+.5)*Math.exp(-e)*s}function _(t,e,s,n){this.type=t,this.value=e,this.line=s,this.column=n}function j(t,e,s,n,r){this.pos=0,this.line=0,this.column=0,this.current=null,this.unaryOps=e,this.binaryOps=s,this.ternaryOps=n,this.consts=r,this.expression=t,this.savedPosition=0,this.savedCurrent=null,this.savedLine=0,this.savedColumn=0}function J(t){return new e(D,t)}function W(t){return new e(H,t)}function V(t){return new e(K,t)}function X(t,e){this.parser=t,this.tokens=e,this.current=null,this.nextToken=null,this.next(),this.savedCurrent=null,this.savedNextToken=null}function $(){this.unaryOps={sin:Math.sin,cos:Math.cos,tan:Math.tan,asin:Math.asin,acos:Math.acos,atan:Math.atan,sinh:Math.sinh||k,cosh:Math.cosh||O,tanh:Math.tanh||T,asinh:Math.asinh||b,acosh:Math.acosh||A,atanh:Math.atanh||C,sqrt:Math.sqrt,log:Math.log,ln:Math.log,lg:Math.log10||N,log10:Math.log10||N,abs:Math.abs,ceil:Math.ceil,floor:Math.floor,round:Math.round,trunc:Math.trunc||F,"-":P,"+":Number,exp:Math.exp,not:I,length:L,"!":R},this.binaryOps={"+":a,"-":u,"*":c,"/":l,"%":f,"^":Math.pow,"||":v,"==":x,"!=":y,">":w,"<":M,">=":d,"<=":g,and:m,or:E},this.ternaryOps={"?":U},this.functions={random:S,fac:R,min:Math.min,max:Math.max,hypot:Math.hypot||q,pyt:Math.hypot||q,pow:Math.pow,atan2:Math.atan2,if:U,gamma:G},this.consts={E:Math.E,PI:Math.PI,true:!0,false:!1}}var z="INUMBER",D="IOP1",H="IOP2",K="IOP3",Q="IVAR",Y="IFUNCALL",Z="IEXPR",tt="IMEMBER";e.prototype.toString=function(){switch(this.type){case z:case D:case H:case K:case Q:return this.value;case Y:return"CALL "+this.value;case tt:return"."+this.value;default:return"Invalid Instruction"}},s.prototype.simplify=function(t){return t=t||{},new s(r(this.tokens,this.unaryOps,this.binaryOps,this.ternaryOps,t),this.parser)},s.prototype.substitute=function(t,e){return e instanceof s||(e=this.parser.parse(String(e))),new s(i(this.tokens,t,e),this.parser)},s.prototype.evaluate=function(t){return t=t||{},o(this.tokens,this,t)},s.prototype.toString=function(){return h(this.tokens,!1)},s.prototype.symbols=function(){var t=[];return p(this.tokens,t),t},s.prototype.variables=function(){var t=[];p(this.tokens,t);var e=this.functions;return t.filter(function(t){return!(t in e)})},s.prototype.toJSFunction=function(t,e){var s=this,n=new Function(t,"with(this.functions) with (this.ternaryOps) with (this.binaryOps) with (this.unaryOps) { return "+h(this.simplify(e).tokens,!0)+"; }");return function(){return n.apply(s,arguments)}};var et=4.7421875,st=[.9999999999999971,57.15623566586292,-59.59796035547549,14.136097974741746,-.4919138160976202,3399464998481189e-20,4652362892704858e-20,-9837447530487956e-20,.0001580887032249125,-.00021026444172410488,.00021743961811521265,-.0001643181065367639,8441822398385275e-20,-26190838401581408e-21,36899182659531625e-22],nt="TOP";_.prototype.toString=function(){return this.type+": "+this.value},j.prototype.newToken=function(t,e,s,n){return new _(t,e,null!=s?s:this.line,null!=n?n:this.column)},j.prototype.save=function(){this.savedPosition=this.pos,this.savedCurrent=this.current,this.savedLine=this.line,this.savedColumn=this.column},j.prototype.restore=function(){this.pos=this.savedPosition,this.current=this.savedCurrent,this.line=this.savedLine,this.column=this.savedColumn},j.prototype.next=function(){return this.pos>=this.expression.length?this.newToken("TEOF","EOF"):this.isWhitespace()||this.isComment()?this.next():this.isNumber()||this.isOperator()||this.isString()||this.isParen()||this.isComma()||this.isNamedOp()||this.isConst()||this.isName()?this.current:void this.parseError('Unknown character "'+this.expression.charAt(this.pos)+'"')},j.prototype.isString=function(){var t=!1,e=this.line,s=this.column,n=this.pos,r=this.expression.charAt(n);if("'"===r||'"'===r){this.pos++,this.column++;for(var i=this.expression.indexOf(r,n+1);i>=0&&this.pos<this.expression.length;){if(this.pos=i+1,"\\"!==this.expression.charAt(i-1)){var o=this.expression.substring(n+1,i);this.current=this.newToken("TSTRING",this.unescape(o),e,s);for(var h=o.indexOf("\n"),p=-1;h>=0;)this.line++,this.column=0,p=h,h=o.indexOf("\n",h+1);this.column+=o.length-p,t=!0;break}i=this.expression.indexOf(r,i+1)}}return t},j.prototype.isParen=function(){var t=this.expression.charAt(this.pos);return("("===t||")"===t)&&(this.current=this.newToken("TPAREN",t),this.pos++,this.column++,!0)},j.prototype.isComma=function(){return","===this.expression.charAt(this.pos)&&(this.current=this.newToken("TCOMMA",","),this.pos++,this.column++,!0)},j.prototype.isConst=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase()&&(e===this.pos||"_"!==s&&"."!==s&&(s<"0"||s>"9")))break}if(e>t){var n=this.expression.substring(t,e);if(n in this.consts)return this.current=this.newToken("TNUMBER",this.consts[n]),this.pos+=n.length,this.column+=n.length,!0}return!1},j.prototype.isNamedOp=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase()&&(e===this.pos||"_"!==s&&(s<"0"||s>"9")))break}if(e>t){var n=this.expression.substring(t,e);if(n in this.binaryOps||n in this.unaryOps||n in this.ternaryOps)return this.current=this.newToken(nt,n),this.pos+=n.length,this.column+=n.length,!0}return!1},j.prototype.isName=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase()&&(e===this.pos||"_"!==s&&(s<"0"||s>"9")))break}if(e>t){var n=this.expression.substring(t,e);return this.current=this.newToken("TNAME",n),this.pos+=n.length,this.column+=n.length,!0}return!1},j.prototype.isWhitespace=function(){for(var t=!1,e=this.expression.charAt(this.pos);!(" "!==e&&"\t"!==e&&"\n"!==e&&"\r"!==e||(t=!0,this.pos++,this.column++,"\n"===e&&(this.line++,this.column=0),this.pos>=this.expression.length));)e=this.expression.charAt(this.pos);return t};var rt=/^[0-9a-f]{4}$/i;return j.prototype.unescape=function(t){var e=t.indexOf("\\");if(e<0)return t;for(var s=t.substring(0,e);e>=0;){var n=t.charAt(++e);switch(n){case"'":s+="'";break;case'"':s+='"';break;case"\\":s+="\\";break;case"/":s+="/";break;case"b":s+="\b";break;case"f":s+="\f";break;case"n":s+="\n";break;case"r":s+="\r";break;case"t":s+="\t";break;case"u":var r=t.substring(e+1,e+5);rt.test(r)||this.parseError("Illegal escape sequence: \\u"+r),s+=String.fromCharCode(parseInt(r,16)),e+=4;break;default:throw this.parseError('Illegal escape sequence: "\\'+n+'"')}++e;var i=t.indexOf("\\",e);s+=t.substring(e,i<0?t.length:i),e=i}return s},j.prototype.isComment=function(){if("/"===this.expression.charAt(this.pos)&&"*"===this.expression.charAt(this.pos+1)){var t=this.pos;this.pos=this.expression.indexOf("*/",this.pos)+2,1===this.pos&&(this.pos=this.expression.length);for(var e=this.expression.substring(t,this.pos),s=e.indexOf("\n");s>=0;)this.line++,this.column=e.length-s,s=e.indexOf("\n",s+1);return!0}return!1},j.prototype.isNumber=function(){for(var t,e=!1,s=this.pos,n=s,r=s,i=this.column,o=i,h=!1,p=!1;s<this.expression.length&&((t=this.expression.charAt(s))>="0"&&t<="9"||!h&&"."===t);)"."===t?h=!0:p=!0,s++,i++,e=p;if(e&&(r=s,o=i),"e"===t||"E"===t){s++,i++;for(var a=!0,u=!1;s<this.expression.length;){if(t=this.expression.charAt(s),!a||"+"!==t&&"-"!==t){if(!(t>="0"&&t<="9"))break;u=!0,a=!1}else a=!1;s++,i++}u||(s=r,i=o)}return e?(this.current=this.newToken("TNUMBER",parseFloat(this.expression.substring(n,s))),this.pos=s,this.column=i):(this.pos=r,this.column=o),e},j.prototype.isOperator=function(){var t=this.expression.charAt(this.pos);if("+"===t||"-"===t||"*"===t||"/"===t||"%"===t||"^"===t||"?"===t||":"===t||"."===t)this.current=this.newToken(nt,t);else if("∙"===t||"•"===t)this.current=this.newToken(nt,"*");else if(">"===t)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(nt,">="),this.pos++,this.column++):this.current=this.newToken(nt,">");else if("<"===t)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(nt,"<="),this.pos++,this.column++):this.current=this.newToken(nt,"<");else if("|"===t){if("|"!==this.expression.charAt(this.pos+1))return!1;this.current=this.newToken(nt,"||"),this.pos++,this.column++}else if("="===t){if("="!==this.expression.charAt(this.pos+1))return!1;this.current=this.newToken(nt,"=="),this.pos++,this.column++}else{if("!"!==t)return!1;"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(nt,"!="),this.pos++,this.column++):this.current=this.newToken(nt,t)}return this.pos++,this.column++,!0},j.prototype.parseError=function(t){throw new Error("parse error ["+(this.line+1)+":"+(this.column+1)+"]: "+t)},X.prototype.next=function(){return this.current=this.nextToken,this.nextToken=this.tokens.next()},X.prototype.tokenMatches=function(e,s){return void 0===s||(Array.isArray(s)?t(s,e.value)>=0:"function"==typeof s?s(e):e.value===s)},X.prototype.save=function(){this.savedCurrent=this.current,this.savedNextToken=this.nextToken,this.tokens.save()},X.prototype.restore=function(){this.tokens.restore(),this.current=this.savedCurrent,this.nextToken=this.savedNextToken},X.prototype.accept=function(t,e){return!(this.nextToken.type!==t||!this.tokenMatches(this.nextToken,e))&&(this.next(),!0)},X.prototype.expect=function(t,e){if(!this.accept(t,e))throw new Error("parse error ["+this.tokens.line+":"+this.tokens.column+"]: Expected "+(e||t))},X.prototype.parseAtom=function(t){if(this.accept("TNAME"))t.push(new e(Q,this.current.value));else if(this.accept("TNUMBER"))t.push(new e(z,this.current.value));else if(this.accept("TSTRING"))t.push(new e(z,this.current.value));else{if(!this.accept("TPAREN","("))throw new Error("unexpected "+this.nextToken);this.parseExpression(t),this.expect("TPAREN",")")}},X.prototype.parseExpression=function(t){this.parseConditionalExpression(t)},X.prototype.parseConditionalExpression=function(t){for(this.parseOrExpression(t);this.accept(nt,"?");){var s=[],n=[];this.parseConditionalExpression(s),this.expect(nt,":"),this.parseConditionalExpression(n),t.push(new e(Z,s)),t.push(new e(Z,n)),t.push(V("?"))}},X.prototype.parseOrExpression=function(t){for(this.parseAndExpression(t);this.accept(nt,"or");)this.parseAndExpression(t),t.push(W("or"))},X.prototype.parseAndExpression=function(t){for(this.parseComparison(t);this.accept(nt,"and");)this.parseComparison(t),t.push(W("and"))},X.prototype.parseComparison=function(t){for(this.parseAddSub(t);this.accept(nt,["==","!=","<","<=",">=",">"]);){var e=this.current;this.parseAddSub(t),t.push(W(e.value))}},X.prototype.parseAddSub=function(t){for(this.parseTerm(t);this.accept(nt,["+","-","||"]);){var e=this.current;this.parseTerm(t),t.push(W(e.value))}},X.prototype.parseTerm=function(t){for(this.parseFactor(t);this.accept(nt,["*","/","%"]);){var e=this.current;this.parseFactor(t),t.push(W(e.value))}},X.prototype.parseFactor=function(t){function e(t){return t.value in s}var s=this.tokens.unaryOps;if(this.save(),this.accept(nt,e))if("-"!==this.current.value&&"+"!==this.current.value&&"TPAREN"===this.nextToken.type&&"("===this.nextToken.value)this.restore(),this.parseExponential(t);else{var n=this.current;this.parseFactor(t),t.push(J(n.value))}else this.parseExponential(t)},X.prototype.parseExponential=function(t){for(this.parsePostfixExpression(t);this.accept(nt,"^");)this.parseFactor(t),t.push(W("^"))},X.prototype.parsePostfixExpression=function(t){for(this.parseFunctionCall(t);this.accept(nt,"!");)t.push(J("!"))},X.prototype.parseFunctionCall=function(t){function s(t){return t.value in n}var n=this.tokens.unaryOps;if(this.accept(nt,s)){var r=this.current;this.parseAtom(t),t.push(J(r.value))}else for(this.parseMemberExpression(t);this.accept("TPAREN","(");)if(this.accept("TPAREN",")"))t.push(new e(Y,0));else{var i=this.parseArgumentList(t);t.push(new e(Y,i))}},X.prototype.parseArgumentList=function(t){for(var e=0;!this.accept("TPAREN",")");)for(this.parseExpression(t),++e;this.accept("TCOMMA");)this.parseExpression(t),++e;return e},X.prototype.parseMemberExpression=function(t){for(this.parseAtom(t);this.accept(nt,".");)this.expect("TNAME"),t.push(new e(tt,this.current.value))},$.parse=function(t){return(new $).parse(t)},$.evaluate=function(t,e){return $.parse(t).evaluate(e)},$.prototype={parse:function(t){var e=[],n=new X(this,new j(t,this.unaryOps,this.binaryOps,this.ternaryOps,this.consts));return n.parseExpression(e),n.expect("TEOF","EOF"),new s(e,this)},evaluate:function(t,e){return this.parse(t).evaluate(e)}},{Parser:$,Expression:s}});
{
"name": "expr-eval",
"version": "1.0.0",
"version": "1.0.1",
"description": "Mathematical expression evaluator",

@@ -5,0 +5,0 @@ "main": "dist/bundle.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc