~james/core

core/web/src/lib/pagination.ts -rw-r--r-- 595 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
export const pageParam = (url: { searchParams: Pick<URLSearchParams, "get"> }): number => {
	const raw = url.searchParams.get("page") ?? "";
	const parsed = /^\d+$/.test(raw) ? Number(raw) : 1;
	return parsed >= 1 ? parsed : 1;
};

export const offsetFor = (page: number, limit: number): number | undefined =>
	page > 1 ? (page - 1) * limit : undefined;

export const pageCount = (total: number, limit: number): number =>
	Math.max(1, Math.ceil(Math.max(total, 0) / limit));

export const discoveredPageCount = (page: number, hasNext: boolean): number =>
	Math.max(1, page + (hasNext ? 1 : 0));