{"size": 2576, "ext": "js", "max_stars_repo_path": "src/utils.js", "max_stars_repo_name": "chenchenalex/swipejob-test", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/utils.js", "max_issues_repo_name": "chenchenalex/swipejob-test", "max_issues_repo_issues_event_min_datetime": "2021-03-11T01:16:10.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T08:03:05.000Z", "max_forks_repo_path": "src/utils.js", "max_forks_repo_name": "chenchenalex/swipejob-test", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 30.3058823529, "max_line_length": 155, "alphanum_fraction": 0.6401397516}
/**
 * Format phone number strings to readable format
 * @param {String} phone eg: 2130010012
 * @return {String} eg: (213)001 0012
 * */
export function formatPhone(phone) {
  if (typeof phone !== "string") return "";

  return `(${phone.slice(0, 3)})${phone.slice(3, 6)} ${phone.slice(6)}`;
}

/**
 * Format Date period into readable format
 * @param {String} startDate eg: 2019-09-04T21:00:00Z
 * @param {String} endDate eg: 2019-09-05T05:00:00Z
 * @return {String} eg: SEP 4, WED 9PM - SEP 5, THU 5AM PDT
 * */
export function formatShiftTimePeriod(startDate, endDate) {
  const startDateObj = new Date(startDate);
  const endDateObj = new Date(endDate);

  // check validity of input
  if (isNaN(startDateObj.getTime()) || isNaN(endDateObj.getTime())) {
    console.error("Invalid time format provided");
    return "";
  }

  const {
    month: startMonth,
    day: startDay,
    dayInWeek: startDayInWeek,
    hour: startHour,
    AMPM: startAMPM,
    timezone,
  } = formatDate(startDateObj);

  const { month: endMonth, day: endDay, dayInWeek: endDayInWeek, hour: endHour, AMPM: endAMPM } = formatDate(
    endDateObj
  );

  // if shift happens on the same day
  if(startMonth === endMonth && startDayInWeek === endDayInWeek && startDay === endDay){
    return `${startMonth} ${startDay}, ${startDayInWeek} ${startHour}${startAMPM} - ${endHour}${endAMPM} ${timezone}`;
  }

  return `${startMonth} ${startDay}, ${startDayInWeek} ${startHour}${startAMPM} - ${endMonth} ${endDay}, ${endDayInWeek} ${endHour}${endAMPM} ${timezone}`;
}

/**
 * Format Date period into readable format
 * @param {Date} dateObj
 * @return {Object}
 * */
export function formatDate(dateObj) {
  if (!(dateObj instanceof Date)) return {};

  const monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
  const dayNames = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
  const year = dateObj.getFullYear();
  const month = monthNames[dateObj.getMonth()];
  const day = dateObj.getDate();
  const dayInWeek = dayNames[dateObj.getDay()];
  let hour = dateObj.getHours();
  let AMPM = hour >= 12 ? "PM" : "AM";
  hour = hour % 12;
  hour = hour ? hour : 12; // the hour '0' should be '12'

  const minute = dateObj.getMinutes();
  const second = dateObj.getSeconds();
  const timezoneOffSetHours = (dateObj.getTimezoneOffset() / 60) * -1;
  const timezone = `UTC${timezoneOffSetHours > 0 ? "+" : ""}${timezoneOffSetHours}`;

  return {
    year,
    month,
    day,
    dayInWeek,
    hour,
    AMPM,
    minute,
    second,
    timezone,
  };
}
