@jsenv/urls 2.6.3 → 2.7.1

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/url_utils.js +21 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/urls",
3
- "version": "2.6.3",
3
+ "version": "2.7.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,6 +26,6 @@
26
26
  ],
27
27
  "sideEffects": false,
28
28
  "dependencies": {
29
- "@jsenv/humanize": "1.3.1"
29
+ "@jsenv/humanize": "1.4.0"
30
30
  }
31
31
  }
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
- if (url.includes("?")) {
42
- // disable on data urls (would mess up base64 encoding)
43
- if (url.startsWith("data:")) {
44
- return url;
45
- }
46
- return url.replace(/[=](?=&|$)/g, "");
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
- return url;
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 urlObject = new URL(url);
83
+ const calledWithString = typeof url === "string";
84
+ const urlObject = calledWithString ? new URL(url) : url;
75
85
  const { searchParams } = urlObject;
76
- Object.keys(params).forEach((key) => {
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
- const urlWithParams = urlObject.href;
85
- return normalizeUrl(urlWithParams);
93
+ }
94
+ return normalizeUrl(calledWithString ? urlObject.href : urlObject);
86
95
  };
87
96
 
88
97
  export const injectQueryParamWithoutEncoding = (url, key, value) => {