Search code examples
javascriptstringunicode-stringcodepoint

How do I reverse `String.fromCodePoint`, i.e. convert a string to an array of code points?


String.fromCodePoint(...[127482, 127480]) gives me a flag of the US (πŸ‡ΊπŸ‡Έ).

How do I turn the flag back to [127482, 127480]?


Solution

  • You're looking for codePointAt, perhaps using spread (etc.) to convert back to array and then mapping each of them.

    console.log(theString.codePointAt(0)); // 127482
    console.log(theString.codePointAt(2)); // 127480
    // Note βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’^
    // It's 2 because the first code point in the string occupies two code *units*
    

    or

    const array = [...theString].map(s => s.codePointAt(0));
    console.log(array); // [127482, 127480]
    

    or skipping an interim step as Sebastian Simon pointed out via Array.from and its mapping callback:

    const array = Array.from(theString, s => s.codePointAt(0));
    console.log(array); // [127482, 127480]
    

    Example:

    const theString = String.fromCodePoint(...[127482, 127480]);
    
    console.log(theString.codePointAt(0)); // 127482
    console.log(theString.codePointAt(2)); // 127480
    
    const array = [...theString].map(s => s.codePointAt(0));
    console.log(array);  // [127482, 127480]
    
    const array2 = Array.from(theString, s => s.codePointAt(0));
    console.log(array2); // [127482, 127480]

    Spread and Array.from both work by using the strings iterator, which works by code points, not code units like most string methods do.