All files / utils pageQuery.js

93.75% Statements 15/16
90.47% Branches 19/21
100% Functions 5/5
93.75% Lines 15/16

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  2x       2x 2x 2x 2x       3x 2x     1x 1x       1x             6x 6x 6x       2x 2x    
function isValidDate(value) {
  return value instanceof Date && !Number.isNaN(value.getTime());
}
 
function formatLocalDate(value) {
  const year = value.getFullYear();
  const month = String(value.getMonth() + 1).padStart(2, "0");
  const day = String(value.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}
 
export function toDayRange(range) {
  if (!Array.isArray(range) || range.length !== 2) {
    return { startTime: null, endTime: null };
  }
 
  const [start, end] = range;
  Iif (!isValidDate(start) || !isValidDate(end)) {
    return { startTime: null, endTime: null };
  }
 
  return {
    startTime: `${formatLocalDate(start)}T00:00:00`,
    endTime: `${formatLocalDate(end)}T23:59:59`,
  };
}
 
export function positiveIntegerFromQuery(value, fallback = 1) {
  const candidate = Array.isArray(value) ? value[0] : value;
  const parsed = Number.parseInt(String(candidate ?? ""), 10);
  return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
}
 
export function pageSizeFromQuery(value, allowed, fallback) {
  const parsed = positiveIntegerFromQuery(value, fallback);
  return allowed.includes(parsed) ? parsed : fallback;
}