forked from smilechun/cantonese2pinyin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cantonese2pinyin.js
34 lines (28 loc) · 925 Bytes
/
cantonese2pinyin.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
'use strict';
var cantCharDict = require('./dict/cantCharDic.json');
var pinyinDict = require('./dict/pinyinDict.json');
var codePointDict = require('./dict/codePointDict.json');
function getPinyin (c) {
var result = (!isNaN(c) ? codePointDict[c] : cantCharDict[c]);
return Array.isArray(result) ? result.map(function (child) {
return child.pinyin
})[0] : null;
}
function getCodePoint (c) {
c = c.toString();
var result = (c.match(/^[a-z]+$/i) ? pinyinDict[c] : cantCharDict[c]);
return Array.isArray(result) ? result.map(function (child) {
return child.codepoint
}) : null;
}
function getChar (c) {
var result = (!isNaN(c) ? codePointDict[c] : pinyinDict[c]);
return Array.isArray(result) ? result.map(function (child) {
return child.char
}) : null;
}
module.exports = {
getPinyin: getPinyin,
getChar: getChar,
getCodePoint: getCodePoint
};