1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { tableify } from "https://esm.town/v/stevekrouse/tableify";
import { email } from "https://esm.town/v/std/email?v=9";
// Customizable notifications for customizable content for a customizable new criteria
// Example usage: @stevekrouse.dateMeNotify
export function notifyNewBy<A>({ getData, isNew, notify, name }: {
getData: (d?: Date) => A[] | Promise<A[]>;
isNew: (params: {
data: A;
lastRunAt: Date;
}) => boolean;
notify?: (data: A[]) => void;
name?: string;
}) {
return async function ({ lastRunAt }: Interval) {
const data = await getData(lastRunAt);
let newData = data.filter((data) => isNew({ data, lastRunAt }));
if (newData.length)
notify ? notify(newData) : await email({
html: await tableify(newData),
subject: `New ${name ?? "Data"}`,
});
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023