import { countDict , orderDict } from './dict' ; import { converter } from './converter' ; import { IConverter , ITradModArg , ITradString } from 'cnchar-types/plugin/trad' ; import { ICnChar , SpellArg , StrokeArg } from 'cnchar-types/main' ; import { ICncharTool } from 'cnchar-types/main/tool' ; import { ISpell } from 'cnchar-types/main/index' ; const arg = { trad : 'trad' , simple : 'simple' , array : 'array' , order : 'order' } ; let _ ; function main ( cnchar ) { if ( cnchar . plugins . indexOf ( 'trad' ) !== - 1 ) { return ; } cnchar . plugins . push ( 'trad' ) ; cnchar . convert = converter ; const _p = String . prototype ; if ( typeof cnchar . type . spell === 'object' ) { cnchar . type . spell . simple = arg . simple ; cnchar . type . spell . trad = arg . trad ; } if ( typeof cnchar . type . stroke === 'object' ) { cnchar . type . stroke . simple = arg . simple ; cnchar . type . stroke . trad = arg . trad ; } reinitSpell ( _p , cnchar ) ; reinitStroke ( _p , cnchar ) ; _p . convertSimpleToTrad = function ( ) : string { return converter . simpleToTrad ( this as string ) ; } ; _p . convertSimpleToSpark = function ( ) : string { return converter . simpleToSpark ( this as string ) ; } ; _p . convertTradToSimple = function ( ) : string { return converter . tradToSimple ( this as string ) ; } ; _p . convertTradToSpark = function ( ) : string { return converter . tradToSpark ( this as string ) ; } ; _p . convertSparkToSimple = function ( ) : string { return converter . sparkToSimple ( this as string ) ; } ; _p . convertSparkToTrad = function ( ) : string { return converter . sparkToTrad ( this as string ) ; } ; _ = cnchar . _ ; _ . convert = converter ; _ . dict . getTradOrders = function ( ) { return orderDict ; } ; _ . dict . getTradCount = function ( ) { return countDict ; } ; } export default function init ( cnchar ? ) { if ( typeof window === 'object' && window . CnChar ) { main ( window . CnChar ) ; } else if ( typeof cnchar !== 'undefined' ) { main ( cnchar ) ; } } function reinitSpell ( proto , cnchar ) { let _spell = cnchar . spell ; const newSpell = function ( str , ... args ) : string | Array < any > { if ( _ . has ( args , arg . simple ) ) { return _spell ( str , ... args ) ; } if ( _ . has ( args , arg . trad ) ) { const isArr = _ . has ( args , arg . array ) ; if ( ! isArr ) { args . push ( arg . array ) ; } const simpleStr = converter . tradToSimple ( str ) ; const simples = [ ] ; let newStr = '' ; for ( let i = 0 ; i < simpleStr . length ; i ++ ) { if ( simpleStr [ i ] !== str [ i ] ) { newStr += simpleStr [ i ] ; } else { simples . push ( { index : i , str : str [ i ] } ) ; } } const res = _spell ( newStr , ... args ) as Array < string > ; for ( let i = 0 ; i < simples . length ; i ++ ) { res . splice ( simples [ i ] . index , 0 , simples [ i ] . str ) ; } return ( isArr ) ? res : res . join ( '' ) ; } str = converter . tradToSimple ( str ) ; return _spell ( str , ... args ) ; } ; proto . spell = function ( ... args ) { return newSpell ( this as string , ... args ) ; } ; cnchar . spell = function ( ... args ) { return newSpell ( ... args ) ; } ; if ( ! cnchar . _ . poly ) { cnchar . _ . _reinitSpellPoly = function ( ) : void { _spell = cnchar . spell ; proto . spell = function ( ... args ) { return newSpell ( this as string , ... args ) ; } ; cnchar . spell = function ( ... args ) { return newSpell ( ... args ) ; } ; } ; ; } } function reinitStroke ( proto , cnchar ) { let _stroke = cnchar . stroke ; const _new = function ( str , ... args ) : number | Array < any > { _ . checkArgs ( 'stroke' , args , true ) ; const isArr = _ . has ( args , arg . array ) ; const isOrder = _ . has ( args , arg . order ) ; if ( ! isArr ) { args . push ( arg . array ) ; } const res = _stroke ( str , ... args ) as Array < any > ; if ( ! isOrder ) { if ( _ . has ( args , arg . simple ) ) { return ( isArr ) ? res : _ . sumStroke ( res as Array < number > ) ; } if ( _ . has ( args , arg . trad ) ) { for ( var j = 0 ; j < res . length ; j ++ ) { if ( res [ j ] !== 0 ) { res [ j ] = - 1 ; } } } for ( const i in countDict ) { const inum = parseInt ( i ) ; for ( var j = 0 ; j < res . length ; j ++ ) { if ( res [ j ] === 0 && countDict [ inum ] . indexOf ( str [ j ] ) !== - 1 ) { res [ j ] = inum ; } } } if ( _ . has ( args , arg . trad ) ) { for ( var j = 0 ; j < res . length ; j ++ ) { if ( res [ j ] === - 1 ) { res [ j ] = 0 ; } } } return ( isArr ) ? res : _ . sumStroke ( res ) ; } else { if ( _ . has ( args , arg . simple ) ) { return res ; } if ( _ . has ( args , arg . trad ) ) { } const igList = [ ] ; for ( var i = 0 ; i < res . length ; i ++ ) { if ( typeof res [ i ] === 'undefined' ) { res [ i ] = orderDict [ str [ i ] ] ; } else { igList . push ( i ) ; } } return _ . orderWithLetters ( res , str , args , igList ) ; } } ; proto . stroke = function ( ... args ) { return _new ( this as string , ... args ) ; } ; cnchar . stroke = function ( ... args ) { return _new ( ... args ) ; } ; if ( ! cnchar . _ . order ) { cnchar . _ . _reinitStrokeOrder = function ( ) { _stroke = cnchar . stroke ; proto . stroke = function ( ... args ) { return _new ( this as string , ... args ) ; } ; cnchar . stroke = function ( ... args ) { return _new ( ... args ) ; } ; } ; ; } } init ( ) ;