Skip to content

Instantly share code, notes, and snippets.

@monokrome
Last active May 25, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monokrome/74d57797d35a80c642ef706f2fd66695 to your computer and use it in GitHub Desktop.
Save monokrome/74d57797d35a80c642ef706f2fd66695 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const DEFAULT_PAGINATION_SIZE = 9
const ELLIPSIS = '...'
function getPagination(currentPage, numberOfPages, maxSize) {
if (numberOfPages === 1) {
return [1]
}
if (currentPage > numberOfPages) {
throw new Error('Current page must be less than total number of pages.')
}
if (typeof maxSize === 'undefined') maxSize = DEFAULT_PAGINATION_SIZE
const
result = [currentPage],
expectedResultLength = Math.min(maxSize, numberOfPages)
let left = currentPage - 1,
right = currentPage + 1
while (result.length < expectedResultLength) {
if (left > 0) {
result.unshift(left)
left--
} else if (right <= numberOfPages) {
result.push(right)
right++
}
if (right <= numberOfPages) {
result.push(right)
right++
} else if (left > 0) {
result.unshift(left)
left--
}
}
// Fill in left side
if (result[0] !== 1) result[0] = 1
if (result[1] !== 2) result[1] = 2
// Fill in right side
if (result[result.length - 1] !== numberOfPages) {
result[result.length - 1] = numberOfPages
}
if (result[result.length - 2] !== numberOfPages - 1) {
result[result.length - 2] = numberOfPages - 1
}
return result
}
function ellipsizeGaps(arr) {
const result = []
let last = 0,
ellipsisCount = 0
for (const index of arr.keys()) {
result.push(arr[index])
if (last && last !== arr[index] - 1) {
const offset = index + ellipsisCount
result.splice(offset, 0, ELLIPSIS)
ellipsisCount++
}
last = arr[index]
}
return result
}
const
large = getPagination(13, 30, 9),
smallRightAligned = getPagination(9, 12, 9),
smallLeftAligned = getPagination(3, 12, 9),
tiny = getPagination(2, 4, 9),
single = getPagination(1, 1, 9)
console.assert(large.length === 9, 'large is wrong length')
console.assert(smallRightAligned.length === 9, 'smallRightAligned is wrong length')
console.assert(smallLeftAligned.length === 9, 'smallLeftAligned is wrong length')
console.assert(tiny.length === 4, 'smallLeftAligned is wrong length')
console.assert(single.length === 1, 'single is wrong length')
console.assert(large.join('.') === [1, 2, 11, 12, 13, 14, 15, 29, 30].join('.'), 'large is wrong')
console.assert(smallRightAligned.join('.') === [1, 2, 6, 7, 8, 9, 10, 11, 12].join('.'), 'smallRightAligned is wrong')
console.assert(smallLeftAligned.join('.') === [1, 2, 3, 4, 5, 6, 7, 11, 12].join('.'), 'smallLeftAligned is wrong')
console.assert(tiny.join('.') === [1, 2, 3, 4].join('.'), 'smallLeftAligned is wrong')
console.assert(single.join('.') === [1].join('.'), 'single is wrong')
console.assert(ellipsizeGaps([1, 2, 5, 6, 9, 10]).join('|') === [1, 2, '...', 5, 6, '...', 9, 10].join('|'))
console.assert(ellipsizeGaps([1, 2, 50, 60]).join('|') === [1, 2, '...', 50, '...', 60].join('|'))
console.assert(ellipsizeGaps([1, 2, 50, 51]).join('|') === [1, 2, '...', 50, 51].join('|'))
console.assert(ellipsizeGaps([1, 2, 50]).join('|') === [1, 2, '...', 50].join('|'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment