Skip to content

Instantly share code, notes, and snippets.

@theory-of-soul
Created July 4, 2019 11:55
Show Gist options
  • Save theory-of-soul/220dfe7a7f383325510be31ffb0d362e to your computer and use it in GitHub Desktop.
Save theory-of-soul/220dfe7a7f383325510be31ffb0d362e 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