Browse Source

feat(server): add public list pagination constants and page normalizer

Made-with: Cursor
main
npmrun 6 hours ago
parent
commit
a31b9db636
  1. 5
      server/constants/public-profile-lists.ts
  2. 36
      server/utils/public-pagination.test.ts
  3. 15
      server/utils/public-pagination.ts

5
server/constants/public-profile-lists.ts

@ -0,0 +1,5 @@
/** 公开主页 profile 聚合接口中每类预览条数 */
export const PUBLIC_PREVIEW_LIMIT = 5;
/** 公开列表子页每页条数(仅服务端使用) */
export const PUBLIC_LIST_PAGE_SIZE = 10;

36
server/utils/public-pagination.test.ts

@ -0,0 +1,36 @@
import { describe, expect, test } from "bun:test";
import { normalizePublicListPage } from "./public-pagination";
describe("normalizePublicListPage", () => {
test("returns 1 for non-finite or invalid input", () => {
expect(normalizePublicListPage(undefined)).toBe(1);
expect(normalizePublicListPage(null)).toBe(1);
expect(normalizePublicListPage("")).toBe(1);
expect(normalizePublicListPage("abc")).toBe(1);
expect(normalizePublicListPage(NaN)).toBe(1);
expect(normalizePublicListPage(Infinity)).toBe(1);
expect(normalizePublicListPage(-Infinity)).toBe(1);
expect(normalizePublicListPage({})).toBe(1);
});
test("returns 1 for values less than 1", () => {
expect(normalizePublicListPage(0)).toBe(1);
expect(normalizePublicListPage(-1)).toBe(1);
expect(normalizePublicListPage(-3.5)).toBe(1);
expect(normalizePublicListPage("0")).toBe(1);
expect(normalizePublicListPage("-1")).toBe(1);
});
test("returns positive integers unchanged (or parsed)", () => {
expect(normalizePublicListPage(1)).toBe(1);
expect(normalizePublicListPage(42)).toBe(42);
expect(normalizePublicListPage("5")).toBe(5);
expect(normalizePublicListPage("10")).toBe(10);
});
test("floors positive floats", () => {
expect(normalizePublicListPage(2.7)).toBe(2);
expect(normalizePublicListPage(3.99)).toBe(3);
expect(normalizePublicListPage(1.0001)).toBe(1);
});
});

15
server/utils/public-pagination.ts

@ -0,0 +1,15 @@
/**
* ?page= query <1 时返回 1否则返回正整数float 向下取整
*/
export function normalizePublicListPage(raw: unknown): number {
const n =
typeof raw === "string"
? Number.parseInt(raw, 10)
: typeof raw === "number"
? raw
: Number.NaN;
if (!Number.isFinite(n) || n < 1) {
return 1;
}
return Math.floor(n);
}
Loading…
Cancel
Save