All files iso-weeks-in-year.ts

100% Statements 22/22
100% Branches 12/12
100% Functions 1/1
100% Lines 22/22

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 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x                                             1x 138x 138x 138x 138x 138x 42x 21x 138x 138x 45x 93x 138x 138x 45x 93x 138x   138x 138x  
import { day, type DayOfWeek, daysPerWeek, month } from './date.ts';
import { isNumber } from './is-number.ts';
import { modulo } from './modulo.ts';
 
/**
 * Options for the {@link isoWeeksInYear} function
 * @group Time
 * @category Week
 */
export type ISOWeeksInYearOptions = {
  /** Use the utc timezone */
  utc?: boolean;
  /** Week 1 is defined as the week with the Gregorian year's first [weekOneIncludes] day in it */
  weekOneIncludes?: DayOfWeek;
};
 
/**
 * Determine the number of ISO weeks within a year
 * @param input - A date within the year, or a year number
 * @param options - see {@link ISOWeeksInYearOptions}
 * @defaultValue weekOneIncludes Thursday
 * @returns The number of weeks in the year (52 or 53)
 * @group Time
 * @category Week
 */
export function isoWeeksInYear(
  input: Date | number,
  { utc = false, weekOneIncludes = day.thursday }: ISOWeeksInYearOptions = {},
): number {
  const year =
    isNumber(input) ? input
    : utc ? input.getUTCFullYear()
    : input.getFullYear();
  const dow0 =
    utc ?
      new Date(Date.UTC(year, month.january, 1)).getUTCDay()
    : new Date(year, month.january, 1).getDay();
  const dow1 =
    utc ?
      new Date(Date.UTC(year, month.december, 31)).getUTCDay()
    : new Date(year, month.december, 31).getDay();
  const target = modulo(weekOneIncludes, daysPerWeek);
 
  return dow0 === target || dow1 === target ? 53 : 52;
}