Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.