-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (85 loc) · 2.32 KB
/
index.js
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
/**
* Class Storage manages saving & retrieving data.
* Can be further extended to suit your needs better.
*
* @author Curveball <[email protected]>
* @license MIT
*/
class Storage {
/**
* Creates a new Storage. Optional object `options` can be passed in. Its properties will be merged into default properties via `Object.assign()`.
* @param {Object.<string, *>} [options] object with options (key/value pairs).
* @property {object} settings the storage settings.
* @property {string} settings.type the default storage type - 'sessionStorage'.
* @property {object} dataObject fallback where the data is kept if storage is not available.
* @property {object} storage link to the corresponding window storage object.
* @this Storage
*/
constructor(options) {
let settings = {
type: 'sessionStorage'
};
this.settings = Object.assign(settings, options);
this.dataObject = {};
this.storage = window[settings.type];
}
/**
* Checks whether the storage available.
* @returns {boolean}
*/
isAvailable() {
try {
let x = '__storage_test__';
this.storage.setItem(x, x);
this.storage.removeItem(x);
return true;
} catch(e) {
return false;
}
}
/**
* Saves the data.
* @param {string} name storage item name.
* @param {object} data data to be stored.
* @returns {boolean|string} returns false or storage item name.
*/
save(name, data) {
let dataStr = JSON.stringify(data);
if(!this.isAvailable()) {
this.dataObject[name] = dataStr;
return false;
}
try {
this.storage.setItem(name, dataStr);
return name;
} catch(e) {
this.dataObject[name] = dataStr;
return false;
}
}
/**
* Returns the requested data.
* @param {string} name storage item name.
* @returns {object} returns requested data.
*/
restore(name) {
let restoredData = this.dataObject[name] ? this.dataObject[name] : this.storage.getItem(name);
return JSON.parse(restoredData);
}
/**
* Removes indicated storage item.
* @param {string} name storage item name.
* @returns {undefined}
*/
remove(name) {
this.storage.removeItem(name);
}
/**
* Clears all the data.
* @returns {undefined}
*/
clear() {
this.storage.clear();
}
}
module.exports = Storage;