-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathstorage.ts
40 lines (38 loc) · 1.61 KB
/
storage.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
import { log } from "./index";
/**
* This class will create a new StoreManager with an appended prefix to it. The generic is there to tell you what that prefix ***is***
*
* **Note: there is already a defaultStore available! In most situations, you'll want to use that.**
*
* <code>
* const newStore = new StoreManager("incog");
*
* // Appends the prefix to the key passed. (EX: "incog||test")
* // Will return a string.
* newStore.getVal("test")
*
* // As stated above the prefix will automatically be appended to the key param (EX: "incog||test")
* newStore.setVal("test", "newVal");
* </code>
*/
class StoreManager<Prefix extends string /* This is here so I know what prefix is appended. It's inferred from the constructor */> {
#prefix: Prefix;
constructor(pref: Prefix) {
this.#prefix = pref;
}
getVal(key: string): string {
log({ type: 'info', bg: true, prefix: true }, `Getting key: ${key} \nFull key: ${this.#prefix}||${key}`);
return localStorage.getItem(`${this.#prefix}||${key}`) as string;
}
setVal(key: string, val: string): void {
log({ type: 'info', bg: false, prefix: true }, `Setting ${key} with value: ${val}`);
localStorage.setItem(`${this.#prefix}||${key}`, val);
}
removeVal(key: string): void {
log({ type: 'info', bg: true, prefix: true }, `Removing ${this.#prefix}||${key}`);
localStorage.removeItem(`${this.#prefix}||${key}`);
}
}
//this is done so I can see the prefix used.
const defaultStore = new StoreManager("nebula");
export { StoreManager, defaultStore };