All files / src/domain list-pagination.ts

88% Statements 44/50
76% Branches 19/25
94.11% Functions 16/17
88% Lines 44/50

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 511x 1x 1x 1x 1x 1x 1x 1x 5x     5x 5x 1x 1x 6x     6x 6x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 2x  
/** Default items per page in planning list webviews. */
export const DEFAULT_PAGE_SIZE = 50
 
export const PAGE_SIZE_OPTIONS = [25, 50, 100] as const
 
export type PageSize = (typeof PAGE_SIZE_OPTIONS)[number]
 
export function totalPages(totalItems: number, pageSize: number): number {
  if (totalItems <= 0 || pageSize <= 0) {
    return 1
  }
  return Math.ceil(totalItems / pageSize)
}
 
export function clampPage(page: number, totalPagesCount: number): number {
  if (totalPagesCount < 1) {
    return 1
  }
  return Math.min(Math.max(1, page), totalPagesCount)
}
 
export function pageSlice<T>(items: readonly T[], page: number, pageSize: number): T[] {
  const pages = totalPages(items.length, pageSize)
  const safePage = clampPage(page, pages)
  const start = (safePage - 1) * pageSize
  return items.slice(start, start + pageSize)
}
 
export function pageRange(
  totalItems: number,
  page: number,
  pageSize: number,
): { from: number; to: number; page: number; totalPages: number } {
  const pages = totalPages(totalItems, pageSize)
  const safePage = clampPage(page, pages)
  if (totalItems === 0) {
    return { from: 0, to: 0, page: 1, totalPages: 1 }
  }
  const from = (safePage - 1) * pageSize + 1
  const to = Math.min(safePage * pageSize, totalItems)
  return { from, to, page: safePage, totalPages: pages }
}
 
export function normalizePageSize(value: unknown): PageSize {
  const n = typeof value === "number" ? value : Number(value)
  if (PAGE_SIZE_OPTIONS.includes(n as PageSize)) {
    return n as PageSize
  }
  return DEFAULT_PAGE_SIZE
}