DEV Community

Discussion on: Country Code to Flag Emoji

Collapse
jonrandy profile image
Jon Randy • Edited on

Nice. I'm gonna save a 1-liner Gist for future use:

const getFlagEmoji = countryCode=>String.fromCodePoint(...[...countryCode.toUpperCase()].map(x=>0x1f1a5+x.charCodeAt()))
Enter fullscreen mode Exit fullscreen mode
Collapse
jorik profile image
Jorik Author

Nice compact improvement! Although I would also suggest optimising for readability :-)

Collapse
jonrandy profile image
Jon Randy

I did. This one was too much... :P

const getFlag=c=>String.fromCodePoint(...[...c.toUpperCase()].map(x=>0x1f1a5+x.charCodeAt()))
Enter fullscreen mode Exit fullscreen mode
Thread Thread
jorik profile image
Jorik Author • Edited on

You inspired me to optimise the example function in the article to have less redundant code. :-)

An alternative implementation could use the replace method to replace each character.

function getFlagEmoji(countryCode) {
  return countryCode.toUpperCase().replace(/./g, char => 
      String.fromCodePoint(127397 + char.charCodeAt())
  );
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
jacobmgevans profile image
Jacob Evans

This turned into a CodeWars challenge lol

Thread Thread
alia5 profile image
Peter Repukat

Combined both of them, and I like this best. lol

(c)=>c.replace(/./g,(ch)=>String.fromCodePoint(0x1f1a5+ch.toUpperCase().charCodeAt()))
Enter fullscreen mode Exit fullscreen mode