-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFanhuaji.js
109 lines (87 loc) · 2.44 KB
/
Fanhuaji.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 本程式使用了繁化姬的 API 服務,以作為正確性備用檢核。繁化姬(https://zhconvert.org/)商用必須付費。
*
* @since 2022/6/30 11:14:58
*/
'use strict';
// modify from wikiapi.js
let CeL;
try {
// Load CeJS library.
CeL = require('cejs');
} catch (e) /* istanbul ignore next: Only for debugging locally */ {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md
require('./_CeL.loader.nodejs.js');
CeL = globalThis.CeL;
}
// assert: typeof CeL === 'function'
// 在非 Windows 平台上避免 fatal 錯誤。
//CeL.env.ignore_COM_error = true;
// Load modules.
CeL.run(['application.debug',
// 載入不同地區語言的功能 for wiki.work()。
'application.locale',
// Add color to console messages. 添加主控端報告的顏色。
'interact.console',
//for CeL.URI()
'application.net',
//for CeL.get_URL()
'application.net.Ajax',
// for 'application.platform.nodejs': CeL.env.arg_hash, CeL.wiki.cache(),
// CeL.fs_mkdir(), CeL.wiki.read_dump()
'application.storage']);
// ----------------------------------------------------------------------------
function Fanhuaji() {
;
}
const convert_separator = '[sep]';
async function Fanhuaji_converter(text, options) {
// https://docs.zhconvert.org/api/convert/
var url = new CeL.URI('https://api.zhconvert.org/convert');
url.search_params.set_parameters({
//apiKey: '',
//outputFormat: 'json',
});
const is_Array = Array.isArray(text);
if (is_Array) {
text = text.join(convert_separator);
}
return new Promise((resolve, reject) => {
CeL.get_URL(url, (XMLHttp, error) => {
if (error) {
reject(error);
return;
}
try {
const result = JSON.parse(XMLHttp.responseText);
result = result.data.text;
if (is_Array) {
result = result.split(convert_separator);
if (text.lenth !== result.lenth)
throw new Error(`Fanhuaji_converter: The length of text list is different! ${text.lenth}!==${result.lenth}`);
}
resolve(result);
} catch (e) {
reject(e);
}
}, null, {
text: text,
converter: options?.converter,
});
});
}
async function Fanhuaji_to_TW(text, options) {
return await Fanhuaji_converter(text, {
converter: 'Traditional'
});
}
async function Fanhuaji_to_CN(text, options) {
return await Fanhuaji_converter(text, {
converter: 'Simplified'
});
}
Object.assign(Fanhuaji, {
to_TW: Fanhuaji_to_TW,
to_CN: Fanhuaji_to_CN,
});
module.exports = Fanhuaji;