/**
 * 汉字 ['a','','c','']
 * 笔画 [1,0,1,0]
 * 拼音 ['shi', '']
 */ import { ICnChar , IdomArg } from 'cnchar-types/main/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 'cnchar-types/plugin/idiom' ; let _cnchar ; export const arg = { char : 'char' , stroke : 'stroke' , spell : 'spell' , tone : 'tone' } ; export const idiom = ( input , ... args ) : 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 ) { 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 ) { return dict . filter ( ( item ) => { return compareCommon ( input , item . split ( '' ) ) ; } ) ; } function idiomWithSpell ( 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 = [ ] ; 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 ; } function idiomWithStroke ( input ) { 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 , target ) { for ( let i = 0 ; i < input . length ; i ++ ) { if ( input [ i ] && input [ i ] !== target [ i ] ) { return false ; } } return true ; } function checkArg ( args , name ) { if ( args . indexOf ( name ) !== - 1 ) { console . warn ( `未引入cnchar,idiom不支持${ name }参数` ) ; } } export function setCnchar ( cnchar ) { _cnchar = cnchar ; }