All files / src/lib/core pagination.ts

100% Statements 104/104
100% Branches 4/4
100% Functions 4/4
100% Lines 104/104

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 1051x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x  
/**
 * Pagination Helper Utilities
 * Provides reusable pagination logic for lists and queries
 */
 
export interface PaginationOptions {
  page?: number;
  limit?: number;
  maxLimit?: number;
}
 
export interface PaginationMeta {
  page: number;
  limit: number;
  total: number;
  totalPages: number;
  hasNextPage: boolean;
  hasPreviousPage: boolean;
}
 
export interface PaginationResult<T> {
  data: T[];
  pagination: PaginationMeta;
}
 
/**
 * Calculate pagination parameters for database queries
 * Sanitizes and validates page and limit values
 */
export function calculatePagination(
  page: number = 1,
  limit: number = 20,
  maxLimit: number = 100
): { skip: number; take: number; page: number; limit: number } {
  // Ensure page is at least 1
  const sanitizedPage = Math.max(1, Math.floor(page));
 
  // Ensure limit is between 1 and maxLimit
  const sanitizedLimit = Math.min(
    Math.max(1, Math.floor(limit)),
    maxLimit
  );
 
  // Calculate skip value for offset-based pagination
  const skip = (sanitizedPage - 1) * sanitizedLimit;
 
  return {
    skip,
    take: sanitizedLimit,
    page: sanitizedPage,
    limit: sanitizedLimit};
}
 
/**
 * Build pagination metadata from results
 * Calculates total pages, hasNext, hasPrevious, etc.
 */
export function buildPaginationMeta(
  page: number,
  limit: number,
  total: number
): PaginationMeta {
  const totalPages = Math.ceil(total / limit);
 
  return {
    page,
    limit,
    total,
    totalPages,
    hasNextPage: page < totalPages,
    hasPreviousPage: page > 1};
}
 
/**
 * Create a complete paginated result
 * Combines data and metadata
 */
export function createPaginatedResult<T>(
  data: T[],
  page: number,
  limit: number,
  total: number
): PaginationResult<T> {
  return {
    data,
    pagination: buildPaginationMeta(page, limit, total)};
}
 
/**
 * Helper to paginate an array in memory
 * Useful for small datasets that don't need database pagination
 */
export function paginateArray<T>(
  array: T[],
  page: number = 1,
  limit: number = 20
): PaginationResult<T> {
  const { skip, take, page: sanitizedPage, limit: sanitizedLimit } = calculatePagination(page, limit);
 
  const data = array.slice(skip, skip + take);
  const total = array.length;
 
  return createPaginatedResult(data, sanitizedPage, sanitizedLimit, total);
}