UNPKG

@bearstudio/lunalink

Version:

Lightweight TypeScript library to efficiently maintain and build URLs

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