subslate
Version:
configurable template string replacement
3 lines (2 loc) • 4.35 kB
JavaScript
const t=t=>Array.isArray(t)&&t.length>0,e=e=>t(e)&&e.every((t=>"string"==typeof t)),o=t=>e(t)&&2===t.length,n=(n,r,s="Missing Provided Input")=>{if(void 0===n)return[r];if("string"==typeof n)return[[n,n]];if(o(n))return[n];if(e(n))return n.map((t=>[t,t]));if((e=>t(e)&&e.every((t=>o(t))))(n))return n;if((e=>t(e)&&e.every((t=>o(t)||"string"==typeof t)))(n))return n.map((t=>o(t)?t:[t,t]));throw Error(s)};function r(t){return t.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}const s=t=>!!t&&"object"==typeof t&&null!==t&&Object.keys(t).every((t=>"symbol"!=typeof t));function a(t,e){return Object.prototype.hasOwnProperty.call(t,e)}const i=Symbol("Blank Value");class l{constructor(t){this.context=t}getContextVal(t,e,o){const n=t.content,a=this.context,{allowRootBracket:l,allowUnquotedProps:c,sanitizer:p}=o,{per:f,opBracket:u,clBracket:d,quote:m,apostrophe:g,backtick:h}=e,y=[m,g,h].flat().filter((t=>n?.includes(t))).map((t=>r(t))),x=[f,u].flat().filter((t=>n?.includes(t))).map((t=>r(t)));function b(t,e,o=0){let s=i,a=0;const p=Object.keys(e);function m(o,n=""){for(const i of p){const l=n+i;if(RegExp(`^[\\s\\S]{${o}}(${r(l)})(?:[${x.join("")}]|$)`).test(t))return s=e[i],a=l.length,!0}return!1}t:if(0!==o||!m(o)){if(0!==o)for(const t of f)if(m(o,t))break t;if(0!==o||l){const i=u.map(((t,e)=>[t,d[e]])).filter((e=>t.includes(e[0])&&t.includes(e[1])));let l=null;for(const[e,n]of i)if(t.startsWith(e,o)){l=[e,n];break}if(null===l)break t;const[f,m]=l.map((t=>r(t)));for(const t of[...y,...c?[""]:[]])for(const i of p){const l=r(i),c=RegExp(`^[\\s\\S]{${o}}(${f}\\s*${t}(${l})${t}\\s*${m})`);c.lastIndex=o;const p=c.exec(n);if(p&&p[2]){s=e[i],a=p[1].length;break t}}}}return[s,a]}let j=i,k=0;for(;n?.length>k;){const t=j===i?a:j;if(!s(t))break;const[e,o]=b(n,t,k);if(0===o)break;k+=o,j=e}if(j=p(j===i?{isEmpty:!0,id:n,at:k}:{isEmpty:!1,value:j,id:n}),"string"!=typeof j)throw Error("Sanitizer did not provide string");return j}}const c={"'":"'",'"':'"',"\\":"\\","`":"`","[":"[","]":"]"},p=t=>a(c,t)?c[t]:t,f={".":["."],"\\":["'"],"'":["'"],'"':["""],"[":["["],"]":["]"]},u=t=>`\\u${Number.prototype.toString.call(t,16).toUpperCase()}`,d=t=>`&#x${Number.prototype.toString.call(t,16).toUpperCase()};`,m=(t,e)=>[...t].map((t=>t.split("").map((t=>({js:u,html:d}[e](String.prototype.charCodeAt.call(t,0))))).join(""))).join(""),g={url:t=>[encodeURIComponent(t)],html:t=>[[...t].map((t=>m(t,"html"))).join(""),[...t].map((t=>(t=>a(f,t)?f[t]:t)(t))).join("")],js:t=>[[...t].map((t=>p(t))).join(""),[...t].map((t=>m(t,"js"))).join("")],json:t=>[[...t].map((t=>p(t))).join(""),[...t].map((t=>m(t,"js"))).join("")]},h=(t,e)=>{const o=new Set;for(const n in e)Object.prototype.hasOwnProperty.call(g,n)&&g[n](t).forEach((t=>o.add(t)));return Array.from(o)},y=t=>({startStopPairs:n(t.startStopPairs,["${","}"],"Invalid startStopPairs"),escapeSep:void 0!==t.escapeSep&&t.escapeSep,allowUnquotedProps:!!t.allowUnquotedProps,allowRootBracket:!!t.allowRootBracket,sanitizer:t.sanitizer||(t=>!0===t.isEmpty?"undefined":String(t.value)),maxNameLength:t.maxNameLength||256}),x=h,b=t=>{const[e,o,n,r,s,a]=[".","[","]",'"',"'","`"].map((e=>t?h(e,t):[e]));return{per:e,opBracket:o,clBracket:n,quote:r,apostrophe:s,backtick:a}},j=(t,e,o)=>{const n=s(o)?o:{},a=y(n),{startStopPairs:i,escapeSep:c,maxNameLength:p}=a,f=i.filter((e=>t.includes(e[0])&&t.includes(e[1]))).map((t=>[r(t[0]),r(t[1])])).map((([t,e])=>`(?:${t}\\s*([\\s\\S]{0,${p}}?)\\s*${e})`)),u=new Map,d=new Map,m=RegExp(f.join("|"),"g"),g=()=>{m.lastIndex=0},h=()=>m.exec(t);for(const t of function(t,e,o){return{[Symbol.iterator]:()=>({next(){const n=t(...e);return n?{done:!1,value:n}:(o&&o(),{done:!0,value:void 0})}})}}(h,[],g))if(null!==t){const{index:e}=t,[o,n]=t.filter((t=>"string"==typeof t));if(!o&&!m.lastIndex)break;if(u.has(o)){const t=u.get(o);t&&t.indexes.push(e)}else u.set(o,{content:n,indexes:[e],get value(){return d.get(this.content)},set value(t){d.set(this.content,t)}})}const x=new l(e);let j=0;const k=[...t],v=b(c);for(const[t,e]of u){void 0===e.value&&(e.value=x.getContextVal(e,v,a));for(const o of e.indexes){const n=String(e.value||"");k.splice(o+j,t.length,...n),j+=n.length-t.length}}return k.join("")};export default j;export{x as createEscapes,y as getDefaultOptions,b as getSeparators,j as subslate};
//# sourceMappingURL=subslate.mjs.map