All files beginning-of-week.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 1/1
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x 1x                     1x 220x 220x 220x 220x 62x 62x 62x 62x 62x 62x 62x 62x   158x 158x 158x 158x 158x 158x  
import { type DateOptions, day, daysPerWeek } from './date.ts';
import { modulo } from './modulo.ts';
 
/**
 * Determine the start of the week for a date
 * @param input - The date
 * @param options - see {@link DateOptions}
 * @defaultValue utc false
 * @returns The date value for midnight on the first day of the specified week
 * @group Time
 * @category Week
 */
export function beginningOfWeek(
  input: Date,
  { utc = false, firstDayOfWeek = day.sunday }: DateOptions = {},
): Date {
  if (utc) {
    return new Date(
      Date.UTC(
        input.getUTCFullYear(),
        input.getUTCMonth(),
        input.getUTCDate() - modulo(input.getUTCDay() + daysPerWeek - firstDayOfWeek, daysPerWeek),
      ),
    );
  }
 
  return new Date(
    input.getFullYear(),
    input.getMonth(),
    input.getDate() - modulo(input.getDay() + daysPerWeek - firstDayOfWeek, daysPerWeek),
  );
}