3 ms·
It seems like certain code points (like '\u{1F4A9}' aka the poop emoji) are a single character but the string report a length of 2. That is the root of all of t
by javajosh 9y ago
It seems like certain code points (like '\u{1F4A9}' aka the poop emoji) are a single character but the string report a length of 2. That is the root of all of those problems. One of your "problems", the length of an array with a single string element, isn't a problem.
- recursive 9y ago> One of your "problems", the length of an array with a single string element That's not what it is. Look again, and pay attention to the ... part.
- javajosh 9y agoThat's really helpful, thanks.
- mbell 9y ago> One of your "problems", the length of an array with a single string element, isn't a problem. You're misunderstand the code, it's using the spread operator on a string: [...'word'] => ["w", "o", "r", "d"] What is being demonstrated is that under the hood, javascript stores astral plane codepoints as surrogate pairs and strings operate on 'characters' which is why '\u{1F4A9}'.length => 2. But, when the spread operator is applied to a string, it breaks up the string into codepoints, not characters. This is also why [...'man\u0303ana'].length => 7, the combining tilde is a separate codepoint. This is an example of how wonky string processing in JS is, [...string].length is actually the most straightforward way to get a count of codepoints in a string.
- colejohnson66 9y agoAny Unicode codepoint above U+FFFF will require 2 UTF-16 characters