feat: radical 库 ts 改造完成

This commit is contained in:
tackchen 2020-12-16 12:13:30 +08:00
parent 713c5af664
commit a0f9f8f283
14 changed files with 7402 additions and 5 deletions

View File

@ -5,8 +5,8 @@ import '../src/plugin/trad/ts';
import '../src/plugin/poly/ts';
import '../src/plugin/draw/ts';
import '../src/plugin/idiom/ts';
import '../src/plugin/xhy';
import '../src/plugin/radical';
import '../src/plugin/xhy/ts';
import '../src/plugin/radical/ts';
// import spell from '../src/main/spell-dict-jian.json';
// // import cncharDraw from '../src/plugin/draw';
@ -64,6 +64,19 @@ console.log(cnchar.convert.sparkToSimple('①个亾'));
console.log('①个亾'.convertSparkToTrad()); // 返回 "壹個人" 等价于 cnchar.convert.sparkToTrad
console.log(cnchar.convert.sparkToTrad('①个亾'));
cnchar.xhy.addXhy('歇后语第一句', '歇后语第二句');
console.log(cnchar.xhy('歇后语第一句'));
console.log(cnchar.xhy('大水冲了龙王庙')); // ['大水冲了龙王庙-自家人不识自家人', '大水冲了龙王庙-一家人不认一家人']
console.log(cnchar.xhy('大水', 'fuzzy')); // ['江河里长大水-泥沙俱下', '江河发大水-后浪推前浪', ... ]
console.log(cnchar.xhy('大水', 'fuzzy', 'answer')); // ['泥沙俱下', '后浪推前浪', ... ]
console.log(cnchar.xhy('上晃下摇', 'fuzzy', 'answer', 'second')); // ['醉汉过铁索桥', '扶着醉汉过破桥']
cnchar.radical.setRadical('你', '口');
console.log(cnchar.radical('你好呀')); // "亻女口"
// 返回数组
console.log(cnchar.radical('你好呀', 'array')); // ["亻", "女", "口"]
// window.keys = Object.keys(spell);
// cncharDraw('你好', {

View File

@ -8,10 +8,11 @@ export declare type OrderToWordArg = 'match' | 'matchorder' | 'contain' | 'star
export declare type IdomArg = 'char' | 'stroke' | 'spell' | 'tone';
export declare type SortSpellArg = 'tone' | 'desc';
export declare type TradArg = 'trad' | 'simple' | 'spark';
export declare type XhyArg = 'fuzzy' | 'answer' | 'second';
export declare type AllArgs = SpellArg | StrokeArg
| SpellToWordArg | StrokeToWordArg | OrderToWordArg
| IdomArg | SortSpellArg | TradArg;
| IdomArg | SortSpellArg | TradArg | XhyArg;
export declare type PluginArg = 'order' | 'trad' | 'poly' | 'draw' | 'idiom' | 'xhy' | 'radical';
export declare type ToneType = 0 | 1 | 2 | 3 | 4;

View File

