-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstorage.ts
More file actions
100 lines (87 loc) · 2.49 KB
/
storage.ts
File metadata and controls
100 lines (87 loc) · 2.49 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Implements Web Storage API if localStorage is not available.
* So it can be available in node.js environment
*/
class CatcherStorage implements Storage {
private items: { [key: string]: string } = {};
/**
* Returns the value of the item with the specified key.
*
* @param key - The key of the item you want to retrieve
*/
public getItem(key: string): string | null {
if (this.isLocalStorageAvailable()) {
return localStorage.getItem(key);
}
return this.items[key] || null;
}
/**
* Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
*
* @param key - The key of the item you want to store
* @param value - The value of the item you want to store
*/
public setItem(key: string, value: string): void {
if (this.isLocalStorageAvailable()) {
localStorage.setItem(key, value);
} else {
this.items[key] = value;
}
}
/**
* Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
*
* @param key - The key of the item you want to remove
*/
public removeItem(key: string): void {
if (this.isLocalStorageAvailable()) {
localStorage.removeItem(key);
} else {
delete this.items[key];
}
}
/**
* Empties the list associated with the object of all key/value pairs, if there are any.
*/
public clear(): void {
if (this.isLocalStorageAvailable()) {
localStorage.clear();
} else {
this.items = {};
}
}
/**
* Returns the number of key/value pairs currently present in the list associated with the object.
*/
public get length(): number {
if (this.isLocalStorageAvailable()) {
return localStorage.length;
}
return Object.keys(this.items).length;
}
/**
* Returns the name of the nth key in the list.
*
* @param index - The index of the key you want to get
*/
public key(index: number): string | null {
if (this.isLocalStorageAvailable()) {
return localStorage.key(index);
}
return Object.keys(this.items)[index] || null;
}
/**
* Checks if localStorage is available
*/
private isLocalStorageAvailable(): boolean {
try {
const testKey = '__test__';
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
} catch (e) {
return false;
}
}
}
export const catcherStorage = new CatcherStorage();