@choksheak/ts-utils 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dateTimeStr.d.ts +14 -0
- package/dateTimeStr.js +71 -0
- package/dateTimeStr.js.map +1 -0
- package/isEmpty.d.ts +4 -0
- package/isEmpty.js +48 -0
- package/isEmpty.js.map +1 -0
- package/kvStore.d.ts +63 -0
- package/kvStore.js +315 -0
- package/kvStore.js.map +1 -0
- package/localStorageCache.d.ts +9 -0
- package/localStorageCache.js +26 -8
- package/localStorageCache.js.map +1 -1
- package/nonEmpty.d.ts +5 -0
- package/nonEmpty.js +58 -0
- package/nonEmpty.js.map +1 -0
- package/nonNil.js +2 -2
- package/nonNil.js.map +1 -1
- package/package.json +6 -5
- package/sleep.d.ts +1 -0
- package/sleep.js +33 -0
- package/sleep.js.map +1 -0
- package/src/dateTimeStr.ts +83 -0
- package/src/isEmpty.ts +32 -0
- package/src/kvStore.ts +382 -0
- package/src/localStorageCache.ts +31 -7
- package/src/nonEmpty.ts +15 -0
- package/src/nonNil.ts +2 -2
- package/src/sleep.ts +3 -0
package/src/localStorageCache.ts
CHANGED
|
@@ -13,7 +13,10 @@ export class LocalStorageCache {
|
|
|
13
13
|
|
|
14
14
|
public static getValue<T>(key: string, logError = true): T | undefined {
|
|
15
15
|
const jsonStr = globalThis.localStorage.getItem(key);
|
|
16
|
-
|
|
16
|
+
|
|
17
|
+
if (!jsonStr || typeof jsonStr !== "string") {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
17
20
|
|
|
18
21
|
try {
|
|
19
22
|
const obj: { value: T; expireMs: number } | undefined =
|
|
@@ -22,17 +25,14 @@ export class LocalStorageCache {
|
|
|
22
25
|
!obj ||
|
|
23
26
|
typeof obj !== "object" ||
|
|
24
27
|
!("value" in obj) ||
|
|
25
|
-
!("expireMs" in obj)
|
|
28
|
+
!("expireMs" in obj) ||
|
|
29
|
+
typeof obj.expireMs !== "number" ||
|
|
30
|
+
Date.now() >= obj.expireMs
|
|
26
31
|
) {
|
|
27
32
|
globalThis.localStorage.removeItem(key);
|
|
28
33
|
return undefined;
|
|
29
34
|
}
|
|
30
35
|
|
|
31
|
-
if (Date.now() >= obj.expireMs) {
|
|
32
|
-
globalThis.localStorage.removeItem(key);
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
36
|
return obj.value;
|
|
37
37
|
} catch (e) {
|
|
38
38
|
if (logError) {
|
|
@@ -43,3 +43,27 @@ export class LocalStorageCache {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
+
|
|
47
|
+
/** Same as above, but saves some config for reuse. */
|
|
48
|
+
export class LocalStorageCacheItem<T> {
|
|
49
|
+
public constructor(
|
|
50
|
+
public readonly key: string,
|
|
51
|
+
public readonly expireDeltaMs: number,
|
|
52
|
+
public readonly logError = true,
|
|
53
|
+
defaultValue?: T,
|
|
54
|
+
) {
|
|
55
|
+
if (defaultValue !== undefined) {
|
|
56
|
+
if (this.get() === undefined) {
|
|
57
|
+
this.set(defaultValue);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public set(value: T) {
|
|
63
|
+
LocalStorageCache.setValue(this.key, value, this.expireDeltaMs);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public get(): T | undefined {
|
|
67
|
+
return LocalStorageCache.getValue(this.key, this.logError);
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/nonEmpty.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isEmpty } from "./isEmpty";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type asserts that `t` is truthy.
|
|
5
|
+
* Throws an error if `t` is null or undefined.
|
|
6
|
+
*/
|
|
7
|
+
export function nonEmpty<T>(
|
|
8
|
+
t: T | null | undefined | "" | 0 | -0 | 0n | false | typeof NaN,
|
|
9
|
+
varName = "value",
|
|
10
|
+
): T {
|
|
11
|
+
if (isEmpty(t)) {
|
|
12
|
+
throw new Error(`Empty ${varName}: ${t}`);
|
|
13
|
+
}
|
|
14
|
+
return t as T;
|
|
15
|
+
}
|
package/src/nonNil.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* Type asserts that `t` is neither null nor undefined.
|
|
3
3
|
* Throws an error if `t` is null or undefined.
|
|
4
4
|
*/
|
|
5
|
-
export function nonNil<T>(t: T | null | undefined, varName = "
|
|
5
|
+
export function nonNil<T>(t: T | null | undefined, varName = "value"): T {
|
|
6
6
|
if (t === null || t === undefined) {
|
|
7
|
-
throw new Error(`Missing ${varName}`);
|
|
7
|
+
throw new Error(`Missing ${varName}: ${t}`);
|
|
8
8
|
}
|
|
9
9
|
return t;
|
|
10
10
|
}
|
package/src/sleep.ts
ADDED