@mewters/smart-storage
Version:
A JavaScript/TypeScript library designed to simplify working with localStorage, sessionStorage, and cookies by offering automatic object conversion and consistent API.
38 lines (37 loc) • 916 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseStorage = void 0;
class BaseStorage {
constructor(storage) {
this.storage = storage;
this.storage = storage;
}
get(key, defaultValue) {
const value = this.storage.getItem(key);
if (value === null || value === undefined) {
return defaultValue;
}
try {
return JSON.parse(value);
}
catch (error) {
return value;
}
}
set(key, value) {
if (typeof value !== 'string') {
value = JSON.stringify(value);
}
this.storage.setItem(key, value);
}
remove(key) {
this.storage.removeItem(key);
}
removeItems(keys) {
keys.forEach(key => this.remove(key));
}
clear() {
this.storage.clear();
}
}
exports.BaseStorage = BaseStorage;