Created
December 17, 2019 10:59
-
-
Save andreasvirkus/ad67c921e86f73aff69be82d2c3cd81e 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
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