Skip to content

Instantly share code, notes, and snippets.

@0xlkda
Forked from hieunc229/countryToFlag.js
Created July 24, 2021 18:48
Show Gist options
  • Save 0xlkda/d8136dcabe1dc992b306eed5dd4b1b0a to your computer and use it in GitHub Desktop.
Save 0xlkda/d8136dcabe1dc992b306eed5dd4b1b0a 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