@ -116,7 +116,7 @@ declare global {
}
declare module 'cnchar' {
interface CnCharStatic {
interface ICnChar {
draw: IDraw;
}
}

View File

@ -11,7 +11,7 @@ export declare interface ISetPolyPhrase {
}
declare module 'cnchar' {
interface CnCharStatic {
interface ICnChar {
setPolyPhrase: ISetPolyPhrase;
}
}

File diff suppressed because it is too large Load Diff

5
src/plugin/radical/ts/index.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
import {IRadical} from './types/common';
declare const radical: IRadical;
export default radical;

View File

@ -0,0 +1,33 @@
import {ICnChar} from 'cnchar/types';
import {IRadical, IInitRadical} from './types/common';
import {radical, arg, setCnchar, setRadical} from './radical';
function main (cnchar: ICnChar & {radical?: IRadical}) {
if (cnchar.plugins.indexOf('radical') !== -1) {
return;
}
cnchar.plugins.push('radical');
cnchar.radical = radical;
cnchar.type.radical = arg;
}
const init: IInitRadical = (cnchar?: ICnChar): void => {
if (typeof window === 'object' && !window.CncharRadical) {
window.CncharRadical = radical;
}
if (typeof window === 'object' && window.CnChar) {
main(window.CnChar);
setCnchar(window.CnChar);
} else if (typeof cnchar !== 'undefined') {
main(cnchar);
setCnchar(cnchar);
}
};
radical.init = init;
radical.setRadical = setRadical;
init();
export default radical;

View File

@ -0,0 +1,47 @@
import {ICnChar} from 'cnchar/types';
import {Json} from 'cnchar/types/common';
import dict from './dict/radicals.json';
import {RadicalArg, ISetRadical, IRadical} from './types/common';
const radicals = dict as Json<string>;
export const arg: {
[prop in RadicalArg]: RadicalArg
} = {
array: 'array'
};
let _cnchar: ICnChar;
export const radical = ((
input:string | Array<string>,
...args: Array<RadicalArg>
): string | Array<string> => {
if (_cnchar) {
_cnchar._.checkArgs('radical', args);
}
let res = '';
for (let i = 0; i < input.length; i++) {
const char = radicals[input[i]];
if (char) {
res += char;
} else {
res += input[i];
}
}
if (args.indexOf(arg.array) !== -1 || input instanceof Array) {
return res.split('');
}
return res;
}) as IRadical;
export function setCnchar (cnchar: ICnChar): void {
_cnchar = cnchar;
}
export const setRadical: ISetRadical = (key: string | Json<string>, value?: string): void => {
_cnchar._.mapJson(key, value, (k, v) => {
radicals[k] = v;
});
};

34
src/plugin/radical/ts/types/common.d.ts vendored Normal file
View File

@ -0,0 +1,34 @@
import {ICnChar} from 'cnchar/types';
import {Json} from 'cnchar/types/common';
export declare type RadicalArg = 'array';
export declare interface IRadicalFn {
(text:string | Array<string>, ...radicalArgs: Array<RadicalArg>): string | Array<string>;
}
export declare interface ISetRadical {
(word: string, radical: string): void;
(json: Json<string>): void;
}
export declare interface IInitRadical {
(cnchar?: ICnChar): void;
}
export declare interface IRadical extends IRadicalFn {
setRadical: ISetRadical;
init: IInitRadical;
}
declare global {
interface Window {
CncharRadical: IRadical,
}
}
declare module 'cnchar' {
interface ICnChar {
radical: IRadical;
}
}

File diff suppressed because one or more lines are too long

5
src/plugin/xhy/ts/index.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
import {IXHY} from './types/common';
declare const xhy: IXHY;
export default xhy;

View File

@ -0,0 +1,33 @@
import {ICnChar} from 'cnchar/types';
import {IXHY, IInitXHY} from './types/common';
import {xhy, arg, addXhy, setCnchar} from './xhy';
function main (cnchar: ICnChar & {xhy?: IXHY}): void {
if (cnchar.plugins.indexOf('xhy') !== -1) {
return;
}
setCnchar(cnchar);
cnchar.plugins.push('xhy');
cnchar.xhy = xhy;
cnchar.type.xhy = arg;
}
const init: IInitXHY = (cnchar?: ICnChar): void => {
if (typeof window === 'object' && !window.CncharXHY) {
window.CncharXHY = xhy;
}
if (typeof window === 'object' && window.CnChar) {
main(window.CnChar);
} else if (typeof cnchar !== 'undefined') {
main(cnchar);
}
};
xhy.init = init;
xhy.addXhy = addXhy;
init();
export default xhy;

30
src/plugin/xhy/ts/types/common.d.ts vendored Normal file
View File

@ -0,0 +1,30 @@
import {ICnChar, XhyArg} from 'cnchar/types/index';
export declare type TAddXhyArg1 = Array<Array<string>> | Array<string>;
export declare interface IAddXhy {
(args: TAddXhyArg1): void;
(xhyHead: string, xhyTail: string): void;
}
export declare interface IInitXHY {
(cnchar?: ICnChar): void;
}
export declare interface IXHY {
(text:string, ...xhyArgs: Array<XhyArg>): Array<string>;
addXhy: IAddXhy;
init: IInitXHY;
}
declare global {
interface Window {
CncharXHY: IXHY,
}
}
declare module 'cnchar' {
interface ICnChar {
xhy: IXHY;
}
}

100
src/plugin/xhy/ts/xhy.ts Normal file
View File

@ -0,0 +1,100 @@
import {ICnChar, XhyArg} from 'cnchar/types';
import {dict} from './dict/xhy.json';
import {IAddXhy, IXHY, TAddXhyArg1} from './types/common';
let _cnchar: ICnChar;
export const arg: {
[prop in XhyArg]: XhyArg
} = {
'fuzzy': 'fuzzy',
'answer': 'answer',
'second': 'second'
};
export const xhy = ((str:string, ...args: Array<XhyArg>): Array<string> => {
if (_cnchar) {
_cnchar._.checkArgs('xhy', args);
}
const isFuzzy = args.indexOf(arg.fuzzy) !== -1;
const onlyAnswer = args.indexOf(arg.answer) !== -1;
const second = args.indexOf(arg.second) !== -1;
const quesIndex = second ? 1 : 0;
const answerIndex = 1 - quesIndex;
if (isFuzzy) {
let res: Array<string> = [];
for (let i = 0; i < dict.length; i++) {
if (dict[i][quesIndex].indexOf(str) !== -1) {
const answer = shapeAnswer(dict[i], onlyAnswer, answerIndex);
res = res.concat(answer);
}
}
return res;
} else {
for (let i = 0; i < dict.length; i++) {
if (dict[i][quesIndex] === str) {
return shapeAnswer(dict[i], onlyAnswer, answerIndex);
}
}
}
return [];
}) as IXHY;
function shapeAnswer (item: Array<string>, onlyAnswer: boolean, answerIndex: number): Array<string> {
let answer: Array<string>;
if (item[answerIndex].indexOf('') !== -1) {
answer = item[answerIndex].split('');
} else {
answer = [item[answerIndex]];
}
if (!onlyAnswer) {
const fn = answerIndex === 1 ? (a: string, index: number) => {
answer[index] = `${item[1 - answerIndex]}-${a}`;
} : (a: string, index: number) => {
answer[index] = `${a}-${item[1 - answerIndex]}`;
};
answer.forEach(fn);
}
return answer;
}
export const addXhy: IAddXhy = (arg: TAddXhyArg1 | string, arg2?: string): void => {
if (typeof arg === 'string' && typeof arg2 === 'string') {
addXhy([arg as string, arg2 as string]);
return;
}
if (!(arg instanceof Array)) {
_cnchar._._warn('addXhy 参数必须为数组');
return;
}
if (arg[0] instanceof Array) {
(arg as Array<Array<string>>).forEach((item: Array<string>) => {
addXhy(item);
});
return;
}
dict.push(arg as Array<string>);
};
// export let addXhy: IAddXhy = (arg: string | Array<Array<string>>, arg2?: string): void => {
// if (typeof arg === 'string' && typeof arg2 === 'string') {
// addXhy([arg, arg2]);
// return;
// }
// if (!(arg instanceof Array)) {
// _cnchar._._warn('addXhy 参数必须为数组');
// return;
// }
// if (arg[0] instanceof Array) {
// arg.forEach(item => {
// xhy.addXhy(item);
// });
// return;
// }
// dict.push(arg);
// };
export function setCnchar (cnchar: ICnChar): void {
_cnchar = cnchar;
}