Last active
April 22, 2020 15:05
-
-
Save tlimpanont/58c5e6d802343c7ef809 to your computer and use it in GitHub Desktop.
Pagination Class for javascript
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
var Pagination = function(list, itemsPerPage) { | |
this.list = list; | |
this.itemsPerPage = itemsPerPage; | |
this.currentPageNumber = null; | |
this.currentPageIndex = null; | |
this.currentPageItems = []; | |
this.totalItems = this.list.length; | |
this.totalPages = Math.ceil(this.totalItems / this.itemsPerPage ); | |
this.toFirstPage(); | |
if(this.itemsPerpage > this.totalItems) { | |
throw Error("itemsPerPage should be less than the length of the list"); | |
} | |
} | |
Pagination.prototype.toLastPage = function() { | |
this.currentPageNumber = this.totalPages; | |
this.currentPageIndex = this.totalPages - 1; | |
this.currentPageItems = this.getCurrentPageItems(); | |
} | |
Pagination.prototype.toFirstPage = function() { | |
this.currentPageNumber = 1; | |
this.currentPageIndex = 0; | |
this.currentPageItems = this.getCurrentPageItems(); | |
} | |
Pagination.prototype.hasNext = function() { | |
return (this.currentPageNumber < this.totalPages && this.currentPageNumber >= 0); | |
} | |
Pagination.prototype.hasPrevious = function() { | |
return (this.currentPageNumber <= 1) ? false : true; | |
} | |
Pagination.prototype.toPageNumber = function(pageNumber) { | |
if(pageNumber <= 0) | |
{ | |
throw Error("pageNumber should be greater than 0"); | |
} | |
else if(pageNumber > this.totalPages) | |
{ | |
throw Error("pageNumber can't be greater than totalPages"); | |
} | |
this.currentPageNumber = pageNumber; | |
this.currentPageIndex = pageNumber - 1; | |
this.currentPageItems = this.getCurrentPageItems(); | |
} | |
Pagination.prototype.toNextPage = function() { | |
this.currentPageNumber += 1; | |
this.currentPageIndex = this.currentPageNumber - 1; | |
this.currentPageItems = this.getCurrentPageItems(); | |
if(!this.hasNext()) | |
{ | |
throw Error("You can't go further in the next stepping"); | |
} | |
} | |
Pagination.prototype.toPreviousPage = function() { | |
this.currentPageNumber -= 1; | |
this.currentPageIndex = this.currentPageNumber - 1; | |
this.currentPageItems = this.getCurrentPageItems(); | |
if(!this.hasPrevious()) | |
{ | |
throw Error("You can't go further in the previous stepping"); | |
} | |
} | |
Pagination.prototype.getCurrentPageItems = function() { | |
return this.list.slice(this.currentPageIndex * this.itemsPerPage , this.itemsPerPage + this.currentPageIndex * 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
describe('Pagination Class',function(){ | |
var list, pagination; | |
beforeEach(function(){ | |
list = [1,2,3,4,5,6,7,8,9,10]; | |
pagination = new Pagination(list, 3); | |
}); | |
it('should Pagination Class with the correct properties values',function(){ | |
expect(pagination.totalItems).toEqual(10); | |
expect(pagination.totalPages).toEqual(4); | |
expect(pagination.currentPageNumber).toEqual(1); | |
expect(pagination.currentPageIndex).toEqual(0); | |
}); | |
it('should slice the items correctly when toFirstPage',function(){ | |
expect(pagination.currentPageItems).toEqual([1,2,3]); | |
}); | |
it('should slice the items correctly when toLastPage',function(){ | |
pagination.toLastPage(); | |
expect(pagination.currentPageItems).toEqual([10]); | |
}); | |
it('should slice the items correctly when pageNumber n',function(){ | |
pagination.toPageNumber(1); | |
expect(pagination.currentPageItems).toEqual([1,2,3]); | |
pagination.toPageNumber(2); | |
expect(pagination.currentPageItems).toEqual([4,5,6]); | |
pagination.toPageNumber(3); | |
expect(pagination.currentPageItems).toEqual([7,8,9]); | |
pagination.toPageNumber(4); | |
expect(pagination.currentPageItems).toEqual([10]); | |
}); | |
it('should slice the items correctly when nextPage n',function(){ | |
pagination.toFirstPage(); | |
pagination.toNextPage(); | |
expect(pagination.currentPageNumber).toEqual(2); | |
expect(pagination.currentPageItems).toEqual([4,5,6]); | |
}); | |
it('should check hasNext and hasPrevious correctly',function(){ | |
pagination.toFirstPage(); | |
expect(pagination.hasNext()).toBe(true); | |
expect(pagination.hasPrevious()).toBe(false); | |
pagination.toLastPage(); | |
expect(pagination.hasNext()).toBe(false); | |
expect(pagination.hasPrevious()).toBe(true); | |
pagination.toPageNumber(2); | |
expect(pagination.hasNext()).toBe(true); | |
expect(pagination.hasPrevious()).toBe(true); | |
pagination.toPageNumber(3); | |
expect(pagination.hasNext()).toBe(true); | |
expect(pagination.hasPrevious()).toBe(true); | |
pagination.toPageNumber(4); | |
expect(pagination.hasNext()).toBe(false); | |
expect(pagination.hasPrevious()).toBe(true); | |
}); | |
it('pagination should have no loop in interations',function(){ | |
pagination.toFirstPage(); | |
expect(function() { | |
pagination.toPreviousPage(); | |
}).toThrow(); | |
pagination.toLastPage(); | |
expect(function() { | |
pagination.toNextPage(); | |
}).toThrow(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment