@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 +1 -1
- package/src/main.js +1 -0
- package/src/url_utils.js +26 -9
package/package.json
CHANGED
package/src/main.js
CHANGED
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
|
-
|
|
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
|
-
|
|
170
|
+
const transformUrlPathname = (url, transformer) => {
|
|
163
171
|
if (typeof url === "string") {
|
|
164
172
|
const urlObject = new URL(url);
|
|
165
173
|
const { pathname } = urlObject;
|
|
166
|
-
|
|
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
|
|
176
|
-
return
|
|
184
|
+
const urlWithPathnameTransformed = `${origin}${pathnameTransformed}${search}${hash}`;
|
|
185
|
+
return urlWithPathnameTransformed;
|
|
177
186
|
}
|
|
178
|
-
|
|
179
|
-
|
|
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);
|