UNPKG

dropkiq

Version:

Liquid Expressions Simplified

1 lines 4.75 kB
"use strict";var __generator=this&&this.__generator||function(e,r){var t,n,a,s,o={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return s={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function u(s){return function(u){return function(s){if(t)throw new TypeError("Generator is already executing.");for(;o;)try{if(t=1,n&&(a=2&s[0]?n.return:s[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,s[1])).done)return a;switch(n=0,a&&(s=[2&s[0],a.value]),s[0]){case 0:case 1:a=s;break;case 4:return o.label++,{value:s[1],done:!1};case 5:o.label++,n=s[1],s=[0];continue;case 7:s=o.ops.pop(),o.trys.pop();continue;default:if(!(a=(a=o.trys).length>0&&a[a.length-1])&&(6===s[0]||2===s[0])){o=0;continue}if(3===s[0]&&(!a||s[1]>a[0]&&s[1]<a[3])){o.label=s[1];break}if(6===s[0]&&o.label<a[1]){o.label=a[1],a=s;break}if(a&&o.label<a[2]){o.label=a[2],o.ops.push(s);break}a[2]&&o.ops.pop(),o.trys.pop();continue}s=r.call(e,o)}catch(e){s=[6,e],n=0}finally{t=a=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,u])}}},__read=this&&this.__read||function(e,r){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var n,a,s=t.call(e),o=[];try{for(;(void 0===r||r-- >0)&&!(n=s.next()).done;)o.push(n.value)}catch(e){a={error:e}}finally{try{n&&!n.done&&(t=s.return)&&t.call(s)}finally{if(a)throw a.error}}return o},__spread=this&&this.__spread||function(){for(var e=[],r=0;r<arguments.length;r++)e=e.concat(__read(arguments[r]));return e},__values=this&&this.__values||function(e){var r="function"==typeof Symbol&&Symbol.iterator,t=r&&e[r],n=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")};Object.defineProperty(exports,"__esModule",{value:!0});var assert_1=require("../util/assert"),range_1=require("./range"),value_1=require("./value"),underscore_1=require("../util/underscore"),operator_1=require("./operator"),Expression=function(){function e(e){void 0===e&&(e=""),this.operands=[],this.postfix=__spread(toPostfix(e))}return e.prototype.evaluate=function(e){var r,t,n,a,s,o,u,l,i,c;return __generator(this,(function(p){switch(p.label){case 0:assert_1.assert(e,"unable to evaluate: context not defined"),p.label=1;case 1:p.trys.push([1,9,10,11]),r=__values(this.postfix),t=r.next(),p.label=2;case 2:return t.done?[3,8]:(n=t.value,operator_1.isOperator(n)?(this.evaluateOnce(n),[3,7]):[3,3]);case 3:return range_1.isRange(n)?(s=(a=this.operands).push,[4,range_1.rangeValue(n,e)]):[3,5];case 4:return s.apply(a,[p.sent()]),[3,7];case 5:return u=(o=this.operands).push,[4,new value_1.Value(n).evaluate(e)];case 6:u.apply(o,[p.sent()]),p.label=7;case 7:return t=r.next(),[3,2];case 8:return[3,11];case 9:return l=p.sent(),i={error:l},[3,11];case 10:try{t&&!t.done&&(c=r.return)&&c.call(r)}finally{if(i)throw i.error}return[7];case 11:return[2,this.operands[0]]}}))},e.prototype.value=function(e){var r;return __generator(this,(function(t){switch(t.label){case 0:return r=underscore_1.toValue,[4,this.evaluate(e)];case 1:return[2,r.apply(void 0,[t.sent()])]}}))},e.prototype.evaluateOnce=function(e){var r=this.operands.pop(),t=this.operands.pop(),n=operator_1.operatorImpls[e](t,r);this.operands.push(n)},e}();function tokenize(e){var r,t,n,a,s;return __generator(this,(function(o){switch(o.label){case 0:r=e.length,t="",n={'"':'"',"'":"'","[":"]","(":")"},a=0,o.label=1;case 1:if(!(a<r))return[3,8];switch(s=e[a],s){case"[":case'"':case"'":return[3,2];case" ":case"\t":case"\n":return[3,3]}return[3,6];case 2:for(t+=s;a+1<r&&(t+=e[++a],e[a]!==n[s]););return[3,7];case 3:return t?[4,t]:[3,5];case 4:o.sent(),o.label=5;case 5:return t="",[3,7];case 6:t+=s,o.label=7;case 7:return a++,[3,1];case 8:return t?[4,t]:[3,10];case 9:o.sent(),o.label=10;case 10:return[2]}}))}function toPostfix(e){var r,t,n,a,s,o,u;return __generator(this,(function(l){switch(l.label){case 0:r=[],l.label=1;case 1:l.trys.push([1,10,11,12]),t=__values(tokenize(e)),n=t.next(),l.label=2;case 2:if(n.done)return[3,9];if(a=n.value,!operator_1.isOperator(a))return[3,6];l.label=3;case 3:return r.length&&operator_1.precedence[r[r.length-1]]>operator_1.precedence[a]?[4,r.pop()]:[3,5];case 4:return l.sent(),[3,3];case 5:return r.push(a),[3,8];case 6:return[4,a];case 7:l.sent(),l.label=8;case 8:return n=t.next(),[3,2];case 9:return[3,12];case 10:return s=l.sent(),o={error:s},[3,12];case 11:try{n&&!n.done&&(u=t.return)&&u.call(t)}finally{if(o)throw o.error}return[7];case 12:return r.length?[4,r.pop()]:[3,14];case 13:return l.sent(),[3,12];case 14:return[2]}}))}exports.Expression=Expression;