All files / src/shared/lib pagination.ts

100% Statements 15/15
95% Branches 19/20
100% Functions 4/4
100% Lines 15/15

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            3x   11x         11x 11x 6x   5x 5x       13x 7x   6x             7x 7x 3x   4x   7x    
import type {
  LocationQuery,
  LocationQueryRaw,
  LocationQueryValue,
} from "vue-router";
 
const firstQueryValue = (
  value: LocationQueryValue | LocationQueryValue[] | undefined,
): LocationQueryValue | undefined => Array.isArray(value) ? value[0] ?? null : value;
 
export function pageFromQuery(
  value: LocationQueryValue | LocationQueryValue[] | undefined,
): number {
  const raw = firstQueryValue(value);
  if (typeof raw !== "string" || !/^\d+$/u.test(raw)) {
    return 1;
  }
  const parsed = Number(raw);
  return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : 1;
}
 
export function pageCount(total: number, pageSize: number): number {
  if (!Number.isFinite(total) || total <= 0 || !Number.isFinite(pageSize) || pageSize <= 0) {
    return 1;
  }
  return Math.max(1, Math.ceil(total / pageSize));
}
 
export function queryWithPage(
  query: LocationQuery,
  page: number,
): LocationQueryRaw {
  const next: LocationQueryRaw = { ...query };
  if (page <= 1) {
    delete next.page;
  } else {
    next.page = String(page);
  }
  return next;
}