91f429f1a0
新增 version-display.ts:公开下载 URL 校验与超长 URL 中段省略展示。 TDD: task-130.test.ts 8 用例 RED→GREEN。
17 lines
850 B
TypeScript
17 lines
850 B
TypeScript
/** 版本下载链接展示(任务 130):公开下载 URL 校验与超长 URL 中段省略;不写入日志;纯逻辑。 */
|
|
|
|
export function isPublicDownloadUrl(value: unknown): boolean {
|
|
const url = typeof value === 'string' ? value.trim() : ''
|
|
return /^https?:\/\//i.test(url)
|
|
}
|
|
|
|
/** 展示用下载地址:超长时保留首尾并中段省略;非法/空返回空串。 */
|
|
export function displayDownloadUrl(value: unknown, maxLength = 80): string {
|
|
const url = typeof value === 'string' ? value.trim() : ''
|
|
if (!isPublicDownloadUrl(url)) return ''
|
|
const max = typeof maxLength === 'number' && Number.isFinite(maxLength) && maxLength > 8 ? Math.floor(maxLength) : 80
|
|
if (url.length <= max) return url
|
|
const keep = Math.max(4, Math.floor((max - 1) / 2))
|
|
return `${url.slice(0, keep)}…${url.slice(url.length - keep)}`
|
|
}
|