-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (69 loc) · 1.54 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Pagination {
constructor({currentPage, totalPages}) {
this.current = currentPage;
this.total = totalPages;
this.value = [];
}
getPages() {
return this
.setCenter()
.filterCenter()
.includeTreeLeft()
.includeTreeRight()
.includeLeftDots()
.includeRightDots()
.includeFirstAndLast()
.value;
}
setCenter() {
this.value = [this.current - 2, this.current - 1, this.current, this.current + 1, this.current + 2];
return this;
}
filterCenter() {
this.value = this.value.filter(position => position > 1 && position < this.total);
return this;
}
includeTreeLeft() {
if (this.value == 5) {
this.value = [2].concat(this.value)
}
return this;
}
includeTreeRight() {
if (this.current == this.total - 4) {
this.value = this.value.concat([this.total - 1]);
}
return this;
}
includeLeftDots() {
if (this.current > 5) {
this.value = ["..."].concat(this.value);
}
return this;
}
includeRightDots() {
if (this.current < this.total - 4) {
this.value = this.value.concat(["..."]);
}
return this;
}
includeFirstAndLast() {
this.value = [1].concat(this.value).concat(this.total);
return this;
}
print(){
console.log(this.value);
return this.value;
}
}
const main = () => {
// Example
const pages = new Pagination({currentPage: 6, totalPages: 10}).getPages();
console.log(pages);
// [
// 1, '...', 30,
// 31, 32, 33,
// 34, '...', 2323
// ]
}
main()