-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (25 loc) · 978 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export default function pagesBadges({ currentPage, pages, numBadges = 5 }) {
const maxBadgesSide = numBadges - 2;
// Without separators case
// ex: [1, 2, 3, 4, 5]
if (pages <= numBadges) {
return Array.from({ length: pages }).map((v, i) => i + 1);
}
const sideBadges = Array.from({ length: numBadges - 1 });
// With a separator at the end case
// ex: [1, 2, 3, 4, null, 49]
if (currentPage <= maxBadgesSide) {
return [...sideBadges.map((v, i) => i + 1), null, pages];
}
// With a separator at the beginning case
// ex: [1, null, 46, 47, 48, 49]
if (currentPage > pages - maxBadgesSide) {
return [1, null, ...sideBadges.map((v, i) => pages - i).reverse()];
}
// In the middle (separator left + right) case
// ex: [1, null, 26, 27, 28, null, 49]
sideBadges.pop();
const curr = Math.floor(sideBadges.length / 2);
const center = sideBadges.map((v, i) => currentPage - curr + i);
return [1, null, ...center, null, pages];
}