@chriscdn/build-url 1.0.9 → 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/README.md +5 -4
- package/lib/build-url.cjs +1 -1
- package/lib/build-url.cjs.map +1 -1
- package/lib/build-url.modern.js +1 -1
- package/lib/build-url.modern.js.map +1 -1
- package/lib/build-url.module.js +1 -1
- package/lib/build-url.module.js.map +1 -1
- package/lib/build-url.umd.js +1 -1
- package/lib/build-url.umd.js.map +1 -1
- package/lib/index.d.ts +4 -3
- package/package.json +6 -3
- package/__tests__/build-url.test.ts +0 -137
- package/jest.config.js +0 -5
- package/src/index.ts +0 -73
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
|
|
9
|
+
I forked this package for three reasons:
|
|
10
10
|
|
|
11
|
-
- to provide an ES6 build,
|
|
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: {
|
package/lib/build-url.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
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
|
package/lib/build-url.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-url.cjs","sources":["../src/index.ts"],"sourcesContent":["export
|
|
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"}
|
package/lib/build-url.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
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
|
|
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"}
|
package/lib/build-url.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function a(a,
|
|
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
|
|
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"}
|
package/lib/build-url.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?
|
|
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
|
package/lib/build-url.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-url.umd.js","sources":["../src/index.ts"],"sourcesContent":["export
|
|
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
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export
|
|
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
|
-
|
|
8
|
+
};
|
|
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.
|
|
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": "^
|
|
27
|
-
}
|
|
26
|
+
"vitest": "^4.0.6"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"lib"
|
|
30
|
+
]
|
|
28
31
|
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } 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
package/src/index.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
export interface 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 default function 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 =
|
|
27
|
-
typeof window === "undefined"
|
|
28
|
-
? "http://example.com"
|
|
29
|
-
: window.location.origin;
|
|
30
|
-
|
|
31
|
-
url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, "")}`);
|
|
32
|
-
} else {
|
|
33
|
-
url =
|
|
34
|
-
typeof window === "undefined"
|
|
35
|
-
? new URL("http://example.com")
|
|
36
|
-
: new URL(window.location.href);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const _options = typeof inputUrl === "string" ? options : inputUrl;
|
|
41
|
-
|
|
42
|
-
if (_options?.queryParams) {
|
|
43
|
-
for (const key in _options.queryParams) {
|
|
44
|
-
if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {
|
|
45
|
-
const element = _options.queryParams[key];
|
|
46
|
-
|
|
47
|
-
if (isEmpty(element)) {
|
|
48
|
-
url.searchParams.delete(key);
|
|
49
|
-
} else {
|
|
50
|
-
url.searchParams.set(key, element);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (_options?.path) {
|
|
57
|
-
url.pathname = _options.path;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (_options?.path === null) {
|
|
61
|
-
url.pathname = "";
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (_options?.hash) {
|
|
65
|
-
url.hash = _options.hash;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (isValidInputUrl && !_options?.returnAbsoluteUrl) {
|
|
69
|
-
return url.pathname + url.search + url.hash;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return url.toString();
|
|
73
|
-
}
|