meval
Version:
Mimic eval in given context
3 lines (2 loc) • 16.3 kB
JavaScript
/* meval v1.1.0 | Copyright 2020 (c) Marek Sierociński| https://github.com/marverix/meval/blob/master/LICENSE */
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).meval=e()}(this,function(){"use strict";function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function u(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function s(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),t}function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&n(t,e)}function a(t){return(a=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function n(t,e){return(n=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function c(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function l(t,e,n){return(l="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(t,e,n){var r=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=a(t)););return t}(t,e);if(r){var i=Object.getOwnPropertyDescriptor(r,e);return i.get?i.get.call(n):i.value}})(t,e,n||t)}var e=function(){function t(){u(this,t),this.data=""}return s(t,[{key:"add",value:function(t){this.data+=t}},{key:"take",value:function(){throw new Error("Overwrite this method!")}},{key:"$pattern",get:function(){return this.constructor.$pattern}}]),t}(),h=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,e),s(t,[{key:"resolve",value:function(){return this.data}},{key:"last",get:function(){return this.data[this.data.length-1]}}]),t}(),f=-1,v=0,d=1,t=function(){function e(){var t;return u(this,e),(t=c(this,a(e).call(this))).hasDot=!1,t}return o(e,h),s(e,[{key:"take",value:function(t){if(this.$pattern.test(t))this.add(t);else{if("."!==t||this.hasDot)return f;this.add(t),this.hasDot=!0}return v}},{key:"resolve",value:function(){if("."===this.last)throw new Error("Unexpected end of the float number!");return this.hasDot?parseFloat(this.data):parseInt(this.data,10)}}]),e}();t.$pattern=/^\d/;var y=t,p=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,h),s(t,[{key:"take",value:function(t){return this.add(t),this.$pattern.test(this.data)?d:v}},{key:"resolve",value:function(){switch(this.data){case"true":return!0;case"false":return!1;default:throw new Error("Unexpected end of the boolean")}}}]),t}();p.$pattern=/^(?:true|false)/;var x=p,k=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,h),s(t,[{key:"take",value:function(t){return this.add(t),this.$pattern.test(this.data)?d:v}},{key:"resolve",value:function(){switch(this.data){case"undefined":return;case"null":return null;case"NaN":return NaN;case"Infinity":return 1/0;default:throw new Error("Unexpected end of the property")}}}]),t}();k.$pattern=/^(?:undefined|null|NaN|Infinity)/;var w=k,S=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,h),s(t,[{key:"take",value:function(t){return this.add(t),this.$pattern.test(this.data)?d:v}},{key:"resolve",value:function(){switch(this.data){case"Date":return Date;case"Math":return Math;case"Number":return Number;case"String":return String;case"Array":return Array;case"Object":return Object;default:throw new Error("Unexpected end of the global object")}}}]),t}();S.$pattern=/^(?:Date|Math|Number|String|Array|Object)/;var $=S,E=function(){function e(){var t;return u(this,e),(t=c(this,a(e).call(this))).delimiter=null,t.escape=!1,t}return o(e,h),s(e,[{key:"take",value:function(t){return null==this.delimiter?(this.delimiter=t,v):t===this.delimiter?this.escape?(this.add(this.delimiter),this.escape=!1,v):(this.delimiter=null,d):(this.escape||"\\"!==t?(this.escape&&(this.add("\\"),this.escape=!1),this.add(t)):this.escape=!0,v)}},{key:"resolve",value:function(){if(null!=this.delimiter)throw new Error("Unexpected end of the string!");return this.data}}]),e}();E.$pattern=/^['"`]/;var m=E,b=/^[$_A-Za-z0-9]/,g=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,h),s(t,[{key:"take",value:function(t){return b.test(t)?(this.add(t),v):f}},{key:"resolve",value:function(t){return t?t[this.data]:this.data}}]),t}();g.$pattern=/^[$_A-Za-z]/;var _=[y,x,w,$,m,g];var O={find:function(t){var e=!0,n=!1,r=void 0;try{for(var i,u=_[Symbol.iterator]();!(e=(i=u.next()).done);e=!0){var s=i.value;if(s.$pattern.test(t))return s}}catch(t){n=!0,r=t}finally{try{e||null==u.return||u.return()}finally{if(n)throw r}}return null},isConsumer:function(t){return t instanceof h}},C=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,e),s(t,[{key:"take",value:function(t){if(null==this.$pattern)throw new Error("Incorrect operator");return this.add(t),this.$pattern.test(this.data)?d:v}},{key:"execute",value:function(){throw new Error("Overwrite this method!")}},{key:"resolveSide",value:function(t,e){return t instanceof h?t.resolve(e):t}},{key:"$priority",get:function(){return this.constructor.$priority}}]),t}(),F=function(){function n(t){var e;return u(this,n),(e=c(this,a(n).call(this))).args=t,e}return o(n,C),s(n,[{key:"execute",value:function(t,e,n){var r=e-1,i=this.resolveSide(n[r],t).apply(null,this.args);n.splice(r,2,i)}}]),n}();F.$pattern=null,F.$prevMustBeConsumer=!0;var I=F,j=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,C),s(t,[{key:"execute",value:function(t,e,n){var r=n[e+1],i=r instanceof C?this._executeForOperator(r,t):this._executeForConsumer(r,t);null===i?n.splice(e,2):n.splice(e,2,i)}},{key:"_executeForOperator",value:function(){throw new Error("Overwrite this method!")}},{key:"_executeForConsumer",value:function(){throw new Error("Overwrite this method!")}}]),t}();j.$prevMustntBeConsumer=!0;var N=j,M=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,C),s(t,[{key:"execute",value:function(t,e,n){var r=e-1,i=this._execute(n[r],n[e+1],t);n.splice(r,3,i)}},{key:"_execute",value:function(){throw new Error("Overwrite this method!")}}]),t}();M.$prevMustBeConsumer=!0;var A=M,D=function(){function e(){return u(this,e),c(this,a(e).call(this))}return o(e,N),s(e,[{key:"_executeForOperator",value:function(t){if(t instanceof e)return null;throw new Error("Invalid usage of logical not")}},{key:"_executeForConsumer",value:function(t,e){return!this.resolveSide(t,e)}}]),e}();D.$pattern=/^!(?!=)/;var P=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return!!this.resolveSide(t,n)&&this.resolveSide(e,n)}}]),t}();P.$pattern=/^&&/;var q=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return!!this.resolveSide(t,n)||this.resolveSide(e,n)}}]),t}();q.$pattern=/^\|\|/;var B={Not:D,And:P,Or:q},L=function(){function e(){return u(this,e),c(this,a(e).call(this))}return o(e,N),s(e,[{key:"_executeForOperator",value:function(t){if(t instanceof e)return new R;if(t instanceof e)return this;throw new Error("Invalid usage of unary negation")}},{key:"_executeForConsumer",value:function(t,e){return-this.resolveSide(t,e)}}]),e}();L.$pattern=/^-/;var R=function(){function e(){return u(this,e),c(this,a(e).call(this))}return o(e,N),s(e,[{key:"_executeForOperator",value:function(t){if(t instanceof e)return this;if(t instanceof L)return new L;throw new Error("Invalid usage of unary plus")}},{key:"_executeForConsumer",value:function(t,e){return+this.resolveSide(t,e)}}]),e}();R.$pattern=/^\+/;var U={Plus:R,Negation:L},G=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)*this.resolveSide(e,n)}}]),t}();G.$pattern=/^\*/;var T=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)/this.resolveSide(e,n)}}]),t}();T.$pattern=/^\//;var z=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)%this.resolveSide(e,n)}}]),t}();z.$pattern=/^%/;var Z=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)+this.resolveSide(e,n)}}]),t}();Z.$pattern=/^\+/;var H=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)-this.resolveSide(e,n)}}]),t}();H.$pattern=/^-/;var J={Multiplication:G,Division:T,Remainder:z,Addition:Z,Subtraction:H},K=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)>=this.resolveSide(e,n)}}]),t}();K.$pattern=/^>=/;var Q=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)<=this.resolveSide(e,n)}}]),t}();Q.$pattern=/^<=/;var V=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)>this.resolveSide(e,n)}}]),t}();V.$pattern=/^>/;var W=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)<this.resolveSide(e,n)}}]),t}();W.$pattern=/^</;var X=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)instanceof this.resolveSide(e,n)}}]),t}();X.$pattern=/^instanceof/;var Y=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)in this.resolveSide(e,n)}}]),t}();Y.$pattern=/^in/;var tt=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)===this.resolveSide(e,n)}}]),t}();tt.$pattern=/^===/;var et=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)!==this.resolveSide(e,n)}}]),t}();et.$pattern=/^!==/;var nt=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)==this.resolveSide(e,n)}}]),t}();nt.$pattern=/^==/;var rt=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){return this.resolveSide(t,n)!=this.resolveSide(e,n)}}]),t}();rt.$pattern=/^!=/;var it={GreaterEq:K,LessEq:Q,Greater:V,Less:W,Instanceof:X,In:Y,Identity:tt,Nonidentity:et,Equality:nt,Inequality:rt},ut="Invalid usage of three-argument operator",st=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,C),s(t,[{key:"execute",value:function(t,e,n){var r=e-1;if(!(n[e+2]instanceof ot))throw new Error(ut);var i=this.resolveSide(n[r],t)?this.resolveSide(n[e+1],t):this.resolveSide(n[e+3],t);n.splice(r,5,i)}}]),t}();st.$prevMustBeConsumer=!0,st.$pattern=/^\?/;var ot=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,C),s(t,[{key:"execute",value:function(){throw new Error(ut)}}]),t}();ot.$prevMustBeConsumer=!0,ot.$pattern=/^:/;var at={Start:st,End:ot},ct=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,A),s(t,[{key:"_execute",value:function(t,e,n){try{var r=(t=this.resolveSide(t,n))[e=this.resolveSide(e,!1)];return"function"==typeof r&&(r=r.bind(t)),r}catch(t){return t}}}]),t}();ct.$pattern=/^\./;var lt=ct,ht=function(){function t(){return u(this,t),c(this,a(t).call(this))}return o(t,N),s(t,[{key:"_executeForOperator",value:function(){throw new Error("Invalid usage of typeof")}},{key:"_executeForConsumer",value:function(t,e){return i(this.resolveSide(t,e))}}]),t}();ht.$pattern=/^typeof(?![A-Za-z0-9$_])/;for(var ft=[lt,I,B.Not,U.Plus,U.Negation,ht,J.Multiplication,J.Division,J.Remainder,J.Addition,J.Subtraction,it.GreaterEq,it.LessEq,it.Greater,it.Less,it.Instanceof,it.In,it.Identity,it.Nonidentity,it.Equality,it.Inequality,B.And,B.Or,at.Start,at.End],vt=0;vt<ft.length;vt++)ft[vt].$priority=ft.length-vt;var dt={find:function(t,e){var n=!0,r=!1,i=void 0;try{for(var u,s=ft[Symbol.iterator]();!(n=(u=s.next()).done);n=!0){var o=u.value;if(null!=o.$pattern&&o.$pattern.test(t)&&(!o.$prevMustBeConsumer&&!e||!o.$prevMustntBeConsumer&&e))return o}}catch(t){r=!0,i=t}finally{try{n||null==s.return||s.return()}finally{if(r)throw i}}return null},isOperator:function(t){return t instanceof C},FunctionCall:I},yt=/\s/,pt=/\(/,xt=/\)/,kt=/,/,wt=function(){function t(){u(this,t),this.entities=[],this.currentEntity=null}return s(t,[{key:"run",value:function(){return this.readExpression(),this.execute()}},{key:"readExpression",value:function(){var t,e,n;for(this.idx;this.idx<this.expression.length;this.idx++){if(t=this.expression[this.idx],null==this.currentEntity){if(this.shouldFinish(t))break;if(this.shouldSkip(t))continue;if(null==(n=this.findEntity()))throw new Error("Unexpected char ".concat(t," at index ").concat(this.idx));this.currentEntity=new n}(e=this.currentEntity.take(t))===d?this.resolveEntity():e===f&&(this.resolveEntity(),--this.idx)}null!=this.currentEntity&&this.resolveEntity()}},{key:"execute",value:function(){for(;1<this.entities.length;){for(var t={priority:0,index:-1},e=0;e<this.entities.length;e++)if(dt.isOperator(this.entities[e])){var n=this.entities[e].$priority;t.priority<n&&(t.priority=n,t.index=e)}this.entities[t.index].execute(this.context,t.index,this.entities)}return O.isConsumer(this.entities[0])&&(this.entities[0]=this.entities[0].resolve(this.context)),this.entities[0]}},{key:"shouldSkip",value:function(t){return!!yt.test(t)||!!pt.test(t)&&(this.subEvaluate(this.isLastEntityConsumer),!0)}},{key:"shouldFinish",value:function(){return!1}},{key:"findEntity",value:function(){var t=this.expression.substr(this.idx),e=dt.find(t,this.isLastEntityConsumer);return null==e&&(e=O.find(t)),e}},{key:"resolveEntity",value:function(){this.entities.push(this.currentEntity),this.currentEntity=null}},{key:"subEvaluate",value:function(t){var e=0<arguments.length&&void 0!==t&&t?Et:$t;this.idx++,this.currentEntity=new e(this).run(),this.resolveEntity()}},{key:"isLastEntityConsumer",get:function(){if(0<this.entities.length){var t=this.entities[this.entities.length-1];return t instanceof dt.FunctionCall||!dt.isOperator(t)}return!1}}]),t}(),St=function(){function r(t,e){var n;if(u(this,r),n=c(this,a(r).call(this)),"string"!=typeof t)throw new TypeError("expression is not a String");if("object"!==i(e)||null===e)throw new TypeError("context is not an Object");return n.expression=t,n.context=e,n.idx=0,n}return o(r,wt),r}(),$t=function(){function n(t){var e;return u(this,n),(e=c(this,a(n).call(this))).parent=t,e}return o(n,wt),s(n,[{key:"shouldFinish",value:function(t){return!(!xt.test(t)&&!kt.test(t))&&(this.idx++,!0)}},{key:"expression",get:function(){return this.parent.expression}},{key:"context",get:function(){return this.parent.context}},{key:"idx",get:function(){return this.parent.idx},set:function(t){this.parent.idx=t}}]),n}(),Et=function(){function e(t){return u(this,e),c(this,a(e).call(this,t))}return o(e,$t),s(e,[{key:"shouldSkip",value:function(t){return l(a(e.prototype),"shouldSkip",this).call(this,t)||(this.idx--,this.subEvaluate(!1)),!0}},{key:"execute",value:function(){return this.idx--,new dt.FunctionCall(this.entities)}}]),e}(),mt=St;return function(t,e){return new mt(t,e).run()}});