feat: idiom ts 改造完成

This commit is contained in:
tackchen 2020-12-15 10:42:10 +08:00
parent 4741973e6c
commit 91af238823
14 changed files with 292 additions and 26 deletions

View File

@ -4,7 +4,7 @@ import '../src/plugin/order';
import '../src/plugin/trad';
import '../src/plugin/poly';
import '../src/plugin/draw/ts';
import '../src/plugin/idiom';
import '../src/plugin/idiom/ts';
import '../src/plugin/xhy';
import '../src/plugin/radical';
@ -40,7 +40,10 @@ console.log(cnchar.spellToWord('lv2'));
console.log(cnchar.spellInfo('lǘ'));
console.log(cnchar.strokeToWord(1));
console.log('美好的地方'.spell('tone'));
// 根据汉字查询成语,末尾的空格可以省略
console.log(cnchar.idiom(['五', '', '十', ''])); // ['五风十雨', '五光十色']
// 根据笔画数查询成语0表示匹配任意笔画末尾的0可以省略
console.log(cnchar.idiom([4, 6, 2, 0], 'stroke')); // ["不当人子", ... ]
// window.keys = Object.keys(spell);
@ -65,25 +68,25 @@ console.log('美好的地方'.spell('tone'));
// }
// }
// });
cnchar.draw('中国', {
type: cnchar.draw.TYPE.TEST,
style: {
radicalColor: '#44f',
backgroundColor: '#eee',
length: 100,
// cnchar.draw('中国', {
// type: cnchar.draw.TYPE.TEST,
// style: {
// radicalColor: '#44f',
// backgroundColor: '#eee',
// length: 100,
},
});
// },
// });
cnchar.draw('你好', {
type: cnchar.draw.TYPE.ANIMATION,
style: {
radicalColor: '#44f',
backgroundColor: '#eee',
length: 120,
// cnchar.draw('你好', {
// type: cnchar.draw.TYPE.ANIMATION,
// style: {
// radicalColor: '#44f',
// backgroundColor: '#eee',
// length: 120,
},
});
// },
// });
window.cnchar = cnchar;

View File

@ -5,7 +5,7 @@ export declare type StrokeArg = 'letter' | 'shape' | 'count' | 'name' | 'detail'
export declare type SpellToWordArg = 'poly' | 'alltone' | 'array' | 'simple' | 'trad';
export declare type StrokeToWordArg = 'array' | 'simple' | 'trad';
declare type OrderToWordArg = 'match' | 'matchorder' | 'contain' | 'start' | 'array' | 'simple' | 'trad';
declare type IdomArg = 'char' | 'stroke' | 'spell' | 'tone';
export declare type IdomArg = 'char' | 'stroke' | 'spell' | 'tone';
export declare type SortSpellArg = 'tone' | 'desc';
export declare type AllArgs = SpellArg | StrokeArg

View File

@ -1,8 +1,5 @@
import {IDraw} from './types/common';
declare const draw: IDraw;
declare module 'cnchar' {
interface CnCharStatic {
draw: IDraw;
}
}
export default draw;

View File

View File

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

View File

