import defDict from './dict/spell-default.json' ; import spellDict from './dict/spell-dict-jian.json' ; import originStrokeDict from './dict/stroke-count-jian.json' ; import infoDict from './dict/info-dict.json' ; import { mapJson } from './util' ; import { transformTone , spell , arg , shapeSpell , stroke } from './tool' ; import { Json , ITransformReturn } from 'cnchar-types/main/common' ; import { ISetIntoJson } from 'cnchar-types/main/tool' ; const strokeDict = originStrokeDict as Json < string > ; export function setSpellDefault ( word , spell ? ) { setIntoJson ( { target : defDict , key : word , value : spell , isSpell : true } ) ; } export const setIntoJson = ( { target , key , value , isSpell = false , } ) : void => { mapJson ( key , value , ( k , v ) => { if ( k && v ) { target [ k ] = isSpell ? shapeSpell ( v ) : v ; } } ) ; } ; function setIntoSpellBase ( dict , currentSpell , word , spells ? , isPoly = false ) { if ( typeof word === 'object' ) { for ( const k in word ) { setIntoSpellBase ( dict , ( currentSpell as Json < Array < ITransformReturn > > ) [ k ] , k , word [ k ] ) ; } return ; } if ( spells instanceof Array ) { spells . forEach ( ( spell => { setIntoSpellBase ( dict , currentSpell , word , spell , true ) ; } ) ) ; return ; } const info = transformTone ( spells as string ) ; const str = dict [ info . spell ] ; currentSpell = currentSpell as Array < ITransformReturn > ; if ( currentSpell . length >= 1 ) { for ( let i = 0 ; i < currentSpell . length ; i ++ ) { const csItem = currentSpell [ i ] ; if ( csItem . spell === info . spell && csItem . tone === info . tone ) { return ; } } if ( ! isPoly ) isPoly = true ; } const appendStr = `${ word }${ info . tone + ( isPoly ? 5 : 0 ) }` ; if ( ! str ) { dict [ info . spell ] = `${ info . index }:` + appendStr ; } else { dict [ info . spell ] = str + appendStr ; } if ( currentSpell . length === 1 ) { const cinfo = currentSpell [ 0 ] ; dict [ cinfo . spell ] = dict [ cinfo . spell ] . replace ( `${ word }${ cinfo . tone }` , `${ word }${ cinfo . tone + 5 }` ) ; } if ( isPoly ) { addPolyWord ( word ) ; } } export function setIntoSpell ( dict , word , spells ? ) { let currentSpell ; if ( typeof word === 'object' ) { currentSpell = { } ; for ( const k in word ) { currentSpell [ k ] = _getCurrentSpellInfo ( dict , k ) ; } } else { currentSpell = _getCurrentSpellInfo ( dict , word ) ; } setIntoSpellBase ( dict , currentSpell , word , spells ) ; } function _getCurrentSpellInfo ( dict , word ) { const spl = spell ( dict , [ word , arg . tone , arg . poly ] ) as string ; if ( spl === word ) { return [ ] ; } let result ; if ( spl . indexOf ( '|' ) !== - 1 ) { result = spl . substring ( 1 , spl . length - 1 ) . split ( '|' ) ; } else { result = [ spl ] ; } return result . map ( sp => { return transformTone ( sp ) ; } ) ; } export function setSpell ( word , spells ? ) { setIntoSpell ( spellDict , word , spells ) ; } export function setStrokeCount ( word , count ? ) { mapJson ( word , count , ( k , v ) => { const oldCount = stroke ( strokeDict , [ k ] ) as number ; if ( oldCount === count ) { return ; } if ( oldCount !== 0 ) { const index = oldCount . toString ( ) ; strokeDict [ index ] = strokeDict [ index ] . replace ( k , '' ) ; } if ( strokeDict [ v ] ) { strokeDict [ v ] += k ; } else { strokeDict [ v ] = k ; } } ) ; } function addPolyWord ( word ) { if ( infoDict . polyWord . indexOf ( word ) === - 1 ) { infoDict . polyWord += word ; } }