import { ICnChar , OrderToWordArg } from 'cnchar-types/main' ; import { Json } from 'cnchar-types/main/common' ; import { ICncharTool } from 'cnchar-types/main/tool' ; import { orders , strokeTable } from './dict' ; import { IBase , ICnCharOrder , IOrderToWord , OrderName , TOrderToWordArg } from 'cnchar-types/plugin/order' ; const arg = { start : 'start' , contain : 'contain' , match : 'match' , matchorder : 'matchorder' , simple : 'simple' , trad : 'trad' , array : 'array' } ; let _ ; export default function initOrderToWord ( cnchar ) { cnchar . orderToWord = orderToWord ; cnchar . type . orderToWord = arg ; _ = cnchar . _ ; } const orderToWord = ( ( input , ... args ) : string | Array < string > => { if ( typeof input === 'string' ) { input = input . split ( ' ' ) as Array < OrderName > ; } _ . checkArgs ( 'orderToWord' , args ) ; const errorOrder = [ ] ; let letters = '' ; input . forEach ( ( name ) => { if ( orderToWord . orders [ name ] ) { letters += orderToWord . orders [ name ] . letter ; } else { errorOrder . push ( name ) ; } } ) ; if ( errorOrder . length > 0 ) { console . error ( 'orderToWord: 参数笔画名数组有误：' + errorOrder . join ( ',' ) ) ; return [ ] ; } const res = [ ] ; const argRes = { start : _ . has ( args , arg . start ) , match : _ . has ( args , arg . match ) , matchorder : _ . has ( args , arg . matchorder ) , contain : _ . has ( args , arg . contain ) , simple : _ . has ( args , arg . simple ) , trad : _ . has ( args , arg . trad ) , array : _ . has ( args , arg . array ) , } ; if ( ! argRes . simple && ! argRes . trad ) { argRes . simple = argRes . trad = true ; } if ( argRes . simple ) { base ( res , letters , argRes , orders ) ; } if ( argRes . trad && _ . dict . getTradOrders ) { base ( res , letters , argRes , _ . dict . getTradOrders ( ) ) ; } if ( _ . has ( args , arg . array ) ) { return res ; } return res . join ( '' ) ; } ) as unknown as IOrderToWord ; ; const base = ( res , letters , args , dict ) : string [ ] => { if ( args . match ) { for ( const k in dict ) { let notcontain = false ; for ( let i = 0 ; i < letters . length ; i ++ ) { if ( dict [ k ] . indexOf ( letters [ i ] ) === - 1 ) { notcontain = true ; break ; } } if ( ! notcontain ) { res . push ( k ) ; } } } else if ( args . matchorder ) { for ( const k in dict ) { let notcontain = false ; let orders = dict [ k ] ; for ( let i = 0 ; i < letters . length ; i ++ ) { const index = orders . indexOf ( letters [ i ] ) ; if ( index === - 1 ) { notcontain = true ; break ; } else { orders = orders . substr ( index + 1 ) ; } } if ( ! notcontain ) { res . push ( k ) ; } } } else if ( args . contain ) { for ( const k in dict ) { if ( dict [ k ] . indexOf ( letters ) !== - 1 ) { res . push ( k ) ; } } } else if ( args . start ) { for ( const k in dict ) { if ( dict [ k ] . indexOf ( letters ) === 0 ) { res . push ( k ) ; } } } else { for ( const k in dict ) { if ( dict [ k ] . length > letters . length ) { break ; } if ( dict [ k ] === letters ) { res . push ( k ) ; } } } return res ; } ; function init ( ) { orderToWord . orders = { } ; orderToWord . _base = base ; for ( const k in strokeTable ) { const single = strokeTable [ k ] ; const name = single . name ; const shape = single . shape ; if ( name . indexOf ( '|' ) !== - 1 ) { const names = name . split ( '|' ) ; const shapes = shape . split ( '|' ) ; addToOrders ( names [ 0 ] , shapes [ 0 ] , k , names [ 1 ] ) ; addToOrders ( names [ 1 ] , shapes [ 1 ] , k , names [ 0 ] ) ; } else { addToOrders ( name , shape , k ) ; } } } function addToOrders ( name , shape , letter , sameLetterTo ? ) { const data = { shape , letter , sameLetterTo } ; if ( ! sameLetterTo ) { delete data . sameLetterTo ; } orderToWord . orders [ name ] = data ; } init ( ) ;