@chriscdn/build-url 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Christopher Meyer
4
+ Copyright (c) googlicius
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # chriscdn/build-url
2
+
3
+ ## Motivation
4
+
5
+ A small utility for generating URLs.
6
+
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
+
9
+ I forked this package for two reasons:
10
+
11
+ - to provide an ES6 build, and
12
+ - to fix [this issue](https://github.com/googlicius/build-url/issues/3).
13
+
14
+ ## Installation
15
+
16
+ Using npm:
17
+
18
+ ```bash
19
+ npm install https://github.com/chriscdn/build-url
20
+ ```
21
+
22
+ Using yarn:
23
+
24
+ ```bash
25
+ yarn add https://github.com/chriscdn/build-url
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Create a url:
31
+
32
+ ```js
33
+ import buildUrl from "@googlicius/build-url";
34
+
35
+ buildUrl("http://my-website.com/post", {
36
+ queryParams: {
37
+ page: 2,
38
+ },
39
+ });
40
+
41
+ // Output: http://my-website.com/post?page=2
42
+ ```
43
+
44
+ Add another query parameter:
45
+
46
+ ```js
47
+ buildUrl("http://my-website.com/post?page=2", {
48
+ queryParams: {
49
+ sort: "title:asc",
50
+ },
51
+ });
52
+
53
+ // Output: http://my-website.com/post?page=2&sort=title%3Aasc
54
+ ```
55
+
56
+ Input url/path is omitted:
57
+
58
+ ```js
59
+ buildUrl({
60
+ queryParams: {
61
+ sort: "title:asc",
62
+ },
63
+ });
64
+
65
+ // Output: /?sort=title%3Aasc
66
+ ```
67
+
68
+ Remove a query parameter:
69
+
70
+ ```js
71
+ buildUrl("images?page=2&sort=title:asc", {
72
+ queryParams: {
73
+ page: null,
74
+ },
75
+ });
76
+
77
+ // Output: /images?sort=title%3Aasc
78
+ ```
79
+
80
+ Return an absolute url:
81
+
82
+ ```js
83
+ // Assume that current url is: http://awesome-website.com
84
+
85
+ buildUrl("/posts", {
86
+ returnAbsoluteUrl: true,
87
+ queryParams: {
88
+ page: 2,
89
+ },
90
+ });
91
+
92
+ // Output: http://awesome-website.com/posts?page=2
93
+ ```
94
+
95
+ ## License
96
+
97
+ MIT
@@ -0,0 +1,128 @@
1
+ import buildUrl from "../src/index";
2
+
3
+ describe("Build Url test", () => {
4
+ test("Add a query param", () => {
5
+ const url = buildUrl("http://my-website.com/post", {
6
+ queryParams: {
7
+ page: 2,
8
+ },
9
+ });
10
+
11
+ expect(url).toEqual("http://my-website.com/post?page=2");
12
+ });
13
+
14
+ test("Add a zero query param", () => {
15
+ const url = buildUrl("http://my-website.com/post", {
16
+ queryParams: {
17
+ page: 0,
18
+ },
19
+ });
20
+
21
+ expect(url).toEqual("http://my-website.com/post?page=0");
22
+ });
23
+
24
+ test("Add another query param", () => {
25
+ const url = buildUrl("http://my-website.com/post?page=2", {
26
+ queryParams: {
27
+ sort: "title:asc",
28
+ },
29
+ });
30
+
31
+ expect(url).toEqual("http://my-website.com/post?page=2&sort=title%3Aasc");
32
+ });
33
+
34
+ test("Url is omitted", () => {
35
+ const url = buildUrl({
36
+ queryParams: {
37
+ page: 2,
38
+ sort: "title:desc",
39
+ },
40
+ });
41
+
42
+ expect(url).toEqual("/?page=2&sort=title%3Adesc");
43
+ });
44
+
45
+ test("Url is relative path", () => {
46
+ const url = buildUrl("images", {
47
+ queryParams: {
48
+ page: 3,
49
+ sort: "title:desc",
50
+ },
51
+ });
52
+
53
+ expect(url).toEqual("/images?page=3&sort=title%3Adesc");
54
+ });
55
+
56
+ test("Changes value of an existing query param", () => {
57
+ const url = buildUrl("images?page=2", {
58
+ queryParams: {
59
+ page: 3,
60
+ sort: "title:desc",
61
+ },
62
+ });
63
+
64
+ expect(url).toEqual("/images?page=3&sort=title%3Adesc");
65
+ });
66
+
67
+ test("Delete existing query param", () => {
68
+ const url = buildUrl("images?page=2&sort=title:asc", {
69
+ queryParams: {
70
+ page: null,
71
+ },
72
+ });
73
+
74
+ expect(url).toEqual("/images?sort=title%3Aasc");
75
+ });
76
+
77
+ test("Add hash", () => {
78
+ const url = buildUrl("/posts", {
79
+ hash: "my-hash",
80
+ });
81
+
82
+ expect(url).toEqual("/posts#my-hash");
83
+ });
84
+
85
+ test("Replace hash", () => {
86
+ const url = buildUrl("/posts?page=2#first-hash", {
87
+ hash: "#second-hash",
88
+ });
89
+
90
+ expect(url).toEqual("/posts?page=2#second-hash");
91
+ });
92
+
93
+ test("Add path", () => {
94
+ const url = buildUrl({ path: "posts" });
95
+ const url2 = buildUrl({ path: "/posts" });
96
+ const url3 = buildUrl("http://my-website.com", {
97
+ path: "posts",
98
+ });
99
+
100
+ expect(url).toEqual("/posts");
101
+ expect(url2).toEqual("/posts");
102
+ expect(url3).toEqual("http://my-website.com/posts");
103
+ });
104
+
105
+ test("Replace path", () => {
106
+ const url = buildUrl("posts?page=2", {
107
+ path: "stories",
108
+ });
109
+ const url2 = buildUrl("http://my-website.com/posts?page=2", {
110
+ path: "stories",
111
+ });
112
+
113
+ expect(url).toEqual("/stories?page=2");
114
+ expect(url2).toEqual("http://my-website.com/stories?page=2");
115
+ });
116
+
117
+ test("Delete path", () => {
118
+ const url = buildUrl("posts?page=2", {
119
+ path: null,
120
+ });
121
+ const url2 = buildUrl("http://my-website.com/posts?page=2", {
122
+ path: null,
123
+ });
124
+
125
+ expect(url).toEqual("/?page=2");
126
+ expect(url2).toEqual("http://my-website.com/?page=2");
127
+ });
128
+ });
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ export default {
3
+ preset: "ts-jest",
4
+ testEnvironment: "node",
5
+ };
package/lib/index.cjs ADDED
@@ -0,0 +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};
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.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"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export interface UrlOptions {
2
+ queryParams?: {
3
+ [x: string]: any;
4
+ };
5
+ hash?: string;
6
+ path?: string | null;
7
+ returnAbsoluteUrl?: boolean;
8
+ }
9
+ export default function buildUrl(inputUrl?: string | UrlOptions, options?: UrlOptions): string;
@@ -0,0 +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};
2
+ //# sourceMappingURL=index.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.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"}
@@ -0,0 +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};
2
+ //# sourceMappingURL=index.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.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"}
@@ -0,0 +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}});
2
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.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 ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@chriscdn/build-url",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "source": "./src/index.ts",
7
+ "main": "./lib/index.cjs",
8
+ "module": "./lib/index.module.js",
9
+ "unpkg": "./lib/index.umd.js",
10
+ "exports": {
11
+ "require": "./lib/index.cjs",
12
+ "default": "./lib/index.modern.js"
13
+ },
14
+ "types": "./lib/index.d.ts",
15
+ "scripts": {
16
+ "build": "rm -rf ./lib/ && microbundle",
17
+ "dev": "microbundle watch",
18
+ "test": "jest"
19
+ },
20
+ "devDependencies": {
21
+ "@types/jest": "^29.5.11",
22
+ "jest": "^29.7.0",
23
+ "microbundle": "^0.15.1",
24
+ "ts-jest": "^29.1.1"
25
+ }
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1,73 @@
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
+ }