@jsenv/urls 2.3.2 → 2.4.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/urls",
3
- "version": "2.3.2",
3
+ "version": "2.4.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
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 {
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
- export const ensurePathnameTrailingSlash = (url) => {
170
+ const transformUrlPathname = (url, transformer) => {
171
171
  if (typeof url === "string") {
172
172
  const urlObject = new URL(url);
173
173
  const { pathname } = urlObject;
174
- if (pathname.endsWith("/")) {
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 urlWithTrailingSlash = `${origin}${pathname}/${search}${hash}`;
184
- return urlWithTrailingSlash;
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);