-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
38 lines (30 loc) · 990 Bytes
/
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
/**
* Get telecode for Chinese
* Returns telecode by each character
* Returns empty string for that character if not found
*
* @param {string} text Text to be translated
* @param {object} options Options object (optional)
* @param {string} options.codeType Traditional or Simplified Chinese or auto (sc preferred)
* @return {Array} Array of telecode
*/
var dict = require("./data/dict.json")
module.exports = function(text, options){
var codes = []
options = options || {}
options.codeType = options.codeType || "auto"
for (var i in text){
var charCode = text.charCodeAt(i).toString(16).toUpperCase()
var telecode = dict[charCode]
if (!telecode) {
codes.push("")
continue
}
if (options.codeType == "auto"){
codes.push(telecode["sc"] || telecode["tc"] || "")
} else {
codes.push(telecode[options.codeType] || "")
}
}
return codes
}