2 lines
25 KiB
JavaScript
2 lines
25 KiB
JavaScript
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).exprEval={})}(this,function(t){"use strict";var y="INUMBER",x="IOP1",w="IOP2",d="IOP3",g="IVAR",M="IVARNAME",E="IFUNCALL",m="IFUNDEF",A="IEXPR",b="IEXPREVAL",k="IMEMBER",O="IENDSTATEMENT",T="IARRAY";function C(t,e){this.type=t,this.value=null!=e?e:0}function c(t){return new C(x,t)}function f(t){return new C(w,t)}function l(t){return new C(d,t)}function N(t,i,o){var e,r,s,n,a,p,h=[];if(I(t))return P(t,o);for(var u=t.length,c=0;c<u;c++){var f=t[c],l=f.type;if(l===y||l===M)h.push(f.value);else if(l===w)r=h.pop(),e=h.pop(),"and"===f.value?h.push(!!e&&!!N(r,i,o)):"or"===f.value?h.push(!!e||!!N(r,i,o)):"="===f.value?(n=i.binaryOps[f.value],h.push(n(e,N(r,i,o),o))):(n=i.binaryOps[f.value],h.push(n(P(e,o),P(r,o))));else if(l===d)s=h.pop(),r=h.pop(),e=h.pop(),"?"===f.value?h.push(N(e?r:s,i,o)):(n=i.ternaryOps[f.value],h.push(n(P(e,o),P(r,o),P(s,o))));else if(l===g)if(f.value in i.functions)h.push(i.functions[f.value]);else if(f.value in i.unaryOps&&i.parser.isOperatorEnabled(f.value))h.push(i.unaryOps[f.value]);else{var v=o[f.value];if(void 0===v)throw new Error("undefined variable: "+f.value);h.push(v)}else if(l===x)e=h.pop(),n=i.unaryOps[f.value],h.push(n(P(e,o)));else if(l===E){for(p=f.value,a=[];0<p--;)a.unshift(P(h.pop(),o));if(!(n=h.pop()).apply||!n.call)throw new Error(n+" is not a function");h.push(n.apply(void 0,a))}else if(l===m)h.push(function(){for(var s=h.pop(),n=[],t=f.value;0<t--;)n.unshift(h.pop());function e(){for(var t=Object.assign({},o),e=0,r=n.length;e<r;e++)t[n[e]]=arguments[e];return N(s,i,t)}var r=h.pop();return Object.defineProperty(e,"name",{value:r,writable:!1}),o[r]=e}());else if(l===A)h.push(S(f,i));else if(l===b)h.push(f);else if(l===k)e=h.pop(),h.push(e[f.value]);else if(l===O)h.pop();else{if(l!==T)throw new Error("invalid Expression");for(p=f.value,a=[];0<p--;)a.unshift(h.pop());h.push(a)}}if(1<h.length)throw new Error("invalid Expression (parity)");return 0===h[0]?0:P(h[0],o)}function S(e,r){return I(e)?e:{type:b,value:function(t){return N(e.value,r,t)}}}function I(t){return t&&t.type===b}function P(t,e){return I(t)?t.value(e):t}function v(t,e){for(var r,s,n,i,o,a,p=[],h=0;h<t.length;h++){var u=t[h],c=u.type;if(c===y)"number"==typeof u.value&&u.value<0?p.push("("+u.value+")"):Array.isArray(u.value)?p.push("["+u.value.map(F).join(", ")+"]"):p.push(F(u.value));else if(c===w)s=p.pop(),r=p.pop(),i=u.value,e?"^"===i?p.push("Math.pow("+r+", "+s+")"):"and"===i?p.push("(!!"+r+" && !!"+s+")"):"or"===i?p.push("(!!"+r+" || !!"+s+")"):"||"===i?p.push("(function(a,b){ return Array.isArray(a) && Array.isArray(b) ? a.concat(b) : String(a) + String(b); }(("+r+"),("+s+")))"):"=="===i?p.push("("+r+" === "+s+")"):"!="===i?p.push("("+r+" !== "+s+")"):"["===i?p.push(r+"[("+s+") | 0]"):p.push("("+r+" "+i+" "+s+")"):"["===i?p.push(r+"["+s+"]"):p.push("("+r+" "+i+" "+s+")");else if(c===d){if(n=p.pop(),s=p.pop(),r=p.pop(),"?"!==(i=u.value))throw new Error("invalid Expression");p.push("("+r+" ? "+s+" : "+n+")")}else if(c===g||c===M)p.push(u.value);else if(c===x)r=p.pop(),"-"===(i=u.value)||"+"===i?p.push("("+i+r+")"):e?"not"===i?p.push("(!"+r+")"):"!"===i?p.push("fac("+r+")"):p.push(i+"("+r+")"):"!"===i?p.push("("+r+"!)"):p.push("("+i+" "+r+")");else if(c===E){for(a=u.value,o=[];0<a--;)o.unshift(p.pop());i=p.pop(),p.push(i+"("+o.join(", ")+")")}else if(c===m){for(s=p.pop(),a=u.value,o=[];0<a--;)o.unshift(p.pop());r=p.pop(),e?p.push("("+r+" = function("+o.join(", ")+") { return "+s+" })"):p.push("("+r+"("+o.join(", ")+") = "+s+")")}else if(c===k)r=p.pop(),p.push(r+"."+u.value);else if(c===T){for(a=u.value,o=[];0<a--;)o.unshift(p.pop());p.push("["+o.join(", ")+"]")}else if(c===A)p.push("("+v(u.value,e)+")");else if(c!==O)throw new Error("invalid Expression")}return 1<p.length&&(p=e?[p.join(",")]:[p.join(";")]),String(p[0])}function F(t){return"string"==typeof t?JSON.stringify(t).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029"):t}function a(t,e){for(var r=0;r<t.leng
|