Last active
May 12, 2019 19:17
-
-
Save vbarbarosh/3b8f2f7eb257c3f26fc00ad4ca4763da to your computer and use it in GitHub Desktop.
js_pager_numerate_google – Generate page numbers like Google does https://codescreens.com
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
| // Generate page numbers like Google does | |
| function pager_numerate_google(active, total, display) | |
| { | |
| const begin = Math.max(1, active - Math.floor(display/2)); | |
| const length = Math.min(total, display); | |
| const x = Math.min(0, total + 1 - begin - length); | |
| return Array(length).fill(0).map((v,i) => begin + i + x) | |
| .map(v => v == active ? `[${v}]` : v); | |
| } | |
| const expected = ` | |
| [1] 2 3 4 5 6 7 8 9 10 | |
| 1 [2] 3 4 5 6 7 8 9 10 | |
| 1 2 [3] 4 5 6 7 8 9 10 | |
| 1 2 3 [4] 5 6 7 8 9 10 | |
| 1 2 3 4 [5] 6 7 8 9 10 | |
| 1 2 3 4 5 [6] 7 8 9 10 | |
| 2 3 4 5 6 [7] 8 9 10 11 | |
| 3 4 5 6 7 [8] 9 10 11 12 | |
| 4 5 6 7 8 [9] 10 11 12 13 | |
| 5 6 7 8 9 [10] 11 12 13 14 | |
| 6 7 8 9 10 [11] 12 13 14 15 | |
| 6 7 8 9 10 11 [12] 13 14 15 | |
| 6 7 8 9 10 11 12 [13] 14 15 | |
| 6 7 8 9 10 11 12 13 [14] 15 | |
| 6 7 8 9 10 11 12 13 14 [15] | |
| `.trim().split('\n'); | |
| for (let i = 0; i < 15; ++i) { | |
| const str = pager_numerate_google(i+1, 15, 10).join(' '); | |
| console.log(str == expected[i], str); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment