All files / components/Game gameCardUtils.js

100% Statements 17/17
100% Branches 15/15
100% Functions 2/2
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  54x 24x             30x 26x 26x 26x 2x     24x 24x             4x       26x   26x   26x 4x     22x         22x           22x        
function extractDateParts(value) {
  if (value instanceof Date && !Number.isNaN(value.getTime())) {
    return {
      year: value.getFullYear(),
      month: value.getMonth() + 1,
      day: value.getDate(),
    };
  }
 
  if (typeof value === "string") {
    const [datePortion] = value.split("T");
    const match = datePortion.match(/^(\d{4})-(\d{2})-(\d{2})$/);
    if (!match) {
      return null;
    }
 
    const [, yearStr, monthStr, dayStr] = match;
    return {
      year: Number.parseInt(yearStr, 10),
      month: Number.parseInt(monthStr, 10),
      day: Number.parseInt(dayStr, 10),
    };
  }
 
  return null;
}
 
function isFutureDate(startingDate, referenceDate = new Date()) {
  const startParts = extractDateParts(startingDate);
  const referenceParts =
    extractDateParts(referenceDate) ?? extractDateParts(new Date());
 
  if (!startParts || !referenceParts) {
    return false;
  }
 
  const startDate = new Date(
    startParts.year,
    startParts.month - 1,
    startParts.day,
  );
  const referenceDateOnly = new Date(
    referenceParts.year,
    referenceParts.month - 1,
    referenceParts.day,
  );
 
  return startDate.getTime() > referenceDateOnly.getTime();
}
 
export { extractDateParts, isFutureDate };