Last active
February 25, 2023 00:27
-
-
Save JacobHsu/8e9802bacd73b5c97639 to your computer and use it in GitHub Desktop.
PaginationHelper class #js
This file contains 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
// TODO: complete this object/class | |
// http://www.codewars.com/kata/515bb423de843ea99400000a/train/javascript | |
// The constructor takes in an array of items and a integer indicating how many | |
// items fit within a single page | |
function PaginationHelper(collection, itemsPerPage){ | |
this.items = collection; | |
this.itemsPerPage = itemsPerPage; | |
} | |
// returns the number of items within the entire collection | |
PaginationHelper.prototype.itemCount = function() { | |
return this.items.length; | |
} | |
// returns the number of pages | |
PaginationHelper.prototype.pageCount = function() { | |
return Math.ceil(this.itemCount() / 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 | |
PaginationHelper.prototype.pageItemCount = function(pageIndex) { | |
var itemsPerPage = this.itemsPerPage, | |
itemCount = this.itemCount(), | |
pageCount = this.pageCount(); | |
if (pageIndex >= pageCount || pageIndex < 0) return -1; | |
return this.items.slice(pageIndex * itemsPerPage, itemCount).splice(0, itemsPerPage).length; | |
} | |
// determines what page an item is on. Zero based indexes | |
// this method should return -1 for itemIndex values that are out of range | |
PaginationHelper.prototype.pageIndex = function(itemIndex) { | |
if (this.itemCount() === 0 | |
|| itemIndex < 0 | |
|| itemIndex > this.itemCount()) return -1; | |
if (itemIndex === 0 | |
|| itemIndex / this.itemsPerPage === 1) return 0; | |
return Math.floor((itemIndex / this.itemsPerPage)); | |
} |
This file contains 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
// Create your own tests here. These are some of the methods available: | |
var helper = new PaginationHelper(['a','b','c','d','e','f'], 4); | |
Test.assertEquals(helper.pageCount(), 2); | |
Test.assertEquals(helper.itemCount(), 6); | |
Test.assertEquals(helper.pageItemCount(0), 4); | |
Test.assertEquals(helper.pageItemCount(1), 2); | |
Test.assertEquals(helper.pageItemCount(2), -1); | |
Test.assertEquals(helper.pageIndex(5), 1); | |
Test.assertEquals(helper.pageIndex(2), 0); | |
Test.assertEquals(helper.pageIndex(20), -1); | |
/* | |
var helper = new PaginationHelper(['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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment