{"size": 1160, "ext": "js", "max_stars_repo_path": "src/components/Section/History/helpers.js", "max_stars_repo_name": "jharper-sec/culper", "max_stars_repo_stars_event_min_datetime": "2016-11-14T13:43:10.000Z", "max_stars_repo_stars_event_max_datetime": "2019-07-29T17:09:53.000Z", "max_issues_repo_path": "src/components/Section/History/helpers.js", "max_issues_repo_name": "jharper-sec/culper", "max_issues_repo_issues_event_min_datetime": "2016-11-15T19:45:47.000Z", "max_issues_repo_issues_event_max_datetime": "2019-08-16T22:57:00.000Z", "max_forks_repo_path": "src/components/Section/History/helpers.js", "max_forks_repo_name": "jharper-sec/culper", "max_forks_repo_forks_event_min_datetime": "2019-09-05T18:02:03.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-19T18:30:14.000Z"}
{"max_stars_count": 19, "max_issues_count": 1469, "max_forks_count": 10, "avg_line_length": 18.7096774194, "max_line_length": 95, "alphanum_fraction": 0.6155172414}
import {
  today,
  daysAgo,
  daysBetween,
  extractDate,
} from 'components/Section/History/dateranges'

/**
 * Figure the total amount of years to collect for the timeline
 */
export const totalYears = (birthdate, years = 10) => {
  let total = years
  if (!birthdate) {
    return total
  }

  const eighteen = daysAgo(birthdate, 365 * -18)
  total = Math.ceil(daysBetween(eighteen, today) / 365)

  // Maximum years required
  if (total > years) {
    total = years
  }

  // A minimum of two years is required
  if (total < 2) {
    total = 2
  }

  return total
}

/**
 * Default sorting of history objects. This assumes that all objects contain a `Dates` property
 * with date range values.
 */
export const sort = (a, b) => {
  // Helper to find the date value or default it to 0
  const getOptionalDate = obj => (((obj || {}).Item || {}).Dates || {}).to

  const first = extractDate(getOptionalDate(a)) || 0
  const second = extractDate(getOptionalDate(b)) || 0

  if (a.type === 'Gap') {
    return 1
  }

  if (b.type === 'Gap') {
    return -1
  }

  if (first < second) {
    return 1
  }

  if (first > second) {
    return -1
  }

  return 0
}
