From 826c7c47785ee01d2b9267919132ada696425344 Mon Sep 17 00:00:00 2001 From: Melonai Date: Wed, 20 Jan 2021 23:18:09 +0100 Subject: Remade the client in SvelteKit --- client/src/utils/addProtocol.ts | 6 ++++++ client/src/utils/checkUrl.ts | 10 ++++++++++ client/src/utils/debounce.ts | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 client/src/utils/addProtocol.ts create mode 100644 client/src/utils/checkUrl.ts create mode 100644 client/src/utils/debounce.ts (limited to 'client/src/utils') diff --git a/client/src/utils/addProtocol.ts b/client/src/utils/addProtocol.ts new file mode 100644 index 0000000..75c6214 --- /dev/null +++ b/client/src/utils/addProtocol.ts @@ -0,0 +1,6 @@ +export default function (url: string) { + if (!/^https?:\/\//.test(url)) { + url = "https://" + url; + } + return url; +} diff --git a/client/src/utils/checkUrl.ts b/client/src/utils/checkUrl.ts new file mode 100644 index 0000000..8ed747f --- /dev/null +++ b/client/src/utils/checkUrl.ts @@ -0,0 +1,10 @@ +import addProtocol from "./addProtocol"; + +export default function (url: string): string | null { + try { + const normalizedUrl = new URL(addProtocol(url)); + return normalizedUrl.toString(); + } catch (e) { + return null; + } +} \ No newline at end of file diff --git a/client/src/utils/debounce.ts b/client/src/utils/debounce.ts new file mode 100644 index 0000000..86ef3db --- /dev/null +++ b/client/src/utils/debounce.ts @@ -0,0 +1,12 @@ +type Procedure = (...args: any[]) => any; + +export default function (f: F, duration: number) { + let timeout: ReturnType | null = null; + return function (...args: Parameters) { + if (timeout !== null) { + clearTimeout(timeout); + timeout = null; + } + timeout = setTimeout(() => f(...args), duration); + }; +} -- cgit 1.4.1