Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-prettier": "^4.1.0",
"jest": "^28.1.2",
"jest-environment-jsdom": "^29.6.1",
"prettier": "^2.7.1",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
Expand Down
19 changes: 14 additions & 5 deletions src/__test__/pagination-with-one-based-index-type.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { calculatePagination } from '../index'
import { calculatePagination, IndexType } from '../index'

it('should return no pagination if not enough items are given', () => {
const page = 1
const size = 5
const total = 2
const pagination = calculatePagination(page, size, total)
const pagination = calculatePagination(page, size, total, {
indexType: IndexType.ONE_BASED,
})

expect(pagination).toBeNull()
})
it('should return only a few pages', () => {
const page = 2
const size = 5
const total = 15
const pagination = calculatePagination(page, size, total)
const pagination = calculatePagination(page, size, total, {
indexType: IndexType.ONE_BASED,
})

expect(pagination).not.toBeNull()
expect(pagination?.pages).toHaveLength(3)
Expand All @@ -33,6 +37,7 @@ it('should return the maximum number of pages', () => {
const total = 50
const maxPages = 5
const pagination = calculatePagination(page, size, total, {
indexType: IndexType.ONE_BASED,
maxPages: maxPages,
})

Expand All @@ -59,7 +64,9 @@ it('should disable the previous link', () => {
const page = 1
const size = 5
const total = 10
const pagination = calculatePagination(page, size, total)
const pagination = calculatePagination(page, size, total, {
indexType: IndexType.ONE_BASED,
})

expect(pagination).not.toBeNull()

Expand All @@ -76,7 +83,9 @@ it('should disable the next link', () => {
const page = 2
const size = 5
const total = 10
const pagination = calculatePagination(page, size, total)
const pagination = calculatePagination(page, size, total, {
indexType: IndexType.ONE_BASED,
})

expect(pagination).not.toBeNull()

Expand Down
23 changes: 16 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ const calculateVisiblePages = (
return pages
}

type CalculatePaginationConfig = {
indexType: IndexType
maxPages: number
}

const defaultCalculatePaginationConfig: CalculatePaginationConfig = {
indexType: IndexType.ZERO_BASED,
maxPages: 5,
}

const calculatePagination = (
page: number,
size: number,
total: number,
config?: {
indexType?: IndexType
maxPages?: number
}
config?: Partial<CalculatePaginationConfig>
): {
previous: {
indexNumber: number
Expand All @@ -71,13 +78,15 @@ const calculatePagination = (
isCurrent: boolean
}[]
} | null => {
const { indexType, maxPages } = {
...defaultCalculatePaginationConfig,
...config,
}

if (total <= size) {
return null
}

const indexType = config?.indexType ?? IndexType.ONE_BASED
const maxPages = config?.maxPages ?? 5

const firstPage = indexType === IndexType.ZERO_BASED ? 0 : 1
const lastPage = Math.ceil(total / size) + (firstPage - 1)
const isCurrentTheFirstPage = page === firstPage
Expand Down