Created
December 17, 2019 10:59
Star
You must be signed in to star a gist
This file contains 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
const calculatePages = (length: number, current: number, total: number) => { | |
if (length === 0) return [] | |
if (length === 1) return [current] | |
if (total <= length + 4) return Array.from({ length: total }, (_v, i) => i + 1) | |
const jumpSize = Math.ceil(length / 2) | |
const centerStart = Math.min(Math.max(current - (length - jumpSize), 3), total - length - 1) | |
const jumpStart = current <= length + 1 ? 2 : `-${jumpSize}` | |
const jumpEnd = current >= total - length ? total - 1 : `+${jumpSize}` | |
return [1, jumpStart, ...Array.from({ length }, (_v, i) => centerStart + i), jumpEnd, total] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment