@nice-code/util 0.2.11 → 0.2.12
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/build/index.js
CHANGED
|
@@ -5,87 +5,143 @@ var EStorageAdapterType;
|
|
|
5
5
|
EStorageAdapterType2["json"] = "json";
|
|
6
6
|
})(EStorageAdapterType ||= {});
|
|
7
7
|
|
|
8
|
+
// src/storage_adapter/typed_storage/createTypedStorage.ts
|
|
9
|
+
function createTypedStorage({
|
|
10
|
+
storageAdapter
|
|
11
|
+
}) {
|
|
12
|
+
const getJson = async (key) => {
|
|
13
|
+
return storageAdapter.getJson(key);
|
|
14
|
+
};
|
|
15
|
+
const getJsonOrDef = async (key, defVal) => {
|
|
16
|
+
return await storageAdapter.getJson(key) ?? defVal;
|
|
17
|
+
};
|
|
18
|
+
const setJson = async (key, val) => {
|
|
19
|
+
return storageAdapter.setJson(key, val);
|
|
20
|
+
};
|
|
21
|
+
const removeItem = async (key) => {
|
|
22
|
+
await storageAdapter.removeItem(key);
|
|
23
|
+
};
|
|
24
|
+
const updateJson = async (key, updater) => {
|
|
25
|
+
await storageAdapter.updateJson(key, updater);
|
|
26
|
+
};
|
|
27
|
+
const updateJsonWithDef = async (key, defaultVal, updater) => {
|
|
28
|
+
await storageAdapter.updateJsonOrDef(key, defaultVal, updater);
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
getJson,
|
|
32
|
+
getJsonOrDef,
|
|
33
|
+
setJson,
|
|
34
|
+
removeItem,
|
|
35
|
+
updateJson,
|
|
36
|
+
updateJsonWithDef,
|
|
37
|
+
clearAll: async () => {
|
|
38
|
+
await storageAdapter.clearAll();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
8
43
|
// src/storage_adapter/StorageAdapter.ts
|
|
9
44
|
class StorageAdapter {
|
|
10
45
|
implementation;
|
|
11
46
|
keyPrefix;
|
|
12
|
-
|
|
47
|
+
adapterStorage;
|
|
48
|
+
constructor({ methods, keyPrefix, trackKeysForClearing: trackKeys }) {
|
|
13
49
|
this.implementation = methods;
|
|
14
50
|
this.keyPrefix = keyPrefix ?? "";
|
|
51
|
+
const _trackKeys = trackKeys ?? true;
|
|
52
|
+
this.adapterStorage = _trackKeys ? createTypedStorage({
|
|
53
|
+
storageAdapter: new StorageAdapter({ methods, keyPrefix, trackKeysForClearing: false })
|
|
54
|
+
}) : undefined;
|
|
55
|
+
}
|
|
56
|
+
getPrefixedKey(rawKey) {
|
|
57
|
+
return `${this.keyPrefix}${rawKey}`;
|
|
15
58
|
}
|
|
16
|
-
|
|
17
|
-
|
|
59
|
+
async trackUsedKey(rawKey) {
|
|
60
|
+
if (!this.adapterStorage)
|
|
61
|
+
return;
|
|
62
|
+
await this.adapterStorage.updateJsonWithDef("__usedKeys__", [], (currentKeys) => {
|
|
63
|
+
if (!currentKeys.includes(rawKey)) {
|
|
64
|
+
return [...currentKeys, rawKey];
|
|
65
|
+
}
|
|
66
|
+
return currentKeys;
|
|
67
|
+
});
|
|
18
68
|
}
|
|
19
|
-
async
|
|
20
|
-
|
|
69
|
+
async untrackUsedKey(rawKey) {
|
|
70
|
+
if (!this.adapterStorage)
|
|
71
|
+
return;
|
|
72
|
+
await this.adapterStorage.updateJsonWithDef("__usedKeys__", [], (currentKeys) => {
|
|
73
|
+
return currentKeys.filter((k) => k !== rawKey);
|
|
74
|
+
});
|
|
21
75
|
}
|
|
22
|
-
async
|
|
76
|
+
async clearAll() {
|
|
77
|
+
if (!this.adapterStorage)
|
|
78
|
+
return;
|
|
79
|
+
const allKeys = await this.adapterStorage.getJsonOrDef("__usedKeys__", []) ?? [];
|
|
80
|
+
await Promise.all(allKeys.map(async (key) => {
|
|
81
|
+
await this.removeItem(key);
|
|
82
|
+
}));
|
|
83
|
+
await this.adapterStorage.setJson("__usedKeys__", []);
|
|
84
|
+
}
|
|
85
|
+
async removeItem(rawKey) {
|
|
86
|
+
await this.implementation.removeItem(this.getPrefixedKey(rawKey));
|
|
87
|
+
await this.untrackUsedKey(rawKey);
|
|
88
|
+
}
|
|
89
|
+
async setJson(rawKey, value) {
|
|
90
|
+
const key = this.getPrefixedKey(rawKey);
|
|
23
91
|
if (this.implementation.type === "string" /* string */) {
|
|
24
|
-
await this.implementation.setItem(
|
|
92
|
+
await this.implementation.setItem(key, JSON.stringify(value));
|
|
25
93
|
} else {
|
|
26
|
-
await this.implementation.setItem(
|
|
94
|
+
await this.implementation.setItem(key, value);
|
|
27
95
|
}
|
|
96
|
+
await this.trackUsedKey(rawKey);
|
|
28
97
|
}
|
|
29
|
-
async getJson(
|
|
98
|
+
async getJson(rawKey) {
|
|
99
|
+
const key = this.getPrefixedKey(rawKey);
|
|
30
100
|
if (this.implementation.type === "string" /* string */) {
|
|
31
|
-
const val = await this.implementation.getItem(
|
|
101
|
+
const val = await this.implementation.getItem(key);
|
|
32
102
|
if (val == null || val === "undefined" || val === "null") {
|
|
33
103
|
return;
|
|
34
104
|
}
|
|
35
105
|
return JSON.parse(val);
|
|
36
106
|
} else {
|
|
37
|
-
const val = await this.implementation.getItem(
|
|
107
|
+
const val = await this.implementation.getItem(key);
|
|
38
108
|
if (val == null || val === "undefined" || val === "null") {
|
|
39
109
|
return;
|
|
40
110
|
}
|
|
41
111
|
return val;
|
|
42
112
|
}
|
|
43
113
|
}
|
|
44
|
-
async getJsonOrDef(
|
|
114
|
+
async getJsonOrDef(rawKey, defVal) {
|
|
45
115
|
if (this.implementation.type === "string" /* string */) {
|
|
46
|
-
const val2 = await this.implementation.getItem(this.getPrefixedKey(
|
|
116
|
+
const val2 = await this.implementation.getItem(this.getPrefixedKey(rawKey));
|
|
47
117
|
if (val2 == null || val2 === "undefined" || val2 === "null") {
|
|
48
118
|
return defVal;
|
|
49
119
|
}
|
|
50
120
|
return JSON.parse(val2);
|
|
51
121
|
}
|
|
52
|
-
const val = await this.implementation.getItem(this.getPrefixedKey(
|
|
122
|
+
const val = await this.implementation.getItem(this.getPrefixedKey(rawKey));
|
|
53
123
|
if (val == null || val === "undefined" || val === "null") {
|
|
54
124
|
return defVal;
|
|
55
125
|
}
|
|
56
126
|
return val;
|
|
57
127
|
}
|
|
58
|
-
|
|
128
|
+
async updateJson(rawKey, updater) {
|
|
129
|
+
const currentVal = await this.getJson(rawKey);
|
|
130
|
+
const newVal = updater(currentVal);
|
|
131
|
+
await this.setJson(rawKey, newVal);
|
|
132
|
+
}
|
|
133
|
+
async updateJsonOrDef(rawKey, defVal, updater) {
|
|
134
|
+
const currentVal = await this.getJsonOrDef(rawKey, defVal);
|
|
135
|
+
const newVal = updater(currentVal);
|
|
136
|
+
await this.setJson(rawKey, newVal);
|
|
137
|
+
}
|
|
138
|
+
createJsonGetterSetter(rawKey) {
|
|
59
139
|
return {
|
|
60
|
-
get: () => this.getJson(
|
|
61
|
-
set: (value) => this.setJson(
|
|
140
|
+
get: () => this.getJson(rawKey),
|
|
141
|
+
set: (value) => this.setJson(rawKey, value)
|
|
62
142
|
};
|
|
63
143
|
}
|
|
64
144
|
}
|
|
65
|
-
// src/storage_adapter/typed_storage/createTypedStorage.ts
|
|
66
|
-
function createTypedStorage({
|
|
67
|
-
storageAdapter
|
|
68
|
-
}) {
|
|
69
|
-
const getJson = async (key) => {
|
|
70
|
-
return storageAdapter.getJson(key);
|
|
71
|
-
};
|
|
72
|
-
const getJsonOrDef = async (key, defVal) => {
|
|
73
|
-
return await storageAdapter.getJson(key) ?? defVal;
|
|
74
|
-
};
|
|
75
|
-
const setJson = async (key, val) => {
|
|
76
|
-
return storageAdapter.setJson(key, val);
|
|
77
|
-
};
|
|
78
|
-
const removeItem = async (key) => {
|
|
79
|
-
await storageAdapter.removeItem(key);
|
|
80
|
-
};
|
|
81
|
-
return {
|
|
82
|
-
getJson,
|
|
83
|
-
getJsonOrDef,
|
|
84
|
-
setJson,
|
|
85
|
-
removeItem
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
145
|
// src/storage_adapter/specific/browser/browser_storage.ts
|
|
90
146
|
function createWebLocalStorageMethods(_localStorage) {
|
|
91
147
|
return {
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import { type IStorageKeyGetterAndSetter, type TStorageAdapterMethods } from "./storage_adapter.types";
|
|
2
2
|
interface IStorageAdapterConstructor {
|
|
3
3
|
methods: TStorageAdapterMethods;
|
|
4
|
+
trackKeysForClearing?: boolean;
|
|
4
5
|
keyPrefix?: string;
|
|
5
6
|
}
|
|
6
7
|
export declare class StorageAdapter {
|
|
7
8
|
private implementation;
|
|
8
9
|
readonly keyPrefix: string;
|
|
9
|
-
|
|
10
|
+
private readonly adapterStorage?;
|
|
11
|
+
constructor({ methods, keyPrefix, trackKeysForClearing: trackKeys }: IStorageAdapterConstructor);
|
|
10
12
|
private getPrefixedKey;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
private trackUsedKey;
|
|
14
|
+
private untrackUsedKey;
|
|
15
|
+
clearAll(): Promise<void>;
|
|
16
|
+
removeItem(rawKey: string): Promise<void>;
|
|
17
|
+
setJson(rawKey: string, value: any): Promise<void>;
|
|
18
|
+
getJson<T>(rawKey: string): Promise<T | undefined>;
|
|
19
|
+
getJsonOrDef<T>(rawKey: string, defVal: T): Promise<T>;
|
|
20
|
+
updateJson<T>(rawKey: string, updater: (currentVal: T | undefined) => T): Promise<void>;
|
|
21
|
+
updateJsonOrDef<T>(rawKey: string, defVal: T, updater: (currentVal: T) => T): Promise<void>;
|
|
22
|
+
createJsonGetterSetter<T>(rawKey: string): IStorageKeyGetterAndSetter<T>;
|
|
16
23
|
}
|
|
17
24
|
export {};
|
|
@@ -5,6 +5,9 @@ export interface ITypedStorage<T extends Record<string, any>> {
|
|
|
5
5
|
getJsonOrDef<K extends StringKeys<T>>(key: K, defVal: T[K]): Promise<T[K]>;
|
|
6
6
|
setJson<K extends StringKeys<T>>(key: K, val: T[K]): Promise<void>;
|
|
7
7
|
removeItem<K extends StringKeys<T>>(key: K): Promise<void>;
|
|
8
|
+
updateJson<K extends StringKeys<T>>(key: K, updater: (currentVal: T[K] | undefined) => T[K]): Promise<void>;
|
|
9
|
+
updateJsonWithDef<K extends StringKeys<T>>(key: K, defaultVal: T[K], updater: (currentVal: T[K]) => T[K]): Promise<void>;
|
|
10
|
+
clearAll(): Promise<void>;
|
|
8
11
|
}
|
|
9
12
|
interface ITypedStorage_Create_Input {
|
|
10
13
|
storageAdapter: StorageAdapter;
|