-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextendStorage.js
38 lines (33 loc) · 989 Bytes
/
extendStorage.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
/*
Wonder how this works?
Storage is the Prototype of both localStorage and sessionStorage.
Got it?
*/
(function() {
'use strict';
Storage.prototype.set = function(key, obj) {
var t = typeof obj;
if (t==='undefined' || obj===null ) this.removeItem(key);
this.setItem(key, (t==='object')?JSON.stringify(obj):obj);
return obj;
};
Storage.prototype.get = function(key) {
var obj = this.getItem(key);
try {
var j = JSON.parse(obj);
if (j && typeof j === "object") return j;
} catch (e) { }
return obj;
};
Storage.prototype.assign = function(key, obj_merge) {
var obj = this.get(key);
if (typeof obj !== "object" || typeof obj_merge !== "object") return null;
Object.assign(obj, obj_merge);
return this.set(key,obj);
};
Storage.prototype.has = window.hasOwnProperty;
Storage.prototype.remove = window.removeItem;
Storage.prototype.keys = function(){
return Object.keys(this.valueOf());
};
})();