diff --git a/server/constants/public-profile-lists.ts b/server/constants/public-profile-lists.ts new file mode 100644 index 0000000..d0cf4c8 --- /dev/null +++ b/server/constants/public-profile-lists.ts @@ -0,0 +1,5 @@ +/** 公开主页 profile 聚合接口中每类预览条数 */ +export const PUBLIC_PREVIEW_LIMIT = 5; + +/** 公开列表子页每页条数(仅服务端使用) */ +export const PUBLIC_LIST_PAGE_SIZE = 10; diff --git a/server/utils/public-pagination.test.ts b/server/utils/public-pagination.test.ts new file mode 100644 index 0000000..9350156 --- /dev/null +++ b/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); + }); +}); diff --git a/server/utils/public-pagination.ts b/server/utils/public-pagination.ts new file mode 100644 index 0000000..b2b36c5 --- /dev/null +++ b/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); +}