import { map , values } from 'lodash' ; import escape from 'escape-string-regexp' ; import { CategoriesType } from './index' ; export interface EmojiType { ascii : string [ ] ; category : string ; code_points : { base : string ; decimal : string ; default_matches : string [ ] ; fully_qualified : string ; greedy_matches : string [ ] ; non_fully_qualified : string ; output : string ; } ; display : number ; diversities : any [ ] ; diversity : string ; gender : string ; genders : string [ ] ; keywords : string [ ] ; name : string ; order : number ; shortname : string ; shortname_alternates : string [ ] ; unicode_version : number ; _key : string ; } export interface EmojiDataType { [ category ] : { [ shortname ] : EmojiType [ ] ; } ; } export function createEmojiDataFromStrategy ( strategy ) { const emojiData = { } ; Object . keys ( strategy ) . sort ( ( a , b ) => { if ( strategy [ a ] . order < strategy [ b ] . order ) { return - 1 ; } if ( strategy [ a ] . order === strategy [ b ] . order ) { return 0 ; } return 1 ; } ) . forEach ( ( key ) => { try { const emoji = strategy [ key ] ; const { shortname } = emoji ; const keyword = shortname . replace ( /:/g , '' ) ; if ( emoji . category === 'modifier' ) { return ; } if ( emoji . category === 'regional' ) { emoji . category = 'symbols' ; } const notFoundIcons = [ ] ; if ( notFoundIcons . includes ( shortname ) ) { return ; } if ( ! emojiData [ emoji . category ] ) { emojiData [ emoji . category ] = { } ; } emoji . _key = key ; emoji . keywords . push ( emoji . name ) ; if ( ! emoji . keywords . includes ( keyword ) ) { emoji . keywords . push ( keyword ) ; } const match = keyword . match ( /(.*?)_tone(\d?)$/ ) ; if ( match ) { if ( ! ! emojiData [ emoji . category ] [ match [ 1 ] ] ) { emojiData [ emoji . category ] [ match [ 1 ] ] . push ( emoji ) ; } } else { emojiData [ emoji . category ] [ keyword ] = [ emoji ] ; } } catch ( e ) { } } ) ; return emojiData ; } /**
 * 查找对应肤色的emoji
 */ export function findEmojiVariant ( emojis , modifier ) { return modifier && emojis [ modifier ] ? emojis [ modifier ] : emojis [ 0 ] ; } export function emojisSelector ( categories , emojiData , modifier , term ) { return map ( categories , ( category , id ) => { const list = emojiData [ id ] || { } ; let emojis = values ( list ) . map ( ( data ) => findEmojiVariant ( data , modifier ) ) ; if ( term ) { emojis = emojis . filter ( ( emoji ) => { const searchTermRegExp = new RegExp ( `^(?:.* +)*${ escape ( term ) }` , 'i' ) ; return emoji . keywords . some ( ( keyword ) => searchTermRegExp . test ( keyword ) ) ; } ) ; } return { category , emojis , id } ; } ) . filter ( ( { emojis } ) => emojis . length > 0 ) . map ( ( { category , emojis , id } ) => ( { heading : { category , id } , emojis : [ ... emojis ] , } ) ) ; } export function createEmojisSelector ( ) { let lastResult = null ; let lastCategories = null ; let lastModifier = null ; let lastTerm = null ; return function memoizedRowsSelector ( categories , emojiData , modifier , term ) { if ( lastCategories !== categories || lastModifier !== modifier || lastTerm !== term ) { lastResult = emojisSelector ( categories , emojiData , modifier , term ) ; lastCategories = categories ; lastModifier = modifier ; lastTerm = term ; } return lastResult ; } ; }