Files
memind/health-chinese-numerals.mjs
T
john 2baf29b3ae feat(health): complete P0 health channel — baseline engine, page-data, MindSpace UI
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>
2026-09-09 18:04:57 +08:00

60 lines
1.4 KiB
JavaScript
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.
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;
}