[feature] prepare shortcut creation

This commit is contained in:
MathieuG-P
2023-07-10 01:36:37 +02:00
parent 215586a41b
commit 8a7ffdc264
6 changed files with 118 additions and 4 deletions
+13
View File
@@ -0,0 +1,13 @@
export function objectFromEntries<T>(entries: Iterable<readonly [PropertyKey, T]>): Record<PropertyKey, T | T[]> {
const temp: Record<PropertyKey, T | T[]> = {};
for (const [key, value] of entries) {
if (temp[key]) {
temp[key] = Array.isArray(temp[key]) ? [...(temp[key] as T[]), value] : [(temp[key] as T), value];
} else {
temp[key] = value;
}
}
return temp;
}
+27
View File
@@ -6,3 +6,30 @@ export function isValidUrl(url: string): boolean {
return false;
}
}
export function buildUrl({
protocol = "https",
host = "about:blank",
path = "",
search = {},
hash = ""
}: {
protocol?: string,
host?: string,
path?: string,
search?: Record<string, string | string[]>,
hash?: string
}): URL {
const url = new URL(`${protocol}://${host}${path}`);
url.hash = hash;
for (const [key, value] of Object.entries(search)) {
if (Array.isArray(value)) {
value.forEach(v => url.searchParams.append(key, v));
} else {
url.searchParams.append(key, value);
}
}
return url;
}