@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: dakkar and other Sharkey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
export function clamp(value: number, min: number, max: number) {
|
||||
if (value > max) return max;
|
||||
if (value < min) return min;
|
||||
return value;
|
||||
}
|
||||
@@ -26,3 +26,20 @@ export function extractDomain(url: string) {
|
||||
const match = url.match(/^(?:https?:)?(?:\/\/)?(?:[^@\n]+@)?([^:\/\n]+)/im);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
export function maybeMakeRelative(urlStr: string, baseStr: string): string {
|
||||
try {
|
||||
const baseObj = new URL(baseStr);
|
||||
const urlObj = new URL(urlStr);
|
||||
/* in all places where maybeMakeRelative is used, baseStr is the
|
||||
* instance's public URL, which can't have path components, so the
|
||||
* relative URL will always have the whole path from the urlStr
|
||||
*/
|
||||
if (urlObj.origin === baseObj.origin) {
|
||||
return urlObj.pathname + urlObj.search + urlObj.hash;
|
||||
}
|
||||
return urlStr;
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user