Skip to content

Instantly share code, notes, and snippets.

@hieunc229
Created June 14, 2021 02:28
Show Gist options
  • Save hieunc229/fa74865534351300c10056b0e6566511 to your computer and use it in GitHub Desktop.
Save hieunc229/fa74865534351300c10056b0e6566511 to your computer and use it in GitHub Desktop.
Get country flag using its code
// ISO 3166-1 alpha-2
// ⚠️ No support for IE 11
// Source: all over the internet
function countryToFlag(code) {
let isoCode = code.split("-").pop()
return typeof String.fromCodePoint !== 'undefined'
? isoCode
.toUpperCase()
.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397))
: isoCode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment