All files / app/selectors dates.ts

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                      12x               5x               12x               5x               12x 5x     12x                                                
import { createSelector } from 'reselect';
 
import { ICommit } from 'app/interfaces';
import {
  getStartDate,
  getEndDate,
  getCommits,
  getEarliestCommitDate,
  getLatestCommitDate,
} from 'app/selectors/stateVars';
 
export const getDefaultedStartDate = createSelector(
  getStartDate,
  getEarliestCommitDate,
  getCommits,
  defaultStartDate
);
 
export function defaultStartDate(startDate, earliestCommitDate, commits) {
  return (
    startDate ||
    earliestCommitDate ||
    (commits && commits.length > 0 && commits[commits.length - 1].authorDate) ||
    0
  );
}
 
export const getDefaultedEndDate = createSelector(
  getEndDate,
  getLatestCommitDate,
  getCommits,
  defaultEndDate
);
 
export function defaultEndDate(endDate, latestCommitDate, commits) {
  return (
    endDate ||
    latestCommitDate ||
    (commits && commits.length > 0 && commits[0].authorDate) || // @ts-ignore
    Math.floor((new Date() as any) / 1000)
  );
}
 
export const hasDates = (startDate, endDate) => {
  return startDate || endDate;
};
 
export const commitsMatchDates = (commit, startDate, endDate) => {
  if (!hasDates(startDate, endDate)) {
    return true;
  }
  const commits = Array.isArray(commit) ? commit : [commit];
  return commits.some(commit => isWithinDates(commit, startDate, endDate));
};
 
export function dateFilteredCommits(commits, startDate, endDate) {
  if (!commits) {
    return null;
  }
  return commits.filter(commit => isWithinDates(commit, startDate, endDate));
}
 
function isWithinDates(
  commit: ICommit,
  startDate: number,
  endDate: number
): boolean {
  return (
    startDate <= commit.authorDate && (!endDate || commit.authorDate <= endDate)
  );
}