Skip to content

Commit 61dd3fb

Browse files
authored
Merge pull request #4 from aboutbits/display-number
Introducing display number
2 parents 0c00e70 + 458c782 commit 61dd3fb

4 files changed

Lines changed: 85 additions & 47 deletions

File tree

readme.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Second, you can call the calculate function by passing the following information
2525
- `size`: The amount of items shown per page
2626
- `total`: The amount of total items in the list/collection
2727
- `config`: A configuration object containing the following possible configuration values:
28-
- `firstPage`: The first page of the pagination (default: 1)
28+
- `indexType`: The starting point of the pagination (default: IndexType.ONE_BASED)
2929
- `maxPages`: The maximum amount of pages that should be shown (default: 5)
3030

3131
In return, you receive an object with all relevant information:
@@ -43,32 +43,37 @@ This would return the following object:
4343
```json
4444
{
4545
"previous": {
46-
"number": 1,
46+
"indexNumber": 1,
4747
"isDisabled": true
4848
},
4949
"next": {
50-
"number": 2,
50+
"indexNumber": 2,
5151
"isDisabled": false
5252
},
5353
"pages": [
5454
{
55-
"number": 1,
55+
"indexNumber": 1,
56+
"displayNumber": 1,
5657
"isCurrent": true
5758
},
5859
{
59-
"number": 2,
60+
"indexNumber": 2,
61+
"displayNumber": 2,
6062
"isCurrent": false
6163
},
6264
{
63-
"number": 3,
65+
"indexNumber": 3,
66+
"displayNumber": 3,
6467
"isCurrent": false
6568
},
6669
{
67-
"number": 4,
70+
"indexNumber": 4,
71+
"displayNumber": 4,
6872
"isCurrent": false
6973
},
7074
{
71-
"number": 5,
75+
"indexNumber": 5,
76+
"displayNumber": 5,
7277
"isCurrent": false
7378
}
7479
]

src/__test__/pagination-with-first-page-1.test.ts renamed to src/__test__/pagination-with-one-based-index-type.test.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ it('should return only a few pages', () => {
1717
expect(pagination).not.toBeNull()
1818
expect(pagination?.pages).toHaveLength(3)
1919

20-
expect(pagination?.pages[0].number).toBe(1)
20+
expect(pagination?.pages[0].indexNumber).toBe(1)
21+
expect(pagination?.pages[0].displayNumber).toBe(1)
2122
expect(pagination?.pages[0].isCurrent).toBeFalsy()
22-
expect(pagination?.pages[1].number).toBe(2)
23+
expect(pagination?.pages[1].indexNumber).toBe(2)
24+
expect(pagination?.pages[1].displayNumber).toBe(2)
2325
expect(pagination?.pages[1].isCurrent).toBeTruthy()
24-
expect(pagination?.pages[2].number).toBe(3)
26+
expect(pagination?.pages[2].indexNumber).toBe(3)
27+
expect(pagination?.pages[2].displayNumber).toBe(3)
2528
expect(pagination?.pages[2].isCurrent).toBeFalsy()
2629
})
2730
it('should return the maximum number of pages', () => {
@@ -36,15 +39,20 @@ it('should return the maximum number of pages', () => {
3639
expect(pagination).not.toBeNull()
3740
expect(pagination?.pages).toHaveLength(5)
3841

39-
expect(pagination?.pages[0].number).toBe(3)
42+
expect(pagination?.pages[0].indexNumber).toBe(3)
43+
expect(pagination?.pages[0].displayNumber).toBe(3)
4044
expect(pagination?.pages[0].isCurrent).toBeFalsy()
41-
expect(pagination?.pages[1].number).toBe(4)
45+
expect(pagination?.pages[1].indexNumber).toBe(4)
46+
expect(pagination?.pages[1].displayNumber).toBe(4)
4247
expect(pagination?.pages[1].isCurrent).toBeFalsy()
43-
expect(pagination?.pages[2].number).toBe(5)
48+
expect(pagination?.pages[2].indexNumber).toBe(5)
49+
expect(pagination?.pages[2].displayNumber).toBe(5)
4450
expect(pagination?.pages[2].isCurrent).toBeTruthy()
45-
expect(pagination?.pages[3].number).toBe(6)
51+
expect(pagination?.pages[3].indexNumber).toBe(6)
52+
expect(pagination?.pages[3].displayNumber).toBe(6)
4653
expect(pagination?.pages[3].isCurrent).toBeFalsy()
47-
expect(pagination?.pages[4].number).toBe(7)
54+
expect(pagination?.pages[4].indexNumber).toBe(7)
55+
expect(pagination?.pages[4].displayNumber).toBe(7)
4856
expect(pagination?.pages[4].isCurrent).toBeFalsy()
4957
})
5058
it('should disable the previous link', () => {
@@ -55,10 +63,10 @@ it('should disable the previous link', () => {
5563

5664
expect(pagination).not.toBeNull()
5765

58-
expect(pagination?.previous.number).toBe(1)
66+
expect(pagination?.previous.indexNumber).toBe(1)
5967
expect(pagination?.previous.isDisabled).toBeTruthy()
6068

61-
expect(pagination?.next.number).toBe(2)
69+
expect(pagination?.next.indexNumber).toBe(2)
6270
expect(pagination?.next.isDisabled).toBeFalsy()
6371

6472
expect(pagination?.pages[0].isCurrent).toBeTruthy()
@@ -72,10 +80,10 @@ it('should disable the next link', () => {
7280

7381
expect(pagination).not.toBeNull()
7482

75-
expect(pagination?.previous.number).toBe(1)
83+
expect(pagination?.previous.indexNumber).toBe(1)
7684
expect(pagination?.previous.isDisabled).toBeFalsy()
7785

78-
expect(pagination?.next.number).toBe(2)
86+
expect(pagination?.next.indexNumber).toBe(2)
7987
expect(pagination?.next.isDisabled).toBeTruthy()
8088

8189
expect(pagination?.pages[0].isCurrent).toBeFalsy()

src/__test__/pagination-with-first-page-0.test.ts renamed to src/__test__/pagination-with-zero-based-index-type.test.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
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 = 0
55
const size = 5
66
const total = 2
7-
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
7+
const pagination = calculatePagination(page, size, total, {
8+
indexType: IndexType.ZERO_BASED,
9+
})
810

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

1721
expect(pagination).not.toBeNull()
1822
expect(pagination?.pages).toHaveLength(3)
1923

20-
expect(pagination?.pages[0].number).toBe(0)
24+
expect(pagination?.pages[0].indexNumber).toBe(0)
25+
expect(pagination?.pages[0].displayNumber).toBe(1)
2126
expect(pagination?.pages[0].isCurrent).toBeFalsy()
22-
expect(pagination?.pages[1].number).toBe(1)
27+
expect(pagination?.pages[1].indexNumber).toBe(1)
28+
expect(pagination?.pages[1].displayNumber).toBe(2)
2329
expect(pagination?.pages[1].isCurrent).toBeTruthy()
24-
expect(pagination?.pages[2].number).toBe(2)
30+
expect(pagination?.pages[2].indexNumber).toBe(2)
31+
expect(pagination?.pages[2].displayNumber).toBe(3)
2532
expect(pagination?.pages[2].isCurrent).toBeFalsy()
2633
})
2734
it('should return the maximum number of pages', () => {
@@ -30,36 +37,43 @@ it('should return the maximum number of pages', () => {
3037
const total = 50
3138
const maxPages = 5
3239
const pagination = calculatePagination(page, size, total, {
33-
firstPage: 0,
40+
indexType: IndexType.ZERO_BASED,
3441
maxPages: maxPages,
3542
})
3643

3744
expect(pagination).not.toBeNull()
3845
expect(pagination?.pages).toHaveLength(5)
3946

40-
expect(pagination?.pages[0].number).toBe(2)
47+
expect(pagination?.pages[0].indexNumber).toBe(2)
48+
expect(pagination?.pages[0].displayNumber).toBe(3)
4149
expect(pagination?.pages[0].isCurrent).toBeFalsy()
42-
expect(pagination?.pages[1].number).toBe(3)
50+
expect(pagination?.pages[1].indexNumber).toBe(3)
51+
expect(pagination?.pages[1].displayNumber).toBe(4)
4352
expect(pagination?.pages[1].isCurrent).toBeFalsy()
44-
expect(pagination?.pages[2].number).toBe(4)
53+
expect(pagination?.pages[2].indexNumber).toBe(4)
54+
expect(pagination?.pages[2].displayNumber).toBe(5)
4555
expect(pagination?.pages[2].isCurrent).toBeTruthy()
46-
expect(pagination?.pages[3].number).toBe(5)
56+
expect(pagination?.pages[3].indexNumber).toBe(5)
57+
expect(pagination?.pages[3].displayNumber).toBe(6)
4758
expect(pagination?.pages[3].isCurrent).toBeFalsy()
48-
expect(pagination?.pages[4].number).toBe(6)
59+
expect(pagination?.pages[4].indexNumber).toBe(6)
60+
expect(pagination?.pages[4].displayNumber).toBe(7)
4961
expect(pagination?.pages[4].isCurrent).toBeFalsy()
5062
})
5163
it('should disable the previous link', () => {
5264
const page = 0
5365
const size = 5
5466
const total = 10
55-
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
67+
const pagination = calculatePagination(page, size, total, {
68+
indexType: IndexType.ZERO_BASED,
69+
})
5670

5771
expect(pagination).not.toBeNull()
5872

59-
expect(pagination?.previous.number).toBe(0)
73+
expect(pagination?.previous.indexNumber).toBe(0)
6074
expect(pagination?.previous.isDisabled).toBeTruthy()
6175

62-
expect(pagination?.next.number).toBe(1)
76+
expect(pagination?.next.indexNumber).toBe(1)
6377
expect(pagination?.next.isDisabled).toBeFalsy()
6478

6579
expect(pagination?.pages[0].isCurrent).toBeTruthy()
@@ -69,14 +83,16 @@ it('should disable the next link', () => {
6983
const page = 1
7084
const size = 5
7185
const total = 10
72-
const pagination = calculatePagination(page, size, total, { firstPage: 0 })
86+
const pagination = calculatePagination(page, size, total, {
87+
indexType: IndexType.ZERO_BASED,
88+
})
7389

7490
expect(pagination).not.toBeNull()
7591

76-
expect(pagination?.previous.number).toBe(0)
92+
expect(pagination?.previous.indexNumber).toBe(0)
7793
expect(pagination?.previous.isDisabled).toBeFalsy()
7894

79-
expect(pagination?.next.number).toBe(1)
95+
expect(pagination?.next.indexNumber).toBe(1)
8096
expect(pagination?.next.isDisabled).toBeTruthy()
8197

8298
expect(pagination?.pages[0].isCurrent).toBeFalsy()

src/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
enum IndexType {
2+
ZERO_BASED,
3+
ONE_BASED,
4+
}
5+
16
const calculateVisiblePages = (
27
page: number,
38
firstPage: number,
@@ -48,52 +53,56 @@ const calculatePagination = (
4853
size: number,
4954
total: number,
5055
config?: {
51-
firstPage?: number
56+
indexType?: IndexType
5257
maxPages?: number
5358
}
5459
): {
5560
previous: {
56-
number: number
61+
indexNumber: number
5762
isDisabled: boolean
5863
}
5964
next: {
60-
number: number
65+
indexNumber: number
6166
isDisabled: boolean
6267
}
6368
pages: {
64-
number: number
69+
indexNumber: number
70+
displayNumber: number
6571
isCurrent: boolean
6672
}[]
6773
} | null => {
6874
if (total <= size) {
6975
return null
7076
}
7177

72-
const firstPage = config?.firstPage ?? 1
78+
const indexType = config?.indexType ?? IndexType.ONE_BASED
7379
const maxPages = config?.maxPages ?? 5
7480

81+
const firstPage = indexType === IndexType.ZERO_BASED ? 0 : 1
7582
const lastPage = Math.ceil(total / size) + (firstPage - 1)
7683
const isCurrentTheFirstPage = page === firstPage
7784
const isCurrentTheLastPage = page === lastPage
7885

7986
return {
8087
previous: {
81-
number: isCurrentTheFirstPage ? firstPage : page - 1,
88+
indexNumber: isCurrentTheFirstPage ? firstPage : page - 1,
8289
isDisabled: isCurrentTheFirstPage,
8390
},
8491
next: {
85-
number: isCurrentTheLastPage ? lastPage : page + 1,
92+
indexNumber: isCurrentTheLastPage ? lastPage : page + 1,
8693
isDisabled: isCurrentTheLastPage,
8794
},
8895
pages: calculateVisiblePages(page, firstPage, lastPage, maxPages).map(
8996
(visiblePage) => {
9097
return {
91-
number: visiblePage,
98+
indexNumber: visiblePage,
99+
displayNumber:
100+
indexType === IndexType.ZERO_BASED ? visiblePage + 1 : visiblePage,
92101
isCurrent: visiblePage === page,
93102
}
94103
}
95104
),
96105
}
97106
}
98107

99-
export { calculatePagination }
108+
export { calculatePagination, IndexType }

0 commit comments

Comments
 (0)