Skip to content

Commit

Permalink
Embedding the configuraitons into an configuraitons object
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlanz committed May 26, 2021
1 parent 146fa2d commit 7502663
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Second, you can call the calculate function by passing the following information
- `page`: The current page
- `size`: The amount of items shown per page
- `total`: The amount of total items in the list/collection
- `maxPages`: The maximum amount of pages that should be shown (default: 5)
- `config`: A configuration object containing the following possible configuration values:
- `firstPage`: The first page of the pagination (default: 1)
- `maxPages`: The maximum amount of pages that should be shown (default: 5)

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

Expand Down
13 changes: 8 additions & 5 deletions src/__test__/pagination-with-first-page-0.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ it('should return no pagination if not enough items are given', () => {
const page = 0
const size = 5
const total = 2
const pagination = calculatePagination(page, size, total, 5, 0)
const pagination = calculatePagination(page, size, total, { firstPage: 0 })

expect(pagination).toBeNull()
})
it('should return only a few pages', () => {
const page = 1
const size = 5
const total = 15
const pagination = calculatePagination(page, size, total, 5, 0)
const pagination = calculatePagination(page, size, total, { firstPage: 0 })

expect(pagination).not.toBeNull()
expect(pagination?.pages).toHaveLength(3)
Expand All @@ -29,7 +29,10 @@ it('should return the maximum number of pages', () => {
const size = 5
const total = 50
const maxPages = 5
const pagination = calculatePagination(page, size, total, maxPages, 0)
const pagination = calculatePagination(page, size, total, {
firstPage: 0,
maxPages: maxPages,
})

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

expect(pagination).not.toBeNull()

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

expect(pagination).not.toBeNull()

Expand Down
4 changes: 3 additions & 1 deletion src/__test__/pagination-with-first-page-1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ it('should return the maximum number of pages', () => {
const size = 5
const total = 50
const maxPages = 5
const pagination = calculatePagination(page, size, total, maxPages)
const pagination = calculatePagination(page, size, total, {
maxPages: maxPages,
})

expect(pagination).not.toBeNull()
expect(pagination?.pages).toHaveLength(5)
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ const calculatePagination = (
page: number,
size: number,
total: number,
maxPages = 5,
firstPage = 1
config?: {
firstPage?: number
maxPages?: number
}
): {
previous: {
number: number
Expand All @@ -67,6 +69,9 @@ const calculatePagination = (
return null
}

const firstPage = config?.firstPage ?? 1
const maxPages = config?.maxPages ?? 5

const lastPage = Math.ceil(total / size) + (firstPage - 1)
const isCurrentTheFirstPage = page === firstPage
const isCurrentTheLastPage = page === lastPage
Expand Down

0 comments on commit 7502663

Please sign in to comment.