-
-
Notifications
You must be signed in to change notification settings - Fork 863
/
ConfigStorage.js
40 lines (39 loc) · 1.35 KB
/
ConfigStorage.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
'use strict';
// idea here is to abstract around the use of chrome.storage.local as it functions differently from "localStorage" and IndexedDB
// localStorage deals with strings, not objects, so the objects have been serialized.
const ConfigStorage = {
// key can be one string, or array of strings
get: function(key) {
let result = {};
if (Array.isArray(key)) {
key.forEach(function (element) {
try {
result = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
} catch (e) {
// is okay
}
});
} else {
const keyValue = window.localStorage.getItem(key);
if (keyValue) {
try {
result = JSON.parse(keyValue);
} catch (e) {
// It's fine if we fail that parse
}
}
}
return result;
},
// set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input) {
Object.keys(input).forEach(function (element) {
const tmpObj = {};
tmpObj[element] = input[element];
window.localStorage.setItem(element, JSON.stringify(tmpObj));
});
},
remove: function(item) {
window.localStorage.removeItem(item);
}
};