@chriscdn/build-url 1.0.8 → 1.0.10

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/README.md CHANGED
@@ -6,10 +6,11 @@ A small utility for building URLs.
6
6
 
7
7
  This package is a fork of [@googlicius/build-url](https://github.com/googlicius/build-url). Full credit to googlicius for the concept, source code, and documentation. 👍
8
8
 
9
- I forked this package for two reasons:
9
+ I forked this package for three reasons:
10
10
 
11
- - to provide an ES6 build, and
12
- - to fix [this issue](https://github.com/googlicius/build-url/issues/3).
11
+ - to provide an ES6 build,
12
+ - to fix [this issue](https://github.com/googlicius/build-url/issues/3), and
13
+ - to provide a named export.
13
14
 
14
15
  ## Installation
15
16
 
@@ -30,7 +31,7 @@ yarn add @chriscdn/build-url
30
31
  Create a url:
31
32
 
32
33
  ```js
33
- import buildUrl from "@chriscdn/build-url";
34
+ import { buildUrl } from "@chriscdn/build-url";
34
35
 
35
36
  buildUrl("http://my-website.com/post", {
36
37
  queryParams: {
@@ -1,4 +1,5 @@
1
- import buildUrl from "../src/index";
1
+ import { describe, expect, test } from "vitest";
2
+ import { buildUrl } from "../src/index";
2
3
 
3
4
  describe("Build Url test", () => {
4
5
  test("Add a query param", () => {
@@ -125,4 +126,12 @@ describe("Build Url test", () => {
125
126
  expect(url).toEqual("/?page=2");
126
127
  expect(url2).toEqual("http://my-website.com/?page=2");
127
128
  });
129
+
130
+ test("only query params", () => {
131
+ const url = buildUrl({
132
+ queryParams: { a: 123 },
133
+ });
134
+
135
+ expect(url).toBe("/?a=123");
136
+ });
128
137
  });
package/lib/build-url.cjs CHANGED
@@ -1,2 +1,2 @@
1
- module.exports=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
+ 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};
2
2
  //# sourceMappingURL=build-url.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.cjs","sources":["../src/index.ts"],"sourcesContent":["export interface 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 default function 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 =\n typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url =\n 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":"eAYwB,SACtBA,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EACc,oBAAXC,OACH,qBACAA,OAAOC,SAASC,OAEtBP,EAAM,IAAIE,IAAOE,MAAQN,EAASU,QAAQ,WAAY,IACvD,MACCR,EACoB,oBAAXK,OACH,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAEjC,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,GAnCbM,MAqCZD,EACVjB,EAAImB,aAAmB,OAACP,GAExBZ,EAAImB,aAAaC,IAAIR,EAAKK,EAE7B,CAgBL,aAZIP,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAnBX,MAAAA,OAAAA,EAAAA,EAAUW,QACZrB,EAAIsB,SAAW,UAGbZ,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,SAAoBS,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// ||(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,2 +1,2 @@
1
- function e(e,t){let a,n=!1;try{a=new URL(e)}catch(t){if(n=!0,"string"==typeof e){const t="undefined"==typeof window?"http://example.com":window.location.origin;a=new URL(`${t}/${e.replace(/^\/|\/$/g,"")}`)}else a="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?a.searchParams.delete(e):a.searchParams.set(e,t)}return null!=r&&r.path&&(a.pathname=r.path),null===(null==r?void 0:r.path)&&(a.pathname=""),null!=r&&r.hash&&(a.hash=r.hash),!n||null!=r&&r.returnAbsoluteUrl?a.toString():a.pathname+a.search+a.hash}export{e as default};
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};
2
2
  //# sourceMappingURL=build-url.modern.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.modern.js","sources":["../src/index.ts"],"sourcesContent":["export interface 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 default function 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 =\n typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url =\n 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":"SAYwBA,EACtBC,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,MAAMM,EACc,oBAAXC,OACH,qBACAA,OAAOC,SAASC,OAEtBP,EAAM,IAAIE,IAAI,GAAGE,KAAQN,EAASU,QAAQ,WAAY,MACvD,MACCR,EACoB,oBAAXK,OACH,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAEjC,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,GAnCbM,MAqCZD,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// ||(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,2 +1,2 @@
1
- function a(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 default};
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};
2
2
  //# sourceMappingURL=build-url.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.module.js","sources":["../src/index.ts"],"sourcesContent":["export interface 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 default function 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 =\n typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url =\n 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":"AAYwB,SAAAA,EACtBC,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EACc,oBAAXC,OACH,qBACAA,OAAOC,SAASC,OAEtBP,EAAM,IAAIE,IAAOE,MAAQN,EAASU,QAAQ,WAAY,IACvD,MACCR,EACoB,oBAAXK,OACH,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAEjC,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,GAnCbM,MAqCZD,EACVjB,EAAImB,aAAmB,OAACP,GAExBZ,EAAImB,aAAaC,IAAIR,EAAKK,EAE7B,CAgBL,aAZIP,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAnBX,MAAAA,OAAAA,EAAAA,EAAUW,QACZrB,EAAIsB,SAAW,UAGbZ,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,SAAoBS,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// ||(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,2 +1,2 @@
1
- !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e||self).buildUrl=n()}(this,function(){return 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){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}});
2
2
  //# sourceMappingURL=build-url.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-url.umd.js","sources":["../src/index.ts"],"sourcesContent":["export interface 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 default function 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 =\n typeof window === \"undefined\"\n ? \"http://example.com\"\n : window.location.origin;\n\n url = new URL(`${host}/${inputUrl.replace(/^\\/|\\/$/g, \"\")}`);\n } else {\n url =\n 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":"kOAYwB,SACtBA,EACAC,GAEA,IAAIC,EACAC,GAAkB,EAEtB,IACED,EAAM,IAAIE,IAAIJ,EACf,CAAC,MAAOK,GAGP,GAFAF,GAAkB,EAEM,iBAAbH,EAAuB,CAChC,IAAMM,EACc,oBAAXC,OACH,qBACAA,OAAOC,SAASC,OAEtBP,EAAM,IAAIE,IAAOE,MAAQN,EAASU,QAAQ,WAAY,IACvD,MACCR,EACoB,oBAAXK,OACH,IAAIH,IAAI,sBACR,IAAIA,IAAIG,OAAOC,SAASG,KAEjC,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,GAnCbM,MAqCZD,EACVjB,EAAImB,aAAmB,OAACP,GAExBZ,EAAImB,aAAaC,IAAIR,EAAKK,EAE7B,CAgBL,aAZIP,GAAAA,EAAUW,OACZrB,EAAIsB,SAAWZ,EAASW,MAGH,QAAnBX,MAAAA,OAAAA,EAAAA,EAAUW,QACZrB,EAAIsB,SAAW,UAGbZ,GAAAA,EAAUa,OACZvB,EAAIuB,KAAOb,EAASa,OAGlBtB,SAAoBS,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// ||(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"}
package/lib/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- export interface UrlOptions {
1
+ export type UrlOptions = {
2
2
  queryParams?: {
3
3
  [x: string]: any;
4
4
  };
5
5
  hash?: string;
6
6
  path?: string | null;
7
7
  returnAbsoluteUrl?: boolean;
8
- }
9
- export default function buildUrl(inputUrl?: string | UrlOptions, options?: UrlOptions): string;
8
+ };
9
+ export declare const buildUrl: (inputUrl?: string | UrlOptions, options?: UrlOptions) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chriscdn/build-url",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
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>",
@@ -19,12 +19,10 @@
19
19
  "scripts": {
20
20
  "build": "rm -rf ./lib/ && microbundle",
21
21
  "dev": "microbundle watch",
22
- "test": "jest"
22
+ "test": "vitest"
23
23
  },
24
24
  "devDependencies": {
25
- "@types/jest": "^29.5.11",
26
- "jest": "^29.7.0",
27
25
  "microbundle": "^0.15.1",
28
- "ts-jest": "^29.1.1"
26
+ "vitest": "^3.1.2"
29
27
  }
30
28
  }
package/src/index.ts CHANGED
@@ -1,19 +1,19 @@
1
- export interface UrlOptions {
1
+ export type UrlOptions = {
2
2
  queryParams?: {
3
3
  [x: string]: any;
4
4
  };
5
5
  hash?: string;
6
6
  path?: string | null;
7
7
  returnAbsoluteUrl?: boolean;
8
- }
8
+ };
9
9
 
10
10
  const isEmpty = (value: any) => value === null || value === undefined;
11
11
  // ||(typeof value === "string" && value.trim().length === 0);
12
12
 
13
- export default function buildUrl(
13
+ export const buildUrl = (
14
14
  inputUrl?: string | UrlOptions,
15
- options?: UrlOptions
16
- ) {
15
+ options?: UrlOptions,
16
+ ) => {
17
17
  let url: URL;
18
18
  let isValidInputUrl = false;
19
19
 
@@ -23,17 +23,15 @@ export default function buildUrl(
23
23
  isValidInputUrl = true;
24
24
 
25
25
  if (typeof inputUrl === "string") {
26
- const host =
27
- typeof window === "undefined"
28
- ? "http://example.com"
29
- : window.location.origin;
26
+ const host = typeof window === "undefined"
27
+ ? "http://example.com"
28
+ : window.location.origin;
30
29
 
31
30
  url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, "")}`);
32
31
  } else {
33
- url =
34
- typeof window === "undefined"
35
- ? new URL("http://example.com")
36
- : new URL(window.location.href);
32
+ url = typeof window === "undefined"
33
+ ? new URL("http://example.com")
34
+ : new URL(window.location.href);
37
35
  }
38
36
  }
39
37
 
@@ -70,4 +68,4 @@ export default function buildUrl(
70
68
  }
71
69
 
72
70
  return url.toString();
73
- }
71
+ };