mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
`[...text]` lowered to an array literal that spread the array of text's code points: the literal's array copied the code point strings, retaining each one, and the code point array then released them. An array literal whose only element is a spread of a string is now the code points array itself, as `Array.from(text)` already was. Both are fresh arrays.
26 lines
969 B
TypeScript
26 lines
969 B
TypeScript
// Spreading strings into arrays: code points of each UTF-8 length, astral
|
|
// pairs, lone surrogates, the empty string, and the fresh array each spread
|
|
// returns.
|
|
|
|
const samples = ["", "a", "abc", "é", "café", "日本語", "😀", "a😀b", "🚀🚀", "\ud800", "a\udc00b", "xé日😀"];
|
|
|
|
for (const s of samples) {
|
|
const chars = [...s];
|
|
console.log(JSON.stringify(s), chars.length, JSON.stringify(chars), chars.join("|"));
|
|
}
|
|
|
|
// Each spread is a new array: changing one leaves the others and the string.
|
|
const text = "héllo 😀";
|
|
const first = [...text];
|
|
const second = [...text];
|
|
first.reverse();
|
|
first.push("!");
|
|
console.log(first.join(""), second.join(""), first === second, text);
|
|
|
|
// The spread result is an ordinary array.
|
|
const letters = [..."naïve"];
|
|
letters.sort();
|
|
console.log(letters, letters.length, Array.isArray(letters));
|
|
console.log([..."abc"].map((c) => c.toUpperCase()).join(""));
|
|
console.log([..."日本"].concat([..."語"]).join("-"));
|