Skip to content

Commit 7502663

Browse files
committed
Embedding the configuraitons into an configuraitons object
1 parent 146fa2d commit 7502663

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Second, you can call the calculate function by passing the following information
2424
- `page`: The current page
2525
- `size`: The amount of items shown per page
2626
- `total`: The amount of total items in the list/collection
27-
- `maxPages`: The maximum amount of pages that should be shown (default: 5)
27+
- `config`: A configuration object containing the following possible configuration values:
28+
- `firstPage`: The first page of the pagination (default: 1)
29+
- `maxPages`: The maximum amount of pages that should be shown (default: 5)
2830

2931
In return, you receive an object with all relevant information:
3032

src/__test__/pagination-with-first-page-0.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ it('should return no pagination if not enough items are given', () => {
44
const page = 0
55
const size = 5
66
const total = 2
7-
const pagination = calculatePagination(page, size, total, 5, 0)
7+
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
88

99
expect(pagination).toBeNull()
1010
})
1111
it('should return only a few pages', () => {
1212
const page = 1
1313
const size = 5
1414
const total = 15
15-
const pagination = calculatePagination(page, size, total, 5, 0)
15+
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
1616

1717
expect(pagination).not.toBeNull()
1818
expect(pagination?.pages).toHaveLength(3)
@@ -29,7 +29,10 @@ it('should return the maximum number of pages', () => {
2929
const size = 5
3030
const total = 50
3131
const maxPages = 5
32-
const pagination = calculatePagination(page, size, total, maxPages, 0)
32+
const pagination = calculatePagination(page, size, total, {
33+
firstPage: 0,
34+
maxPages: maxPages,
35+
})
3336

3437
expect(pagination).not.toBeNull()
3538
expect(pagination?.pages).toHaveLength(5)
@@ -49,7 +52,7 @@ it('should disable the previous link', () => {
4952
const page = 0
5053
const size = 5
5154
const total = 10
52-
const pagination = calculatePagination(page, size, total, 5, 0)
55+
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
5356

5457
expect(pagination).not.toBeNull()
5558

@@ -66,7 +69,7 @@ it('should disable the next link', () => {
6669
const page = 1
6770
const size = 5
6871
const total = 10
69-
const pagination = calculatePagination(page, size, total, 5, 0)
72+
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
7073

7174
expect(pagination).not.toBeNull()
7275

src/__test__/pagination-with-first-page-1.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ it('should return the maximum number of pages', () => {
2929
const size = 5
3030
const total = 50
3131
const maxPages = 5
32-
const pagination = calculatePagination(page, size, total, maxPages)
32+
const pagination = calculatePagination(page, size, total, {
33+
maxPages: maxPages,
34+
})
3335

3436
expect(pagination).not.toBeNull()
3537
expect(pagination?.pages).toHaveLength(5)

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ const calculatePagination = (
4747
page: number,
4848
size: number,
4949
total: number,
50-
maxPages = 5,
51-
firstPage = 1
50+
config?: {
51+
firstPage?: number
52+
maxPages?: number
53+
}
5254
): {
5355
previous: {
5456
number: number
@@ -67,6 +69,9 @@ const calculatePagination = (
6769
return null
6870
}
6971

72+
const firstPage = config?.firstPage ?? 1
73+
const maxPages = config?.maxPages ?? 5
74+
7075
const lastPage = Math.ceil(total / size) + (firstPage - 1)
7176
const isCurrentTheFirstPage = page === firstPage
7277
const isCurrentTheLastPage = page === lastPage

0 commit comments

Comments
 (0)