-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
41 lines (37 loc) · 1.03 KB
/
index.js
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
'use strict'
const hanziToPinyin = require('hanzi-to-pinyin')
const splitPinyin = require('pinyin-split')
const pinyinOrHanzi = require('pinyin-or-hanzi')
const utils = require('pinyin-utils')
const convertPinyin = async (text, type, opts) => {
let words = splitPinyin(text, true)
if (opts.numbered && type !== 'pinyin-numbered') {
words = utils.markToNumber(words, false)
}
else if (opts.marked && type !== 'pinyin-marked') {
words = utils.numberToMark(words)
}
else if (!opts.numbered) {
if (type === 'pinyin-marked') {
words = utils.markToNumber(words, false)
}
if (type === 'pinyin-numbered') {
words = utils.numberToMark(words)
}
}
return words.join('')
}
const convert = async (text, opts = {}) => {
const type = await pinyinOrHanzi(text)
if (type === 'other' || type === 'zhuyin') {
return text
}
if (type === 'mandarin') {
return hanziToPinyin(text, opts)
}
if (type.substr(0, 6) === 'pinyin') {
return convertPinyin(text, type, opts)
}
}
module.exports = convert
module.exports.init = hanziToPinyin.init