详情及下载请到github

中文转拼音或者首字母

说明

该工具有以下两点功能:

  • 中文转拼音
  • 中文转首字母

可以传入不同的参数控制输出结果。

安装

使用npm安装:

1
npm install chinese2pinyin

使用

const convert = require('chinese2pinyin');

// 输出拼音ZhongWenZhuanPinYinHuoShouZiMu
convert({ 
    cn: '中文转拼音或首字母',
    result: 'P'
})

// 输出首字母ZWZPYHSZM
convert({ 
    cn: '中文转拼音或首字母',
    result: 'F'
})

// 输出结果为包含两个key(pinyin和first)的对象
convert({ 
    cn: '中文转拼音或首字母',
    result: 'A'
})


/* 使用threshold */

//  threshold=5输出ZWZPYHSZM,threshold=9输出ZhongWenZhuanPinYinHuoShouZiMu
convert({ 
    cn: '中文转拼音或首字母',
    result: 'P',
    threshold: 5
})

/* 使用concatKey */

// 输出Zhong_Wen_Zhuan_Pin_Yin_Huo_Shou_Zi_Mu
convert({
    cn: '中文转拼音或首字母',
    result: 'P',
    concatKey: '_'
})

/* 使用remainSpecial */

// remainSpecial为true输出ZW-Z-PYH_SZM,否则输出ZWZPYHSZM
convert({
    cn: '中文-转-拼音或_首字母',
    result: 'F',
    remainSpecial: true
})

/* 使用double */

//double为true输出ZHWZHPYHSHZM,否则输出ZWZPYHSZM
convert({
    cn: '中文-转-拼音或_首字母',
    result: 'F',
    double: true
})