Skip to content

Instantly share code, notes, and snippets.

@jebarjonet
Last active February 6, 2023 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jebarjonet/c6b05d7f43327856320de5a1a89aaffb to your computer and use it in GitHub Desktop.
Save jebarjonet/c6b05d7f43327856320de5a1a89aaffb to your computer and use it in GitHub Desktop.
Transforms a country code (ISO 3166-1 alpha-2) into corresponding flag emoji
import { toUpper } from 'lodash'
export const codeToFlag = (code: string): string =>
String.fromCodePoint(
...toUpper(code)
.split('')
.map(c => 127397 + c.charCodeAt(0)),
)
// codeToFlag('fr') => 🇫🇷
// codeToFlag('gb') => 🇬🇧
// codeToFlag('jp') => 🇯🇵
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment