1
0
silverbullet/plugs/lib/util.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

import { flashNotification } from "../../syscall/silverbullet-syscall/editor.ts";
2022-07-25 18:52:05 +00:00
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()!);
}
2022-07-25 18:52:05 +00:00
export function isServer() {
return (
typeof window === "undefined" || typeof window.document === "undefined"
); // if something defines window the same way as the browser, this will fail.
2022-07-25 18:52:05 +00:00
}
// this helps keep if's condition as positive
export function isBrowser() {
return !isServer();
}
export async function notifyUser(message: string, type?: "info" | "error") {
2022-07-25 18:52:05 +00:00
if (isBrowser()) {
return 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;
}