subslate
Version:
configurable template string replacement
3 lines (2 loc) • 4.43 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});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:p,sanitizer:c}=o,{per:f,opBracket:u,clBracket:d,quote:m,apostrophe:g,backtick:y}=e,h=[m,g,y].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 c=Object.keys(e);function m(o,n=""){for(const i of c){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[...h,...p?[""]:[]])for(const i of c){const l=r(i),p=RegExp(`^[\\s\\S]{${o}}(${f}\\s*${t}(${l})${t}\\s*${m})`);p.lastIndex=o;const c=p.exec(n);if(c&&c[2]){s=e[i],a=c[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=c(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 p={"'":"'",'"':'"',"\\":"\\","`":"`","[":"[","]":"]"},c=t=>a(p,t)?p[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=>c(t))).join(""),[...t].map((t=>m(t,"js"))).join("")],json:t=>[[...t].map((t=>c(t))).join(""),[...t].map((t=>m(t,"js"))).join("")]},y=(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)},h=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=y,b=t=>{const[e,o,n,r,s,a]=[".","[","]",'"',"'","`"].map((e=>t?y(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=h(n),{startStopPairs:i,escapeSep:p,maxNameLength:c}=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,${c}}?)\\s*${e})`)),u=new Map,d=new Map,m=RegExp(f.join("|"),"g"),g=()=>{m.lastIndex=0},y=()=>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})}})}}(y,[],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(p);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("")};exports.createEscapes=x,exports.default=j,exports.getDefaultOptions=h,exports.getSeparators=b,exports.subslate=j;
//# sourceMappingURL=subslate.cjs.js.map