@w6s/simple-storage
Version:
Base on Local Storage and Session Storage.
45 lines (36 loc) • 852 B
JavaScript
class Storage {
constructor(type) {
if (typeof window !== 'undefined') {
this.storage = window[type];
}
}
add(key, value, parse) {
this.storage.setItem(key, parse ? JSON.stringify(value) : value);
}
get(key, parse) {
const value = this.storage.getItem(key);
if (value === null) return false;
if (value && parse) return JSON.parse(value);
if (value) return value;
}
has(key) {
const value = this.storage.getItem(key);
if (value === null || value == undefined) {
return false;
}
return true;
}
del(key) {
this.storage.removeItem(key);
}
clear() {
this.storage.clear();
}
}
const types = ['localStorage', 'sessionStorage'];
const localStorage = new Storage(types[0]);
const sessionStorage = new Storage(types[1]);
export {
localStorage,
sessionStorage
};