You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Counting code points before ES6 was tricky, as the language didn't make an effort to help in the Unicode department. Take for instance `Object.keys`, as seen in the following code snippet. It returns five keys for our three-emoji string, because those three code points use five code units in total.
If we now consider a `for` loop, we can observe more clearly how this is a problem. In the following example, we wanted to extract each individual emoji from the `text` string, but we got each code unit instead of the code points they form.
@@ -1129,11 +1129,11 @@ Luckily for us, in ES6 strings adhere to the iterable protocol. We can use the s
1129
1129
Given ((("String.prototypex", id="spsi7")))((("strings", "splitting into code points", id="s7sicp")))((("Unicode", "String.prototypex", id="u7spsi")))the problems with looping by code units, the iterables produced by the string iterator yield code points instead.
@@ -1142,7 +1142,7 @@ Measuring the length of a string in terms of code points, as we saw earlier, is
1142
1142
We could use the spread operator, which relies on the iterator protocol, to split a string into an array made up of its conforming code points and then pull that array's `length`, getting the correct code point count, as seen next.
@@ -1211,7 +1211,7 @@ Let's look at more Unicode-related methods introduced in ES6.
1211
1211
1212
1212
==== String#codePointAt
1213
1213
1214
-
We can use `String#codePointAt` to get the numeric representation of a code point at a given position in a string. Note that the expected start position is indexed by code unit, not by code point. In the following example we print the code points for each of the three emoji in our demo 'pass:[<code><img src="images/1f40e.png" style="width: 12px" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" style="width: 12px" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" style="width: 12px" al="heart" data-emoji-embed="heart" /></code>]' string.
1214
+
We can use `String#codePointAt` to get the numeric representation of a code point at a given position in a string. Note that the expected start position is indexed by code unit, not by code point. In the following example we print the code points for each of the three emoji in our demo 'pass:[<code><img src="images/1f40e.png" style="width: 12px" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" style="width: 12px" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" style="width: 12px" alt="heart" data-emoji-embed="heart" /></code>]' string.
1215
1215
1216
1216
[source,javascript]
1217
1217
----
@@ -1265,7 +1265,7 @@ We could wrap those base-16 values in `'\u{codePoint}'` and voilá: you'd get th
0 commit comments