UNPKG

leopard-template

Version:
2 lines (1 loc) 1.89 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.Leopard=r()}(this,function(){"use strict";var e=function(e){return String(e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")};function r(e){return e.replace(/"/g,'\\"')}function t(e){this.options=e||{}}var n=t.prototype;function i(e){return e.split("|").reduce((e,r)=>r.trim()+"("+e.trim()+")")}n.parse=function(e,t){t=t||{};var n="var lines = [];\nvar rst;\nwith(data) {\n";function o(e){n+="lines.push("+e+");\n"}function a(e){var r=e.charAt(0);if(["=","-"].indexOf(r)>-1){var t=e.substr(1).trim();if(""===t)return;o("="===r?"escape("+i(t)+")":i(t))}else n+=e+"\n"}var u=/\S/;function s(e){return u.test(e)}for(var l,c=e.split("\n"),f=/<%(.+?)%>/g,p=/\s*$/,d=0,v=c.length;d<v;d++){var h=null,g=null,y=d===v-1;if(s(l=c[d].replace(p,"")))if(null!==(h=f.exec(l))){for(;h;){var b=l.substring(null!==g?g.index+g[0].length:0,h.index);s(b)&&o('"'+r(b)+'"');var m=h[1].trim();m&&a(m),g=h,h=f.exec(l)}var x=l.substr(g.index+g[0].length);s(x)&&o('"'+r(x)+(y?'"':'\\n"'))}else o('"'+r(l)+(y?'"':'\\n"'));else n+="\n"}return n+='rst = lines.join("");\n}\nreturn rst;'},n.compile=function(r,t){t=t||{};var i=this.parse(r,t);return new Function("escape","data",...Object.keys(n),i).call(n,e,t,...Object.values(n))};var o=t;var a=function(e,r){if("function"!=typeof r)throw new TypeError('Leopard: filter requires a function as handler, but got "'+typeof r+'" in filter "'+e+'"');if(e in this.prototype)throw new Error('Leopard: filter "'+e+'" has been declared');return this.prototype[e]=r,this},u={capitalize:function(e){return e.charAt(0).toUpperCase()+e.slice(1)},reverse:function(e){return e.split("").reverse().join("")}};o.filter=a;for(var s,l=Object.keys(u),c=0,f=l.length;c<f;c++)s=l[c],o.filter(s,u[s]);return o});