Skip to content

Instantly share code, notes, and snippets.

@segunadebayo
Created November 20, 2020 20:22
Show Gist options
  • Save segunadebayo/9aa939bea1c6a62b36d47275d2c413f3 to your computer and use it in GitHub Desktop.
Save segunadebayo/9aa939bea1c6a62b36d47275d2c413f3 to your computer and use it in GitHub Desktop.
export interface PaginationOptions {
total: number;
current: number;
noOfSiblings?: number;
}
export function pagination(options: PaginationOptions) {
const { total, noOfSiblings, current } = validate(options);
const max = 2 * noOfSiblings + 5;
const breakpoint = noOfSiblings + 3;
const needEllipsis = total > max;
const pages = range(1, total);
if (!needEllipsis) return pages;
if (current <= breakpoint) {
const from = 1;
const to = max - 2;
return [...range(from, to), '...', total];
}
if (current > total - breakpoint) {
const from = total - (max - 3);
const to = total;
return [1, '...', ...range(from, to)];
}
const from = current - noOfSiblings;
const to = current + noOfSiblings;
return [1, '...', ...range(from, to), '...', total];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment