@jsenv/urls 2.6.2 → 2.7.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/package.json +1 -1
- package/src/url_utils.js +21 -12
package/package.json
CHANGED
package/src/url_utils.js
CHANGED
|
@@ -38,14 +38,23 @@ export const asSpecifierWithoutSearch = (specifier) => {
|
|
|
38
38
|
// becoming "file:///file.css?css_module="
|
|
39
39
|
// we want to get rid of the "=" and consider it's the same url
|
|
40
40
|
export const normalizeUrl = (url) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return url
|
|
41
|
+
const calledWithString = typeof url === "string";
|
|
42
|
+
const urlObject = calledWithString ? new URL(url) : url;
|
|
43
|
+
let urlString = urlObject.href;
|
|
44
|
+
|
|
45
|
+
if (!urlString.includes("?")) {
|
|
46
|
+
return url;
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
// disable on data urls (would mess up base64 encoding)
|
|
49
|
+
if (urlString.startsWith("data:")) {
|
|
50
|
+
return url;
|
|
51
|
+
}
|
|
52
|
+
urlString = urlString.replace(/[=](?=&|$)/g, "");
|
|
53
|
+
if (calledWithString) {
|
|
54
|
+
return urlString;
|
|
55
|
+
}
|
|
56
|
+
urlObject.href = urlString;
|
|
57
|
+
return urlObject;
|
|
49
58
|
};
|
|
50
59
|
|
|
51
60
|
export const injectQueryParamsIntoSpecifier = (specifier, params) => {
|
|
@@ -71,18 +80,18 @@ export const injectQueryParamsIntoSpecifier = (specifier, params) => {
|
|
|
71
80
|
};
|
|
72
81
|
|
|
73
82
|
export const injectQueryParams = (url, params) => {
|
|
74
|
-
const
|
|
83
|
+
const calledWithString = typeof url === "string";
|
|
84
|
+
const urlObject = calledWithString ? new URL(url) : url;
|
|
75
85
|
const { searchParams } = urlObject;
|
|
76
|
-
Object.keys(params)
|
|
86
|
+
for (const key of Object.keys(params)) {
|
|
77
87
|
const value = params[key];
|
|
78
88
|
if (value === undefined) {
|
|
79
89
|
searchParams.delete(key);
|
|
80
90
|
} else {
|
|
81
91
|
searchParams.set(key, value);
|
|
82
92
|
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return normalizeUrl(urlWithParams);
|
|
93
|
+
}
|
|
94
|
+
return normalizeUrl(calledWithString ? urlObject.href : urlObject);
|
|
86
95
|
};
|
|
87
96
|
|
|
88
97
|
export const injectQueryParamWithoutEncoding = (url, key, value) => {
|