-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPaginationHelper.js
52 lines (43 loc) · 1.75 KB
/
PaginationHelper.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
// For this exercise you will be strengthening your page-fu mastery. You will complete the PaginationHelper class, which is a utility class helpful for querying paging information related to an array.
// The class is designed to take in an array of values and an integer indicating how many items will be allowed per each page. The types of values contained within the collection/array are not relevant.
// The following are some examples of how this class is used:
// PaginationHelper<Character> helper = new PaginationHelper(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f'), 4);
// helper.pageCount(); //should == 2
// helper.itemCount(); //should == 6
// helper.pageItemCount(0); //should == 4
// helper.pageItemCount(1); // last page - should == 2
// helper.pageItemCount(2); // should == -1 since the page is invalid
// // pageIndex takes an item index and returns the page that it belongs on
// helper.pageIndex(5); //should == 1 (zero based index)
// helper.pageIndex(2); //should == 0
// helper.pageIndex(20); //should == -1
// helper.pageIndex(-10); //should == -1
class PaginationHelper {
constructor(arr, len) {
this.content = arr;
this.total = arr.length;
this.perPage = len;
}
pageCount(n = this.total) {
return Math.ceil((n + 1) / this.perPage);
}
itemCount() {
return this.total;
}
pageItemCount(page) {
if (page === this.pageCount() - 1) {
return this.total % this.perPage;
} else if (page < this.pageCount() - 1) {
return this.perPage;
} else {
return 0;
}
}
pageIndex(num) {
if (num < this.total && num >= 0) {
return this.pageCount(num) - 1;
} else {
return -1;
}
}
}