From 7502663b4c640908a073efeea1bcc7064c41cc1f Mon Sep 17 00:00:00 2001 From: Alex Lanz Date: Wed, 26 May 2021 13:59:55 +0200 Subject: [PATCH] Embedding the configuraitons into an configuraitons object --- readme.md | 4 +++- src/__test__/pagination-with-first-page-0.test.ts | 13 ++++++++----- src/__test__/pagination-with-first-page-1.test.ts | 4 +++- src/index.ts | 9 +++++++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/readme.md b/readme.md index 1d62e16..0aac788 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,9 @@ Second, you can call the calculate function by passing the following information - `page`: The current page - `size`: The amount of items shown per page - `total`: The amount of total items in the list/collection -- `maxPages`: The maximum amount of pages that should be shown (default: 5) +- `config`: A configuration object containing the following possible configuration values: + - `firstPage`: The first page of the pagination (default: 1) + - `maxPages`: The maximum amount of pages that should be shown (default: 5) In return, you receive an object with all relevant information: diff --git a/src/__test__/pagination-with-first-page-0.test.ts b/src/__test__/pagination-with-first-page-0.test.ts index ae8a0fc..f450b50 100644 --- a/src/__test__/pagination-with-first-page-0.test.ts +++ b/src/__test__/pagination-with-first-page-0.test.ts @@ -4,7 +4,7 @@ it('should return no pagination if not enough items are given', () => { const page = 0 const size = 5 const total = 2 - const pagination = calculatePagination(page, size, total, 5, 0) + const pagination = calculatePagination(page, size, total, { firstPage: 0 }) expect(pagination).toBeNull() }) @@ -12,7 +12,7 @@ it('should return only a few pages', () => { const page = 1 const size = 5 const total = 15 - const pagination = calculatePagination(page, size, total, 5, 0) + const pagination = calculatePagination(page, size, total, { firstPage: 0 }) expect(pagination).not.toBeNull() expect(pagination?.pages).toHaveLength(3) @@ -29,7 +29,10 @@ it('should return the maximum number of pages', () => { const size = 5 const total = 50 const maxPages = 5 - const pagination = calculatePagination(page, size, total, maxPages, 0) + const pagination = calculatePagination(page, size, total, { + firstPage: 0, + maxPages: maxPages, + }) expect(pagination).not.toBeNull() expect(pagination?.pages).toHaveLength(5) @@ -49,7 +52,7 @@ it('should disable the previous link', () => { const page = 0 const size = 5 const total = 10 - const pagination = calculatePagination(page, size, total, 5, 0) + const pagination = calculatePagination(page, size, total, { firstPage: 0 }) expect(pagination).not.toBeNull() @@ -66,7 +69,7 @@ it('should disable the next link', () => { const page = 1 const size = 5 const total = 10 - const pagination = calculatePagination(page, size, total, 5, 0) + const pagination = calculatePagination(page, size, total, { firstPage: 0 }) expect(pagination).not.toBeNull() diff --git a/src/__test__/pagination-with-first-page-1.test.ts b/src/__test__/pagination-with-first-page-1.test.ts index c66e582..33f3879 100644 --- a/src/__test__/pagination-with-first-page-1.test.ts +++ b/src/__test__/pagination-with-first-page-1.test.ts @@ -29,7 +29,9 @@ it('should return the maximum number of pages', () => { const size = 5 const total = 50 const maxPages = 5 - const pagination = calculatePagination(page, size, total, maxPages) + const pagination = calculatePagination(page, size, total, { + maxPages: maxPages, + }) expect(pagination).not.toBeNull() expect(pagination?.pages).toHaveLength(5) diff --git a/src/index.ts b/src/index.ts index 466113d..b5f3436 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,8 +47,10 @@ const calculatePagination = ( page: number, size: number, total: number, - maxPages = 5, - firstPage = 1 + config?: { + firstPage?: number + maxPages?: number + } ): { previous: { number: number @@ -67,6 +69,9 @@ const calculatePagination = ( return null } + const firstPage = config?.firstPage ?? 1 + const maxPages = config?.maxPages ?? 5 + const lastPage = Math.ceil(total / size) + (firstPage - 1) const isCurrentTheFirstPage = page === firstPage const isCurrentTheLastPage = page === lastPage