| 144 | * @returns The normalized string. |
| 145 | */ |
| 146 | const normalize = (date: string): string => { |
| 147 | let str = date.toLowerCase().trim(); |
| 148 | |
| 149 | if (REGEX_JUST_NOW.test(str)) { |
| 150 | return 'just now'; |
| 151 | } |
| 152 | |
| 153 | // 1. Quantifiers: 'a'/'an' -> '1' |
| 154 | str = str.replaceAll(/(^|\s)an?(\s)/g, '$11$2'); |
| 155 | |
| 156 | // 2. Vague 'x' (English needs boundary) |
| 157 | str = str.replaceAll(/(^|\s)x(\s|$)/g, '$13$2'); |
| 158 | |
| 159 | // 3. Vague Chinese '几/数' (No boundary needed) |
| 160 | str = str.replaceAll(/[几幾数]/g, '3'); |
| 161 | |
| 162 | // 4. Chinese numerals |
| 163 | str = str.replaceAll(/[一二两三四五六七八九十]/g, (match) => CN_NUM_MAP[match] || match); |
| 164 | |
| 165 | // 5. Remove commas |
| 166 | str = str.replaceAll(',', ''); |
| 167 | |
| 168 | return str; |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * Parses a string to extract the total duration based on predefined unit patterns. |