As far as I know the position in a string given by Twitch is based on Unicode code points. As JavaScript uses UTF-16 instead of UTF-32 JavaScript may use 2 code units for one code point which means one βcharacterβ can have a length of 1 or 2. It gets more complicated when you want to count an emoji as a length of 1 because they may actually be made up of multiple code points which is what your linked stackoverflow question is trying to do. Twitch simply counts the amount of code points.
Hereβs roughly what I do:
let get_codepoint_to_codeunit_map = function(string){
let array = [];
let count = 0;
for(let char of string){
array.push(count);
count += char.length;
}
return array;
};
When you iterate with βfor let X of Yβ JavaScript gives you single code points which have a length of 1 or 2 (code units) which I use to count the length of the previous code points. When you put in your string it will return an array. When Twitch says an emote starts or ends at index 6 you check index 6 of that array which in this case has the value 9. The character at index 9 in JavaScript is the K of Kappa.