<commit_before>// contains some helper functions for date manipulation/formatting
// all functions take in a valid ISO 8601 date string or a Date object

import { format, isValid, parseISO } from 'date-fns';


// this exists because unlike moment-js, date-fns doesn't have a generalized toDate function
/**
 * @param  {Date|string} date
 * @param {boolean} suppressError
*/
export const toDate = (date, suppressError) => {
  if (date instanceof Date) {
    return date;
  }
  // should not really happen in production, but it's here for making the tests pass
  // this is what moment js does for invalid dates
  if (date === null || date === undefined) {
    if (!suppressError) {
      // eslint-disable-next-line no-console
      console.error('date is null or undefined');
    }
    return new Date();
  }

  const parsedDate = parseISO(date);
  if (!isValid(parsedDate)) {
    // eslint-disable-next-line no-console
    console.log(`Invalid date - ${date}`);
  }
  return parsedDate;
};
/**
 * @param  {Date|string} date
 * @returns {string}
 */
export const formatDateWithTime = (date) => {
  // example - 2022-07-21 11:02 AM
  // everything is 0 padded
  return format(toDate(date), 'yyyy-MM-dd KK:mm a');
};

/**
 * @param  {Date|string} date
 * @returns {string}
 */
export const formatDateWithoutTime = (date) => {
  // example - 2022-07-21
  return format(toDate(date), 'yyyy-MM-dd');
};

// since date-fns is based on the inbuilt Date object(which doesn't carry Timezone information),
// we need this helper to get the date in UTC
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString<commit_msg>make hours start at 1 instead of 0<commit_after>// contains some helper functions for date manipulation/formatting
// all functions take in a valid ISO 8601 date string or a Date object

import { format, isValid, parseISO } from 'date-fns';


// this exists because unlike moment-js, date-fns doesn't have a generalized toDate function
/**
 * @param  {Date|string} date
 * @param {boolean} suppressError
*/
export const toDate = (date, suppressError) => {
  if (date instanceof Date) {
    return date;
  }
  // should not really happen in production, but it's here for making the tests pass
  // this is what moment js does for invalid dates
  if (date === null || date === undefined) {
    if (!suppressError) {
      // eslint-disable-next-line no-console
      console.error('date is null or undefined');
    }
    return new Date();
  }

  const parsedDate = parseISO(date);
  if (!isValid(parsedDate)) {
    // eslint-disable-next-line no-console
    console.log(`Invalid date - ${date}`);
  }
  return parsedDate;
};
/**
 * @param  {Date|string} date
 * @returns {string}
 */
export const formatDateWithTime = (date) => {
  // example - 2022-07-21 11:02 AM
  // everything is 0 padded
  return format(toDate(date), 'yyyy-MM-dd hh:mm a');
};

/**
 * @param  {Date|string} date
 * @returns {string}
 */
export const formatDateWithoutTime = (date) => {
  // example - 2022-07-21
  return format(toDate(date), 'yyyy-MM-dd');
};

// since date-fns is based on the inbuilt Date object(which doesn't carry Timezone information),
// we need this helper to get the date in UTC
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
{'fraction_non_alphanumeric': 0.13125561881929876, 'fraction_numerical': 0.015582858855259216, 'mean_word_length': 3.392105263157895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '6333466', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1012, 'n_words': 346}