-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpaginationhelper.js
52 lines (44 loc) · 1.76 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
class PaginationHelper {
// The constructor takes in an array of items and a integer indicating how many
// items fit within a single page
constructor(collection, itemsPerPage) {
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
// returns the number of items within the entire collection
itemCount() {
return this.collection.length;
}
// returns the number of pages
pageCount() {
return Math.ceil(this.collection.length / this.itemsPerPage);
}
// returns the number of items on the current page. page_index is zero based.
// this method should return -1 for pageIndex values that are out of range
pageItemCount(pageIndex) {
if (pageIndex > this.pageCount() - 1) return -1;
// if this is not the last page...
if (pageIndex < this.pageCount() - 1) {
return this.itemsPerPage;
} else {
return this.itemCount() - ((this.pageCount() - 1) * this.itemsPerPage);
}
}
// determines what page an item is on. Zero based indexes
// this method should return -1 for itemIndex values that are out of range
pageIndex(itemIndex) {
if (this.itemCount() == 0 || itemIndex < 0 || itemIndex > this.itemCount()) return -1;
return Math.floor(itemIndex / this.itemsPerPage);
}
}
// const helper = new PaginationHelper(['a','b','c','d','e','f'], 4);
// console.log(helper.pageCount(), 2);
// console.log(helper.itemCount(), 6);
// console.log(helper.pageItemCount(0), 4);
// console.log(helper.pageItemCount(1), 2);
// console.log(helper.pageItemCount(2), -1);
// // pageIndex takes an item index and returns the page that it belongs on
// console.log(helper.pageIndex(5), 1);
// console.log(helper.pageIndex(2), 0);
// console.log(helper.pageIndex(20), -1);
// console.log(helper.pageIndex(-10), -1);