Skip to content

Commit 6e2b589

Browse files
author
devgioele
committed
refactor config of calculatePagination
1 parent 30fc8f6 commit 6e2b589

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

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.ONE_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)