Created
November 20, 2020 20:22
-
-
Save segunadebayo/9aa939bea1c6a62b36d47275d2c413f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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