3 changed files with 56 additions and 0 deletions
@ -0,0 +1,5 @@ |
|||
/** 公开主页 profile 聚合接口中每类预览条数 */ |
|||
export const PUBLIC_PREVIEW_LIMIT = 5; |
|||
|
|||
/** 公开列表子页每页条数(仅服务端使用) */ |
|||
export const PUBLIC_LIST_PAGE_SIZE = 10; |
|||
@ -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); |
|||
}); |
|||
}); |
|||
@ -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…
Reference in new issue