Skip to content

Commit d6109d7

Browse files
author
devgioele
authored
Merge pull request #9 from aboutbits/default-index-type
Change default index type to zero-based
2 parents 30fc8f6 + 655cfde commit d6109d7

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"eslint-plugin-jest": "^26.5.3",
5656
"eslint-plugin-prettier": "^4.1.0",
5757
"jest": "^28.1.2",
58+
"jest-environment-jsdom": "^29.6.1",
5859
"prettier": "^2.7.1",
5960
"ts-jest": "^28.0.5",
6061
"typescript": "^4.7.4"

src/__test__/pagination-with-one-based-index-type.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import { calculatePagination } from '../index'
1+
import { calculatePagination, IndexType } from '../index'
22

33
it('should return no pagination if not enough items are given', () => {
44
const page = 1
55
const size = 5
66
const total = 2
7-
const pagination = calculatePagination(page, size, total)
7+
const pagination = calculatePagination(page, size, total, {
8+
indexType: IndexType.ONE_BASED,
9+
})
810

911
expect(pagination).toBeNull()
1012
})
1113
it('should return only a few pages', () => {
1214
const page = 2
1315
const size = 5
1416
const total = 15
15-
const pagination = calculatePagination(page, size, total)
17+
const pagination = calculatePagination(page, size, total, {
18+
indexType: IndexType.ONE_BASED,
19+
})
1620

1721
expect(pagination).not.toBeNull()
1822
expect(pagination?.pages).toHaveLength(3)
@@ -33,6 +37,7 @@ it('should return the maximum number of pages', () => {
3337
const total = 50
3438
const maxPages = 5
3539
const pagination = calculatePagination(page, size, total, {
40+
indexType: IndexType.ONE_BASED,
3641
maxPages: maxPages,
3742
})
3843

@@ -59,7 +64,9 @@ it('should disable the previous link', () => {
5964
const page = 1
6065
const size = 5
6166
const total = 10
62-
const pagination = calculatePagination(page, size, total)
67+
const pagination = calculatePagination(page, size, total, {
68+
indexType: IndexType.ONE_BASED,
69+
})
6370

6471
expect(pagination).not.toBeNull()
6572

@@ -76,7 +83,9 @@ it('should disable the next link', () => {
7683
const page = 2
7784
const size = 5
7885
const total = 10
79-
const pagination = calculatePagination(page, size, total)
86+
const pagination = calculatePagination(page, size, total, {
87+
indexType: IndexType.ONE_BASED,
88+
})
8089

8190
expect(pagination).not.toBeNull()
8291

src/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,21 @@ const calculateVisiblePages = (
4848
return pages
4949
}
5050

51+
type CalculatePaginationConfig = {
52+
indexType: IndexType
53+
maxPages: number
54+
}
55+
56+
const defaultCalculatePaginationConfig: CalculatePaginationConfig = {
57+
indexType: IndexType.ZERO_BASED,
58+
maxPages: 5,
59+
}
60+
5161
const calculatePagination = (
5262
page: number,
5363
size: number,
5464
total: number,
55-
config?: {
56-
indexType?: IndexType
57-
maxPages?: number
58-
}
65+
config?: Partial<CalculatePaginationConfig>
5966
): {
6067
previous: {
6168
indexNumber: number
@@ -71,13 +78,15 @@ const calculatePagination = (
7178
isCurrent: boolean
7279
}[]
7380
} | null => {
81+
const { indexType, maxPages } = {
82+
...defaultCalculatePaginationConfig,
83+
...config,
84+
}
85+
7486
if (total <= size) {
7587
return null
7688
}
7789

78-
const indexType = config?.indexType ?? IndexType.ONE_BASED
79-
const maxPages = config?.maxPages ?? 5
80-
8190
const firstPage = indexType === IndexType.ZERO_BASED ? 0 : 1
8291
const lastPage = Math.ceil(total / size) + (firstPage - 1)
8392
const isCurrentTheFirstPage = page === firstPage

0 commit comments

Comments
 (0)