-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathpinyin-tool.js
More file actions
38 lines (35 loc) · 1.08 KB
/
pinyin-tool.js
File metadata and controls
38 lines (35 loc) · 1.08 KB
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
/**
* 拼音工具模块
*
* 提供中文文本转拼音首字母的功能,主要用于:
* - 生成中文内容的首字母缩写
* - 创建搜索索引
* - 文件名或标识符生成
*
* @author drpy-node
* @version 1.0.0
*/
import {pinyin} from 'pinyin';
/**
* 获取中文文本的拼音首字母
*
* 将输入的中文文本转换为对应的拼音首字母大写组合
* 例如:"中国" -> "ZG","北京市" -> "BJS"
*
* @param {string} text - 需要转换的中文文本
* @returns {string} 拼音首字母大写组合字符串
*
* @example
* // 基本用法
* getFirstLetter('中国'); // 返回 'ZG'
* getFirstLetter('北京市'); // 返回 'BJS'
* getFirstLetter('上海浦东新区'); // 返回 'SHPDXQ'
*/
export function getFirstLetter(text) {
// 使用pinyin库将中文转换为拼音首字母
const pinyinArray = pinyin(text, {
style: pinyin.STYLE_FIRST_LETTER, // 设置样式为首字母
});
// 将拼音数组转换为大写字母并连接成字符串
return pinyinArray.map(item => item[0].toUpperCase()).join('');
}