1 | export class Storage { |
||
2 | constructor() { |
||
3 | this.namespace = 'bgControls'; |
||
4 | } |
||
5 | |||
6 | /** |
||
7 | * Get an item from local storage. |
||
8 | * |
||
9 | * @since 1.0.0 |
||
10 | * |
||
11 | * @param {string} key Items index. |
||
12 | * @return {any} Value of item. |
||
13 | */ |
||
14 | getItem( key ) { |
||
15 | const storage = this.getStorage(); |
||
16 | return storage[key]; |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * Set a namespaced item within local storage. |
||
21 | * |
||
22 | * @since 1.0.0 |
||
23 | * |
||
24 | * @param {string} key Key to save under. |
||
25 | * @param {string} value Value to save. |
||
26 | */ |
||
27 | setItem( key, value ) { |
||
28 | const storage = this.getStorage(); |
||
29 | storage[key] = value; |
||
30 | localStorage.setItem( this.namespace, JSON.stringify( storage ) ); |
||
0 ignored issues
–
show
|
|||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Set a namespaced item within local storage. |
||
35 | * |
||
36 | * @since 1.0.0 |
||
37 | * |
||
38 | * @param {string} key Key to delete. |
||
39 | */ |
||
40 | removeItem( key ) { |
||
41 | const storage = this.getStorage(); |
||
42 | delete storage[key]; |
||
43 | localStorage.setItem( this.namespace, JSON.stringify( storage ) ); |
||
0 ignored issues
–
show
The variable
localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Remove All localstorage items. |
||
48 | * |
||
49 | * @since 1.0.0 |
||
50 | */ |
||
51 | clear() { |
||
52 | localStorage.removeItem( this.namespace ); |
||
0 ignored issues
–
show
The variable
localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Get storage, cover. |
||
57 | * |
||
58 | * @since 1.0.0 |
||
59 | * |
||
60 | * @return {object} Get entire storage object. |
||
61 | */ |
||
62 | getStorage() { |
||
63 | const storage = localStorage.getItem( this.namespace ) || '{}'; |
||
0 ignored issues
–
show
The variable
localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
64 | return JSON.parse( storage ); |
||
65 | } |
||
66 | } |
||
67 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.