Skip to content

Instantly share code, notes, and snippets.

@rgruesbeck
Created September 27, 2019 02:49
Show Gist options
  • Save rgruesbeck/e490b7b87046d1a88cd021ad2af78083 to your computer and use it in GitHub Desktop.
Save rgruesbeck/e490b7b87046d1a88cd021ad2af78083 to your computer and use it in GitHub Desktop.
localStorage wrapper for KaiOS
class Storage {
constructor(name) {
this.name = name;
this.read();
}
// return data
list() {
return this.read();
}
// add an item
add(item) {
this.data.push({
id: Math.random().toString(16).slice(2),
date: new Date().toISOString(),
data: item
})
return this.write();
}
// update item
update(id, cb) {
let item = this.data.find(itm => itm.id === id)
// ignore invalid ids
if (!item) { return; }
item.data = {
...item.data,
...cb(item.data)
};
return this.write();
}
// remove item by id
remove(id) {
this.data = this.data
.filter(item => {
return item.id !== id
});
return this.write();
}
// destroy all items
destroy() {
this.data = [];
localStorage.clear();
}
// read and write to localStorage
read() {
this.data = JSON.parse(localStorage.getItem(this.name)) || [];
return [...this.data];
}
write() {
localStorage.setItem(this.name, JSON.stringify(this.data));
return this.read();
}
}
module.exports.default = Storage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment