@saihu/common 1.2.3 → 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,2 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取中文字符串的拼音首字母。
|
|
3
|
+
*
|
|
4
|
+
* - 保留 `-` 分隔符(每段独立取首字母后用 `-` 重新拼接)
|
|
5
|
+
* - 非中文字符被忽略
|
|
6
|
+
* - 特殊处理:`调-` → `条-`
|
|
7
|
+
*
|
|
8
|
+
* @example chnInitials('爱国') // 'ag'
|
|
9
|
+
* @example chnInitials('调-味品') // 'tw-wp'
|
|
10
|
+
*/
|
|
1
11
|
export declare function chnInitials(text: string): string;
|
|
2
12
|
//# sourceMappingURL=chn-initials.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chn-initials.d.ts","sourceRoot":"","sources":["../../src/util/chn-initials.ts"],"names":[],"mappings":"AAEA,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"}
|
|
@@ -2,14 +2,48 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.chnInitials = chnInitials;
|
|
4
4
|
const pinyin_pro_1 = require("pinyin-pro");
|
|
5
|
+
/**
|
|
6
|
+
* 获取中文字符串的拼音首字母。
|
|
7
|
+
*
|
|
8
|
+
* - 保留 `-` 分隔符(每段独立取首字母后用 `-` 重新拼接)
|
|
9
|
+
* - 非中文字符被忽略
|
|
10
|
+
* - 特殊处理:`调-` → `条-`
|
|
11
|
+
*
|
|
12
|
+
* @example chnInitials('爱国') // 'ag'
|
|
13
|
+
* @example chnInitials('调-味品') // 'tw-wp'
|
|
14
|
+
*/
|
|
5
15
|
function chnInitials(text) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
if (!text || typeof text !== 'string')
|
|
17
|
+
return '';
|
|
18
|
+
// 特殊处理"调-"到"条-"
|
|
19
|
+
text = text.replace(/调-/g, '条-');
|
|
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
|
+
}
|
|
11
31
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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) : '';
|
|
15
49
|
}
|