From 6e2b58985573c12ce84aaca8c731f8db7aa37b36 Mon Sep 17 00:00:00 2001 From: devgioele Date: Fri, 14 Jul 2023 16:51:30 +0200 Subject: [PATCH 1/4] refactor config of calculatePagination --- src/index.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7d62ba9..5e7e908 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,14 +48,21 @@ const calculateVisiblePages = ( return pages } +type CalculatePaginationConfig = { + indexType: IndexType + maxPages: number +} + +const defaultCalculatePaginationConfig: CalculatePaginationConfig = { + indexType: IndexType.ONE_BASED, + maxPages: 5, +} + const calculatePagination = ( page: number, size: number, total: number, - config?: { - indexType?: IndexType - maxPages?: number - } + config?: Partial ): { previous: { indexNumber: number @@ -71,13 +78,15 @@ const calculatePagination = ( isCurrent: boolean }[] } | null => { + const { indexType, maxPages } = { + ...defaultCalculatePaginationConfig, + ...config, + } + if (total <= size) { return null } - const indexType = config?.indexType ?? IndexType.ONE_BASED - const maxPages = config?.maxPages ?? 5 - const firstPage = indexType === IndexType.ZERO_BASED ? 0 : 1 const lastPage = Math.ceil(total / size) + (firstPage - 1) const isCurrentTheFirstPage = page === firstPage From e95246dc3998c841aeab3683ee3e0a6e2c960e44 Mon Sep 17 00:00:00 2001 From: devgioele Date: Fri, 14 Jul 2023 16:52:42 +0200 Subject: [PATCH 2/4] change default index type to zero-based --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 5e7e908..85de459 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,7 +54,7 @@ type CalculatePaginationConfig = { } const defaultCalculatePaginationConfig: CalculatePaginationConfig = { - indexType: IndexType.ONE_BASED, + indexType: IndexType.ZERO_BASED, maxPages: 5, } From aa0669c0e3f0a0068f9e5bd8e2790d32885101d3 Mon Sep 17 00:00:00 2001 From: devgioele Date: Fri, 14 Jul 2023 16:58:11 +0200 Subject: [PATCH 3/4] install jest-environment-jsdom to run tests again --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 6988f08..b0051af 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "eslint-plugin-jest": "^26.5.3", "eslint-plugin-prettier": "^4.1.0", "jest": "^28.1.2", + "jest-environment-jsdom": "^29.6.1", "prettier": "^2.7.1", "ts-jest": "^28.0.5", "typescript": "^4.7.4" From 655cfde8431a32acc64ca2ae4a4788a428509c90 Mon Sep 17 00:00:00 2001 From: devgioele Date: Fri, 14 Jul 2023 17:03:24 +0200 Subject: [PATCH 4/4] fix tests --- ...gination-with-one-based-index-type.test.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/__test__/pagination-with-one-based-index-type.test.ts b/src/__test__/pagination-with-one-based-index-type.test.ts index 29f4cc8..2b2b658 100644 --- a/src/__test__/pagination-with-one-based-index-type.test.ts +++ b/src/__test__/pagination-with-one-based-index-type.test.ts @@ -1,10 +1,12 @@ -import { calculatePagination } from '../index' +import { calculatePagination, IndexType } from '../index' it('should return no pagination if not enough items are given', () => { const page = 1 const size = 5 const total = 2 - const pagination = calculatePagination(page, size, total) + const pagination = calculatePagination(page, size, total, { + indexType: IndexType.ONE_BASED, + }) expect(pagination).toBeNull() }) @@ -12,7 +14,9 @@ it('should return only a few pages', () => { const page = 2 const size = 5 const total = 15 - const pagination = calculatePagination(page, size, total) + const pagination = calculatePagination(page, size, total, { + indexType: IndexType.ONE_BASED, + }) expect(pagination).not.toBeNull() expect(pagination?.pages).toHaveLength(3) @@ -33,6 +37,7 @@ it('should return the maximum number of pages', () => { const total = 50 const maxPages = 5 const pagination = calculatePagination(page, size, total, { + indexType: IndexType.ONE_BASED, maxPages: maxPages, }) @@ -59,7 +64,9 @@ it('should disable the previous link', () => { const page = 1 const size = 5 const total = 10 - const pagination = calculatePagination(page, size, total) + const pagination = calculatePagination(page, size, total, { + indexType: IndexType.ONE_BASED, + }) expect(pagination).not.toBeNull() @@ -76,7 +83,9 @@ it('should disable the next link', () => { const page = 2 const size = 5 const total = 10 - const pagination = calculatePagination(page, size, total) + const pagination = calculatePagination(page, size, total, { + indexType: IndexType.ONE_BASED, + }) expect(pagination).not.toBeNull()