@rr0/cms
Version:
RR0 Content Management System (CMS)
29 lines (28 loc) • 990 B
JavaScript
export class UrlUtil {
static normalized(url) {
if (!url.endsWith("/")) {
url += "/";
}
return url;
}
static absolute(url) {
if (!url.startsWith("/")) {
url = "/" + url;
}
return this.normalized(url);
}
static join(first, second) {
const firstEndsWithSlash = first.endsWith("/");
const secondStartsWithSlash = second.startsWith("/");
if (firstEndsWithSlash) {
return secondStartsWithSlash ? first + second.substring(1) : first + second;
}
else {
const firstEndsWithExtension = first.length > 5 && first.lastIndexOf(".") >= first.length - 5;
return secondStartsWithSlash || firstEndsWithExtension ? first + second : first + "/" + second;
}
}
static objToQueryParams(obj) {
return Object.entries(obj).map(entry => encodeURIComponent(entry[0]) + "=" + encodeURIComponent(entry[1])).join("&");
}
}