Last active
May 31, 2017 13:07
-
-
Save marcusandre/35bc2fd4ae5aae1b438aaaaef552d337 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
var pager = require('pager') | |
for (let i = 1, l = 20; i <= l; i++) { | |
console.log(`Selected page ${i}:`, pager(i, l)) | |
} |
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
module.exports = pager | |
function pager (c, m) { | |
var current = c | |
var last = m | |
var delta = 2 | |
var left = current - delta | |
var right = current + delta + 1 | |
var range = [] | |
var rangeDotted = [] | |
var l | |
for (let i = 1; i <= last; i++) { | |
if (i === 1 || i === last || i >= left && i < right) { | |
range.push(i) | |
} | |
} | |
for (let i of range) { | |
if (l) { | |
if (i - l === 2) { | |
rangeDotted.push(l + 1) | |
} else if (i - l !== 1) { | |
rangeDotted.push('...') | |
} | |
} | |
rangeDotted.push(i) | |
l = i | |
} | |
return rangeDotted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment