freeCodeCamp/client/commonFramework/code-storage.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-11-18 05:25:16 +00:00
// depends on: codeUri
window.common = (function(global) {
const {
localStorage,
common = { init: [] }
} = global;
var codeStorage = {
getStoredValue(key) {
if (
!localStorage ||
typeof localStorage.getItem !== 'function' ||
!key ||
typeof key !== 'string'
) {
2015-12-06 09:17:19 +00:00
console.log('unable to read from storage');
return '';
}
2015-11-18 05:25:16 +00:00
return '' + localStorage.getItem(key + 'Val');
},
isAlive: function(key) {
var val = this.getStoredValue(key);
2015-11-18 05:25:16 +00:00
return val !== 'null' &&
val !== 'undefined' &&
(val && val.length > 0);
},
updateStorage(key, code) {
if (
!localStorage ||
typeof localStorage.setItem !== 'function' ||
2015-11-18 05:25:16 +00:00
!key ||
typeof key !== 'string'
) {
console.log('unable to save to storage');
return code;
}
localStorage.setItem(key + 'Val', code);
return code;
2015-11-18 05:25:16 +00:00
}
};
common.codeStorage = codeStorage;
return common;
}(window, window.common));