umami/lib/url.js

44 lines
883 B
JavaScript
Raw Normal View History

export function removeTrailingSlash(url) {
2020-09-01 05:25:31 +02:00
return url && url.length > 1 && url.endsWith('/') ? url.slice(0, -1) : url;
}
export function removeWWW(url) {
return url && url.length > 1 && url.startsWith('www.') ? url.slice(4) : url;
}
export function getDomainName(str) {
try {
return new URL(str).hostname;
2020-09-01 05:25:31 +02:00
} catch (e) {
return str;
}
}
2020-09-26 07:31:18 +02:00
2020-09-26 17:43:05 +02:00
export function getQueryString(params = {}) {
2020-09-26 07:31:18 +02:00
const map = Object.keys(params).reduce((arr, key) => {
if (params[key] !== undefined) {
return arr.concat(`${key}=${encodeURIComponent(params[key])}`);
}
return arr;
}, []);
if (map.length) {
return `?${map.join('&')}`;
}
return '';
}
2020-10-09 10:04:06 +02:00
export function makeUrl(url, params) {
return `${url}${getQueryString(params)}`;
}
export function safeDecodeURI(s) {
try {
return decodeURI(s);
} catch (e) {
console.error(e);
}
return s;
}