storage-editor/lib/storage-store.js
2025-07-16 02:09:42 +08:00

52 lines
1.1 KiB
JavaScript

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;
}
}