Skip to content

Instantly share code, notes, and snippets.

@anna-sunberg
Last active February 24, 2020 08:38
Show Gist options
  • Save anna-sunberg/49cd3da6f3860826c6f1976ba4335aaf to your computer and use it in GitHub Desktop.
Save anna-sunberg/49cd3da6f3860826c6f1976ba4335aaf to your computer and use it in GitHub Desktop.
Aflred Reminders workflow
function run(argv) {
const string = argv.join(' ');
const INTERVAL_REGEX = /(\d+) ?([a-zA-Z]+)/;
const weekdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
const weekdaysLong = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
const getWeekdayIndex = (due) => {
if (due === 'next week') {
return 1;
}
const weekdayIndex = weekdays.indexOf(due);
if (weekdayIndex > -1) {
return weekdayIndex;
}
const weekdayLongIndex = weekdaysLong.indexOf(due);
if (weekdayLongIndex > -1) {
return weekdayLongIndex;
}
return null;
}
const createInterval = (value, unit) => {
const now = new Date();
switch (unit) {
case 'y':
case 'year':
case 'years':
now.setMonth(now.getMonth() + value * 12);
return { alldayDueDate: now };
case 'm':
case 'month':
case 'months':
now.setMonth(now.getMonth() + value);
return { alldayDueDate: now };
case 'w':
case 'week':
case 'weeks':
return { alldayDueDate: new Date(now.valueOf() + 1000 * 60 * 60 * 24 * 7 * value) };
case 'd':
case 'day':
case 'days':
return { alldayDueDate: new Date(now.valueOf() + 1000 * 60 * 60 * 24 * value) };
case 'h':
case 'hour':
case 'hours':
return { dueDate: new Date(now.valueOf() + 1000 * 60 * 60 * value) };
case 'min':
case 'minute':
case 'minutes':
return { dueDate: new Date(now.valueOf() + 1000 * 60 * 60 * value) };
default:
return { dueDate: now };
}
}
const getDueDate = (dueString = '') => {
const due = dueString.trim().toLowerCase();
const now = new Date();
if (due === 'now') {
return { dueDate: now };
}
if (due === 'tod' || due === 'today') {
return { alldayDueDate: new Date(now.getFullYear(), now.getMonth(), now.getDate()) };
}
if (due === 'tom' || due === 'tomorrow') {
return { alldayDueDate: new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1) };
}
const match = due.match(INTERVAL_REGEX);
if (match) {
const [m, value, unit] = match;
return createInterval(parseInt(value), unit.toLowerCase());
}
const weekdayIndex = getWeekdayIndex(due);
if (weekdayIndex !== null) {
const currentWeekday = now.getDay();
if (currentWeekday >= weekdayIndex) {
return new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 7 - currentWeekday + weekdayIndex
);
}
return { alldayDueDate: new Date(now.getFullYear(), now.getMonth(), now.getDate() - currentWeekday + weekdayIndex) };
}
if (dueString) {
return { dueDate: new Date(dueString) };
}
return null;
}
const [name, due] = string.split(',');
const dueDate = getDueDate(due);
const Reminders = Application('Reminders')
const reminder = Reminders.Reminder({ name, ...dueDate });
Reminders.defaultList().reminders.push(reminder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment