@bearstudio/lunalink
Version:
Lightweight TypeScript library to efficiently maintain and build URLs
2 lines (1 loc) • 743 B
JavaScript
import u from"query-string";function R(e,n,t){let r=l(e,n,t);return t?.baseURL?$(t.baseURL.toString(),r,"/"):r}var g=/:[_A-Za-z]+\w*/g;function l(e,n,t){let r=new Map(Object.entries(n)),i=new Set,P=t?.encodeURIComponent??encodeURIComponent,o=e.replace(g,s=>{let a=s.slice(1);if(!r.has(a))throw new Error(`Missing parameter ${a}`);return i.add(a),P(n[a])}),m=[...r].filter(([s])=>!i.has(s));if(m.length===0)return o;let c=u.stringify(Object.fromEntries(m));return`${f(o)}?${c}`}function f(e){return e.endsWith("?")?e.slice(0,-1):e}function $(e,n,t="/"){return/^https?:\/\//.test(n)&&!/^https?:\/\//.test(e)&&([e,n]=[n,e]),e=e.replace(new RegExp(`${t}+$`),""),n=n.replace(new RegExp(`^${t}+`),""),`${e}${t}${n}`}export{$ as join,R as lunalink};