/
info.ts
60 lines (48 loc) · 1.62 KB
/
info.ts
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
import fiveElementMap from './dict/five-element.json';
import methodMap from './dict/method.json';
import infoDict from './dict/info.json';
import {IInfo, IInfoResult} from 'cnchar-types/plugin/info';
import ICnChar from 'cnchar-types';
import {Json} from 'cnchar-types/main/common';
import {findEqualKeyInMap, mapJson} from '@common/util';
export function getDict () {
return {
fiveElementMap,
methodMap,
info: infoDict
};
}
let cnchar: ICnChar;
export function setCnchar (_cnchar: ICnChar) {
cnchar = _cnchar;
}
export const info = ((input: string) => {
const result: IInfoResult[] = [];
for (let i = 0; i < input.length; i++) {
const word = input[i];
const dictResult = getSingleWordInfo(infoDict, word);
result.push(dictResult);
}
return result;
}) as IInfo;
info.setInfo = (words:string | Json<IInfoResult>, data?: IInfoResult) => {
mapJson(words, data, (k, v) => {
const method = findEqualKeyInMap(methodMap, v.method || '-');
const fiveElement = findEqualKeyInMap(fiveElementMap, v.fiveElement || '-');
(infoDict as any)[k] = `${method}${fiveElement}${v.markSpell}`;
});
};
function getSingleWordInfo (dict: Json<string>, word: string): IInfoResult {
let str = dict[word];
if (!str) {
if (cnchar?.hasPlugin('trad')) {
str = cnchar.trad.dict?.info?.[word];
}
}
if (!str) return {method: '', fiveElement: '', markSpell: ''};
return {
method: (methodMap as Json)[str[0]],
fiveElement: (fiveElementMap as Json)[str[1]],
markSpell: str.substring(2),
};
}