-
Notifications
You must be signed in to change notification settings - Fork 839
/
format.ts
71 lines (61 loc) · 2.15 KB
/
format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { ENUM_PINYIN_STYLE, PHONETIC_SYMBOL, INITIALS } from "./constant";
import { hasKey } from "./util";
/*
* 格式化拼音为声母(Initials)形式。
* @param {string} py 原始拼音字符串。
* @return {string} 转换后的拼音声母部分。
*/
function initials(py: string): string {
for (let i = 0, l = INITIALS.length; i < l; i++){
if (py.indexOf(INITIALS[i]) === 0) {
return INITIALS[i];
}
}
return "";
}
const RE_PHONETIC_SYMBOL = new RegExp("([" + Object.keys(PHONETIC_SYMBOL).join("") + "])", "g");
const RE_TONE2 = /([aeoiuvnm])([0-4])$/;
/**
* 格式化拼音风格。
*
* @param {string} pinyin TONE 风格的拼音。
* @param {ENUM_PINYIN_STYLE} style 目标转换的拼音风格。
* @return {string} 转换后的拼音。
*/
export function toFixed(pinyin: string, style: ENUM_PINYIN_STYLE): string {
let tone = ""; // 声调。
let first_letter: string;
let py: string;
switch(style) {
case ENUM_PINYIN_STYLE.INITIALS:
return initials(pinyin);
case ENUM_PINYIN_STYLE.FIRST_LETTER:
first_letter = pinyin.charAt(0);
if (hasKey(PHONETIC_SYMBOL, first_letter)) {
first_letter = PHONETIC_SYMBOL[first_letter].charAt(0);
}
return first_letter;
case ENUM_PINYIN_STYLE.NORMAL:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1");
});
case ENUM_PINYIN_STYLE.PASSPORT:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1").replace("v", "YU");
}).toUpperCase();
case ENUM_PINYIN_STYLE.TO3NE:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic];
});
case ENUM_PINYIN_STYLE.TONE2:
py = pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1: string){
// 声调数值。
tone = PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$2");
return PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$1");
});
return py + tone;
case ENUM_PINYIN_STYLE.TONE:
default:
return pinyin;
}
}