All files / random date-day.ts

100% Statements 13/13
100% Branches 15/15
100% Functions 2/2
100% Lines 13/13

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 342x   2x 2x         2x 8x 8x   8x   8x     2x 8x               2x   3x   3x      
import { isLeapYear } from '../assert/is-leap-year';
 
import { randomMonth } from './date-month';
import { randomNumber } from './number';
 
/**
 * Returns a random day for a month. 1=january, etc.
 */
export function randomDay(month?: number, year?: number): number {
  const currentMonth = month ? month : randomMonth();
  const leapYear = !!year && isLeapYear(year);
 
  const maxDay = calculateMaxDayForMonth(currentMonth, leapYear);
 
  return randomNumber(1, maxDay);
}
 
export function calculateMaxDayForMonth(month: number, isLeapYear: boolean): number {
  switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31;
    case 2:
      return isLeapYear ? 29 : 28;
    default:
      return 30;
  }
}