UNPKG

subslate

Version:

configurable template string replacement

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