UNPKG

5.56 kBJavaScriptView Raw
1var t=(0).constructor,r=t.isFinite,e=(t.isInteger,[].constructor.isArray);function n(t){return null!==t&&"object"==typeof t}function o(t,r){return void 0===r&&(r=0),"string"==typeof t&&t.length>=r}function i(t){return r(t)}function s(t){return e(t)}function a(t,r){return n(t)&&r in t}var h=new(function(){function t(t){this.size=t,this.reset()}return t.prototype.reset=function(){this.oldestIndex=0,this.map={},this.cachedKeys=new Array(this.size)},t.prototype.get=function(t){return this.map[t]},t.prototype.set=function(t,r){this.map[t]=r;var e=this.cachedKeys[this.oldestIndex];void 0!==e&&delete this.map[e],this.cachedKeys[this.oldestIndex]=t,this.oldestIndex++,this.oldestIndex%=this.size},t}())(1e3);function f(t){var r=t.charAt(0),e=t.substr(-1);if("'\"`".includes(r)||"'\"`".includes(e)){if(t.length<2||r!==e)throw new SyntaxError('Mismatching string quotation: "'+t+'"');return t.substring(1,t.length-1)}if(t.includes("["))throw new SyntaxError('Missing ] in varName "'+t+'"');return"+"===r?t.substr(1):t}function u(t,r,e){var n=r.trim();if(""===n)return t;if(n.startsWith(".")){if(!e)throw new SyntaxError('Unexpected . at the start of "'+r+'"');if(""===(n=n.substr(1).trim()))return t}else if(e)throw new SyntaxError('Missing . at the start of "'+r+'"');if(n.endsWith("."))throw new SyntaxError('Unexpected "." at the end of "'+r+'"');for(var o=0,i=n.split(".");o<i.length;o++){var s=i[o].trim();if(""===s)throw new SyntaxError('Empty prop name when parsing "'+r+'"');t.push(s)}return t}function c(t){if(!o(t))throw new TypeError("Cannot parse path. Expected string. Got a "+typeof t);for(var r,e,n=0,i=!1,s=new Array(0),a=0;a<t.length&&-1!==(r=t.indexOf("[",a));a=n){if(-1===(n=t.indexOf("]",r)))throw new SyntaxError('Missing ] in varName "'+t+'"');if(0===(e=t.substring(r+1,n).trim()).length)throw new SyntaxError("Unexpected token ]");n++,u(s,t.substring(a,r),i),s.push(f(e)),i=!0}return u(s,t.substring(n),i)}function p(t,r,e){if(void 0===e&&(e={}),!n(e))throw new TypeError("get expects an object option. Got "+typeof e);var o=e.depth,s=void 0===o?10:o;if(!i(s)||s<=0)throw new RangeError("Expected a positive number for depth. Got "+s);var h=Array.isArray(r)?r:c.cached(r),f=function(){return h.join(" > ")};if(h.length>s)throw new ReferenceError("The path cannot be deeper than "+s+' levels. Got "'+f()+'"');for(var u=t,p=0,d=h;p<d.length;p++){var v=d[p];if(!a(u,v)){if(e.propsExist)throw new ReferenceError(v+' is not defined in the scope at path: "'+f()+'"');return}u=u[v]}return u}c.cached=function(t){var r=h.get(t);return void 0===r&&(r=c(t),h.set(t,r)),r};var d=function(){function t(t,r){var e=this;if(void 0===r&&(r={}),this.tokens=t,this.options=r,this.render=function(t){void 0===t&&(t={});var r=e.tokens.varNames.length;e.cacheParsedPaths();for(var n=new Array(r),o=0;o<r;o++)n[o]=p(t,e.toPathCache[o],e.options);return e.stringify(n)},this.renderFn=function(t,r){void 0===r&&(r={});var n=e.resolveVarNames(t,r);return e.stringify(n)},this.renderFnAsync=function(t,r){return void 0===r&&(r={}),Promise.all(e.resolveVarNames(t,r)).then((function(t){return e.stringify(t)}))},!n(t)||!s(t.strings)||!s(t.varNames)||t.strings.length!==t.varNames.length+1)throw new TypeError("Invalid tokens object");if(!n(r))throw new TypeError("Options should be an object. Got a "+typeof r);r.validateVarNames&&this.cacheParsedPaths()}return t.prototype.cacheParsedPaths=function(){var t=this.tokens.varNames;if(void 0===this.toPathCache){this.toPathCache=new Array(t.length);for(var r=0;r<t.length;r++)this.toPathCache[r]=c.cached(t[r])}},t.prototype.resolveVarNames=function(t,r){void 0===r&&(r={});var e=this.tokens.varNames;if("function"!=typeof t)throw new TypeError("Expected a resolver function. Got "+String(t));for(var n=e.length,o=new Array(n),i=0;i<n;i++)o[i]=t.call(null,e[i],r);return o},t.prototype.stringify=function(t){for(var r=this.tokens.strings,e=this.options.explicit,n=t.length,o="",i=0;i<n;i++){o+=r[i];var s=t[i];(e||null!=s)&&(o+=s)}return o+=r[n]},t}();function v(t,r){if(void 0===r&&(r={}),!o(t))throw new TypeError("The template parameter must be a string. Got a "+typeof t);if(!n(r))throw new TypeError("Options should be an object. Got a "+typeof r);var e=r.tags,a=void 0===e?["{{","}}"]:e,h=r.maxVarNameLength,f=void 0===h?1e3:h;if(!s(a)||2!==a.length)throw TypeError("tags should be an array of two elements. Got "+String(a));var u=a[0],c=a[1];if(!o(u,1)||!o(c,1)||u===c)throw new TypeError('The open and close symbols should be two distinct non-empty strings. Got "'+u+'" and "'+c+'"');if(!i(f)||f<=0)throw new Error("Expected a positive number for maxVarNameLength. Got "+f);for(var p,d,v=u.length,l=c.length,g=0,w=[],y=[],m=0;m<t.length&&-1!==(p=t.indexOf(u,m));){var x=p+v;if(-1===(g=t.substr(x,f+l).indexOf(c)))throw new SyntaxError('Missing "'+c+'" in the template for the "'+u+'" at position '+p+" within "+f+" characters");if(g+=x,0===(d=t.substring(x,g).trim()).length)throw new SyntaxError('Unexpected "'+c+'" tag found at position '+p);if(d.includes(u))throw new SyntaxError('Variable names cannot have "'+u+'". But at position '+p+'. Got "'+d+'"');y.push(d),g+=l,w.push(t.substring(m,p)),m=g}return w.push(t.substring(g)),{strings:w,varNames:y}}function l(t,r){void 0===r&&(r={});var e=v(t,r);return new d(e,r)}function g(t,r,e){return l(t,e).render(r)}function w(t,r,e,n){return l(t,n).renderFn(r,e)}function y(t,r,e,n){return l(t,n).renderFnAsync(r,e)}export{d as Renderer,l as compile,p as get,g as render,w as renderFn,y as renderFnAsync,v as tokenize};
2//# sourceMappingURL=micromustache.min.mjs.map