import { TwitchEmote } from "../emote" ; import { TwitchEmoteList } from "../emotes" ; import { parseIntThrowing } from "./common" ; import { ParseError } from "./parse-error" ; export function parseEmotes ( messageText , emotesSrc ) { const emotes = [ ] ; if ( emotesSrc . length <= 0 ) { return emotes ; } const messageCharacters = [ ... messageText ] ; for ( const emoteInstancesSrc of emotesSrc . split ( "/" ) ) { const [ emoteID , instancesSrc ] = emoteInstancesSrc . split ( ":" , 2 ) ; for ( const instanceSrc of instancesSrc . split ( "," ) ) { let [ startIndex , endIndex ] = instanceSrc . split ( "-" ) . map ( parseIntThrowing ) ; if ( endIndex == null ) { throw new ParseError ( `No - found in emote index range "${ instanceSrc }"` ) ; } endIndex = endIndex + 1 ; if ( startIndex < 0 ) { startIndex = 0 ; } if ( endIndex > messageCharacters . length ) { endIndex = messageCharacters . length ; } const emoteText = messageCharacters . slice ( startIndex , endIndex ) . join ( "" ) ; emotes . push ( new TwitchEmote ( emoteID , startIndex , endIndex , emoteText ) ) ; } } emotes . sort ( ( a , b ) => a . startIndex - b . startIndex ) ; return emotes ; }