Functional Weave
Code in Rust

todo.reschedule-recurrence@1.0.0

impl/typescript.ts

1,015 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { addDays } from "./dates_add_days.ts";  ← from dates.add-days ^1.0.0 · built alongside by fune
import { type Recurrence } from "./todo_item.ts";  ← from todo.item ^1.0.0 · built alongside by fune

/**
 * The repeat a todo keeps when its due date is edited. Every occurrence is
 * counted from the anchor, so moving the date moves the series: the new date
 * becomes the anchor. An unchanged date keeps the anchor, which matters once
 * a series has rolled forward (monthly from 31 January is due 28 February,
 * and re-anchoring there would lose the month end).
 */
export function rescheduleRecurrence(recurrence: Recurrence | null, fromDue: string | null, toDue: string | null): Recurrence | null {
  // addDays(x, 0) is the date check: it throws dates.add-days's message.
  if (fromDue !== null) addDays(fromDue, 0);
  if (toDue !== null) addDays(toDue, 0);
  if (recurrence === null) return null;
  addDays(recurrence.anchor, 0);
  // No date: todo.item's validateDraft says a repeating todo needs one.
  if (toDue === null || toDue === fromDue) return recurrence;
  return { ...recurrence, anchor: toDue };
}