1
0
silverbullet/plug-api/lib/util.ts
2022-10-14 15:11:33 +02:00

37 lines
1.1 KiB
TypeScript

import { editor } from "$sb/silverbullet-syscall/mod.ts";
export async function replaceAsync(
str: string,
regex: RegExp,
asyncFn: (match: string, ...args: any[]) => Promise<string>,
) {
const promises: Promise<string>[] = [];
str.replace(regex, (match: string, ...args: any[]): string => {
const promise = asyncFn(match, ...args);
promises.push(promise);
return "";
});
const data = await Promise.all(promises);
return str.replace(regex, () => data.shift()!);
}
export function isServer() {
return (
typeof window === "undefined" || typeof window.document === "undefined"
); // if something defines window the same way as the browser, this will fail.
}
// this helps keep if's condition as positive
export function isBrowser() {
return !isServer();
}
export function notifyUser(message: string, type?: "info" | "error") {
if (isBrowser()) {
return editor.flashNotification(message, type);
}
const log = type === "error" ? console.error : console.log;
log(message); // we should end up sending the message to the user, users dont read logs.
return;
}