Functional Weave
Code in Rust

dates.day-of-week@1.0.0

impl/typescript.ts

1,293 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 { epochDayFromIso } from "./dates_add_days.ts";  ← from dates.add-days ^1.0.0 · built alongside by fune

/**
 * The ISO 8601 day of the week: 1 = Monday through 7 = Sunday.
 *
 * ISO numbering rather than the 0 = Sunday that JavaScript's
 * Date.getDay() uses, because ISO is what the rest of the business world
 * means by "day 1" and because a weekend test becomes `day >= 6` instead of
 * `day === 0 || day === 6`.
 */
export function dayOfWeek(iso: string): number {
  // 1970-01-01, epoch day 0, was a Thursday. Shifting by 3 moves the origin of
  // the cycle to Monday so that the remainder is already ISO numbering.
  const epochDay = epochDayFromIso(iso);
  return ((((epochDay + 3) % 7) + 7) % 7) + 1;
}

/** True for Saturday and Sunday. */
export function isWeekend(iso: string): boolean {
  return dayOfWeek(iso) >= 6;
}

/** True for Monday to Friday. Says nothing about public holidays. */
export function isWeekday(iso: string): boolean {
  return dayOfWeek(iso) <= 5;
}

const NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

/** The English name, for logs and labels. Not localised, deliberately: a
 * capability that needed locale data would need a locale argument and a data
 * file to pin it. */
export function dayName(iso: string): string {
  return NAMES[dayOfWeek(iso) - 1];
}