public
Last active

Adding Multidimensional Support for javascript / html5 sessionStorage , localStorage or just Storage object

  • Download Gist
HTML5StorageMultidimensional.js
JavaScript
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
/**
*
* 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>
*/
//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 false;
if(path.length == 0)
return o;
for(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 = JSON.parse(this.getItem(_key)); //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 false;
if(path.length == 1) {
o[path[0]] = value;
return o;
}
var val = null;
for(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 = JSON.parse(this.getItem(_key));
if(o == null)
return false;
 
key.splice(0,1);
return this.__walker(key,o);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.