update
This commit is contained in:
51
lib/storage-store.js
Normal file
51
lib/storage-store.js
Normal file
@@ -0,0 +1,51 @@
|
||||
export class StorageStore {
|
||||
storage = null;
|
||||
constructor(storage) {
|
||||
const defaultStorage = typeof window !== 'undefined' ? window.localStorage : null;
|
||||
this.storage = storage || defaultStorage;
|
||||
}
|
||||
getItem(key) {
|
||||
return this.storage.getItem(key);
|
||||
}
|
||||
setItem(key, value) {
|
||||
this.storage.setItem(key, value);
|
||||
}
|
||||
removeItem(key) {
|
||||
this.storage.removeItem(key);
|
||||
}
|
||||
clear() {
|
||||
this.storage.clear();
|
||||
}
|
||||
key(index) {
|
||||
return this.storage.key(index);
|
||||
}
|
||||
get length() {
|
||||
return this.storage.length;
|
||||
}
|
||||
getAllKeys() {
|
||||
const keys = [];
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
keys.push(this.key(i));
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
getAllItems() {
|
||||
const items = {};
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
const key = this.key(i);
|
||||
items[key] = this.getItem(key);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
getAllItemsAsArray() {
|
||||
const items = [];
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
const key = this.key(i);
|
||||
items.push({ key, value: this.getItem(key) });
|
||||
}
|
||||
return items;
|
||||
}
|
||||
hasItem(key) {
|
||||
return this.storage.getItem(key) !== null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user