-
Notifications
You must be signed in to change notification settings - Fork 14
/
helpers.ts
45 lines (40 loc) · 1.13 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { browser } from "$app/environment";
export function fromLocalStorage(key: string, defaultValue: boolean | number | string) {
if (browser) {
const val = localStorage.getItem(`setting.${key}`);
if (val) {
return JSON.parse(val);
}
}
return defaultValue;
}
export function toLocalStorage(key: string, value: boolean | number | string) {
if (browser) {
localStorage.setItem(`setting.${key}`, JSON.stringify(value));
}
}
export function getBoolFromURL(url: URL, key: string, defaultValue: boolean): boolean {
const val = url.searchParams.get(key);
if (val) {
if (val === "true") return true;
if (val === "false") return false;
}
return defaultValue;
}
export function getIntFromURL(url: URL, key: string, defaultValue: number): number {
const val = url.searchParams.get(key);
if (val) {
const parsedVal = parseInt(val);
if (!isNaN(parsedVal)) {
return parsedVal;
}
}
return defaultValue;
}
export function getStringFromURL(url: URL, key: string, defaultValue: string): string {
const val = url.searchParams.get(key);
if (val) {
return val;
}
return defaultValue;
}