<commit_before>import { toUTCDate } from 'nodemod/dist/calendar/helpers/to-utc-date.js';

import type { MaybeDate } from './typings.js';

export function toResolvedDate(date?: MaybeDate): Date {
  /**
   * NOTE: Only allow undefined `date` so that calling function without any parameter will
   * always return today's date. Return `Invalid Date` object for all falsy values.
   *
   * Chrome returns valid date for new Date('0') while Firefox returns `Invalid Date`.
   * There's no problem parsing a number in both platforms. Try to parse input as number and
   * use that to construct date before proceeding to use original value.
   */
  const tryDate =
    typeof date === 'string' && !Number.isNaN(Number(date)) ?
      Number(date) :
      date;
  const dateDate = tryDate === undefined ? new Date() : new Date(tryDate || NaN);
  const isUTCDateFormat = typeof tryDate === 'string' && (
    /^\d{4}-\d{2}-\d{2}$/.test(tryDate) ||
    /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(?:Z|\+00:00|-00:00)$/.test(tryDate));
  const isUnixTimestamp = typeof tryDate === 'number' &&
    tryDate > 0 &&
    isFinite(tryDate);

  let fy = dateDate.getFullYear();
  let m = dateDate.getMonth();
  let d = dateDate.getDate();

  /**
   * NOTE: Depends on the input date string, browser will interpret the Date object differently.
   * For instance, a simple date string `2020-01-03` will default to UTC timezone. In order to get
   * the correct expected date that is `3`, `.getUTCDate` is required as `.getDate` will return a
   * date value that is based on the local timezone after the date conversion by the browser. In PST
   * timezone, that will return `2`.
   *
   * ```ts
   * // In PST (UTC-08:00) timezone, the following code will output:
   * const dateString = '2020-01-03';
   * const dateDate = new Date(dateString); // UTC time is '2020-01-03T00:00:00.000+08:00'
   *
   * dateDate.getUTCDate(); // 3
   * dateDate.getDate(); // 2
   * ```
   */
  if (isUTCDateFormat || isUnixTimestamp) {
    fy = dateDate.getUTCFullYear();
    m = dateDate.getUTCMonth();
    d = dateDate.getUTCDate();<commit_msg>test: ignore LoC that causes code coverage to fail sometimes<commit_after>import { toUTCDate } from 'nodemod/dist/calendar/helpers/to-utc-date.js';

import type { MaybeDate } from './typings.js';

export function toResolvedDate(date?: MaybeDate): Date {
  /**
   * NOTE: Only allow undefined `date` so that calling function without any parameter will
   * always return today's date. Return `Invalid Date` object for all falsy values.
   *
   * Chrome returns valid date for new Date('0') while Firefox returns `Invalid Date`.
   * There's no problem parsing a number in both platforms. Try to parse input as number and
   * use that to construct date before proceeding to use original value.
   */
  const tryDate =
    typeof date === 'string' && !Number.isNaN(Number(date)) ?
      Number(date) :
      date;
  /**
   * FIXME(motss): Temporarily disabling the code coverage for this line as it is caused by
   * `wtr` not being able to generate the correct coverage report for this line which might be
   * caused by wrong sourcemap generated during the tests. But it is 100% covered by all the
   * written tests.
   */
  /* c8 ignore start */
  const dateDate = tryDate === undefined ?
  /* c8 ignore stop */
    new Date() :
    new Date(tryDate || NaN);
  const isUTCDateFormat = typeof tryDate === 'string' && (
    /^\d{4}-\d{2}-\d{2}$/.test(tryDate) ||
    /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(?:Z|\+00:00|-00:00)$/.test(tryDate));
  const isUnixTimestamp = typeof tryDate === 'number' &&
    tryDate > 0 &&
    isFinite(tryDate);

  let fy = dateDate.getFullYear();
  let m = dateDate.getMonth();
  let d = dateDate.getDate();

  /**
   * NOTE: Depends on the input date string, browser will interpret the Date object differently.
   * For instance, a simple date string `2020-01-03` will default to UTC timezone. In order to get
   * the correct expected date that is `3`, `.getUTCDate` is required as `.getDate` will return a
   * date value that is based on the local timezone after the date conversion by the browser. In PST
   * timezone, that will return `2`.
   *
   * ```ts
   * // In PST (UTC-08:00) timezone, the following code will output:
   * const dateString = '2020-01-03';
   * const dateDate = new Date(dateString); // UTC time is '2020-01-03T00:00:00.000+08:00'
   *
   * dateDate.getUTCDate(); // 3
   * dateDate.getDate(); // 2
   * ```
   */
  if (isUTCDateFormat || isUnixTimestamp) {
    fy = dateDate.getUTCFullYear();
    m = dateDate.getUTCMonth();
    d = dateDate.getUTCDate();
{'fraction_non_alphanumeric': 0.1368148467846353, 'fraction_numerical': 0.02913249892101856, 'mean_word_length': 3.232876712328767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4869219', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1477, 'n_words': 504}