Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created December 17, 2019 10:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andreasvirkus/ad67c921e86f73aff69be82d2c3cd81e to your computer and use it in GitHub Desktop.
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