Created
April 21, 2017 01:48
-
-
Save mahmoodkhan/ce1f765696cc6b9886128ef2635e29e8 to your computer and use it in GitHub Desktop.
This file contains 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
function pagination(c, m) { | |
var current = c, | |
last = m, | |
delta = 2, | |
left = current - delta, | |
right = current + delta + 1, | |
range = [], | |
rangeWithDots = [], | |
l; | |
range.push(1) | |
for (let i = c - delta; i <= c + delta; i++) { | |
if (i >= left && i < right && i < m && i > 1) { | |
range.push(i); | |
} | |
} | |
range.push(m); | |
for (let i of range) { | |
if (l) { | |
if (i - l === 2) { | |
rangeWithDots.push(l + 1); | |
} else if (i - l !== 1) { | |
rangeWithDots.push('...'); | |
} | |
} | |
rangeWithDots.push(i); | |
l = i; | |
} | |
return rangeWithDots; | |
} | |
/* | |
Test it: | |
for (let i = 1, l = 20; i <= l; i++) | |
console.log(`Selected page ${i}:`, pagination(i, l)); | |
Expected output: | |
Selected page 1: [1, 2, 3, "...", 20] | |
Selected page 2: [1, 2, 3, 4, "...", 20] | |
Selected page 3: [1, 2, 3, 4, 5, "...", 20] | |
Selected page 4: [1, 2, 3, 4, 5, 6, "...", 20] | |
Selected page 5: [1, 2, 3, 4, 5, 6, 7, "...", 20] | |
Selected page 6: [1, "...", 4, 5, 6, 7, 8, "...", 20] | |
Selected page 7: [1, "...", 5, 6, 7, 8, 9, "...", 20] | |
Selected page 8: [1, "...", 6, 7, 8, 9, 10, "...", 20] | |
Selected page 9: [1, "...", 7, 8, 9, 10, 11, "...", 20] | |
Selected page 10: [1, "...", 8, 9, 10, 11, 12, "...", 20] | |
Selected page 11: [1, "...", 9, 10, 11, 12, 13, "...", 20] | |
Selected page 12: [1, "...", 10, 11, 12, 13, 14, "...", 20] | |
Selected page 13: [1, "...", 11, 12, 13, 14, 15, "...", 20] | |
Selected page 14: [1, "...", 12, 13, 14, 15, 16, "...", 20] | |
Selected page 15: [1, "...", 13, 14, 15, 16, 17, "...", 20] | |
Selected page 16: [1, "...", 14, 15, 16, 17, 18, 19, 20] | |
Selected page 17: [1, "...", 15, 16, 17, 18, 19, 20] | |
Selected page 18: [1, "...", 16, 17, 18, 19, 20] | |
Selected page 19: [1, "...", 17, 18, 19, 20] | |
Selected page 20: [1, "...", 18, 19, 20] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment