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