Created
January 5, 2018 19:52
-
-
Save evsar3/709d933a59874ef15a13f54eee5b3ec8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalStorage = { | |
preffix: "", | |
set: function (key, value, encode) { | |
key = this.preffix + key; | |
this.preffix = ""; | |
if (encode) { | |
key = LocalStorage.encode(key); | |
value = LocalStorage.encode(value); | |
} | |
localStorage.setItem(key, value); | |
}, | |
get: function (key, defaultValue) { | |
key = this.preffix + key; | |
defaultValue = defaultValue || ""; | |
this.preffix = ""; | |
var value = localStorage.getItem(key); | |
if (value === null) { | |
value = localStorage.getItem(this.encode(key)); | |
if (value === null) { | |
return defaultValue; | |
} else { | |
return this.decode(value); | |
} | |
} else { | |
return value; | |
} | |
}, | |
remove: function (key) { | |
key = this.preffix + key; | |
this.preffix = ""; | |
localStorage.removeItem(key); | |
localStorage.removeItem(this.encode(key)); | |
}, | |
exists: function (key) { | |
key = this.preffix + key; | |
this.preffix = ""; | |
var value = localStorage.getItem(key); | |
if (value === null) { | |
value = localStorage.getItem(this.encode(key)); | |
if (value === null) { | |
return false; | |
} else { | |
return true; | |
} | |
} else { | |
return true; | |
} | |
}, | |
profile: function (name) { | |
this.preffix = name + "_"; | |
return this; | |
}, | |
encode: function (data) { | |
return btoa(unescape(encodeURIComponent(data))); | |
}, | |
decode: function (data) { | |
return decodeURIComponent(escape(atob(data))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment