tarkine
Version:
Tarkine - A lightweight and high-performance template engine for Node.js, designed for speed and simplicity.
9 lines (8 loc) • 4.78 kB
JavaScript
var Tarkine=(()=>{var u=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports);var f=u((b,g)=>{g.exports={ext:"tark",caches:new Map,alterVoidAttributes(e){let r=/\s+(checked|disabled|readonly|required|autofocus|multiple|selected|hidden|open|ismap|defer|async|novalidate|formnovalidate|allowfullscreen|itemscope|reversed|autoplay|controls|loop|muted|default)\s*=\s*"([^"]*?)"/g;return e.replace(r,(t,n,s)=>` {{:if(${s})}}${n}{{/if}}`)},createErrorSnippet(e,r){let t=e.split(`
`);return t.map((n,s)=>{let o=s+1;return`${r==o?">>":" "} ${o}| ${n}`}).slice(Math.max(r-3,0),Math.min(t.length,r+3)).join(`
`)},throwError(e,r,t,n,s){let o=new Error;throw o.name=e,o.message=`${t}
SOURCE: ${r||"CODE"}
${this.createErrorSnippet(s,n)}
`,o},__show(e){if(e==null||e===!1)return"";let r={"&":"&","<":"<",">":">",'"':""","'":"'"};return String(e).replace(/[&<>"']/g,t=>r[t])},__loop(e){if(!e||typeof e!="object")throw new TypeError(`Expected array or object for loops, but received ${typeof e}`);return Array.isArray(e)?e.map((r,t)=>[t,r]):Object.entries(e)}}});var m=u((v,d)=>{var $=f();function x(e,r){switch(e){case"/if":case"/for":return"}";case":for":{let[t,n]=r.split(" in "),[s,o="__index"]=t.split(",");return`for(const [${o}, ${s}] of __loop(${n})){`}case":if":return`if(${r}){`;case":else":return r.startsWith("if ")?`} else if(${r.slice(3)}){`:"} else {";case"#":return"";case"-":return`__out.push(${r});`;case"~":return`${r};`;default:return`__out.push(__show(${r}));`}}function E(e){let r=/\{\{\s*(#|-|~|\/if|\/for|:if|:else|:for)?\s*([\s\S]*?)\s*\}\}/g,t="const __out = [];",n=0,s=1,o;for(;(o=r.exec(e))!==null;){let a=e.slice(n,o.index);a&&(t+=`__out.push(${JSON.stringify(a)});`);let l=o[1]?o[1].trim():o[1],p=o[2].trim();s+=(a.match(/\n/g)||[]).length,t+=`__err.l = ${s};`;let h=x(l,p);h&&(t+=h),o[0].includes(`
`)&&(s+=(o[0].match(/\n/g)||[]).length),n=r.lastIndex}return t+=`__out.push(${JSON.stringify(e.slice(n))}); return __out.join('');`,`const __err = { l: 0 }; try { ${t} } catch(e) {__err.e = e.message; return __err;}`}function D(e){return E($.alterVoidAttributes(e))}d.exports={compile:D}});var y=u((Y,_)=>{var i={uppercase(e){return typeof e!="string"?e:e.toUpperCase()},lowercase(e){return typeof e!="string"?e:e.toLowerCase()},capitalize(e){return typeof e!="string"?e:e.charAt(0).toUpperCase()+e.slice(1)},isEmail(e){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)},randomNum(e,r){return Math.floor(Math.random()*(r-e+1))+e},randomStr(e){let r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";return Array.from({length:e},()=>r.charAt(Math.floor(Math.random()*r.length))).join("")},randomChar(){let e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";return e.charAt(Math.floor(Math.random()*e.length))},formatDate(e,r="YYYY-MM-DD"){let t=new Date(e),n=t.getFullYear(),s=String(t.getMonth()+1).padStart(2,"0"),o=String(t.getDate()).padStart(2,"0"),a=String(t.getHours()).padStart(2,"0"),l=String(t.getMinutes()).padStart(2,"0"),p=String(t.getSeconds()).padStart(2,"0");return r.replace("YYYY",n).replace("MM",s).replace("DD",o).replace("HH",a).replace("mm",l).replace("ss",p)},formatTime(e){let r=new Date(e),t=r.getHours().toString().padStart(2,"0"),n=r.getMinutes().toString().padStart(2,"0"),s=r.getSeconds().toString().padStart(2,"0");return`${t}:${n}:${s}`},formatBytes(e,r=2){if(e===0)return"0 Bytes";let t=1024,n=r<0?0:r,s=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],o=Math.floor(Math.log(e)/Math.log(t));return parseFloat((e/Math.pow(t,o)).toFixed(n))+" "+s[o]},truncate(e,r,t="..."){return e.length>r?e.substring(0,r-t.length)+t:e},slugify(e){return e.toLowerCase().trim().replace(/[^\w\s-]/g,"").replace(/[\s_-]+/g,"-").replace(/^-+|-+$/g,"")},currency(e,r="USD",t="en-US"){return new Intl.NumberFormat(t,{style:"currency",currency:r}).format(e)},pluralize(e,r,t){return e===1?r:t||r+"s"},timeSince(e){let r=Math.floor((new Date-new Date(e))/1e3),t=r/31536e3;return t>1?Math.floor(t)+" years ago":(t=r/2592e3,t>1?Math.floor(t)+" months ago":(t=r/86400,t>1?Math.floor(t)+" days ago":(t=r/3600,t>1?Math.floor(t)+" hours ago":(t=r/60,t>1?Math.floor(t)+" minutes ago":Math.floor(r)+" seconds ago"))))}};_.exports={set(e,r){i[e]=r},get(e){return i[e]},getAll(){return i},delete(e){delete i[e]},clear(){i={}}}});var A=u((O,w)=>{var S=m(),c=f(),M=y();function C(e,r={},t){let n=c.caches.get(e);if(!n)try{n=new Function(`$,__show,__loop,include,{${Object.keys(r)}}`,S.compile(e)),c.caches.set(e,n)}catch(a){throw new Error(`CompileError: ${a.message}
SOURCE: CODE`)}let s=typeof t=="function"?t:()=>"",o=n(M.getAll(),c.__show,c.__loop,s,r);return typeof o!="string"&&c.throwError("RenderError",null,o.e,o.l,e),o}w.exports={render:C,store:M,ext:c.ext,compile:S.compile,resetCache:c.caches.clear}});return A();})();