@chriscdn/build-url 1.0.10 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/build-url.cjs CHANGED
@@ -1,2 +1,2 @@
1
- exports.buildUrl=function(e,a){var r,n=!1;try{r=new URL(e)}catch(a){if(n=!0,"string"==typeof e){var t="undefined"==typeof window?"http://example.com":window.location.origin;r=new URL(t+"/"+e.replace(/^\/|\/$/g,""))}else r="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}var l="string"==typeof e?a:e;if(null!=l&&l.queryParams)for(var o in l.queryParams)if(Object.prototype.hasOwnProperty.call(l.queryParams,o)){var h=l.queryParams[o];null==h?r.searchParams.delete(o):r.searchParams.set(o,h)}return null!=l&&l.path&&(r.pathname=l.path),null===(null==l?void 0:l.path)&&(r.pathname=""),null!=l&&l.hash&&(r.hash=l.hash),!n||null!=l&&l.returnAbsoluteUrl?r.toString():r.pathname+r.search+r.hash};
1
+ var e=function(e){return null==e};exports.buildUrl=function(a,r){var n,t=!1;try{n=new URL(a)}catch(e){if(t=!0,"string"==typeof a){var l="undefined"==typeof window?"http://example.com":window.location.origin;n=new URL(l+"/"+a.replace(/^\/|\/$/g,""))}else n="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}var o="string"==typeof a?r:a;if(null!=o&&o.queryParams)for(var h in o.queryParams)if(Object.prototype.hasOwnProperty.call(o.queryParams,h)){var i=o.queryParams[h];e(i)?n.searchParams.delete(h):n.searchParams.set(h,i)}return null!=o&&o.path&&(n.pathname=o.path),null===(null==o?void 0:o.path)&&(n.pathname=""),null!=o&&o.hash&&(n.hash=o.hash),!t||null!=o&&o.returnAbsoluteUrl?n.toString():n.pathname+n.search+n.hash};
2
2
  //# sourceMappingURL=build-url.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.cjs","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n// ||(typeof value === \"string\" && value.trim().length === 0);\n\nexport const buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n"],"names":["inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","value","searchParams","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"iBAYwB,SACtBA,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAOE,EAAQN,IAAAA,EAASU,QAAQ,WAAY,IACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,IAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,SAAIY,GAAAA,EAAUC,YACZ,IAAK,IAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,IAAMK,EAAUP,EAASC,YAAYC,GAjCbM,MAmCZD,EACVjB,EAAImB,aAAY,OAAQP,GAExBZ,EAAImB,aAAaC,IAAIR,EAAKK,EAE7B,CAgBL,aAZIP,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAnBX,MAAAA,OAAAA,EAAAA,EAAUW,QACZrB,EAAIsB,SAAW,IAGL,MAARZ,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,GAA4B,MAARS,GAAAA,EAAUc,kBAI3BxB,EAAIyB,WAHFzB,EAAIsB,SAAWtB,EAAI0B,OAAS1B,EAAIuB,IAI3C"}
1
+ {"version":3,"file":"build-url.cjs","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n\nconst buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n\nexport { buildUrl };\n"],"names":["isEmpty","value","inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","searchParams","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"AASA,IAAMA,EAAU,SAACC,GAAU,OAAKA,OAAqC,mBAEpD,SACfC,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAOE,EAAQN,IAAAA,EAASU,QAAQ,WAAY,IACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,IAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,GAAIY,MAAAA,GAAAA,EAAUC,YACZ,IAAK,IAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,IAAMK,EAAUP,EAASC,YAAYC,GAEjChB,EAAQqB,GACVjB,EAAIkB,oBAAoBN,GAExBZ,EAAIkB,aAAaC,IAAIP,EAAKK,EAE7B,CAgBL,OAZIP,MAAAA,GAAAA,EAAUU,OACZpB,EAAIqB,SAAWX,EAASU,MAGH,QAAnBV,MAAAA,OAAAA,EAAAA,EAAUU,QACZpB,EAAIqB,SAAW,IAGL,MAARX,GAAAA,EAAUY,OACZtB,EAAIsB,KAAOZ,EAASY,OAGlBrB,GAAoBS,MAAAA,GAAAA,EAAUa,kBAI3BvB,EAAIwB,WAHFxB,EAAIqB,SAAWrB,EAAIyB,OAASzB,EAAIsB,IAI3C"}
@@ -1,2 +1,2 @@
1
- const e=(e,t)=>{let n,a=!1;try{n=new URL(e)}catch(t){if(a=!0,"string"==typeof e){const t="undefined"==typeof window?"http://example.com":window.location.origin;n=new URL(`${t}/${e.replace(/^\/|\/$/g,"")}`)}else n="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}const r="string"==typeof e?t:e;if(null!=r&&r.queryParams)for(const e in r.queryParams)if(Object.prototype.hasOwnProperty.call(r.queryParams,e)){const t=r.queryParams[e];null==t?n.searchParams.delete(e):n.searchParams.set(e,t)}return null!=r&&r.path&&(n.pathname=r.path),null===(null==r?void 0:r.path)&&(n.pathname=""),null!=r&&r.hash&&(n.hash=r.hash),!a||null!=r&&r.returnAbsoluteUrl?n.toString():n.pathname+n.search+n.hash};export{e as buildUrl};
1
+ const e=e=>null==e,t=(t,n)=>{let a,r=!1;try{a=new URL(t)}catch(e){if(r=!0,"string"==typeof t){const e="undefined"==typeof window?"http://example.com":window.location.origin;a=new URL(`${e}/${t.replace(/^\/|\/$/g,"")}`)}else a="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}const o="string"==typeof t?n:t;if(null!=o&&o.queryParams)for(const t in o.queryParams)if(Object.prototype.hasOwnProperty.call(o.queryParams,t)){const n=o.queryParams[t];e(n)?a.searchParams.delete(t):a.searchParams.set(t,n)}return null!=o&&o.path&&(a.pathname=o.path),null===(null==o?void 0:o.path)&&(a.pathname=""),null!=o&&o.hash&&(a.hash=o.hash),!r||null!=o&&o.returnAbsoluteUrl?a.toString():a.pathname+a.search+a.hash};export{t as buildUrl};
2
2
  //# sourceMappingURL=build-url.modern.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n// ||(typeof value === \"string\" && value.trim().length === 0);\n\nexport const buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n"],"names":["buildUrl","inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","value","searchParams","delete","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"AASA,MAGaA,EAAWA,CACtBC,EACAC,KAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,MAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAI,GAAGE,KAAQN,EAASU,QAAQ,WAAY,MACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,MAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,GAAIY,MAAAA,GAAAA,EAAUC,YACZ,IAAK,MAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,MAAMK,EAAUP,EAASC,YAAYC,GAjCbM,MAmCZD,EACVjB,EAAImB,aAAaC,OAAOR,GAExBZ,EAAImB,aAAaE,IAAIT,EAAKK,EAE7B,CAgBL,OAZY,MAARP,GAAAA,EAAUY,OACZtB,EAAIuB,SAAWb,EAASY,MAGH,QAAX,MAARZ,OAAQ,EAARA,EAAUY,QACZtB,EAAIuB,SAAW,IAGL,MAARb,GAAAA,EAAUc,OACZxB,EAAIwB,KAAOd,EAASc,OAGlBvB,GAA4B,MAARS,GAAAA,EAAUe,kBAI3BzB,EAAI0B,WAHF1B,EAAIuB,SAAWvB,EAAI2B,OAAS3B,EAAIwB,IAI3C"}
1
+ {"version":3,"file":"build-url.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n\nconst buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n\nexport { buildUrl };\n"],"names":["isEmpty","value","buildUrl","inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","searchParams","delete","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"AASA,MAAMA,EAAWC,GAAeA,QAE1BC,EAAWA,CACfC,EACAC,KAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,MAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAI,GAAGE,KAAQN,EAASU,QAAQ,WAAY,MACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,MAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,GAAY,MAARY,GAAAA,EAAUC,YACZ,IAAK,MAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,MAAMK,EAAUP,EAASC,YAAYC,GAEjCjB,EAAQsB,GACVjB,EAAIkB,aAAaC,OAAOP,GAExBZ,EAAIkB,aAAaE,IAAIR,EAAKK,EAE7B,CAgBL,OAZIP,MAAAA,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAX,MAARX,OAAQ,EAARA,EAAUW,QACZrB,EAAIsB,SAAW,IAGbZ,MAAAA,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,GAAoBS,MAAAA,GAAAA,EAAUc,kBAI3BxB,EAAIyB,WAHFzB,EAAIsB,SAAWtB,EAAI0B,OAAS1B,EAAIuB"}
@@ -1,2 +1,2 @@
1
- var a=function(a,e){var r,n=!1;try{r=new URL(a)}catch(e){if(n=!0,"string"==typeof a){var t="undefined"==typeof window?"http://example.com":window.location.origin;r=new URL(t+"/"+a.replace(/^\/|\/$/g,""))}else r="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}var l="string"==typeof a?e:a;if(null!=l&&l.queryParams)for(var o in l.queryParams)if(Object.prototype.hasOwnProperty.call(l.queryParams,o)){var h=l.queryParams[o];null==h?r.searchParams.delete(o):r.searchParams.set(o,h)}return null!=l&&l.path&&(r.pathname=l.path),null===(null==l?void 0:l.path)&&(r.pathname=""),null!=l&&l.hash&&(r.hash=l.hash),!n||null!=l&&l.returnAbsoluteUrl?r.toString():r.pathname+r.search+r.hash};export{a as buildUrl};
1
+ var e=function(e){return null==e},a=function(a,r){var n,t=!1;try{n=new URL(a)}catch(e){if(t=!0,"string"==typeof a){var l="undefined"==typeof window?"http://example.com":window.location.origin;n=new URL(l+"/"+a.replace(/^\/|\/$/g,""))}else n="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}var o="string"==typeof a?r:a;if(null!=o&&o.queryParams)for(var h in o.queryParams)if(Object.prototype.hasOwnProperty.call(o.queryParams,h)){var i=o.queryParams[h];e(i)?n.searchParams.delete(h):n.searchParams.set(h,i)}return null!=o&&o.path&&(n.pathname=o.path),null===(null==o?void 0:o.path)&&(n.pathname=""),null!=o&&o.hash&&(n.hash=o.hash),!t||null!=o&&o.returnAbsoluteUrl?n.toString():n.pathname+n.search+n.hash};export{a as buildUrl};
2
2
  //# sourceMappingURL=build-url.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.module.js","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n// ||(typeof value === \"string\" && value.trim().length === 0);\n\nexport const buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n"],"names":["buildUrl","inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","value","searchParams","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"AASA,IAGaA,EAAW,SACtBC,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAOE,EAAQN,IAAAA,EAASU,QAAQ,WAAY,IACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,IAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,SAAIY,GAAAA,EAAUC,YACZ,IAAK,IAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,IAAMK,EAAUP,EAASC,YAAYC,GAjCbM,MAmCZD,EACVjB,EAAImB,aAAY,OAAQP,GAExBZ,EAAImB,aAAaC,IAAIR,EAAKK,EAE7B,CAgBL,aAZIP,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAnBX,MAAAA,OAAAA,EAAAA,EAAUW,QACZrB,EAAIsB,SAAW,IAGL,MAARZ,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,GAA4B,MAARS,GAAAA,EAAUc,kBAI3BxB,EAAIyB,WAHFzB,EAAIsB,SAAWtB,EAAI0B,OAAS1B,EAAIuB,IAI3C"}
1
+ {"version":3,"file":"build-url.module.js","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n\nconst buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n\nexport { buildUrl };\n"],"names":["isEmpty","value","buildUrl","inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","searchParams","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"AASA,IAAMA,EAAU,SAACC,GAAU,OAAKA,OAAqC,EAE/DC,EAAW,SACfC,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAOE,EAAQN,IAAAA,EAASU,QAAQ,WAAY,IACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,IAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,GAAIY,MAAAA,GAAAA,EAAUC,YACZ,IAAK,IAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,IAAMK,EAAUP,EAASC,YAAYC,GAEjCjB,EAAQsB,GACVjB,EAAIkB,oBAAoBN,GAExBZ,EAAIkB,aAAaC,IAAIP,EAAKK,EAE7B,CAgBL,OAZIP,MAAAA,GAAAA,EAAUU,OACZpB,EAAIqB,SAAWX,EAASU,MAGH,QAAnBV,MAAAA,OAAAA,EAAAA,EAAUU,QACZpB,EAAIqB,SAAW,IAGL,MAARX,GAAAA,EAAUY,OACZtB,EAAIsB,KAAOZ,EAASY,OAGlBrB,GAAoBS,MAAAA,GAAAA,EAAUa,kBAI3BvB,EAAIwB,WAHFxB,EAAIqB,SAAWrB,EAAIyB,OAASzB,EAAIsB,IAI3C"}
@@ -1,2 +1,2 @@
1
- !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).buildUrl={})}(this,function(e){e.buildUrl=function(e,n){var a,t=!1;try{a=new URL(e)}catch(n){if(t=!0,"string"==typeof e){var r="undefined"==typeof window?"http://example.com":window.location.origin;a=new URL(r+"/"+e.replace(/^\/|\/$/g,""))}else a="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}var o="string"==typeof e?n:e;if(null!=o&&o.queryParams)for(var l in o.queryParams)if(Object.prototype.hasOwnProperty.call(o.queryParams,l)){var i=o.queryParams[l];null==i?a.searchParams.delete(l):a.searchParams.set(l,i)}return null!=o&&o.path&&(a.pathname=o.path),null===(null==o?void 0:o.path)&&(a.pathname=""),null!=o&&o.hash&&(a.hash=o.hash),!t||null!=o&&o.returnAbsoluteUrl?a.toString():a.pathname+a.search+a.hash}});
1
+ !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).buildUrl={})}(this,function(e){var n=function(e){return null==e};e.buildUrl=function(e,t){var a,r=!1;try{a=new URL(e)}catch(n){if(r=!0,"string"==typeof e){var o="undefined"==typeof window?"http://example.com":window.location.origin;a=new URL(o+"/"+e.replace(/^\/|\/$/g,""))}else a="undefined"==typeof window?new URL("http://example.com"):new URL(window.location.href)}var l="string"==typeof e?t:e;if(null!=l&&l.queryParams)for(var i in l.queryParams)if(Object.prototype.hasOwnProperty.call(l.queryParams,i)){var u=l.queryParams[i];n(u)?a.searchParams.delete(i):a.searchParams.set(i,u)}return null!=l&&l.path&&(a.pathname=l.path),null===(null==l?void 0:l.path)&&(a.pathname=""),null!=l&&l.hash&&(a.hash=l.hash),!r||null!=l&&l.returnAbsoluteUrl?a.toString():a.pathname+a.search+a.hash}});
2
2
  //# sourceMappingURL=build-url.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n// ||(typeof value === \"string\" && value.trim().length === 0);\n\nexport const buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n"],"names":["inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","value","searchParams","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"6OAYwB,SACtBA,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAOE,EAAQN,IAAAA,EAASU,QAAQ,WAAY,IACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,IAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,SAAIY,GAAAA,EAAUC,YACZ,IAAK,IAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,IAAMK,EAAUP,EAASC,YAAYC,GAjCbM,MAmCZD,EACVjB,EAAImB,aAAY,OAAQP,GAExBZ,EAAImB,aAAaC,IAAIR,EAAKK,EAE7B,CAgBL,aAZIP,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAnBX,MAAAA,OAAAA,EAAAA,EAAUW,QACZrB,EAAIsB,SAAW,IAGL,MAARZ,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,GAA4B,MAARS,GAAAA,EAAUc,kBAI3BxB,EAAIyB,WAHFzB,EAAIsB,SAAWtB,EAAI0B,OAAS1B,EAAIuB,IAI3C"}
1
+ {"version":3,"file":"build-url.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type UrlOptions = {\n queryParams?: {\n [x: string]: any;\n };\n hash?: string;\n path?: string | null;\n returnAbsoluteUrl?: boolean;\n};\n\nconst isEmpty = (value: any) => value === null || value === undefined;\n\nconst buildUrl = (\n inputUrl?: string | UrlOptions,\n options?: UrlOptions,\n) => {\n let url: URL;\n let isValidInputUrl = false;\n\n try {\n url = new URL(inputUrl as string);\n } catch (error) {\n isValidInputUrl = true;\n\n if (typeof inputUrl === \"string\") {\n const host = typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url = typeof window === \"undefined\"\n ? new URL(\"http://example.com\")\n : new URL(window.location.href);\n }\n }\n\n const _options = typeof inputUrl === \"string\" ? options : inputUrl;\n\n if (_options?.queryParams) {\n for (const key in _options.queryParams) {\n if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {\n const element = _options.queryParams[key];\n\n if (isEmpty(element)) {\n url.searchParams.delete(key);\n } else {\n url.searchParams.set(key, element);\n }\n }\n }\n }\n\n if (_options?.path) {\n url.pathname = _options.path;\n }\n\n if (_options?.path === null) {\n url.pathname = \"\";\n }\n\n if (_options?.hash) {\n url.hash = _options.hash;\n }\n\n if (isValidInputUrl && !_options?.returnAbsoluteUrl) {\n return url.pathname + url.search + url.hash;\n }\n\n return url.toString();\n};\n\nexport { buildUrl };\n"],"names":["isEmpty","value","inputUrl","options","url","isValidInputUrl","URL","error","host","window","location","origin","replace","href","_options","queryParams","key","Object","prototype","hasOwnProperty","call","element","searchParams","set","path","pathname","hash","returnAbsoluteUrl","toString","search"],"mappings":"kOASA,IAAMA,EAAU,SAACC,GAAU,OAAKA,OAAqC,aAEpD,SACfC,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EAAyB,oBAAXC,OAChB,qBACAA,OAAOC,SAASC,OAEpBP,EAAM,IAAIE,IAAOE,EAAQN,IAAAA,EAASU,QAAQ,WAAY,IACvD,MACCR,EAAwB,oBAAXK,OACT,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAE/B,CAED,IAAMC,EAA+B,iBAAbZ,EAAwBC,EAAUD,EAE1D,GAAIY,MAAAA,GAAAA,EAAUC,YACZ,IAAK,IAAMC,KAAOF,EAASC,YACzB,GAAIE,OAAOC,UAAUC,eAAeC,KAAKN,EAASC,YAAaC,GAAM,CACnE,IAAMK,EAAUP,EAASC,YAAYC,GAEjChB,EAAQqB,GACVjB,EAAIkB,oBAAoBN,GAExBZ,EAAIkB,aAAaC,IAAIP,EAAKK,EAE7B,CAgBL,OAZIP,MAAAA,GAAAA,EAAUU,OACZpB,EAAIqB,SAAWX,EAASU,MAGH,QAAnBV,MAAAA,OAAAA,EAAAA,EAAUU,QACZpB,EAAIqB,SAAW,IAGL,MAARX,GAAAA,EAAUY,OACZtB,EAAIsB,KAAOZ,EAASY,OAGlBrB,GAAoBS,MAAAA,GAAAA,EAAUa,kBAI3BvB,EAAIwB,WAHFxB,EAAIqB,SAAWrB,EAAIyB,OAASzB,EAAIsB,IAI3C"}
package/lib/index.d.ts CHANGED
@@ -6,4 +6,5 @@ export type UrlOptions = {
6
6
  path?: string | null;
7
7
  returnAbsoluteUrl?: boolean;
8
8
  };
9
- export declare const buildUrl: (inputUrl?: string | UrlOptions, options?: UrlOptions) => string;
9
+ declare const buildUrl: (inputUrl?: string | UrlOptions, options?: UrlOptions) => string;
10
+ export { buildUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chriscdn/build-url",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "A small utility for building URLs.",
5
5
  "repository": "https://github.com/chriscdn/build-url",
6
6
  "author": "Christopher Meyer <chris@schwiiz.org>",
@@ -23,6 +23,9 @@
23
23
  },
24
24
  "devDependencies": {
25
25
  "microbundle": "^0.15.1",
26
- "vitest": "^3.1.2"
27
- }
26
+ "vitest": "^4.0.6"
27
+ },
28
+ "files": [
29
+ "lib"
30
+ ]
28
31
  }
@@ -1,137 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { buildUrl } from "../src/index";
3
-
4
- describe("Build Url test", () => {
5
- test("Add a query param", () => {
6
- const url = buildUrl("http://my-website.com/post", {
7
- queryParams: {
8
- page: 2,
9
- },
10
- });
11
-
12
- expect(url).toEqual("http://my-website.com/post?page=2");
13
- });
14
-
15
- test("Add a zero query param", () => {
16
- const url = buildUrl("http://my-website.com/post", {
17
- queryParams: {
18
- page: 0,
19
- },
20
- });
21
-
22
- expect(url).toEqual("http://my-website.com/post?page=0");
23
- });
24
-
25
- test("Add another query param", () => {
26
- const url = buildUrl("http://my-website.com/post?page=2", {
27
- queryParams: {
28
- sort: "title:asc",
29
- },
30
- });
31
-
32
- expect(url).toEqual("http://my-website.com/post?page=2&sort=title%3Aasc");
33
- });
34
-
35
- test("Url is omitted", () => {
36
- const url = buildUrl({
37
- queryParams: {
38
- page: 2,
39
- sort: "title:desc",
40
- },
41
- });
42
-
43
- expect(url).toEqual("/?page=2&sort=title%3Adesc");
44
- });
45
-
46
- test("Url is relative path", () => {
47
- const url = buildUrl("images", {
48
- queryParams: {
49
- page: 3,
50
- sort: "title:desc",
51
- },
52
- });
53
-
54
- expect(url).toEqual("/images?page=3&sort=title%3Adesc");
55
- });
56
-
57
- test("Changes value of an existing query param", () => {
58
- const url = buildUrl("images?page=2", {
59
- queryParams: {
60
- page: 3,
61
- sort: "title:desc",
62
- },
63
- });
64
-
65
- expect(url).toEqual("/images?page=3&sort=title%3Adesc");
66
- });
67
-
68
- test("Delete existing query param", () => {
69
- const url = buildUrl("images?page=2&sort=title:asc", {
70
- queryParams: {
71
- page: null,
72
- },
73
- });
74
-
75
- expect(url).toEqual("/images?sort=title%3Aasc");
76
- });
77
-
78
- test("Add hash", () => {
79
- const url = buildUrl("/posts", {
80
- hash: "my-hash",
81
- });
82
-
83
- expect(url).toEqual("/posts#my-hash");
84
- });
85
-
86
- test("Replace hash", () => {
87
- const url = buildUrl("/posts?page=2#first-hash", {
88
- hash: "#second-hash",
89
- });
90
-
91
- expect(url).toEqual("/posts?page=2#second-hash");
92
- });
93
-
94
- test("Add path", () => {
95
- const url = buildUrl({ path: "posts" });
96
- const url2 = buildUrl({ path: "/posts" });
97
- const url3 = buildUrl("http://my-website.com", {
98
- path: "posts",
99
- });
100
-
101
- expect(url).toEqual("/posts");
102
- expect(url2).toEqual("/posts");
103
- expect(url3).toEqual("http://my-website.com/posts");
104
- });
105
-
106
- test("Replace path", () => {
107
- const url = buildUrl("posts?page=2", {
108
- path: "stories",
109
- });
110
- const url2 = buildUrl("http://my-website.com/posts?page=2", {
111
- path: "stories",
112
- });
113
-
114
- expect(url).toEqual("/stories?page=2");
115
- expect(url2).toEqual("http://my-website.com/stories?page=2");
116
- });
117
-
118
- test("Delete path", () => {
119
- const url = buildUrl("posts?page=2", {
120
- path: null,
121
- });
122
- const url2 = buildUrl("http://my-website.com/posts?page=2", {
123
- path: null,
124
- });
125
-
126
- expect(url).toEqual("/?page=2");
127
- expect(url2).toEqual("http://my-website.com/?page=2");
128
- });
129
-
130
- test("only query params", () => {
131
- const url = buildUrl({
132
- queryParams: { a: 123 },
133
- });
134
-
135
- expect(url).toBe("/?a=123");
136
- });
137
- });
package/jest.config.js DELETED
@@ -1,5 +0,0 @@
1
- /** @type {import('ts-jest').JestConfigWithTsJest} */
2
- export default {
3
- preset: "ts-jest",
4
- testEnvironment: "node",
5
- };
package/src/index.ts DELETED
@@ -1,71 +0,0 @@
1
- export type UrlOptions = {
2
- queryParams?: {
3
- [x: string]: any;
4
- };
5
- hash?: string;
6
- path?: string | null;
7
- returnAbsoluteUrl?: boolean;
8
- };
9
-
10
- const isEmpty = (value: any) => value === null || value === undefined;
11
- // ||(typeof value === "string" && value.trim().length === 0);
12
-
13
- export const buildUrl = (
14
- inputUrl?: string | UrlOptions,
15
- options?: UrlOptions,
16
- ) => {
17
- let url: URL;
18
- let isValidInputUrl = false;
19
-
20
- try {
21
- url = new URL(inputUrl as string);
22
- } catch (error) {
23
- isValidInputUrl = true;
24
-
25
- if (typeof inputUrl === "string") {
26
- const host = typeof window === "undefined"
27
- ? "http://example.com"
28
- : window.location.origin;
29
-
30
- url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, "")}`);
31
- } else {
32
- url = typeof window === "undefined"
33
- ? new URL("http://example.com")
34
- : new URL(window.location.href);
35
- }
36
- }
37
-
38
- const _options = typeof inputUrl === "string" ? options : inputUrl;
39
-
40
- if (_options?.queryParams) {
41
- for (const key in _options.queryParams) {
42
- if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {
43
- const element = _options.queryParams[key];
44
-
45
- if (isEmpty(element)) {
46
- url.searchParams.delete(key);
47
- } else {
48
- url.searchParams.set(key, element);
49
- }
50
- }
51
- }
52
- }
53
-
54
- if (_options?.path) {
55
- url.pathname = _options.path;
56
- }
57
-
58
- if (_options?.path === null) {
59
- url.pathname = "";
60
- }
61
-
62
- if (_options?.hash) {
63
- url.hash = _options.hash;
64
- }
65
-
66
- if (isValidInputUrl && !_options?.returnAbsoluteUrl) {
67
- return url.pathname + url.search + url.hash;
68
- }
69
-
70
- return url.toString();
71
- };