Created
March 23, 2017 11:33
-
-
Save swapnil-webonise/3f79724a156d980a11ea546b11f7d889 to your computer and use it in GitHub Desktop.
Browser Storage - Handling localstorage, sessionStorage, cookie and alternativeStorage if storage not available
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
function Storage() { | |
} | |
function SessionStorageProvider() { | |
try { | |
sessionStorage.setItem("storage", ""); | |
sessionStorage.removeItem("storage"); | |
this.storage = sessionStorage; | |
} catch (err) { | |
this.storage = new LocalStorageAlternative(); | |
} | |
} | |
function LocalStorageProvider() { | |
try { | |
localStorage.setItem("storage", ""); | |
localStorage.removeItem("storage"); | |
this.storage = localStorage; | |
} catch (err) { | |
this.storage = new LocalStorageAlternative(); | |
} | |
} | |
function CookieStorageProvider() { | |
} | |
function LocalStorageAlternative() { | |
var structureLocalStorage = {}; | |
this.setItem = function (key, value) { | |
structureLocalStorage[key] = value; | |
} | |
this.getItem = function (key) { | |
if(typeof structureLocalStorage[key] != 'undefined' ) { | |
return structureLocalStorage[key]; | |
} | |
else { | |
return null; | |
} | |
} | |
this.removeItem = function (key) { | |
structureLocalStorage[key] = undefined; | |
} | |
} | |
Storage.prototype.set = function (key, value) { | |
if(_.isEmpty(key)) { | |
throw new Error('Key or value missing while storing to storage'); | |
} | |
this.storage.setItem(key, JSON.stringify(value)); | |
}; | |
Storage.prototype.get = function(key) { | |
if(_.isEmpty(key)) { | |
throw new Error('Key missing while reading from storage'); | |
} | |
return JSON.parse(this.storage.getItem(key)) | |
}; | |
Storage.prototype.clear = function() { | |
this.storage.clear(); | |
}; | |
Storage.prototype.removeItem = function(key) { | |
if(_.isEmpty(key)) { | |
throw new Error('Key missing while deleting from storage'); | |
} | |
this.storage.removeItem(key); | |
}; | |
CookieStorageProvider.prototype.get = function(key) { | |
if(!key || !_.isString(key)) { | |
return; | |
} | |
var allCookies = document.cookie.split(';'); | |
var resultCookies = _.chain(allCookies).map(function(cookie) { | |
var cookieObj = {}; | |
var split = _.map(cookie.split('='), function(data) { | |
return data.trim(); | |
}); | |
if(_.first(split) === key) { | |
return JSON.parse(split[1] || ''); | |
} | |
return false; | |
}).compact().value(); | |
return resultCookies.length ? _.first(resultCookies) : null; | |
}; | |
CookieStorageProvider.prototype.set = function(key, value) { | |
var cookie = key.toString() + '=' + JSON.stringify(value); | |
document.cookie = cookie; | |
}; | |
CookieStorageProvider.prototype.removeItem = function(key) { | |
if(!key) { | |
return; | |
} | |
//setting previous time to expires will delete cookie. | |
document.cookie = key + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
}; | |
SessionStorageProvider.prototype = Object.create(Storage.prototype); | |
SessionStorageProvider.prototype.constructor = SessionStorageProvider; | |
LocalStorageProvider.prototype = Object.create(Storage.prototype); | |
LocalStorageProvider.prototype.constructor = LocalStorageProvider; | |
var storage = { | |
sessionStorageProvider: new SessionStorageProvider(), | |
localStorageProvider: new LocalStorageProvider(), | |
cookieStorageProvider: new CookieStorageProvider() | |
}; | |
export default storage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment