Skip to content

Commit

Permalink
Merge pull request #5 from simonsinclair/totalpages-length
Browse files Browse the repository at this point in the history
Cater for `totalPages` being less than `length`
  • Loading branch information
simonsinclair committed May 23, 2021
2 parents 74b857f + 4ac0455 commit dd29dbc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
9 changes: 7 additions & 2 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ describe('getRange', () => {
});

describe('pgr', () => {
it('returns a pagination range for an odd number of pages', () => {
it("returns valid pagination ranges when 'totalPages' is less than or equal to 'length'", () => {
expect(pgr(6, 1, 10)).toEqual([1, 2, 3, 4, 5, 6]);
expect(pgr(6, 3, 6)).toEqual([1, 2, 3, 4, 5, 6]);
});

it("returns valid pagination ranges when 'length' is an odd number", () => {
expect(pgr(9, 1, 7)).toEqual([1, 2, 3, 4, 5, 6, 7]);
expect(pgr(9, 2, 7)).toEqual([1, 2, 3, 4, 5, 6, 7]);
expect(pgr(9, 3, 7)).toEqual([1, 2, 3, 4, 5, 6, 7]);
Expand All @@ -20,7 +25,7 @@ describe('pgr', () => {
expect(pgr(9, 9, 7)).toEqual([3, 4, 5, 6, 7, 8, 9]);
});

it('returns a pagination range for an even number of pages', () => {
it("returns valid pagination ranges when 'length' is an even number", () => {
expect(pgr(10, 1, 6)).toEqual([1, 2, 3, 4, 5, 6]);
expect(pgr(10, 2, 6)).toEqual([1, 2, 3, 4, 5, 6]);
expect(pgr(10, 3, 6)).toEqual([1, 2, 3, 4, 5, 6]);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@simonsinclair/pgr",
"version": "0.2.0",
"version": "0.3.0",
"description": "A tiny pagination range creator.",
"main": "dist/index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const getRange = (count: number, startIndex: number): number[] => {
};

const pgr: Pgr = (totalPages, currentPage, length) => {
if (length >= totalPages) return getRange(totalPages, 1);

const centerPage = Math.floor(length / 2);
let pages;

Expand Down

0 comments on commit dd29dbc

Please sign in to comment.