UNPKG

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