@jsenv/urls 2.3.2 → 2.4.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.
- package/package.json +2 -2
- package/src/main.js +3 -0
- package/src/url_to_relative_url.js +6 -2
- package/src/url_utils.js +17 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/urls",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"test": "node --conditions=development ./scripts/test.mjs"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@jsenv/humanize": "1.2.
|
|
31
|
+
"@jsenv/humanize": "1.2.6"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/main.js
CHANGED
|
@@ -14,6 +14,7 @@ export {
|
|
|
14
14
|
setUrlExtension,
|
|
15
15
|
setUrlFilename,
|
|
16
16
|
ensurePathnameTrailingSlash,
|
|
17
|
+
removePathnameTrailingSlash,
|
|
17
18
|
asUrlUntilPathname,
|
|
18
19
|
} from "./url_utils.js";
|
|
19
20
|
export {
|
|
@@ -39,3 +40,5 @@ export { urlToPathname } from "./url_to_pathname.js";
|
|
|
39
40
|
export { urlToRelativeUrl } from "./url_to_relative_url.js";
|
|
40
41
|
export { urlToResource } from "./url_to_resource.js";
|
|
41
42
|
export { urlToScheme } from "./url_to_scheme.js";
|
|
43
|
+
|
|
44
|
+
export { getCommonPathname } from "./common_pathname.js";
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { getCommonPathname } from "./common_pathname.js";
|
|
2
2
|
|
|
3
|
-
export const urlToRelativeUrl = (
|
|
3
|
+
export const urlToRelativeUrl = (
|
|
4
|
+
url,
|
|
5
|
+
baseUrl,
|
|
6
|
+
{ preferRelativeNotation } = {},
|
|
7
|
+
) => {
|
|
4
8
|
const urlObject = new URL(url);
|
|
5
9
|
const baseUrlObject = new URL(baseUrl);
|
|
6
10
|
|
|
@@ -44,7 +48,7 @@ export const urlToRelativeUrl = (url, baseUrl) => {
|
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
const relativeUrl = `${specificPathname}${search}${hash}`;
|
|
47
|
-
return relativeUrl;
|
|
51
|
+
return preferRelativeNotation ? `./${relativeUrl}` : relativeUrl;
|
|
48
52
|
};
|
|
49
53
|
|
|
50
54
|
const pathnameToParentPathname = (pathname) => {
|
package/src/url_utils.js
CHANGED
|
@@ -167,11 +167,12 @@ export const setUrlFilename = (url, filename) => {
|
|
|
167
167
|
return `${origin}${parentPathname}${filename}${search}${hash}`;
|
|
168
168
|
};
|
|
169
169
|
|
|
170
|
-
|
|
170
|
+
const transformUrlPathname = (url, transformer) => {
|
|
171
171
|
if (typeof url === "string") {
|
|
172
172
|
const urlObject = new URL(url);
|
|
173
173
|
const { pathname } = urlObject;
|
|
174
|
-
|
|
174
|
+
const pathnameTransformed = transformer(pathname);
|
|
175
|
+
if (pathnameTransformed === pathname) {
|
|
175
176
|
return url;
|
|
176
177
|
}
|
|
177
178
|
let { origin } = urlObject;
|
|
@@ -180,15 +181,23 @@ export const ensurePathnameTrailingSlash = (url) => {
|
|
|
180
181
|
origin = "file://";
|
|
181
182
|
}
|
|
182
183
|
const { search, hash } = urlObject;
|
|
183
|
-
const
|
|
184
|
-
return
|
|
185
|
-
}
|
|
186
|
-
if (url.pathname.endsWith("/")) {
|
|
187
|
-
return url;
|
|
184
|
+
const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
|
|
185
|
+
return urlWithPathnameTransformed;
|
|
188
186
|
}
|
|
189
|
-
url.pathname
|
|
187
|
+
const pathnameTransformed = transformer(url.pathname);
|
|
188
|
+
url.pathname = pathnameTransformed;
|
|
190
189
|
return url;
|
|
191
190
|
};
|
|
191
|
+
export const ensurePathnameTrailingSlash = (url) => {
|
|
192
|
+
return transformUrlPathname(url, (pathname) => {
|
|
193
|
+
return pathname.endsWith("/") ? pathname : `${pathname}/`;
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
export const removePathnameTrailingSlash = (url) => {
|
|
197
|
+
return transformUrlPathname(url, (pathname) => {
|
|
198
|
+
return pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
|
|
199
|
+
});
|
|
200
|
+
};
|
|
192
201
|
|
|
193
202
|
export const asUrlUntilPathname = (url) => {
|
|
194
203
|
const urlObject = new URL(url);
|