forked from tsironis/lockr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockr.js
194 lines (155 loc) · 4.4 KB
/
lockr.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
(function(root, factory) {
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = factory(root, exports);
}
} else if (typeof define === 'function' && define.amd) {
define(['exports'], function(exports) {
root.Lockr = factory(root, exports);
});
} else {
root.Lockr = factory(root, {});
}
}(this, function(root, Lockr) {
'use strict';
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
Lockr.prefix = "";
Lockr._getPrefixedKey = function(key, options) {
options = options || {};
if (options.noPrefix) {
return key;
} else {
return this.prefix + key;
}
};
Lockr.set = function (key, value, options) {
var query_key = this._getPrefixedKey(key, options);
try {
localStorage.setItem(query_key, JSON.stringify({"data": value}));
} catch (e) {
if (console) console.warn("Lockr didn't successfully save the '{"+ key +": "+ value +"}' pair, because the localStorage is full.");
}
};
Lockr.get = function (key, missing, options) {
var query_key = this._getPrefixedKey(key, options),
value;
try {
value = JSON.parse(localStorage.getItem(query_key));
} catch (e) {
if(localStorage[query_key]) {
value = {data: localStorage.getItem(query_key)};
} else{
value = null;
}
}
if(!value) {
return missing;
}
else if (typeof value === 'object' && typeof value.data !== 'undefined') {
return value.data;
}
};
Lockr.sadd = function(key, value, options) {
var query_key = this._getPrefixedKey(key, options),
json;
var values = Lockr.smembers(key);
if (values.indexOf(value) > -1) {
return null;
}
try {
values.push(value);
json = JSON.stringify({"data": values});
localStorage.setItem(query_key, json);
} catch (e) {
console.log(e);
if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full.");
}
};
Lockr.smembers = function(key, options) {
var query_key = this._getPrefixedKey(key, options),
value;
try {
value = JSON.parse(localStorage.getItem(query_key));
} catch (e) {
value = null;
}
return (value && value.data) ? value.data : [];
};
Lockr.sismember = function(key, value, options) {
return Lockr.smembers(key).indexOf(value) > -1;
};
Lockr.keys = function() {
var keys = [];
var allKeys = Object.keys(localStorage);
if (Lockr.prefix.length === 0) {
return allKeys;
}
allKeys.forEach(function (key) {
if (key.indexOf(Lockr.prefix) !== -1) {
keys.push(key.replace(Lockr.prefix, ''));
}
});
return keys;
};
Lockr.getAll = function (includeKeys) {
var keys = Lockr.keys();
if (includeKeys) {
return keys.reduce(function (accum, key) {
var tempObj = {};
tempObj[key] = Lockr.get(key);
accum.push(tempObj);
return accum;
}, []);
}
return keys.map(function (key) {
return Lockr.get(key);
});
};
Lockr.srem = function(key, value, options) {
var query_key = this._getPrefixedKey(key, options),
json,
index;
var values = Lockr.smembers(key, value);
index = values.indexOf(value);
if (index > -1)
values.splice(index, 1);
json = JSON.stringify({"data": values});
try {
localStorage.setItem(query_key, json);
} catch (e) {
if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key);
}
};
Lockr.rm = function (key) {
var queryKey = this._getPrefixedKey(key);
localStorage.removeItem(queryKey);
};
Lockr.flush = function () {
if (Lockr.prefix.length) {
Lockr.keys().forEach(function(key) {
localStorage.removeItem(Lockr._getPrefixedKey(key));
});
} else {
localStorage.clear();
}
};
return Lockr;
}));