refactor(static): split schedule and weather to modules
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
export class DataStorage<T extends { id: string }> {
|
||||
constructor(private storageKey: string) {}
|
||||
|
||||
private readValues(): T[] {
|
||||
const objects = JSON.parse(window.localStorage.getItem(this.storageKey) || "[]");
|
||||
if (!Array.isArray(objects)) {
|
||||
return Object.values(objects);
|
||||
}
|
||||
return objects;
|
||||
}
|
||||
|
||||
private writeValues(values: T[]) {
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(values));
|
||||
}
|
||||
|
||||
getAll(): T[] {
|
||||
return this.readValues();
|
||||
}
|
||||
|
||||
add(value: T) {
|
||||
const values = this.readValues();
|
||||
for (const v of values) {
|
||||
if (v.id == value.id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
values.push(value);
|
||||
this.writeValues(values);
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
const values = this.readValues();
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
if (values[i].id == id) {
|
||||
values.splice(i, 1);
|
||||
this.writeValues(values);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user