generated from tpl/obsidian-sample-plugin
Upsert reading log entries to have one per day per book
This commit is contained in:
parent
7706544710
commit
a7276211d4
|
@ -8,6 +8,14 @@ export interface ReadingLogEntry {
|
|||
createdAt: Date;
|
||||
}
|
||||
|
||||
function isSameDay(a: Date, b: Date): boolean {
|
||||
return (
|
||||
a.getFullYear() === b.getFullYear() &&
|
||||
a.getMonth() === b.getMonth() &&
|
||||
a.getDate() === b.getDate()
|
||||
);
|
||||
}
|
||||
|
||||
export class ReadingLog {
|
||||
private entries: ReadingLogEntry[] = [];
|
||||
|
||||
|
@ -77,7 +85,12 @@ export class ReadingLog {
|
|||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
await this.addRawEntry(newEntry);
|
||||
if (lastEntry && isSameDay(lastEntry.createdAt, newEntry.createdAt)) {
|
||||
newEntry.pagesRead += lastEntry.pagesRead;
|
||||
await this.updateEntry(this.entries.indexOf(lastEntry), lastEntry);
|
||||
} else {
|
||||
await this.addRawEntry(newEntry);
|
||||
}
|
||||
}
|
||||
|
||||
public async addRawEntry(entry: ReadingLogEntry) {
|
||||
|
|
Loading…
Reference in New Issue