Skip to content

Commit

Permalink
feat: add direct skip param
Browse files Browse the repository at this point in the history
  • Loading branch information
maddocnc committed Oct 23, 2023
1 parent e2f0b2b commit e4e5747
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ function paginateWithDefaults<T>(options?: PaginationOptions<T> | null) {
const after = options?.after ?? undefined
const before = options?.before ?? undefined

const limit =
Number.parseInt(options?.limit, 10) > 0
? Number.parseInt(options?.limit, 10)
: 0
const limit = options?.limit || 0

let page = 1
let skip = 0

if (options?.page) {
page = Number.parseInt(options?.page, 10)
if (options?.skip) {
skip = options.skip
page = (skip / limit) + 1
} else if (options?.page) {
page = options?.page
skip = (page - 1) * limit
}

return {
key,
after,
Expand Down
8 changes: 6 additions & 2 deletions src/pagination-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ export interface PaginationOptions<T> extends PopulateOption {
/**
* page number
*/
page?: any | undefined
page?: number | undefined

/**
* docs skip, overrides page
*/
skip?: number | undefined
/**
* Docs per page
*/
limit?: any | undefined
limit?: number | undefined
}

0 comments on commit e4e5747

Please sign in to comment.