UNPKG

@zvenigora/jse-eval

Version:

JavaScript expression parsing and evaluation.

3 lines (2 loc) 15.9 kB
import e from"jsep";export{default as jsep,default as parse}from"jsep";function n(){return n=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},n.apply(this,arguments)}function t(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}var r={undefined:void 0,null:null,true:!0,false:!1},i=/*#__PURE__*/function(){function e(){}return e.getValue=function(n,t,r){var i=e.getScopedKeyValue(n,t,r);if(void 0!==i){var o=i[1],a=e.getBindFunction(o,t,r,i[2]);return"function"==typeof a?a:o}},e.getScopePair=function(n,t,r){if(void 0!==n&&null!=r&&r.scopes){var i=e.getKeyValue(r.scopes,t,r);if(void 0!==i){var o=i[1];if(o)return[i[0],o]}}},e.getBindFunction=function(n,t,r,i){var o,a=e.hasProperty(null==r?void 0:r.scopes,i)?null==r||null==(o=r.scopes[i])?void 0:o.options:r;if("function"==typeof n&&null!=a&&a.functionBindings){var s=e.getKeyValue(a.functionBindings,t,r);if(void 0!==s){var u=s[1];if(u){var l=u;return null!=l&&l.arguments?n.bind.apply(n,[null==l?void 0:l.thisRef].concat(l.arguments)):n.bind(null==l?void 0:l.thisRef)}}}},e.hasProperty=function(e,n){return!(!e||!n)&&Object.prototype.hasOwnProperty.call(e,n)},e.getScopedKeyValue=function(n,t,r){var i=[""],o=e.hasProperty(n,null==r?void 0:r.currentScopeName)&&e.hasProperty(null==r?void 0:r.scopes,null==r?void 0:r.currentScopeName),a=e.hasProperty(n,null==r?void 0:r.globalScopeName)&&e.hasProperty(null==r?void 0:r.scopes,null==r?void 0:r.globalScopeName);o&&i.push(null==r?void 0:r.currentScopeName),a&&i.push(null==r?void 0:r.globalScopeName);for(var s=0,u=i;s<u.length;s++){var l=u[s],c=this.getKeyValue(l?n[l]:n,t,r);if(void 0!==c)return[c[0],c[1],l]}},e.getKeyValue=function(n,t,r){if(e.blockListTest(n,t,r),e.allowListTest(n,t,r),null!=r&&r.caseSensitive||"string"!=typeof t)return[t,n[t]];if("string"==typeof t){var i=n;do{var o=Object.getOwnPropertyNames(i);if(Array.isArray(o)){var a=o.find(function(e){return 0===e.localeCompare(t,"en",{sensitivity:"base"})});if(a)return[a,i[a]]}}while(i=Object.getPrototypeOf(i))}},e.getLiteralKeyValue=function(n,t,r){if(e.blockListTest(n,t,r),e.allowListTest(n,t,r),null!=r&&r.caseSensitive)return[t,n[t]];for(var i=0,o=Object.entries(n);i<o.length;i++){var a=o[i],s=a[0],u=a[1];if(0===s.localeCompare(t,"en",{sensitivity:"base"}))return[s,u]}},e.blockListTest=function(e,n,t){if(null!=t&&t.blockList&&"string"==typeof n&&t.blockList.find(t.caseSensitive?function(e){return e===n}:function(e){return 0===e.localeCompare(n,"en",{sensitivity:"base"})}))throw Error('Access to member "'+n+'" from blockList is not permitted.')},e.allowListTest=function(e,n,t){if(null!=t&&t.allowList&&"string"==typeof n&&!t.allowList.find(null!=t&&t.caseSensitive?function(e){return e===n}:function(e){return 0===e.localeCompare(n,"en",{sensitivity:"base"})}))throw Error('Access to member "'+n+'" not in allowList is not permitted.')},e}(),o=/*#__PURE__*/function(){function o(e,n,t){this.context=void 0,this.isAsync=void 0,this.options=void 0,this.context=e,this.isAsync=n,this.options=t||o.defaultOptions}o.addUnaryOp=function(n,t){e.addUnaryOp(n),o.unops[n]=t},o.addBinaryOp=function(n,t,r,i){var a,s,u;"function"==typeof t?u=t:(a=t,"function"==typeof r?u=r:(s=r,u=i)),e.addBinaryOp(n,a||1,s),o.binops[n]=u},o.addEvaluator=function(e,n){o.evaluators[e]=n},o.addConditionalEvaluator=function(e,n,t){o.conditionalEvaluators[e]||(o.conditionalEvaluators[e]=[]),o.conditionalEvaluators[e].push({evaluator:t,predicate:n})},o.registerPlugin=function(){[].slice.call(arguments).forEach(function(e){e.init&&o.parse.plugins.register(e),e.initEval&&e.initEval.call(o,o)})},o.addOptions=function(e){o.defaultOptions=e},o.eval=function(e,n,t){return new o(n,void 0,t).eval(e)},o.evalAsync=function(e,n,t){try{return Promise.resolve(new o(n,!0,t).eval(e))}catch(e){return Promise.reject(e)}},o.compile=function(e){return o.eval.bind(null,o.jsep(e))},o.compileAsync=function(e){return o.evalAsync.bind(null,o.jsep(e))},o.evalExpr=function(e,n){return o.compile(e)(n)},o.evalExprAsync=function(e,n){return o.compileAsync(e)(n)};var a,s,u=o.prototype;return u.eval=function(e,n){void 0===n&&(n=function(e){return e});var r=o.conditionalEvaluators[e.type];if(r)for(var i,a=function(e,n){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,n){if(e){if("string"==typeof e)return t(e,n);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(e,n):void 0}}(e))){r&&(e=r);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r);!(i=a()).done;){var s=i.value;if(s.predicate.call(this,e,this.context))return this.evalSyncAsync(s.evaluator.bind(this)(e,this.context),function(t){return e._value=t,n(t)})}var u=o.evaluators[e.type];if(void 0===u)throw new Error("unknown node type: "+JSON.stringify(e,null,2));return this.evalSyncAsync(u.bind(this)(e,this.context),function(t){return e._value=t,n(t)})},u.evalSyncAsync=function(e,n){return this.isAsync?Promise.resolve(e).then(n):n(e)},u.evalArrayExpression=function(e){return this.evalArray(e.elements)},u.evalArray=function(e){var n=this,t=e.map(function(e){return n.eval(e)}),r=function(n){return n.reduce(function(n,t,r){return"SpreadElement"===e[r].type?[].concat(n,t):(n.push(t),n)},[])};return this.isAsync?Promise.all(t).then(r):r(t)},u.evalBinaryExpression=function(e){var n=this;if("||"===e.operator)return this.eval(e.left,function(t){return t||n.eval(e.right)});if("&&"===e.operator)return this.eval(e.left,function(t){return t&&n.eval(e.right)});var t=[this.eval(e.left),this.eval(e.right)],r=function(n){return o.binops[e.operator](n[0],n[1])};return this.isAsync?Promise.all(t).then(r):r(t)},u.evalCompoundExpression=function(e){var n=this;return this.isAsync?e.body.reduce(function(e,t){return e.then(function(){return n.eval(t)})},Promise.resolve()):e.body.map(function(e){return n.eval(e)})[e.body.length-1]},u.evalCallExpression=function(e){var n=this;return this.evalSyncAsync(this.evalCall(e.callee),function(t){var r=t[0],i=t[1];return n.evalSyncAsync(n.evalArray(e.arguments),function(t){return r.apply(i===e.callee?n.context:i,t)})})},u.evalCall=function(e){if("MemberExpression"===e.type)return this.evalSyncAsync(this.evaluateMember(e),function(n){return o.validateFnAndCall(n[1],n[0],e)});if("Identifier"===e.type){var n=i.getValue(this.context,e.name,this.options);return o.validateFnAndCall(n,e)}return this.eval(e,function(n){return o.validateFnAndCall(n,e)})},u.evalConditionalExpression=function(e){var n=this;return this.eval(e.test,function(t){return n.eval(t?e.consequent:e.alternate)})},u.evalIdentifier=function(e){if(this.caseSensitive)return i.getValue(this.context,e.name,this.options);if(0===e.name.localeCompare("this","en",{sensitivity:"base"}))return this.evalThisExpression();var n=i.getLiteralKeyValue(r,e.name,this.options);if(n)return n[1];var t=i.getValue(this.context,e.name,this.options);if(void 0!==t&&this.options.scopes){var o=i.getScopePair(this.context,e.name,this.options);o&&(e._scope=o[1])}return t},o.evalLiteral=function(e){return e.value},u.evalMemberExpression=function(e){return this.evalSyncAsync(this.evaluateMember(e),function(e){return e[1]})},u.evaluateMember=function(e){var t=this;return this.eval(e.object,function(r){return t.evalSyncAsync(e.computed?t.eval(e.property):e.property.name,function(o){if(/^__proto__|prototype|constructor$/.test(o))throw Error('Access to member "'+o+'" disallowed.');var a=e.optional?r||{}:r,s=e.object._scope,u=s?n({},t.options,s.options):t.options,l=i.getValue(a,o,u);return[r,l,o]})})},u.evalThisExpression=function(){return this.context},u.evalUnaryExpression=function(e){return this.eval(e.argument,function(n){return o.unops[e.operator](n)})},u.evalArrowFunctionExpression=function(e){var n=this;return this.isAsync!==e.async?o[e.async?"evalAsync":"eval"](e,this.context):function(){var t=n.evalArrowContext(e,[].slice.call(arguments));return o[e.async?"evalAsync":"eval"](e.body,t)}},u.evalArrowContext=function(e,t){var r=this,i=n({},this.context);return(e.params||[]).forEach(function(e,o){if("AssignmentExpression"===e.type&&(void 0===t[o]&&(t[o]=r.eval(e.right)),e=e.left),"Identifier"===e.type)i[e.name]=t[o];else if("ArrayExpression"===e.type)e.elements.forEach(function(e,n){var a=t[o][n];if("AssignmentExpression"===e.type&&(void 0===a&&(a=r.eval(e.right)),e=e.left),"Identifier"!==e.type)throw new Error("Unexpected arrow function argument");i[e.name]=a});else if("ObjectExpression"===e.type){var a=[];e.properties.forEach(function(e){var s,u=e;if("AssignmentExpression"===u.type&&(u=u.left),"Property"===u.type)s="Identifier"===u.key.type?u.key.name:r.eval(u.key).toString();else if("Identifier"===u.type)s=u.name;else{if("SpreadElement"!==u.type||"Identifier"!==u.argument.type)throw new Error("Unexpected arrow function argument");s=u.argument.name}var l=t[o][s];"SpreadElement"===u.type?(l=n({},t[o]),a.forEach(function(e){delete l[e]})):void 0===l&&"AssignmentExpression"===e.type&&(l=r.eval(e.right)),i[s]=l,a.push(s)})}else{if("SpreadElement"!==e.type||"Identifier"!==e.argument.type)throw new Error("Unexpected arrow function argument");i[e.argument.name]=t.slice(o)}}),i},u.evalAssignmentExpression=function(e){var n=this;return this.evalSyncAsync(this.getContextAndKey(e.left),function(t){var r=t[0],i=t[1];return n.eval(e.right,function(n){return o.assignOps[e.operator](r,i,n)})})},u.evalUpdateExpression=function(e){return this.evalSyncAsync(this.getContextAndKey(e.argument),function(n){return o.evalUpdateOperation(e,n[0],n[1])})},u.evalAwaitExpression=function(e){return o.evalAsync(e.argument,this.context)},o.evalUpdateOperation=function(e,n,t){return e.prefix?"++"===e.operator?++n[t]:--n[t]:"++"===e.operator?n[t]++:n[t]--},u.getContextAndKey=function(e){var n=this;if("MemberExpression"===e.type)return this.evalSyncAsync(this.evaluateMember(e),function(e){var t=e[0],r=e[2];return[t,(i.getKeyValue(t,r,n.options)||[r,void 0])[0]]});if("Identifier"===e.type){var t=i.getKeyValue(this.context,e.name,this.options);return[this.context,(t||[e.name,void 0])[0]]}if("ConditionalExpression"===e.type)return this.eval(e.test,function(t){return n.getContextAndKey(t?e.consequent:e.alternate)});throw new Error("Invalid Member Key")},u.evalNewExpression=function(e){var n=this;return this.evalSyncAsync(this.evalCall(e.callee),function(t){var r=t[0];return n.evalSyncAsync(n.evalArray(e.arguments),function(n){return o.construct(r,n,e)})})},u.evalObjectExpression=function(e){var n=this,t={},r=e.properties.map(function(e){if("SpreadElement"===e.type)Object.assign(t,o.eval(e.argument,n.context,n.options));else if("Property"===e.type)return n.evalSyncAsync("Identifier"===e.key.type?e.key.name:n.eval(e.key),function(r){return n.eval(e.shorthand?e.key:e.value,function(e){t[r]=e})})});return this.isAsync?Promise.all(r).then(function(){return t}):t},u.evalSpreadElement=function(e){return this.eval(e.argument)},u.evalTaggedTemplateExpression=function(e){var n=[this.evalCall(e.tag),this.evalSyncAsync(this.evalArray(e.quasi.expressions),function(n){return[e.quasi.quasis.map(function(e){return e.value.cooked})].concat(n)})],t=function(e){var n=e[0];return n[0].apply(n[1],e[1])};return this.isAsync?Promise.all(n).then(t):t(n)},u.evalTemplateLiteral=function(e){return this.evalSyncAsync(this.evalArray(e.expressions),function(n){return e.quasis.reduce(function(e,t,r){return e+=t.value.cooked,t.tail||(e+=n[r]),e},"")})},o.construct=function(e,n,t){try{return new(Function.prototype.bind.apply(e,[null].concat(n)))}catch(e){throw new Error(o.nodeFunctionName(t.callee)+" is not a constructor")}},o.validateFnAndCall=function(e,n,t){if("function"!=typeof e){if(!e&&t&&t.optional)return[function(){},n];var r=o.nodeFunctionName(t||n);throw new Error("'"+r+"' is not a function")}return[e,n]},o.nodeFunctionName=function(e){return e&&(e.name||e.property&&e.property.name)},o.getKeyValue=function(e,n,t){return i.getKeyValue(e,n,t)},a=o,(s=[{key:"caseSensitive",get:function(){return!(!this.options||!this.options.caseSensitive)}}])&&function(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(a.prototype,s),Object.defineProperty(a,"prototype",{writable:!1}),o}();o.jsep=e,o.parse=e,o.evaluate=o.eval,o.evaluators={ArrayExpression:o.prototype.evalArrayExpression,LogicalExpression:o.prototype.evalBinaryExpression,BinaryExpression:o.prototype.evalBinaryExpression,CallExpression:o.prototype.evalCallExpression,Compound:o.prototype.evalCompoundExpression,ConditionalExpression:o.prototype.evalConditionalExpression,Identifier:o.prototype.evalIdentifier,Literal:o.evalLiteral,OptionalMemberExpression:o.prototype.evalMemberExpression,MemberExpression:o.prototype.evalMemberExpression,ThisExpression:o.prototype.evalThisExpression,UnaryExpression:o.prototype.evalUnaryExpression,ArrowFunctionExpression:o.prototype.evalArrowFunctionExpression,AssignmentExpression:o.prototype.evalAssignmentExpression,UpdateExpression:o.prototype.evalUpdateExpression,AwaitExpression:o.prototype.evalAwaitExpression,NewExpression:o.prototype.evalNewExpression,ObjectExpression:o.prototype.evalObjectExpression,SpreadElement:o.prototype.evalSpreadElement,TaggedTemplateExpression:o.prototype.evalTaggedTemplateExpression,TemplateLiteral:o.prototype.evalTemplateLiteral},o.conditionalEvaluators={},o.DEFAULT_PRECEDENCE={"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},o.binops={"||":function(e,n){return e||n},"&&":function(e,n){return e&&n},"|":function(e,n){return e|n},"^":function(e,n){return e^n},"&":function(e,n){return e&n},"==":function(e,n){return e==n},"!=":function(e,n){return e!=n},"===":function(e,n){return e===n},"!==":function(e,n){return e!==n},"<":function(e,n){return e<n},">":function(e,n){return e>n},"<=":function(e,n){return e<=n},">=":function(e,n){return e>=n},"<<":function(e,n){return e<<n},">>":function(e,n){return e>>n},">>>":function(e,n){return e>>>n},"+":function(e,n){return e+n},"-":function(e,n){return e-n},"*":function(e,n){return e*n},"/":function(e,n){return e/n},"%":function(e,n){return e%n}},o.unops={"-":function(e){return-e},"+":function(e){return+e},"~":function(e){return~e},"!":function(e){return!e}},o.assignOps={"=":function(e,n,t){return e[n]=t},"*=":function(e,n,t){return e[n]*=t},"**=":function(e,n,t){var r;return e[r=n]=Math.pow(e[r],t)},"/=":function(e,n,t){return e[n]/=t},"%=":function(e,n,t){return e[n]%=t},"+=":function(e,n,t){return e[n]+=t},"-=":function(e,n,t){return e[n]-=t},"<<=":function(e,n,t){return e[n]<<=t},">>=":function(e,n,t){return e[n]>>=t},">>>=":function(e,n,t){return e[n]>>>=t},"&=":function(e,n,t){return e[n]&=t},"^=":function(e,n,t){return e[n]^=t},"|=":function(e,n,t){return e[n]|=t}},o.defaultOptions={caseSensitive:!0};var a=o.DEFAULT_PRECEDENCE,s=o.evaluators,u=o.binops,l=o.unops,c=o.assignOps,p=o.addUnaryOp,f=o.addBinaryOp,v=o.addEvaluator,y=o.registerPlugin,d=o.addOptions,h=o.eval,m=o.evalAsync,E=o.compile,g=o.compileAsync,x=o.evalExpr,A=o.evalExprAsync,b=o.getKeyValue;export{a as DEFAULT_PRECEDENCE,f as addBinaryOp,v as addEvaluator,d as addOptions,p as addUnaryOp,c as assignOps,u as binops,E as compile,g as compileAsync,o as default,m as evalAsync,x as evalExpr,A as evalExprAsync,h as evaluate,s as evaluators,b as getKeyValue,y as registerPlugin,l as unops}; //# sourceMappingURL=jse-eval.module.js.map