UNPKG

subslate

Version:

configurable template string replacement

3 lines (2 loc) 4.63 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).subslate=t.subslate||{})}(this,(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:f,opBracket:u,clBracket:d,quote:m,apostrophe:g,backtick:y}=e,h=[m,g,y].flat().filter((t=>n?.includes(t))).map((t=>s(t))),b=[f,u].flat().filter((t=>n?.includes(t))).map((t=>s(t)));function x(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 f)if(m(o,t))break t;if(0!==o||i){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=>s(t)));for(const t of[...h,...c?[""]:[]])for(const i of p){const l=s(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]){r=e[i],a=p[1].length;break t}}}}return[r,a]}let j=l,k=0;for(;n?.length>k;){const t=j===l?r:j;if(!a(t))break;const[e,o]=x(n,t,k);if(0===o)break;k+=o,j=e}if(j=p(j===l?{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={"'":"'",'"':'"',"\\":"\\","`":"`","[":"[","]":"]"},f=t=>i(p,t)?p[t]:t,u={".":["."],"\\":["'"],"'":["'"],'"':["""],"[":["["],"]":["]"]},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(""),y={url:t=>[encodeURIComponent(t)],html:t=>[[...t].map((t=>g(t,"html"))).join(""),[...t].map((t=>(t=>i(u,t)?u[t]:t)(t))).join("")],js:t=>[[...t].map((t=>f(t))).join(""),[...t].map((t=>g(t,"js"))).join("")],json:t=>[[...t].map((t=>f(t))).join(""),[...t].map((t=>g(t,"js"))).join("")]},h=(t,e)=>{const o=new Set;for(const n in e)Object.prototype.hasOwnProperty.call(y,n)&&y[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}),x=h,j=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}},k=(t,e,o)=>{const n=a(o)?o:{},r=b(n),{startStopPairs:i,escapeSep:l,maxNameLength:p}=r,f=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})`)),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 h=new c(e);let x=0;const k=[...t],v=j(l);for(const[t,e]of u){void 0===e.value&&(e.value=h.getContextVal(e,v,r));for(const o of e.indexes){const n=String(e.value||"");k.splice(o+x,t.length,...n),x+=n.length-t.length}}return k.join("")};t.createEscapes=x,t.default=k,t.getDefaultOptions=b,t.getSeparators=j,t.subslate=k,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=subslate.js.map