Javascript : convert country code to emoji flag

In one of my apps, I wanted to display in a select menu a list of countries. Since you can't put images in a select tag, I decided to put the corresponding emoji flag next to the country.

Here is my function, compatible in all javascript platforms for your convenience:

// 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;

This article is my oldest. It is 122 words long

© 2019 Stan Larroque. All rights reserved.