Skip to content

Instantly share code, notes, and snippets.

@honbin
Last active December 12, 2015 03:09
Show Gist options
  • Save honbin/4704712 to your computer and use it in GitHub Desktop.
Save honbin/4704712 to your computer and use it in GitHub Desktop.
var LocalStorage = LocalStorage || (function() {
var LOCAL_STORAGE_KEY = "key";
var connect = function() {
if (!localStorage.hasOwnProperty(LOCAL_STORAGE_KEY) ) {
var item = {};
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(item));
}
};
var getList = function() {
var item = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
return item[LOCAL_STORAGE_KEY];
};
var isValueKey = function(key) {
var item = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
return item.hasOwnProperty(key);
};
var getValue = function(key) {
var item = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
return item[key];
};
var setValue = function(key, val) {
var item = {};
item = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
item[key] = val;
return localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(item));
};
var deleteValue = function(key) {
var item = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
delete(item[key]);
return localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(item));
};
return {
connect : connect,
getList : getList,
isValueKey : isValueKey,
getValue : getValue,
setValue : setValue,
deleteValue : deleteValue
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment