import { initToneCodes , getCharCode } from './compareSpell' ; import { ICnChar , CompareType , SortSpellArg , SpellArg } from 'cnchar-types/main' ; let _cnchar ; const arg = { 'tone' : 'tone' , 'desc' : 'desc' , } ; export function initSort ( __cnchar ) { _cnchar = __cnchar ; initToneCodes ( __cnchar ) ; } const TYPE = { MORE : 'more' , LESS : 'less' , EVEN : 'even' , ERROR : 'error' } ; function pretreat ( spell , tone ) { const arr = [ 'low' ] ; if ( tone ) { arr . push ( 'tone' ) ; } if ( _cnchar . isCnChar ( spell ) ) { return _cnchar . spell ( spell , ... arr ) as string ; } return _cnchar . _ . transformTone ( spell , tone ) . spell ; } export function compareSpell ( spell1 , spell2 , tone = false ) { spell1 = pretreat ( spell1 , tone ) ; spell2 = pretreat ( spell2 , tone ) ; for ( let i = 0 ; i < spell1 . length ; i ++ ) { if ( ! spell2 [ i ] ) { return TYPE . MORE ; } const code = getCharCode ( spell1 [ i ] , tone ) ; const code2 = getCharCode ( spell2 [ i ] , tone ) ; if ( code > code2 ) { return TYPE . MORE ; } else if ( code < code2 ) { return TYPE . LESS ; } } if ( spell1 . length === spell2 . length ) { return TYPE . EVEN ; } return TYPE . LESS ; } export function sortSpell ( spells , ... args ) { let isString = false ; if ( typeof spells === 'string' ) { spells = spells . split ( '' ) ; isString = true ; } const tone = args . indexOf ( arg . tone ) !== - 1 ; const desc = args . indexOf ( arg . desc ) !== - 1 ; const more = desc ? - 1 : 1 ; const less = more * - 1 ; const result = spells . sort ( ( a , b ) => { const res = compareSpell ( a , b , tone ) ; if ( res === TYPE . MORE ) { return more ; } if ( res === TYPE . LESS ) { return less ; } return 0 ; } ) ; if ( isString ) { return result . join ( '' ) ; } return result ; } export function compareStroke ( stroke1 , stroke2 ) { if ( typeof stroke1 === 'string' ) { stroke1 = _cnchar . stroke ( stroke1 ) as number ; } if ( typeof stroke2 === 'string' ) { stroke2 = _cnchar . stroke ( stroke2 ) as number ; } if ( stroke1 > stroke2 ) { return TYPE . MORE ; } if ( stroke1 < stroke2 ) { return TYPE . LESS ; } return TYPE . EVEN ; } export function sortStroke ( strokes , desc ? ) { let isString = false ; if ( typeof strokes === 'string' ) { strokes = strokes . split ( '' ) ; isString = true ; } const more = ( desc === arg . desc ) ? - 1 : 1 ; const less = more * - 1 ; const result = strokes . sort ( ( a , b ) => { const res = compareStroke ( a , b ) ; if ( res === TYPE . MORE ) { return more ; } if ( res === TYPE . LESS ) { return less ; } return 0 ; } ) ; if ( isString ) { return result . join ( '' ) ; } return result ; }