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
Copy file name to clipboardExpand all lines: chapters/ch01.asciidoc
+1-1
Original file line number
Diff line number
Diff line change
@@ -59,7 +59,7 @@ Proposals in stage 2 offer an initial draft of the specification. At this point,
59
59
60
60
Proposals in stage 3 are candidate recommendations. At this point, implementors have expressed interest in the proposal. In practice, proposals move to this level with at least one browser implementation, a high-fidelity polyfill or when supported by a build-time compiler like Babel. At this stage, a proposal is unlikely to change beyond fixes to issues identified in the wild.
61
61
62
-
In order for a proposal to attain stage 4 status, two independent implementations need to pass conformance tests. Proposals that make their way through to stage four will be included in the next revision of ECMAScript.
62
+
In order for a proposal to attain stage 4 status, two independent implementations need to pass acceptance tests. Proposals that make their way through to stage four will be included in the next revision of ECMAScript.
63
63
64
64
Starting with ES6, new releases of the specification are expected to be published every year from now on. To accommodate the yearly release schedule, versions will now be referred to by their publication year. Thus ES6 becomes ES2015, ES7 is ES2016, and so on. Colloquially, ES2015 hasn't taken and is still largely regarded as ES6. ES7 had been announced before the naming convention changed, thus it is only sometimes referred to as ES2016.
Copy file name to clipboardExpand all lines: chapters/ch07.asciidoc
+73-6
Original file line number
Diff line number
Diff line change
@@ -980,9 +980,76 @@ c`, 2)
980
980
// <- ` a\n b\n c`
981
981
----
982
982
983
+
==== 7.3.5 String Padding and Trimming
984
+
985
+
At the time of this writing, there's two new string padding methods slated for publication in ES2017: +String#padStart+ and +String#padEnd+. When performing string manipulation, we often want to pad a string so that it's formatted consistently with a style we have in mind. This can be useful when formatting numbers, currency, HTML, and in a variety of other cases usually involving monospaced text.
986
+
987
+
Using +padStart+, we will specify the desired length for the target string and the padding string, which defaults to a single space character. If the original string is at least as long as the specified length, +padStart+ will result in a null operation, returning the original string unchanged.
988
+
989
+
In the following example, the desired length of a properly padded string is 5, and the original string already has a length of at least 5, so it's returned unchanged.
990
+
991
+
[source,javascript]
992
+
----
993
+
'01.23'.padStart(5)
994
+
// <- '01.23'
995
+
----
996
+
997
+
In the next example, the original string has a length of 4, thus +padStart+ adds a single space at the beginning of the string, bringing the length to the desired value of 5.
998
+
999
+
[source,javascript]
1000
+
----
1001
+
'1.23'.padStart(5)
1002
+
// <- ' 1.23'
1003
+
----
1004
+
1005
+
The next example is just like the previous one, except it uses +'0'+ for padding instead of the default +' '+ value.
1006
+
1007
+
[source,javascript]
1008
+
----
1009
+
'1.23'.padStart(5, '0')
1010
+
// <- '01.23'
1011
+
----
1012
+
1013
+
Note that +padStart+ will keep padding the string until the maximum length is reached.
1014
+
1015
+
[source,javascript]
1016
+
----
1017
+
'1.23'.padStart(7, '0')
1018
+
// <- '0001.23'
1019
+
----
1020
+
1021
+
However, if the padding string is too long, it may be truncated. The provided length is the maximum length of the padded string, except in the case where the original string is already larger than that.
1022
+
1023
+
[source,javascript]
1024
+
----
1025
+
'1.23'.padStart(7, 'abcdef')
1026
+
// <- 'abc1.23'
1027
+
----
1028
+
1029
+
The +padEnd+ method has a similar API, but it adds the padding at the end of the original string, instead of at the beginning. The following snippet illustrates the difference.
1030
+
1031
+
[source,javascript]
1032
+
----
1033
+
'01.23'.padEnd(5) // <- '01.23'
1034
+
'1.23'.padEnd(5) // <- '1.23 '
1035
+
'1.23'.padEnd(5, '0') // <- '1.230'
1036
+
'1.23'.padEnd(7, '0') // <- '1.23000'
1037
+
'1.23'.padEnd(7, 'abcdef') // <- '1.23abc'
1038
+
----
1039
+
1040
+
At the time of this writing, there's a proposal for string trimming in stage 2, containing the +String#trimStart+ and +String#trimEnd+ methods. Using +trimStart+ removes any whitespace from the beginning of a string, while using +trimEnd+ removes any whitespace from the end of a string.
1041
+
1042
+
[source,javascript]
1043
+
----
1044
+
' this should be left-aligned '.trimStart()
1045
+
// <- 'this should be left-aligned '
1046
+
' this should be right-aligned '.trimEnd()
1047
+
// <- ' this should be right-aligned'
1048
+
----
1049
+
983
1050
Let's switch protocols and learn about Unicode.
984
1051
985
-
==== 7.3.5 Unicode
1052
+
==== 7.3.6 Unicode
986
1053
987
1054
JavaScript strings are represented using UTF-16 code unitsfootnote:[Learn more about UCS-2, UCS-4, UTF-16 and UTF-32 here: https://mjavascript.com/out/unicode-encodings.]. Each code unit can be used to represent a code point in the +[U+0000, U+FFFF]+ range -- also known as the BMP, short for basic multilingual plane. You can represent individual code points in the BMP plane using the +`\u3456`+ syntax. You could also represent code units in the +[U+0000, U+0255]+ using the +\x00..\xff+ notation. For instance, +`\xbb`+ represents +`»`+, the +187+ character, as you can verify by doing +parseInt(`bb`, 16)+ -- or +String.fromCharCode(187)+.
988
1055
@@ -1033,7 +1100,7 @@ for (let i = 0; i < text.length; i++) {
1033
1100
1034
1101
Luckily for us, in ES6 strings adhere to the iterable protocol. We can use the string iterator to go over code points, even when those code points are made of surrogate pairs.
1035
1102
1036
-
==== 7.3.6 +String.prototype[Symbol.iterator]+
1103
+
==== 7.3.7 +String.prototype[Symbol.iterator]+
1037
1104
1038
1105
Given the problems with looping by code units, the iterables produced by the string iterator yield code points instead.
1039
1106
@@ -1097,7 +1164,7 @@ As Unicode expert Mathias Bynens points out, splitting by code points isn't enou
1097
1164
1098
1165
Let's look at more Unicode-related methods introduced in ES6.
1099
1166
1100
-
==== 7.3.7 +String#codePointAt+
1167
+
==== 7.3.8 +String#codePointAt+
1101
1168
1102
1169
We can use +String#codePointAt+ to get the base-10 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 example below we print the code points for each of the three emoji in our demo +`🐎👱❤`+ string.
1103
1170
@@ -1157,7 +1224,7 @@ We could wrap those base-16 values in +`\u{codePoint}`+ and voilá: you'd get th
1157
1224
// <- `❤`
1158
1225
----
1159
1226
1160
-
==== 7.3.8 +String.fromCodePoint+
1227
+
==== 7.3.9 +String.fromCodePoint+
1161
1228
1162
1229
This method takes in a number and returns a code point. Note how I can use the +0x+ prefix with the terse base-16 code points we got from +String#codePointAt+ moments ago.
1163
1230
@@ -1205,7 +1272,7 @@ const text = `\ud83d\udc0e\ud83d\udc71\u2764`
1205
1272
1206
1273
Reversing an string has potential to cause issues as well.
1207
1274
1208
-
==== 7.3.9 Unicode-Aware String Reversal
1275
+
==== 7.3.10 Unicode-Aware String Reversal
1209
1276
1210
1277
Consider the following piece of code.
1211
1278
@@ -1237,7 +1304,7 @@ This way we avoid breaking up code points. Once again, keep in mind that this wo
1237
1304
1238
1305
The last Unicode-related method we'll be addressing is +.normalize+.
1239
1306
1240
-
==== 7.3.10 +String#normalize+
1307
+
==== 7.3.11 +String#normalize+
1241
1308
1242
1309
There are different ways of representing strings that look identical to humans even though their code points differ. Consider the following example, where two seemingly identical strings aren't deemed equal by any JavaScript runtime.
0 commit comments