78 lines
1.7 KiB
TypeScript
78 lines
1.7 KiB
TypeScript
import { extractNumericPart } from '@/utils/numeric-input-parse'
|
|
|
|
const CN_DIGIT: Record<string, number> = {
|
|
零: 0,
|
|
一: 1,
|
|
二: 2,
|
|
两: 2,
|
|
三: 3,
|
|
四: 4,
|
|
五: 5,
|
|
六: 6,
|
|
七: 7,
|
|
八: 8,
|
|
九: 9,
|
|
十: 10
|
|
}
|
|
|
|
/** 解析简单中文数字(一~十、十一等),无法解析返回 null */
|
|
export function parseChineseNumeral(text: string): number | null {
|
|
const v = (text || '').trim()
|
|
if (!v) return null
|
|
|
|
if (/^\d+$/.test(v)) {
|
|
const n = Number(v)
|
|
return Number.isFinite(n) ? n : null
|
|
}
|
|
|
|
if (v.length === 1 && CN_DIGIT[v] != null) {
|
|
return CN_DIGIT[v]
|
|
}
|
|
|
|
if (v.startsWith('十')) {
|
|
const rest = v.slice(1)
|
|
if (!rest) return 10
|
|
const tail = CN_DIGIT[rest]
|
|
return tail != null ? 10 + tail : null
|
|
}
|
|
|
|
if (v.endsWith('十')) {
|
|
const head = CN_DIGIT[v.slice(0, 1)]
|
|
return head != null ? head * 10 : null
|
|
}
|
|
|
|
const shiIdx = v.indexOf('十')
|
|
if (shiIdx > 0) {
|
|
const head = CN_DIGIT[v.slice(0, shiIdx)]
|
|
const tail = CN_DIGIT[v.slice(shiIdx + 1)] ?? 0
|
|
if (head != null) return head * 10 + tail
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
/** 解析胎次:阿拉伯数字、中文数字、「N胎」 */
|
|
export function parseBirthOrder(value: string): number | null | undefined {
|
|
const v = (value || '').trim()
|
|
if (!v) return undefined
|
|
|
|
if (/头胎|第一胎/.test(v)) return 1
|
|
|
|
const num = extractNumericPart(v)
|
|
if (num != null && num !== '') {
|
|
const n = Number(num)
|
|
return Number.isFinite(n) ? n : undefined
|
|
}
|
|
|
|
const taiMatch = v.match(/([一二三四五六七八九十两]+)胎/)
|
|
if (taiMatch) {
|
|
const n = parseChineseNumeral(taiMatch[1])
|
|
if (n != null) return n
|
|
}
|
|
|
|
const cn = parseChineseNumeral(v)
|
|
if (cn != null) return cn
|
|
|
|
return undefined
|
|
}
|