Files
usa/src/utils/tickerText.ts
2026-03-02 16:29:11 +08:00

36 lines
946 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 滚动情报文本处理:转为简体中文,过滤非中文内容
*/
import { Converter } from 'opencc-js/t2cn'
const t2s = Converter({ from: 'twp', to: 'cn' })
/** 简体中文字符范围 */
const ZH_REGEX = /[\u4e00-\u9fff]/g
/** 文本中中文占比是否达标至少30% */
export function isMostlyChinese(text: string): boolean {
if (!text?.trim()) return false
const zh = text.match(ZH_REGEX)
const zhCount = zh ? zh.length : 0
return zhCount / text.length >= 0.3
}
/** 繁体转简体 */
export function toSimplifiedChinese(text: string): string {
if (!text?.trim()) return text
try {
return t2s(text)
} catch {
return text
}
}
/** 处理滚动情报项:转为简体,非中文为主则过滤 */
export function processTickerText(text: string): string | null {
const t = (text || '').trim()
if (!t) return null
if (!isMostlyChinese(t)) return null
return toSimplifiedChinese(t)
}