@chriscdn/build-url 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +72 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +11 -0
- package/lib/index.d.ts +11 -9
- package/{src/index.ts → lib/index.js} +11 -35
- package/lib/index.js.map +1 -0
- package/package.json +24 -14
- package/__tests__/build-url.test.ts +0 -137
- package/jest.config.js +0 -5
- package/lib/build-url.cjs +0 -2
- package/lib/build-url.cjs.map +0 -1
- package/lib/build-url.modern.js +0 -2
- package/lib/build-url.modern.js.map +0 -1
- package/lib/build-url.module.js +0 -2
- package/lib/build-url.module.js.map +0 -1
- package/lib/build-url.umd.js +0 -2
- package/lib/build-url.umd.js.map +0 -1
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
buildUrl: () => buildUrl
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var isEmpty = (value) => value === null || value === void 0;
|
|
27
|
+
var buildUrl = (inputUrl, options) => {
|
|
28
|
+
let url;
|
|
29
|
+
let isValidInputUrl = false;
|
|
30
|
+
try {
|
|
31
|
+
url = new URL(inputUrl);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
isValidInputUrl = true;
|
|
34
|
+
if (typeof inputUrl === "string") {
|
|
35
|
+
const host = typeof window === "undefined" ? "http://example.com" : window.location.origin;
|
|
36
|
+
url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, "")}`);
|
|
37
|
+
} else {
|
|
38
|
+
url = typeof window === "undefined" ? new URL("http://example.com") : new URL(window.location.href);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const _options = typeof inputUrl === "string" ? options : inputUrl;
|
|
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
|
+
if (isEmpty(element)) {
|
|
47
|
+
url.searchParams.delete(key);
|
|
48
|
+
} else {
|
|
49
|
+
url.searchParams.set(key, element);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (_options?.path) {
|
|
55
|
+
url.pathname = _options.path;
|
|
56
|
+
}
|
|
57
|
+
if (_options?.path === null) {
|
|
58
|
+
url.pathname = "";
|
|
59
|
+
}
|
|
60
|
+
if (_options?.hash) {
|
|
61
|
+
url.hash = _options.hash;
|
|
62
|
+
}
|
|
63
|
+
if (isValidInputUrl && !_options?.returnAbsoluteUrl) {
|
|
64
|
+
return url.pathname + url.search + url.hash;
|
|
65
|
+
}
|
|
66
|
+
return url.toString();
|
|
67
|
+
};
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
buildUrl
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,IAAM,UAAU,CAAC,UAAe,UAAU,QAAQ,UAAU;AAE5D,IAAM,WAAW,CACf,UACA,YACG;AACH,MAAI;AACJ,MAAI,kBAAkB;AAEtB,MAAI;AACF,UAAM,IAAI,IAAI,QAAkB;AAAA,EAClC,SAAS,OAAO;AACd,sBAAkB;AAElB,QAAI,OAAO,aAAa,UAAU;AAChC,YAAM,OAAO,OAAO,WAAW,cAC3B,uBACA,OAAO,SAAS;AAEpB,YAAM,IAAI,IAAI,GAAG,IAAI,IAAI,SAAS,QAAQ,YAAY,EAAE,CAAC,EAAE;AAAA,IAC7D,OAAO;AACL,YAAM,OAAO,WAAW,cACpB,IAAI,IAAI,oBAAoB,IAC5B,IAAI,IAAI,OAAO,SAAS,IAAI;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,WAAW,OAAO,aAAa,WAAW,UAAU;AAE1D,MAAI,UAAU,aAAa;AACzB,eAAW,OAAO,SAAS,aAAa;AACtC,UAAI,OAAO,UAAU,eAAe,KAAK,SAAS,aAAa,GAAG,GAAG;AACnE,cAAM,UAAU,SAAS,YAAY,GAAG;AAExC,YAAI,QAAQ,OAAO,GAAG;AACpB,cAAI,aAAa,OAAO,GAAG;AAAA,QAC7B,OAAO;AACL,cAAI,aAAa,IAAI,KAAK,OAAO;AAAA,QACnC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,MAAI,UAAU,MAAM;AAClB,QAAI,WAAW,SAAS;AAAA,EAC1B;AAEA,MAAI,UAAU,SAAS,MAAM;AAC3B,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,UAAU,MAAM;AAClB,QAAI,OAAO,SAAS;AAAA,EACtB;AAEA,MAAI,mBAAmB,CAAC,UAAU,mBAAmB;AACnD,WAAO,IAAI,WAAW,IAAI,SAAS,IAAI;AAAA,EACzC;AAEA,SAAO,IAAI,SAAS;AACtB;","names":[]}
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type UrlOptions = {
|
|
2
|
+
queryParams?: {
|
|
3
|
+
[x: string]: any;
|
|
4
|
+
};
|
|
5
|
+
hash?: string;
|
|
6
|
+
path?: string | null;
|
|
7
|
+
returnAbsoluteUrl?: boolean;
|
|
8
|
+
};
|
|
9
|
+
declare const buildUrl: (inputUrl?: string | UrlOptions, options?: UrlOptions) => string;
|
|
10
|
+
|
|
11
|
+
export { type UrlOptions, buildUrl };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
queryParams?: {
|
|
3
|
-
[x: string]: any;
|
|
4
|
-
};
|
|
5
|
-
hash?: string;
|
|
6
|
-
path?: string | null;
|
|
7
|
-
returnAbsoluteUrl?: boolean;
|
|
8
|
-
};
|
|
9
|
-
|
|
1
|
+
type UrlOptions = {
|
|
2
|
+
queryParams?: {
|
|
3
|
+
[x: string]: any;
|
|
4
|
+
};
|
|
5
|
+
hash?: string;
|
|
6
|
+
path?: string | null;
|
|
7
|
+
returnAbsoluteUrl?: boolean;
|
|
8
|
+
};
|
|
9
|
+
declare const buildUrl: (inputUrl?: string | UrlOptions, options?: UrlOptions) => string;
|
|
10
|
+
|
|
11
|
+
export { type UrlOptions, buildUrl };
|
|
@@ -1,47 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
hash?: string;
|
|
6
|
-
path?: string | null;
|
|
7
|
-
returnAbsoluteUrl?: boolean;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const isEmpty = (value: any) => value === null || value === undefined;
|
|
11
|
-
// ||(typeof value === "string" && value.trim().length === 0);
|
|
12
|
-
|
|
13
|
-
export const buildUrl = (
|
|
14
|
-
inputUrl?: string | UrlOptions,
|
|
15
|
-
options?: UrlOptions,
|
|
16
|
-
) => {
|
|
17
|
-
let url: URL;
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var isEmpty = (value) => value === null || value === void 0;
|
|
3
|
+
var buildUrl = (inputUrl, options) => {
|
|
4
|
+
let url;
|
|
18
5
|
let isValidInputUrl = false;
|
|
19
|
-
|
|
20
6
|
try {
|
|
21
|
-
url = new URL(inputUrl
|
|
7
|
+
url = new URL(inputUrl);
|
|
22
8
|
} catch (error) {
|
|
23
9
|
isValidInputUrl = true;
|
|
24
|
-
|
|
25
10
|
if (typeof inputUrl === "string") {
|
|
26
|
-
const host = typeof window === "undefined"
|
|
27
|
-
? "http://example.com"
|
|
28
|
-
: window.location.origin;
|
|
29
|
-
|
|
11
|
+
const host = typeof window === "undefined" ? "http://example.com" : window.location.origin;
|
|
30
12
|
url = new URL(`${host}/${inputUrl.replace(/^\/|\/$/g, "")}`);
|
|
31
13
|
} else {
|
|
32
|
-
url = typeof window === "undefined"
|
|
33
|
-
? new URL("http://example.com")
|
|
34
|
-
: new URL(window.location.href);
|
|
14
|
+
url = typeof window === "undefined" ? new URL("http://example.com") : new URL(window.location.href);
|
|
35
15
|
}
|
|
36
16
|
}
|
|
37
|
-
|
|
38
17
|
const _options = typeof inputUrl === "string" ? options : inputUrl;
|
|
39
|
-
|
|
40
18
|
if (_options?.queryParams) {
|
|
41
19
|
for (const key in _options.queryParams) {
|
|
42
20
|
if (Object.prototype.hasOwnProperty.call(_options.queryParams, key)) {
|
|
43
21
|
const element = _options.queryParams[key];
|
|
44
|
-
|
|
45
22
|
if (isEmpty(element)) {
|
|
46
23
|
url.searchParams.delete(key);
|
|
47
24
|
} else {
|
|
@@ -50,22 +27,21 @@ export const buildUrl = (
|
|
|
50
27
|
}
|
|
51
28
|
}
|
|
52
29
|
}
|
|
53
|
-
|
|
54
30
|
if (_options?.path) {
|
|
55
31
|
url.pathname = _options.path;
|
|
56
32
|
}
|
|
57
|
-
|
|
58
33
|
if (_options?.path === null) {
|
|
59
34
|
url.pathname = "";
|
|
60
35
|
}
|
|
61
|
-
|
|
62
36
|
if (_options?.hash) {
|
|
63
37
|
url.hash = _options.hash;
|
|
64
38
|
}
|
|
65
|
-
|
|
66
39
|
if (isValidInputUrl && !_options?.returnAbsoluteUrl) {
|
|
67
40
|
return url.pathname + url.search + url.hash;
|
|
68
41
|
}
|
|
69
|
-
|
|
70
42
|
return url.toString();
|
|
71
43
|
};
|
|
44
|
+
export {
|
|
45
|
+
buildUrl
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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"],"mappings":";AASA,IAAM,UAAU,CAAC,UAAe,UAAU,QAAQ,UAAU;AAE5D,IAAM,WAAW,CACf,UACA,YACG;AACH,MAAI;AACJ,MAAI,kBAAkB;AAEtB,MAAI;AACF,UAAM,IAAI,IAAI,QAAkB;AAAA,EAClC,SAAS,OAAO;AACd,sBAAkB;AAElB,QAAI,OAAO,aAAa,UAAU;AAChC,YAAM,OAAO,OAAO,WAAW,cAC3B,uBACA,OAAO,SAAS;AAEpB,YAAM,IAAI,IAAI,GAAG,IAAI,IAAI,SAAS,QAAQ,YAAY,EAAE,CAAC,EAAE;AAAA,IAC7D,OAAO;AACL,YAAM,OAAO,WAAW,cACpB,IAAI,IAAI,oBAAoB,IAC5B,IAAI,IAAI,OAAO,SAAS,IAAI;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,WAAW,OAAO,aAAa,WAAW,UAAU;AAE1D,MAAI,UAAU,aAAa;AACzB,eAAW,OAAO,SAAS,aAAa;AACtC,UAAI,OAAO,UAAU,eAAe,KAAK,SAAS,aAAa,GAAG,GAAG;AACnE,cAAM,UAAU,SAAS,YAAY,GAAG;AAExC,YAAI,QAAQ,OAAO,GAAG;AACpB,cAAI,aAAa,OAAO,GAAG;AAAA,QAC7B,OAAO;AACL,cAAI,aAAa,IAAI,KAAK,OAAO;AAAA,QACnC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,MAAI,UAAU,MAAM;AAClB,QAAI,WAAW,SAAS;AAAA,EAC1B;AAEA,MAAI,UAAU,SAAS,MAAM;AAC3B,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,UAAU,MAAM;AAClB,QAAI,OAAO,SAAS;AAAA,EACtB;AAEA,MAAI,mBAAmB,CAAC,UAAU,mBAAmB;AACnD,WAAO,IAAI,WAAW,IAAI,SAAS,IAAI;AAAA,EACzC;AAEA,SAAO,IAAI,SAAS;AACtB;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,28 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/build-url",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
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>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"
|
|
9
|
+
"main": "./lib/index.cjs",
|
|
10
|
+
"module": "./lib/index.js",
|
|
11
|
+
"types": "./lib/index.d.ts",
|
|
10
12
|
"exports": {
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
".": {
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
|
+
"default": "./lib/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./lib/index.d.cts",
|
|
20
|
+
"default": "./lib/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
14
23
|
},
|
|
15
|
-
"main": "./lib/build-url.cjs",
|
|
16
|
-
"module": "./lib/build-url.module.js",
|
|
17
|
-
"unpkg": "./lib/build-url.umd.js",
|
|
18
|
-
"types": "./lib/index.d.ts",
|
|
19
24
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"watch": "yarn build --watch",
|
|
22
27
|
"test": "vitest"
|
|
23
28
|
},
|
|
24
29
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
30
|
+
"@tsconfig/strictest": "^2.0.8",
|
|
31
|
+
"tsup": "^8.5.1",
|
|
32
|
+
"typescript": "^5.9.3",
|
|
33
|
+
"vitest": "^4.0.8"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"lib"
|
|
37
|
+
]
|
|
28
38
|
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { buildUrl } from "../src/index";
|
|
3
|
-
|
|
4
|
-
describe("Build Url test", () => {
|
|
5
|
-
test("Add a query param", () => {
|
|
6
|
-
const url = buildUrl("http://my-website.com/post", {
|
|
7
|
-
queryParams: {
|
|
8
|
-
page: 2,
|
|
9
|
-
},
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
expect(url).toEqual("http://my-website.com/post?page=2");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("Add a zero query param", () => {
|
|
16
|
-
const url = buildUrl("http://my-website.com/post", {
|
|
17
|
-
queryParams: {
|
|
18
|
-
page: 0,
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
expect(url).toEqual("http://my-website.com/post?page=0");
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("Add another query param", () => {
|
|
26
|
-
const url = buildUrl("http://my-website.com/post?page=2", {
|
|
27
|
-
queryParams: {
|
|
28
|
-
sort: "title:asc",
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
expect(url).toEqual("http://my-website.com/post?page=2&sort=title%3Aasc");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("Url is omitted", () => {
|
|
36
|
-
const url = buildUrl({
|
|
37
|
-
queryParams: {
|
|
38
|
-
page: 2,
|
|
39
|
-
sort: "title:desc",
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
expect(url).toEqual("/?page=2&sort=title%3Adesc");
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("Url is relative path", () => {
|
|
47
|
-
const url = buildUrl("images", {
|
|
48
|
-
queryParams: {
|
|
49
|
-
page: 3,
|
|
50
|
-
sort: "title:desc",
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
expect(url).toEqual("/images?page=3&sort=title%3Adesc");
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test("Changes value of an existing query param", () => {
|
|
58
|
-
const url = buildUrl("images?page=2", {
|
|
59
|
-
queryParams: {
|
|
60
|
-
page: 3,
|
|
61
|
-
sort: "title:desc",
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
expect(url).toEqual("/images?page=3&sort=title%3Adesc");
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test("Delete existing query param", () => {
|
|
69
|
-
const url = buildUrl("images?page=2&sort=title:asc", {
|
|
70
|
-
queryParams: {
|
|
71
|
-
page: null,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
expect(url).toEqual("/images?sort=title%3Aasc");
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("Add hash", () => {
|
|
79
|
-
const url = buildUrl("/posts", {
|
|
80
|
-
hash: "my-hash",
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
expect(url).toEqual("/posts#my-hash");
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test("Replace hash", () => {
|
|
87
|
-
const url = buildUrl("/posts?page=2#first-hash", {
|
|
88
|
-
hash: "#second-hash",
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
expect(url).toEqual("/posts?page=2#second-hash");
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test("Add path", () => {
|
|
95
|
-
const url = buildUrl({ path: "posts" });
|
|
96
|
-
const url2 = buildUrl({ path: "/posts" });
|
|
97
|
-
const url3 = buildUrl("http://my-website.com", {
|
|
98
|
-
path: "posts",
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
expect(url).toEqual("/posts");
|
|
102
|
-
expect(url2).toEqual("/posts");
|
|
103
|
-
expect(url3).toEqual("http://my-website.com/posts");
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test("Replace path", () => {
|
|
107
|
-
const url = buildUrl("posts?page=2", {
|
|
108
|
-
path: "stories",
|
|
109
|
-
});
|
|
110
|
-
const url2 = buildUrl("http://my-website.com/posts?page=2", {
|
|
111
|
-
path: "stories",
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
expect(url).toEqual("/stories?page=2");
|
|
115
|
-
expect(url2).toEqual("http://my-website.com/stories?page=2");
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test("Delete path", () => {
|
|
119
|
-
const url = buildUrl("posts?page=2", {
|
|
120
|
-
path: null,
|
|
121
|
-
});
|
|
122
|
-
const url2 = buildUrl("http://my-website.com/posts?page=2", {
|
|
123
|
-
path: null,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
expect(url).toEqual("/?page=2");
|
|
127
|
-
expect(url2).toEqual("http://my-website.com/?page=2");
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test("only query params", () => {
|
|
131
|
-
const url = buildUrl({
|
|
132
|
-
queryParams: { a: 123 },
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
expect(url).toBe("/?a=123");
|
|
136
|
-
});
|
|
137
|
-
});
|
package/jest.config.js
DELETED
package/lib/build-url.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=build-url.cjs.map
|
package/lib/build-url.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/lib/build-url.modern.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=build-url.modern.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/lib/build-url.module.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=build-url.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/lib/build-url.umd.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=build-url.umd.js.map
|
package/lib/build-url.umd.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|