@jsenv/urls 2.3.1 → 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.1",
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
@@ -144,7 +144,15 @@ export const setUrlExtension = (url, extension) => {
144
144
  const pathnameWithoutExtension = currentExtension
145
145
  ? pathname.slice(0, -currentExtension.length)
146
146
  : pathname;
147
- const newPathname = `${pathnameWithoutExtension}${extension}`;
147
+ let newPathname;
148
+ if (pathnameWithoutExtension.endsWith("/")) {
149
+ newPathname = pathnameWithoutExtension.slice(0, -1);
150
+ newPathname += extension;
151
+ newPathname += "/";
152
+ } else {
153
+ newPathname = pathnameWithoutExtension;
154
+ newPathname += extension;
155
+ }
148
156
  return `${origin}${newPathname}${search ? `?${search}` : ""}`;
149
157
  };
150
158
 
@@ -159,11 +167,12 @@ export const setUrlFilename = (url, filename) => {
159
167
  return `${origin}${parentPathname}${filename}${search}${hash}`;
160
168
  };
161
169
 
162
- export const ensurePathnameTrailingSlash = (url) => {
170
+ const transformUrlPathname = (url, transformer) => {
163
171
  if (typeof url === "string") {
164
172
  const urlObject = new URL(url);
165
173
  const { pathname } = urlObject;
166
- if (pathname.endsWith("/")) {
174
+ const pathnameTransformed = transformer(pathname);
175
+ if (pathnameTransformed === pathname) {
167
176
  return url;
168
177
  }
169
178
  let { origin } = urlObject;
@@ -172,15 +181,23 @@ export const ensurePathnameTrailingSlash = (url) => {
172
181
  origin = "file://";
173
182
  }
174
183
  const { search, hash } = urlObject;
175
- const urlWithTrailingSlash = `${origin}${pathname}/${search}${hash}`;
176
- return urlWithTrailingSlash;
184
+ const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
185
+ return urlWithPathnameTransformed;
177
186
  }
178
- if (url.pathname.endsWith("/")) {
179
- return url;
180
- }
181
- url.pathname += "/";
187
+ const pathnameTransformed = transformer(url.pathname);
188
+ url.pathname = pathnameTransformed;
182
189
  return url;
183
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
+ };
184
201
 
185
202
  export const asUrlUntilPathname = (url) => {
186
203
  const urlObject = new URL(url);