@chriscdn/build-url 1.0.8 → 1.0.9

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.
@@ -1,3 +1,4 @@
1
+ import { describe, test, expect } from "vitest";
1
2
  import buildUrl from "../src/index";
2
3
 
3
4
  describe("Build Url test", () => {
@@ -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
  });
@@ -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 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 +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 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 +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 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 +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 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"}
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.9",
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.0.7"
29
27
  }
30
28
  }
package/src/index.ts CHANGED
@@ -12,7 +12,7 @@ const isEmpty = (value: any) => value === null || value === undefined;
12
12
 
13
13
  export default function buildUrl(
14
14
  inputUrl?: string | UrlOptions,
15
- options?: UrlOptions
15
+ options?: UrlOptions,
16
16
  ) {
17
17
  let url: URL;
18
18
  let isValidInputUrl = false;