P171 文字列とUnicode

const str = "リンゴ🍎";
const codePoints = convertCodePoints(str);
console.log(codePoints); // ["30ea", "30f3", "30b4", "1f34e"]
const codeUnits = covertCodeUnits(str);
console.log(codeUnits); // ["30ea", "30f3", "30b4", "d83c", "df4e"]

Unicodeは4バイトの表現が可能なため🍎が1文字
UTF-16サロゲートペア文字列は2バイト×2で表現しないといけないため2文字

インデックス 0 1 2 3 4
文字列 🍎
UnicodeのCode Point(16進数) 0x30ea 0x30f3 0x30b4 0x1f34e
UTF-16のCode Unit(16進数) 0x30ea 0x30f3 0x30b4 0xd83c 0xdf4e

JavaScriptには、文字列におけるCode Unitを数えるプロパティ(String#length)は存在するが、Code Pointを数えるメソッド等は現在存在していない。
 そのため、Array.fromを用いて変換後に数えるしか現状はない。

const codePoints = Array.from("リンゴ🍎");
console.log(codePoints.length); // 4
console.log(codePoints); // ["リ", "ン", "ゴ", "🍎"]