-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStorageObjectsPolyfill.js
More file actions
116 lines (98 loc) · 3.16 KB
/
StorageObjectsPolyfill.js
File metadata and controls
116 lines (98 loc) · 3.16 KB
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
/**
*
* MOVED TO: https://github.com/iFind/html5MultidimensionalStorage
*
* This methods extends the default HTML5 Storage object and add support
* to set and get multidimensional data
*
* @example Storage.setObj('users.albums.sexPistols',"blah");
* @example Storage.setObj('users.albums.sexPistols',{ sid : "My Way", nancy : "Bitch" });
* @example Storage.setObj('users.albums.sexPistols.sid',"Other songs");
*
* @example Storage.getObj('users');
* @example Storage.getObj('users.albums');
* @example Storage.getObj('users.albums.sexPistols');
* @example Storage.getObj('users.albums.sexPistols.sid');
* @example Storage.getObj('users.albums.sexPistols.nancy');
*
* This is just a prototype and is not recommended to use at production apps
* USE AT YOUR OWN RISK
*
* @author Klederson Bueno <klederson@klederson.com>
* @author Gabor Zsoter <helo@zsitro.com>
*/
//Add Storage support for objects
Storage.prototype.__walker = function(path,o) {
//Validate if path is an object otherwise returns false
if(typeof path !== "object")
return undefined;
if(path.length === 0){
return o;
}
for(var i in path){
var prop = path[i];
//Check if path step exists
if(o.hasOwnProperty(prop)){
var val = o[prop];
if(typeof val == 'object'){
path.splice(0,1);
return this.__walker(path,val);
} else {
return val;
}
}
}
};
Storage.prototype.setObj = function(key, value) {
var path = key.split('.');
//First level is always the localStorage key pair item
var _key = path[0];
var os = this.getItem(_key) !== null ? JSON.parse(this.getItem(_key)) : null; //general storage key pair element
path.splice(0,1);
if(os === null) {
os = {};
this.setItem(_key,JSON.stringify(os));
}
var innerWalker = function(path,o) {
//Validate if path is an object otherwise returns false
if(typeof path !== "object")
return undefined;
if(path.length == 1) {
o[path[0]] = value;
return o;
} else if(path.length === 0) {
os = value;
return os;
}
var val = null;
for(var i in path){
var prop = path[i];
//Check if path step exists
if(o.hasOwnProperty(prop)) {
val = o[prop];
if(typeof val == 'object'){
path.splice(0,1);
return innerWalker(path,val);
}
} else {
//create depth
o[prop] = {};
val = o[prop];
path.splice(0,1);
return innerWalker(path,val);
}
}
};
innerWalker(path,os);
this.setItem(_key,JSON.stringify(os));
};
Storage.prototype.getObj = function(key) {
key = key.split('.');
//First level is always the localStorage key pair item
var _key = key[0];
var o = this.getItem(_key) ? JSON.parse(this.getItem(_key)) : null;
if(o === null)
return undefined;
key.splice(0,1);
return this.__walker(key,o);
};