-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbredis.js
92 lines (73 loc) · 2.08 KB
/
bredis.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
let bredis = {
db : localStorage,
prefix : 'bredis-',
_arguments : function (arg){
},
init : function(){
console.log('Browser Redis initialized')
},
use : function (prefix){
this.prefix = prefix;
console.log('Prefix changed to::', prefix)
},
set : function (){
var self = this;
var _set = function (key, value){
var str_value = value;
if (typeof value != 'string'){
try{
str_value = JSON.stringify(value);
} catch (e){
str_value = String(value)
}
}
key = self.prefix + key
self.db.setItem(key, str_value)
}
if (arguments.length === 0){
throw 'Invalid parameters provided to set function'
return
}
if (arguments.length === 1){
if (Array.isArray( arguments[0] )){
//Process multi insert
if (arguments[0].length % 2 === 0){
for (var i = 0; i < arguments[0].length; i += 2){
_set(arguments[0][i], arguments[0][i + 1])
}
} else {
throw 'Invalid length for array push'
}
} else {
throw 'Invalid length for array push'
}
return
}
if (arguments.length === 2){
_set(arguments[0], arguments[1])
}
},
get : function (key){
var self = this;
if (Array.isArray(key)){
var out = [];
key.forEach(function(k){
out.push(self.get(k))
})
return out;
} else {
var out = self.db.getItem(self.prefix + key)
try{
return JSON.parse(out);
} catch (e){
return out;
}
}
},
remove : function (key){
var self = this;
return self.db.removeItem(self.prefix + key)
},
}
bredis.init();
window.bredis = bredis;