Skip to content

Commit 059bbbd

Browse files
Add pagination methods and tests for JsonServer class
1 parent 720fb92 commit 059bbbd

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/lib/server.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,57 @@ export class JsonServer {
134134
}
135135
}
136136

137+
/**
138+
* Creates pagination metadata based on request parameters
139+
*
140+
* @param total - Total number of items
141+
* @param page - Current page number
142+
* @param perPage - Items per page
143+
* @returns Pagination metadata with first, prev, next, last, pages, and items
144+
*/
145+
private createPaginationMetadata(
146+
total: number,
147+
page: number,
148+
perPage: number
149+
): Record<string, any> {
150+
const totalPages = Math.ceil(total / perPage);
151+
152+
return {
153+
first: 1,
154+
prev: page > 1 ? page - 1 : null,
155+
next: page < totalPages ? page + 1 : null,
156+
last: totalPages || 1,
157+
pages: totalPages || 1,
158+
items: total,
159+
};
160+
}
161+
162+
/**
163+
* Get paginated data with metadata
164+
*
165+
* @param collection - Data collection to paginate
166+
* @param page - Current page number (default: 1)
167+
* @param perPage - Items per page (default: 10)
168+
* @returns Object with pagination metadata and data
169+
*/
170+
private getPaginatedData(
171+
collection: any[],
172+
page: number = 1,
173+
perPage: number = 10
174+
): Record<string, any> {
175+
const total = collection.length;
176+
const start = (page - 1) * perPage;
177+
const end = Math.min(start + perPage, total);
178+
const data = collection.slice(start, end);
179+
180+
const paginationMeta = this.createPaginationMetadata(total, page, perPage);
181+
182+
return {
183+
...paginationMeta,
184+
data,
185+
};
186+
}
187+
137188
/**
138189
* Get paginated resources from a collection
139190
*
@@ -190,7 +241,10 @@ export class JsonServer {
190241
* @returns Boolean indicating if there are more pages to iterate
191242
*/
192243
continueToIterate(currentPage: number, pageSize: number, totalItems: number): boolean {
193-
return currentPage * pageSize < totalItems;
244+
// The pagination should continue if there are still more items to display
245+
const startIndex = (currentPage - 1) * pageSize;
246+
const endIndex = startIndex + pageSize;
247+
return endIndex < totalItems;
194248
}
195249

196250
/**

test/server.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,23 @@ describe('JsonServer', () => {
7979
expect(server.setIdField('_id')).toBe(server);
8080
});
8181
});
82+
83+
describe('pagination methods', () => {
84+
it('should determine if pagination should continue', () => {
85+
// Should continue - more pages available
86+
expect(server.continueToIterate(1, 10, 25)).toBe(true);
87+
88+
// Should continue - exactly one more page
89+
expect(server.continueToIterate(1, 10, 20)).toBe(true);
90+
91+
// Should not continue - on last page
92+
expect(server.continueToIterate(2, 10, 20)).toBe(false);
93+
94+
// Should not continue - on last page with incomplete items
95+
expect(server.continueToIterate(3, 10, 25)).toBe(false);
96+
97+
// Edge case - single page
98+
expect(server.continueToIterate(1, 20, 15)).toBe(false);
99+
});
100+
});
82101
});

0 commit comments

Comments
 (0)