@saihu/common 1.2.4 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chn-initials.d.ts","sourceRoot":"","sources":["../../src/util/chn-initials.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"chn-initials.d.ts","sourceRoot":"","sources":["../../src/util/chn-initials.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAyBhD"}
|
|
@@ -17,12 +17,33 @@ function chnInitials(text) {
|
|
|
17
17
|
return '';
|
|
18
18
|
// 特殊处理"调-"到"条-"
|
|
19
19
|
text = text.replace(/调-/g, '条-');
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
const parts = text.split('-');
|
|
21
|
+
const result = [];
|
|
22
|
+
for (const part of parts) {
|
|
23
|
+
const partInitials = [];
|
|
24
|
+
for (const c of part) {
|
|
25
|
+
if (isChinese(c)) {
|
|
26
|
+
const firstLetter = getFirstLetter(c);
|
|
27
|
+
if (firstLetter.length > 0) {
|
|
28
|
+
partInitials.push(firstLetter);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (partInitials.length > 0) {
|
|
33
|
+
result.push(partInitials.join(''));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result.join('-');
|
|
37
|
+
}
|
|
38
|
+
function isChinese(c) {
|
|
39
|
+
const code = c.charCodeAt(0);
|
|
40
|
+
return code >= 0x4e00 && code <= 0x9fff;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 获取单个汉字的拼音首字母(无声调)
|
|
44
|
+
*/
|
|
45
|
+
function getFirstLetter(c) {
|
|
46
|
+
// toneType: 'none' → 不带声调的完整拼音,如 爱 → 'ai'
|
|
47
|
+
const py = (0, pinyin_pro_1.pinyin)(c, { toneType: 'none' });
|
|
48
|
+
return py.length > 0 ? py.charAt(0) : '';
|
|
28
49
|
}
|