Skip to content

Commit

Permalink
make sure the getCurrentPage does not return anything below 1
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski committed Apr 2, 2024
1 parent 9ab3a37 commit e6019e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@curiousleaf/utils",
"description": "A lightweight set of utilities",
"version": "1.0.21",
"version": "1.0.22",
"license": "MIT",
"type": "module",
"author": {
Expand Down
6 changes: 6 additions & 0 deletions src/params/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ describe("getCurrentPage", () => {

expect(currentPage).toBe(1)
})

it("returns 1 if the provided page is less than 1", () => {
const currentPage = getCurrentPage("0")

expect(currentPage).toBe(1)
})
})

describe("getPageLink", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/params/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getSearchParams = (request: Request) => {
* @returns The current page number as a number.
*/
export const getCurrentPage = (page?: string | null) => {
return page && !Number.isNaN(Number(page)) ? parseInt(page || "1", 10) : 1
return Math.max(page && !Number.isNaN(Number(page)) ? parseInt(page || "1", 10) : 1, 1)
}

/**
Expand Down

0 comments on commit e6019e7

Please sign in to comment.