Skip to content

Instantly share code, notes, and snippets.

@calmarti
Forked from theory-of-soul/country2emoji.js
Created August 8, 2021 14:58
Show Gist options
  • Save calmarti/eb34da325df4aa1b93d3e53ec7c948af to your computer and use it in GitHub Desktop.
Save calmarti/eb34da325df4aa1b93d3e53ec7c948af to your computer and use it in GitHub Desktop.
Javascript : convert country code to emoji flag by Stan Larroque
// Assume the country_code is a ISO 3166-1 alpha-2 string (eg: "US")
function country2emoji(country_code) {
var OFFSET = 127397;
var cc = country_code.toUpperCase();
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
return /^[A-Z]{2}$/.test(cc) ? String.fromCodePoint.apply(String, _toConsumableArray([].concat(_toConsumableArray(cc)).map(function (c) {
return c.charCodeAt() + OFFSET;
}))) : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment