forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalStorage.ts
More file actions
50 lines (39 loc) · 1.34 KB
/
LocalStorage.ts
File metadata and controls
50 lines (39 loc) · 1.34 KB
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
46
47
48
49
50
import { IStorage } from "./IStorage.js";
export class LocalStorage implements IStorage {
constructor(private prefix: string = "exceptionless-", private storage: Storage = globalThis.localStorage) { }
public length(): Promise<number> {
return Promise.resolve(this.getKeys().length);
}
public clear(): Promise<void> {
for (const key of this.getKeys()) {
this.storage.removeItem(this.getKey(key));
}
return Promise.resolve();
}
public getItem(key: string): Promise<string | null> {
return Promise.resolve(this.storage.getItem(this.getKey(key)));
}
public key(index: number): Promise<string | null> {
const keys = this.getKeys();
return Promise.resolve(index < keys.length ? keys[index] : null);
}
public keys(): Promise<string[]> {
return Promise.resolve(this.getKeys());
}
public removeItem(key: string): Promise<void> {
this.storage.removeItem(this.getKey(key));
return Promise.resolve();
}
public setItem(key: string, value: string): Promise<void> {
this.storage.setItem(this.getKey(key), value);
return Promise.resolve();
}
private getKeys(): string[] {
return Object.keys(this.storage)
.filter(key => key.startsWith(this.prefix))
.map(key => key?.substr(this.prefix.length));
}
private getKey(key: string): string {
return this.prefix + key;
}
}