dropkiq
Version:
Liquid Expressions Simplified
1 lines • 3.08 kB
JavaScript
;var __read=this&&this.__read||function(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var i,n,s=t.call(r),o=[];try{for(;(void 0===e||e-- >0)&&!(i=s.next()).done;)o.push(i.value)}catch(r){n={error:r}}finally{try{i&&!i.done&&(t=s.return)&&t.call(s)}finally{if(n)throw n.error}}return o},__spread=this&&this.__spread||function(){for(var r=[],e=0;e<arguments.length;e++)r=r.concat(__read(arguments[e]));return r};Object.defineProperty(exports,"__esModule",{value:!0});var drop_1=require("../drop/drop"),tslib_1=require("tslib"),assert_1=require("../util/assert"),liquid_options_1=require("../liquid-options"),underscore_1=require("../util/underscore"),Context=function(){function r(r,e,t){void 0===r&&(r={}),void 0===t&&(t=!1),this.scopes=[{}],this.registers={},this.sync=t,this.opts=liquid_options_1.applyDefault(e),this.environments=r}return r.prototype.getRegister=function(r,e){return void 0===e&&(e={}),this.registers[r]=this.registers[r]||e},r.prototype.setRegister=function(r,e){return this.registers[r]=e},r.prototype.getAll=function(){return __spread([this.environments],this.scopes).reduce((function(r,e){return tslib_1.__assign(r,e)}),{})},r.prototype.get=function(r){var e=this.parseProp(r),t=this.findScope(e[0])||this.environments;return this.getFromScope(t,e)},r.prototype.getFromScope=function(r,e){var t=this;return underscore_1.isArray(e)||(e=this.parseProp(e)),e.reduce((function(r,e){if(r=readProperty(r,e),underscore_1.isNil(r)&&t.opts.strictVariables)throw new TypeError("undefined variable: "+e);return r}),r)},r.prototype.push=function(r){return this.scopes.push(r)},r.prototype.pop=function(){return this.scopes.pop()},r.prototype.front=function(){return this.scopes[0]},r.prototype.findScope=function(r){for(var e=this.scopes.length-1;e>=0;e--){var t=this.scopes[e];if(r in t)return t}return null},r.prototype.parseProp=function(r){r=String(r);for(var e,t=[],i="",n=0;n<r.length;)switch(r[n]){case"[":o();var s=r[n+1];/['"]/.test(s)?(e=r.indexOf(s,n+2),assert_1.assert(-1!==e,"unbalanced "+s+": "+r),i=r.slice(n+2,e),o(),n=e+2):(e=matchRightBracket(r,n+1),assert_1.assert(-1!==e,"unbalanced []: "+r),i=r.slice(n+1,e),/^[+-]?\d+$/.test(i)||(i=String(this.get(i))),o(),n=e+1);break;case".":o(),n++;break;default:i+=r[n++]}if(o(),!t.length)throw new TypeError('invalid path:"'+r+'"');return t;function o(){i.length&&t.push(i),i=""}},r}();function readProperty(r,e){return underscore_1.isNil(r)?r:(r=underscore_1.toLiquid(r))instanceof drop_1.Drop?underscore_1.isFunction(r[e])?r[e]():r.hasOwnProperty(e)?r[e]:r.liquidMethodMissing(e):"size"===e?readSize(r):"first"===e?readFirst(r):"last"===e?readLast(r):r[e]}function readFirst(r){return underscore_1.isArray(r)?r[0]:r.first}function readLast(r){return underscore_1.isArray(r)?r[r.length-1]:r.last}function readSize(r){return underscore_1.isArray(r)||underscore_1.isString(r)?r.length:r.size}function matchRightBracket(r,e){for(var t=1,i=e;i<r.length;i++)if("["===r[i]&&t++,"]"===r[i]&&0===--t)return i;return-1}exports.Context=Context,exports.readProperty=readProperty;