-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.45 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
import ajax from 'bet-ajax';
import seed from 'bet-seed';
import Logger from 'bet-logger';
import helper from 'bet-helper';
const log = new Logger(`BET:api-chrome:${seed}`);
let chromeApi;
export default chromeApi = {
localStorage: {
set: function (key, value) {
log('localStorage set key: %s', key);
let __value;
if(typeof value !== 'string') {
__value = helper.stringifyJson(value);
} else {
__value = value;
}
return localStorage[key] = __value;
},
get: function (key) {
log('localStorage get key: %s', key);
return localStorage[key];
},
clear: function(save) {
log('localStorage clear all, save: ', save);
if (save) {
if (typeof save === 'string') {
log('localStorage clear, but saved: %s', save);
let hash = {};
hash[save] = this.get(save);
let saved = [hash];
}
if (Array.isArray(save)) {
let saved = save.map((key) => {
log('localStorage clear, but saved: %s', key);
let hash = {};
hash[key] = this.get(key);
return hash;
});
}
// TODO: add save method by regexp
}
localStorage.clear();
if (saved) {
saved.forEach((hash) => {
for(let key in hash) {
this.set(key, hash[key]);
}
});
}
}
},
ajax: ajax
}
export let ls = chromeApi.localStorage;