@ -45,6 +45,7 @@ function idiom (...args) {
} else {
_cnchar._.checkArgs('idiom', args);
if (_cnchar._.has(args, arg.spell)) {
debugger;
res = idiomWithSpell(input, _cnchar._.has(args, arg.tone));
} else if (_cnchar._.has(args, arg.stroke)) {
res = idiomWithStroke(input);
@ -67,6 +68,7 @@ function idiomWithSpell (input, tone) {
const _dict = tone ? spellDict : spellNoToneDict;
if (tone) {
debugger;
input = _cnchar._.transformTone(input, true).spell;
}

View File

@ -1,7 +1,7 @@
declare type idiomArg = 'char' | 'stroke' | 'spell' | 'tone';
export declare interface Idiom {
(text:string | Array<string|number>, ...idiomArgs: Array<idiomArg>):Array<string>;
(text:string | number | Array<string|number>, ...idiomArgs: Array<idiomArg>):Array<string>;
}
declare const idiom: Idiom;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,199 @@
/**
* ['a','','c','']
* [1,0,1,0]
* ['shi', '']
*/
// 小程序中json直接放一个数组会有异常所以包一个key
import {ICnChar, IdomArg} from 'cnchar/types/index';
import {idiom as dict} from './dict/idiom.json';
import {spell as spellDict} from './dict/spell.json';
import {spell as spellNoToneDict} from './dict/spell.notone.json';
import {IIdiom, TIdiomArg, TIdiomInput} from './types/common';
let _cnchar: ICnChar;
export const arg: TIdiomArg = {
char: 'char',
stroke: 'stroke',
spell: 'spell',
tone: 'tone'
};
// spell > stroke > char
// spell 和 stroke 仅在 引入cnchar之后才可用
export const idiom: IIdiom = (input: TIdiomInput, ...args: Array<IdomArg>): Array<string> | void => {
if (!input) {
console.warn('idiom: 请输入搜索项');
return;
}
if (args.indexOf(arg.spell) !== -1 && typeof input !== 'string') {
console.warn('idiom spell 模式下仅支持查询首个汉字的拼音');
return;
}
if (!(input instanceof Array || args.indexOf(arg.spell) !== -1 || (typeof input === 'number' && args.indexOf(arg.stroke) !== -1))) {
console.warn('idiom 输入参数仅支持数组 或 stroke模式下的数字');
return;
}
let res = null;
if (!_cnchar) { // 单独使用的idiom 只支持汉字查询方式
checkArg(args, arg.stroke);
checkArg(args, arg.spell);
res = idiomWithChar(input as string[]);
} else {
_cnchar._.checkArgs('idiom', args);
if (_cnchar._.has(args, arg.spell)) {
res = idiomWithSpell(input as string, _cnchar._.has(args, arg.tone));
} else if (_cnchar._.has(args, arg.stroke)) {
res = idiomWithStroke(input as number | number[]);
} else {
res = idiomWithChar(input as string[]);
}
}
return res;
};
function idiomWithChar (input: string[]): Array<string> {
return dict.filter((item) => {
return compareCommon(input, item.split(''));
});
}
// needTone是否需要匹配音调
function idiomWithSpell (input: string, tone: boolean): Array<string> {
// console.log(input, tone);
const total = dict.length;
const _dict = tone ? spellDict : spellNoToneDict;
if (tone) {
input = _cnchar._.transformTone(input, true).spell;
}
const filter = _dict.filter((item) => {
return input === item.split(':')[0];
});
if (filter.length === 0) {
return [];
}
let res: Array<string> = [];
const n = _dict.length - 1;
filter.forEach(item => {
const index = _dict.indexOf(item);
const curDIndex = parseInt(item.split(':')[1]);
const nextDIndex = index === n ? total : parseInt(_dict[index + 1].split(':')[1]);
res = res.concat(dict.slice(curDIndex, nextDIndex));
});
return res;
// let low = 0;
// let high = total;
// input = input.toLowerCase();
// if (tone) {
// input = _cnchar._.transformTone(input, 'tone').spell;
// }
// let target = input.split('').map(c => getCharCode(c, tone));
// let area = step1FindArea(low, high, target, tone);
// if (area === TYPE.ERROR) {
// return [];
// }
// let floor = step2FindFloor(area.low, area.mid, target, tone).mid;
// let ceil = step3FindCeil(area.mid, area.high, target, tone).mid;
// return dict.slice(floor, ceil + 1);
}
// 二分查找拼音依赖拼音准确率,可能会不准确,故放弃
// function binarySearch (low, high, condition) {
// if (low > high) {
// return TYPE.ERROR;
// }
// let mid = Math.floor((low + high) / 2);
// let res = condition(mid);
// if (res === TYPE.MORE) {
// return binarySearch(low, mid - 1, condition);
// } else if (res === TYPE.LESS) {
// return binarySearch(mid + 1, high, condition);
// } else {
// return {low, high, mid};
// }
// }
// function step1FindArea (low, high, target, tone) { // 找到一个区间,该区间包含所有拼音,且中值为该拼音
// return binarySearch(low, high, (mid) => {
// return _cnchar._.compareSpell(mid, target, tone);
// });
// }
// function step2FindFloor (low, high, target, tone) { // 查找下界
// return binarySearch(low, high, (mid) => {
// return compareBoundary(mid, target, TYPE.MORE, TYPE.LESS, -1, tone);
// });
// }
// function step3FindCeil (low, high, target, tone) { // 查找上界
// return binarySearch(low, high, (mid) => {
// return compareBoundary(mid, target, TYPE.LESS, TYPE.MORE, 1, tone);
// });
// }
// typeCenter 朝区域中心的大小type typeSide 朝区域两端的大小type
// function compareBoundary (mid, target, typeCenter, typeSide, neighborPlus, tone) {
// let res = _cnchar._.compareSpell(mid, target, tone);
// if (res === typeSide) {
// return res;
// }
// if (res === TYPE.EVEN) {
// let neighborIndex = mid + neighborPlus;
// if (neighborIndex < 0 || neighbor >= total) {
// return TYPE.EVEN;
// }
// let neighbor = _cnchar._.compareSpell(neighborIndex, target, tone); // + 1
// if (neighbor === TYPE.EVEN) {
// return typeCenter;
// } else if (neighbor === typeSide) {
// return TYPE.EVEN;
// } else {
// console.error(neighbor);
// throw new Error('idoim Error');
// }
// }
// }
function idiomWithStroke (input: number | number[]): Array<string> {
if (typeof input === 'number') { // 总笔画数为多少
return dict.filter((item) => {
return input === _cnchar.stroke(item);
});
}
return dict.filter((item) => {
return compareCommon(input as number[], _cnchar.stroke(item, 'array') as number[]);
});
}
/**
* ['五','','十',''],['五','光','十','色'] => true
* ['wu','','shi',''],['wu','guang','shi','se'] => true
* [4, 0, 2, 0],[4, 6, 2, 6] => true
*/
//
//
//
function compareCommon (input: Array<string | number>, target: Array<string | number>): boolean {
for (let i = 0; i < input.length; i++) {
if (input[i] && input[i] !== target[i] ) {
return false;
}
}
return true;
}
function checkArg (args: IdomArg[], name: IdomArg): void {
if (args.indexOf(name) !== -1) {
console.warn(`未引入cnchar,idiom不支持${name}参数`);
}
}
export function setCnchar (cnchar: ICnChar): void {
_cnchar = cnchar;
// initToneCodes(cnchar);
}

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

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

View File

@ -0,0 +1,31 @@
import {ICnChar} from 'cnchar/types';
import {idiom, arg, setCnchar} from './idiom';
import {IIdiom} from './types/common';
function main (cnchar: ICnChar & {idiom?: IIdiom}) {
if (cnchar.plugins.indexOf('idiom') !== -1) {
return;
}
cnchar.plugins.push('idiom');
cnchar.idiom = idiom;
cnchar.type.idiom = arg;
}
function init (cnchar?: ICnChar) {
if (typeof window === 'object' && !window.CncharIdiom) {
window.CncharIdiom = idiom;
}
if (typeof window === 'object' && window.CnChar) {
main(window.CnChar);
setCnchar(window.CnChar);
} else if (typeof cnchar !== 'undefined') {
main(cnchar);
setCnchar(cnchar);
}
}
idiom.init = init;
init();
export default idiom;

26
src/plugin/idiom/ts/types/common.d.ts vendored Normal file
View File

@ -0,0 +1,26 @@
import {ICnChar, IdomArg} from 'cnchar/types';
export declare type TIdiomArg = {
[prop in IdomArg]: IdomArg
}
export declare type TIdiomInput = string | number | Array<string|number>;
export declare interface IIdiom {
(input: TIdiomInput, ...args: Array<IdomArg>): Array<string> | void;
init?(cnchar?: ICnChar): void;
}
declare global {
interface Window {
CncharIdiom: IIdiom,
}
}
declare module 'cnchar' {
interface ICnChar {
idiom: IIdiom;
}
}