Compare commits
2 Commits
3b7307dac3
...
d331866f46
| Author | SHA1 | Date | |
|---|---|---|---|
|
d331866f46
|
|||
|
3919eed679
|
@@ -7,5 +7,5 @@ buildNpmPackage {
|
||||
pname = "obsidian-tasks-reminder";
|
||||
version = "0.0.1";
|
||||
src = ./.;
|
||||
npmDepsHash = "sha256-rhwGWuw/X7YGdPwIrgkIGukFNpAwpx/a6H3D/2cVP5Y=";
|
||||
npmDepsHash = "sha256-d8uZWYmroWoju976WXnCaYX+0uTLK/tc6hS/WgEHv/o=";
|
||||
}
|
||||
|
||||
17
src/database/deserializer.ts
Normal file
17
src/database/deserializer.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import fs from "fs";
|
||||
import { NotificationDatabase } from "../types/notification";
|
||||
|
||||
/**
|
||||
* Loads and deserializes database from JSON formatted file.
|
||||
*/
|
||||
export function loadDatabase(file: string): NotificationDatabase {
|
||||
const text = fs.readFileSync(file).toString();
|
||||
return deserializeDatabase(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes database from JSON format.
|
||||
*/
|
||||
export function deserializeDatabase(json: string): NotificationDatabase {
|
||||
return JSON.parse(json) as NotificationDatabase;
|
||||
}
|
||||
2
src/database/index.ts
Normal file
2
src/database/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { serializeDatabase } from "./serializer";
|
||||
export { deserializeDatabase } from "./deserializer";
|
||||
34
src/database/serializer.ts
Normal file
34
src/database/serializer.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import fs from "fs";
|
||||
import { Task } from "../types/task";
|
||||
import { Notification, NotificationDatabase } from "../types/notification";
|
||||
|
||||
/**
|
||||
* Applies the mapper for each task from list, groups them by time and dumps the data into JSON formatted file.
|
||||
*/
|
||||
export function dumpDatabase(file: string, tasks: Task[], mapper: (task: Task) => Notification[]) {
|
||||
const data = serializeDatabase(tasks, mapper);
|
||||
fs.writeFileSync(file, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the mapper for each task from list, groups them by time and serializes into JSON format.
|
||||
*/
|
||||
export function serializeDatabase(tasks: Task[], mapper: (task: Task) => Notification[]): string {
|
||||
const output = tasks.flatMap(wrapWithTimeFiller(mapper)).reduce((acc, n) => {
|
||||
if (n.time) {
|
||||
(acc[n.time] = (acc[n.time] || [])).push(n);
|
||||
};
|
||||
|
||||
return acc;
|
||||
}, {} as NotificationDatabase);
|
||||
|
||||
return JSON.stringify(output);
|
||||
}
|
||||
|
||||
function wrapWithTimeFiller(mapper: (task: Task) => Notification[]): (task: Task) => Notification[] {
|
||||
return (task: Task) => mapper(task)
|
||||
.map(notification => ({
|
||||
...notification,
|
||||
time: task.reminder ?? notification.time,
|
||||
}));
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import dayjs from "dayjs";
|
||||
import { createInterface } from "readline";
|
||||
import { parse } from "../generated/grammar/task";
|
||||
import { Task as DefaultTask } from "../model";
|
||||
import { ParseResult } from "../types/grammar/task";
|
||||
import { ParseResult } from "../types/grammar";
|
||||
import { Task, TaskPriority } from "../types/task";
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import dayjs, { Dayjs } from "dayjs";
|
||||
import { ParsedTask, ParsedTaskDate, ParsedTaskDependency, ParsedTaskMeta } from "../types/grammar/task";
|
||||
import { ParsedTask, ParsedTaskDate, ParsedTaskDependency, ParsedTaskMeta } from "../types/grammar";
|
||||
import { Task, TaskPriority } from "../types/task";
|
||||
|
||||
export class LazyTask implements Task {
|
||||
|
||||
9
src/types/notification.ts
Normal file
9
src/types/notification.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import dayjs, { Dayjs } from "dayjs"
|
||||
|
||||
export type Notification = {
|
||||
time?: string;
|
||||
title: string;
|
||||
text: string;
|
||||
};
|
||||
|
||||
export type NotificationDatabase = Record<string, Notification[]>;
|
||||
Reference in New Issue
Block a user