32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
/** 历史页“指定用户”下拉选项解析(module 13 task 259):/api/admin/users?page_size=999 的用户精简选项。纯逻辑。 */
|
|
import { unwrap } from '../../api/envelope.ts'
|
|
|
|
export interface HistoryUserOption {
|
|
id: number
|
|
username: string
|
|
}
|
|
|
|
function idOf(value: unknown): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) && value >= 1 ? Math.floor(value) : null
|
|
}
|
|
|
|
function text(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
export function parseHistoryUserOptions(payload: unknown): HistoryUserOption[] {
|
|
const core = unwrap<unknown>(payload)
|
|
if (!core || typeof core !== 'object') return []
|
|
const items = (core as Record<string, unknown>).items
|
|
if (!Array.isArray(items)) return []
|
|
const options: HistoryUserOption[] = []
|
|
for (const raw of items) {
|
|
if (!raw || typeof raw !== 'object') continue
|
|
const row = raw as Record<string, unknown>
|
|
const id = idOf(row.id)
|
|
if (id === null) continue
|
|
options.push({ id, username: text(row.username) || `#${id}` })
|
|
}
|
|
return options
|
|
}
|