JSFiddle - React, Tailwind, and code Playground

by jacobwsmith

TypeScript

console.clear();

function subtractDaysHelper(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() - days);
  return result;
}

function sortByDateDescHelper(a, b) {
	if (a.date < b.date) {
      return 11;
    }
    if (a.date > b.date) {
      return -1;
    }
    // must be equal
    return 0;
}

function getDayDifferenceHelper(end, start) {
	 return (new Date(end) - new Date(start))/(1000 * 60 * 60 * 24) + 1;
}

function checkEmptyDates(list: string[], start: string, end: string) {
  // Sorts the dates
  const sortedList = list.sort(sortByDateDescHelper);
  // Track the sorted index as we add them to the new list
  let sortedIndex = 0;
  // Return the new merge list "daily only"
  return Array(getDayDifferenceHelper(end, start)).fill().map((_, index) => {
  	const newDate = subtractDaysHelper(new Date(end), index);
    const newStringDate = newDate.toISOString().split('T')[0];
    if(sortedList.length > sortedIndex && newStringDate === sortedList[sortedIndex].date){
    	const result =  {date: newStringDate, failureCount: sortedList[sortedIndex].failureCount };
      sortedIndex++;
      return result;
    }
    return  {date: newStringDate, failureCount: 0};
  })
}

// Test 1 
{
  const inputList = [
    {date: '2019-01-09', failureCount: 244},
    {date: '2019-01-10', failureCount: 333}
  ];
  const inputStart = '2019-01-01';
  const inputEnd = '2019-01-10';
  console.log(
    checkEmptyDates(inputList, inputStart, inputEnd)
  );
}

// Test 2
{
  const inputList = [
    {date: '2019-01-09', failureCount: 244},
    {date: '2019-01-02', failureCount: 333}
  ];
  const inputStart = '2019-01-01';
  const inputEnd = '2019-01-10';
  console.log(
    checkEmptyDates(inputList, inputStart, inputEnd)
  );
}

// Test 3
{
  const inputList = [
    {date: '2019-01-01', failureCount: 244},
    {date: '2019-01-02', failureCount: 333}
  ];
  const inputStart = '2019-01-01';
  const inputEnd = '2019-01-10';
  console.log(
   ...