tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.55k stars 214 forks source link

Parse chat message and emotes with emoji #280

Closed crs38c28 closed 5 years ago

crs38c28 commented 6 years ago

I'm using this function to parse chat message.

Actual behaviour:

`Kappa 😮 Kappa`

with emoji

Expected behaviour:

`Kappa 😮 Kappa`

--

The main reason is because the emoji's length. in user object :

'emotes': { '25': [ '0-4' ,'8-12' ] }

After

var splitText = text.split(''); 

It will become

(14) ["K", "a", "p", "p", "a", " ", "�", "�", " ", "K", "a", "p", "p", "a"]
 [ '0-4' ] = "Kappa"
 [ '8-12'] = " Kapp"

So it will replace wrong words.

--

use Array.from instead of split can fix this.

var splitText = Array.from(text);

Result :

(13) ["K", "a", "p", "p", "a", " ", "😮", " ", "K", "a", "p", "p", "a"]
 [ '0-4' ] = "Kappa"
 [ '8-12'] = "Kappa"
celluj34 commented 6 years ago

Neat. I don't think we had Array.from back then, but that seems like a nice simple solution.