Skip to content

Instantly share code, notes, and snippets.

@supachailllpay
Last active December 29, 2017 22:21
Show Gist options
  • Save supachailllpay/b5f6f6d34f3e820843654616c009995c to your computer and use it in GitHub Desktop.
Save supachailllpay/b5f6f6d34f3e820843654616c009995c to your computer and use it in GitHub Desktop.
Simple Pagination (n length)
const pagination = (c, m, n) => {
let l, r, h, t, x
if (m <= n) {
x = Array(m).fill(0).map((x, i) => i)
} else {
h = Math.floor(n / 2)
l = Math.max(0, c - h)
t = Math.max(0, h - (c - l))
r = Math.min(m - 1, c + h + t)
t = Math.max(0, h + t - (r - c))
l = Math.max(0, l - t)
x = Array(r - l + 1).fill(0).map((x, i) => i + l)
if (l !== 0) x = [0, '.', ...x.slice(2)]
if (r !== m - 1) x = [...x.slice(0, x.length - 2), '.', m - 1]
}
return x
}
// let m = 20
// let n = 10
// for (let i = 0; i < m; i += 1) {
// console.log(pagination(i, m, n))
// }
//
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 19 ]
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 19 ]
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 19 ]
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 19 ]
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 19 ]
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 19 ]
// [ 0, '.', 3, 4, 5, 6, 7, 8, 9, '.', 19 ]
// [ 0, '.', 4, 5, 6, 7, 8, 9, 10, '.', 19 ]
// [ 0, '.', 5, 6, 7, 8, 9, 10, 11, '.', 19 ]
// [ 0, '.', 6, 7, 8, 9, 10, 11, 12, '.', 19 ]
// [ 0, '.', 7, 8, 9, 10, 11, 12, 13, '.', 19 ]
// [ 0, '.', 8, 9, 10, 11, 12, 13, 14, '.', 19 ]
// [ 0, '.', 9, 10, 11, 12, 13, 14, 15, '.', 19 ]
// [ 0, '.', 10, 11, 12, 13, 14, 15, 16, '.', 19 ]
// [ 0, '.', 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
// [ 0, '.', 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
// [ 0, '.', 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
// [ 0, '.', 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
// [ 0, '.', 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
// [ 0, '.', 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment