/**
* @namespace DynamicEngine.plugins.Storage
*/
/**
* @exports Storage
*/
var StorageImpl = {
/**
* get localStorage value for key (as a string or number)
*
* @param {String} key the item key
* @return {String} item or null
*/
get: function(key) {
return localStorage.getItem(key);
},
/**
* get localStorage value for key as an Object
*
* @param {String} key the item key
* @return {Object} item or null|undefined
*/
getObject: function(key){
var rawValue = localStorage.getItem(key);
if(rawValue === "undefined"){
return undefined;
}
if(rawValue){
return JSON.parse(rawValue);
}
return rawValue;
},
/**
* save value in localStorage as a string
*
* @param {String} key the item key
* @param {String} value value to store
*/
set: function(key, value) {
localStorage.setItem(key, value);
},
/**
* save value in localStorage as an Object
*
* @param {String} key the item key
* @param {Object} value value to store
*/
setObject: function(key, value){
localStorage.setItem(key,JSON.stringify(value));
},
/**
* remove item from localStorage by key
*
* @param {String} key the item key
*/
remove: function(key) {
localStorage.removeItem(key);
},
/**
* Clear the localStorage
*/
clear: function() {
localStorage.clear();
},
/**
* get the number of items inside the localStorage
* @return {number} number of items
*/
getLength: function() {
return localStorage.length;
},
/**
* print all localStorage items (Debug purposes only)
*/
printAllData: function() {
for (var i = 0; i < localStorage.length; i++) {
console.log(localStorage.key(i) + ": " + localStorage.getItem(localStorage.key(i)));
}
}
};
module.exports = StorageImpl;