-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
79 lines (72 loc) · 2.22 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const toneParse = require('pinyin-tone-parse')
const tones = {
a: ['a', 'ā', 'á', 'ǎ', 'à', 'a'],
A: ['A', 'Ā', 'Á', 'Ǎ', 'À', 'A'],
e: ['e', 'ē', 'é', 'ě', 'è', 'e'],
E: ['E', 'Ē', 'É', 'Ě', 'È', 'E'],
i: ['i', 'ī', 'í', 'ǐ', 'ì', 'i'],
I: ['I', 'Ī', 'Í', 'Ǐ', 'Ì', 'I'],
o: ['o', 'ō', 'ó', 'ǒ', 'ò', 'o'],
O: ['O', 'Ō', 'Ó', 'Ǒ', 'Ò', 'O'],
u: ['u', 'ū', 'ú', 'ǔ', 'ù', 'u'],
U: ['U', 'Ū', 'Ú', 'Ǔ', 'Ù', 'U'],
ü: ['ü', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
Ü: ['Ü', 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ', 'Ü'],
}
/**
* Convert pinyin tone numbers to tone marks.
* @param {String} text
* @param {Object} options
* @returns {String}
* @throws {Error}
*/
function convert(text, options) {
if (typeof text !== 'string') {
throw new Error('Parameter `text` must be a string.')
}
let result = ''
toneParse(text, options).forEach(elem => {
if (Array.isArray(elem)) {
result += mark(...elem)
} else {
result += elem
}
})
return result
}
/**
* @param {String} word
* @param {Number} toneNumber
* @returns {String} Word with marked tone
*/
function mark(word, toneNumber) {
if (toneNumber === 5) {
return word
}
// Check for syllables in priority order
if (word.includes('a')) {
return word.replace('a', tones.a[toneNumber])
} else if (word.includes('A')) {
return word.replace('A', tones.A[toneNumber])
} else if (word.includes('e')) {
return word.replace('e', tones.e[toneNumber])
} else if (word.includes('E')) {
return word.replace('E', tones.E[toneNumber])
} else if (word.includes('o')) {
return word.replace('o', tones.o[toneNumber])
} else if (word.includes('O')) {
return word.replace('O', tones.O[toneNumber])
} else {
// Mark last syllable
for (let i = word.length - 1; i >= 0; i--) {
const index = 'iouüIOUÜ'.indexOf(word[i])
if (index !== -1) {
// last syllable found
return word.replace(word[i], tones[word[i]][toneNumber])
}
}
// No syllables found
return word
}
}
module.exports = convert