Skip to content

Commit 384d2b3

Browse files
committed
Edited ch07.asciidoc with Atlas code editor
1 parent 7be4e8a commit 384d2b3

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

ch07.asciidoc

+14-14
Original file line numberDiff line numberDiff line change
@@ -1090,28 +1090,28 @@ The `'\ud83d\udc0e\ud83d\udc71\u2764'` string, found in the next code snippet, e
10901090

10911091
++++
10921092
<pre data-type="programlisting" data-code-language="javascript">'\ud83d\udc0e\ud83d\udc71\u2764'
1093-
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />'</pre>
1093+
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'</pre>
10941094
++++
10951095

10961096
While that string consists of five code units, we know that the length should really be 3--as there are only three emoji.
10971097

10981098
++++
10991099
<pre data-type="programlisting" data-code-language="javascript">'\ud83d\udc0e\ud83d\udc71\u2764'.length
11001100
// &lt;- 5
1101-
'<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />'.length
1101+
'<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'.length
11021102
++++
11031103

11041104
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.
11051105

11061106
++++
1107-
<pre data-type="programlisting" data-code-language="javascript">Object.keys('<img src="images/1f40e.png" al="heart" data-emoji-embed="heart"e" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />')
1107+
<pre data-type="programlisting" data-code-language="javascript">Object.keys('<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />')
11081108
// &lt;- ['0', '1', '2', '3', '4']</pre>
11091109
++++
11101110

11111111
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.
11121112

11131113
++++
1114-
<pre data-type="programlisting" data-code-language="javascript">const text = '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />'
1114+
<pre data-type="programlisting" data-code-language="javascript">const text = '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'
11151115
for (let i = 0; i &lt; text.length; i++) {
11161116
console.log(text[i])
11171117
// &lt;- '\ud83d'
@@ -1129,11 +1129,11 @@ Luckily for us, in ES6 strings adhere to the iterable protocol. We can use the s
11291129
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.
11301130

11311131
++++
1132-
<pre data-type="programlisting" data-code-language="javascript">for (const codePoint of '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />') {
1132+
<pre data-type="programlisting" data-code-language="javascript">for (const codePoint of '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />') {
11331133
console.log(codePoint)
11341134
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/>'
11351135
// &lt;- '<img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/>'
1136-
// &lt;- '<img src="images/2764.png" al="heart" data-emoji-embed="heart" />'
1136+
// &lt;- '<img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'
11371137
}</pre>
11381138
++++
11391139

@@ -1142,7 +1142,7 @@ Measuring the length of a string in terms of code points, as we saw earlier, is
11421142
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.
11431143

11441144
++++
1145-
<pre data-type="programlisting" data-code-language="javascript">[...'<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />'].length
1145+
<pre data-type="programlisting" data-code-language="javascript">[...'<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse"/><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'].length
11461146
// &lt;- 3</pre>
11471147
++++
11481148

@@ -1211,7 +1211,7 @@ Let's look at more Unicode-related methods introduced in ES6.
12111211

12121212
==== String#codePointAt
12131213

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.
12151215

12161216
[source,javascript]
12171217
----
@@ -1265,7 +1265,7 @@ We could wrap those base-16 values in `'\u{codePoint}'` and voilá: you'd get th
12651265
'\u{1f471}'
12661266
// &lt;- '<img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot" />'
12671267
'\u{2764}'
1268-
// &lt;- '<img src="images/2764.png" al="heart" data-emoji-embed="heart" />'</pre>
1268+
// &lt;- '<img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'</pre>
12691269
++++
12701270

12711271
==== String.fromCodePoint
@@ -1278,7 +1278,7 @@ This ((("StringcodePointAtx", startref="scpa7")))((("String.fromCodePoint")))(((
12781278
String.fromCodePoint(0x1f471)
12791279
// &lt;- '<img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/>'
12801280
String.fromCodePoint(0x2764)
1281-
// &lt;- '<img src="images/2764.png" al="heart" data-emoji-embed="heart" />'</pre>
1281+
// &lt;- '<img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'</pre>
12821282
++++
12831283

12841284
You can just as well use plain base-10 literals and achieve the same results.
@@ -1289,14 +1289,14 @@ You can just as well use plain base-10 literals and achieve the same results.
12891289
String.fromCodePoint(128113)
12901290
// &lt;- '<img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/>'
12911291
String.fromCodePoint(10084)
1292-
// &lt;- '<img src="images/2764.png" al="heart" data-emoji-embed="heart" />'</pre>
1292+
// &lt;- '<img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'</pre>
12931293
++++
12941294

12951295
You can pass in as many code points as you'd like to `String.fromCodePoint`.
12961296

12971297
++++
12981298
<pre data-type="programlisting" data-code-language="javascript">String.fromCodePoint(0x1f40e, 0x1f471, 0x2764)
1299-
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />'</pre>
1299+
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'</pre>
13001300
++++
13011301

13021302
As an exercise in futility, we could map a string to their numeric representation of code points, and back to the code points themselves.
@@ -1307,7 +1307,7 @@ As an exercise in futility, we could map a string to their numeric representatio
13071307
.map(cp =&gt; cp.codePointAt(0))
13081308
.map(cp =&gt; String.fromCodePoint(cp))
13091309
.join('')
1310-
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" al="heart" data-emoji-embed="heart" />'</pre>
1310+
// &lt;- '<img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/2764.png" alt="heart" data-emoji-embed="heart" />'</pre>
13111311
++++
13121312

13131313
Reversing an string has potential to cause issues as well.
@@ -1330,7 +1330,7 @@ The problem is that we're reversing individual code units, while we'd have to re
13301330
++++
13311331
<pre data-type="programlisting" data-code-language="javascript">const text = '\ud83d\udc0e\ud83d\udc71\u2764'
13321332
[...text].reverse().join('')
1333-
// &lt;- '<img src="images/2764.png" al="heart" data-emoji-embed="heart" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" />'</pre>
1333+
// &lt;- '<img src="images/2764.png" alt="heart" data-emoji-embed="heart" /><img src="images/1f471.png" alt="man" data-emoji-embed="smiling idiot"/><img src="images/1f40e.png" alt="horse" data-emoji-embed="running horse" />'</pre>
13341334
++++
13351335

13361336
This way we avoid breaking up code points. Once again, keep in mind that this won't work for all grapheme clusters.

0 commit comments

Comments
 (0)