Skip to content

Commit eeba83f

Browse files
committed
refactor(pagination): make pagination optional & provide unpaginated rows
1 parent f9b22d3 commit eeba83f

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@chronicstone/array-query",
33
"type": "module",
4-
"version": "0.0.0",
4+
"version": "0.1.0",
55
"description": "A simple and lightweight array query library",
66
"author": "Cyprien THAO <cthao.pro@gmail.com>",
77
"license": "MIT",

src/query.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { FilterMatchMode, GenericObject, MatchModeProcessorMap, QueryParams } from './types'
1+
import type { FilterMatchMode, GenericObject, MatchModeProcessorMap, QueryParams, QueryResult } from './types'
22
import { MatchModeProcessor, getObjectProperty, validateBetweenPayload } from './utils'
33

4-
export function query<T extends GenericObject>(
4+
export function query<T extends GenericObject, P extends QueryParams>(
55
data: T[],
6-
params: QueryParams,
7-
) {
6+
params: P,
7+
): QueryResult<T, P> {
88
let result: T[] = [...data]
99

1010
if (params.search?.value) {
@@ -152,16 +152,24 @@ export function query<T extends GenericObject>(
152152
})
153153
}
154154

155-
const totalRows = result.length
156-
const totalPages = Math.ceil(totalRows / params.limit)
157-
const start = (params.page - 1) * params.limit
158-
const end = start + params.limit
159-
result = result.slice(start, end)
155+
if (typeof params.limit === 'undefined') {
156+
return { rows: result } as QueryResult<T, P>
157+
}
158+
159+
else {
160+
const unpaginatedRows = [...result]
161+
const totalRows = result.length
162+
const totalPages = Math.ceil(totalRows / params.limit)
163+
const start = params.offset ?? 0
164+
const end = start + params.limit
165+
result = result.slice(start, end)
160166

161-
return {
162-
totalRows,
163-
totalPages,
164-
rows: result,
167+
return {
168+
totalRows,
169+
totalPages,
170+
rows: result,
171+
unpaginatedRows,
172+
} as QueryResult<T, P>
165173
}
166174
}
167175

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ export type QueryFilter = {
7070
} & MatchModeCore
7171

7272
export interface QueryParams {
73-
page: number
74-
limit: number
7573
sort?: { key: string, dir?: 'asc' | 'desc' }
7674
search?: {
7775
value: string
7876
keys: string[]
7977
}
8078
filter?: QueryFilter[]
79+
limit?: number
80+
offset?: number
8181
}
82+
export type QueryResult<T extends GenericObject, P extends QueryParams> = P extends { limit: number } ? { totalRows: number, totalPages: number, rows: T[], unpaginatedRows: T[] } : { totalRows: number, totalPages: number, rows: T[] }
8283

8384
export interface MatchModeProcessorMap {
8485
equals: ({ value, filter }: { value: any, filter: any }) => boolean

test/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe('should filter users', () => {
88
it('should filter active users', () => {
99
const { totalPages, totalRows } = query(users, {
1010
limit: 10,
11-
page: 1,
1211
sort: {
1312
key: 'createdAt',
1413
dir: 'desc',

0 commit comments

Comments
 (0)