diff options
Diffstat (limited to 'client/src/lib/utils')
| -rw-r--r-- | client/src/lib/utils/addProtocol.ts | 6 | ||||
| -rw-r--r-- | client/src/lib/utils/checkUrl.ts | 10 | ||||
| -rw-r--r-- | client/src/lib/utils/debounce.ts | 12 |
3 files changed, 28 insertions, 0 deletions
diff --git a/client/src/lib/utils/addProtocol.ts b/client/src/lib/utils/addProtocol.ts new file mode 100644 index 0000000..75c6214 --- /dev/null +++ b/client/src/lib/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/lib/utils/checkUrl.ts b/client/src/lib/utils/checkUrl.ts new file mode 100644 index 0000000..8ed747f --- /dev/null +++ b/client/src/lib/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/lib/utils/debounce.ts b/client/src/lib/utils/debounce.ts new file mode 100644 index 0000000..86ef3db --- /dev/null +++ b/client/src/lib/utils/debounce.ts @@ -0,0 +1,12 @@ +type Procedure = (...args: any[]) => any; + +export default function <F extends Procedure>(f: F, duration: number) { + let timeout: ReturnType<typeof setTimeout> | null = null; + return function (...args: Parameters<F>) { + if (timeout !== null) { + clearTimeout(timeout); + timeout = null; + } + timeout = setTimeout(() => f(...args), duration); + }; +} |
