import { Context } from '../../Context' ; import { isEmpty , isNil , Lru , unique } from 'licia' ; import { pinyin as pinyinPro } from 'pinyin-pro' ; param text const chineseRegex = /[\u4e00-\u9fa5]+/g ; const pinyinCache = new Lru ( 5000 ) ; const generateSearchKeyWithPinyin2 = text => { if ( isNil ( text ) || isEmpty ( text ) ) return [ ] ; if ( ! chineseRegex . test ( text ) ) return [ text ] ; let matches = text . match ( chineseRegex ) ?? [ ] , results = [ ] ; unique ( matches ) . forEach ( word => { if ( pinyinCache . has ( word ) ) { results . push ( ... pinyinCache . get ( word ) ) ; } else { let array = [ pinyinPro ( word , { toneType : 'none' , type : 'array' } ) . join ( '' ) , pinyinPro ( word , { pattern : 'first' , toneType : 'none' , type : 'array' } ) . join ( '' ) , ] ; results . push ( ... array ) ; pinyinCache . set ( word , array ) ; } } ) ; return results ; } ; export const generatePinyinIndex = ( context , text ) => { return context . enablePinyinIndex ? generateSearchKeyWithPinyin2 ( text ) : [ ] ; } ;