2baf29b3ae
Deliver encrypted health zone, observation API, baseline maturity pipeline, page-data bindings, and H5/WeChat channel integration for health P0. Co-authored-by: Cursor <cursoragent@cursor.com>
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
const DIGIT_MAP = Object.freeze({
|
||
零: 0,
|
||
〇: 0,
|
||
一: 1,
|
||
壹: 1,
|
||
二: 2,
|
||
两: 2,
|
||
贰: 2,
|
||
三: 3,
|
||
叁: 3,
|
||
四: 4,
|
||
肆: 4,
|
||
五: 5,
|
||
伍: 5,
|
||
六: 6,
|
||
陆: 6,
|
||
七: 7,
|
||
柒: 7,
|
||
八: 8,
|
||
捌: 8,
|
||
九: 9,
|
||
玖: 9,
|
||
});
|
||
|
||
/** 解析常见中文整数(0–199),供老年用户口述血压使用。 */
|
||
export function parseChineseInteger(text) {
|
||
const raw = String(text ?? '').trim();
|
||
if (!raw) return null;
|
||
if (/^\d{1,3}$/.test(raw)) {
|
||
const n = Number(raw);
|
||
return Number.isFinite(n) ? n : null;
|
||
}
|
||
if (raw === '十') return 10;
|
||
if (raw.includes('百')) {
|
||
const [hundredsPart, rest = ''] = raw.split('百');
|
||
const hundreds = hundredsPart ? (DIGIT_MAP[hundredsPart] ?? (hundredsPart === '' ? 1 : 0)) : 1;
|
||
let remainder = 0;
|
||
if (rest) {
|
||
remainder = parseChineseInteger(rest);
|
||
if (remainder == null) return null;
|
||
}
|
||
return hundreds * 100 + remainder;
|
||
}
|
||
if (raw.startsWith('十')) {
|
||
const ones = DIGIT_MAP[raw.slice(1)] ?? 0;
|
||
return 10 + ones;
|
||
}
|
||
if (raw.endsWith('十') && raw.length === 2) {
|
||
return (DIGIT_MAP[raw[0]] ?? 0) * 10;
|
||
}
|
||
if (raw.includes('十')) {
|
||
const [tensPart, onesPart = ''] = raw.split('十');
|
||
const tens = tensPart ? (DIGIT_MAP[tensPart] ?? 0) : 1;
|
||
const ones = onesPart ? (DIGIT_MAP[onesPart] ?? 0) : 0;
|
||
return tens * 10 + ones;
|
||
}
|
||
if (raw.length === 1 && raw in DIGIT_MAP) return DIGIT_MAP[raw];
|
||
return null;
|
||
}
|