-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Storage.ts
66 lines (59 loc) · 1.23 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
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
import * as PropertySymbol from '../PropertySymbol.js';
/**
* Storage.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Storage
*/
export default class Storage {
public [PropertySymbol.data]: { [key: string]: string } = {};
/**
* Returns length.
*
* @returns Length.
*/
public get length(): number {
return Object.keys(this[PropertySymbol.data]).length;
}
/**
* Returns name of the nth key.
*
* @param index Index.
* @returns Name.
*/
public key(index: number): string | null {
const name = Object.keys(this[PropertySymbol.data])[index];
return name !== undefined ? name : null;
}
/**
* Sets item.
*
* @param name Name.
* @param item Item.
*/
public setItem(name: string, item: string): void {
this[PropertySymbol.data][name] = String(item);
}
/**
* Returns item.
*
* @param name Name.
* @returns Item.
*/
public getItem(name: string): string | null {
return this[PropertySymbol.data][name] !== undefined ? this[PropertySymbol.data][name] : null;
}
/**
* Removes item.
*
* @param name Name.
*/
public removeItem(name: string): void {
delete this[PropertySymbol.data][name];
}
/**
* Clears storage.
*/
public clear(): void {
this[PropertySymbol.data] = {};
}
}