Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function returning ISO 3 country code #1

Merged
merged 3 commits into from
Jun 16, 2015

Conversation

maximeloizeau
Copy link

I needed the country code in ISO 3 format so I just added a simple function.

I didn't know if I should use a external json file or internal variable to store the ISO2-ISO3 mappings, feel free to comment about it.

@elad
Copy link
Owner

elad commented Jun 12, 2015

A variable is fine, but could you please reformat so that it's not one long line but lexically-ordered, line-delimited per country along with the country name? As in:

var ccISO2toISO3 = {
  ...
  "BD": "BGD", // Bangladesh
  ...
};

Here's code to help you extract the data from Wikipedia:

// npm install request cheerio
var request = require('request'),
    cheerio = require('cheerio');
request('https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3', function (err, res, body) {
  var $ = cheerio.load(body);
  var $table = $('table > tr > td > tt:contains("ABW")').closest('table');
  var $countries = $table.find('tr');
  var iso3codes = {};
  $countries.each(function (i, el) {
    var $el = $(el);
    var iso3code = $el.find('tt').text();
    var name = $el.find('a').text();
    iso3codes[iso3code] = name;
  });
  console.log(iso3codes);
});

@maximeloizeau
Copy link
Author

Thanks. I edited the variable as you requested.

elad added a commit that referenced this pull request Jun 16, 2015
Added function returning ISO 3 country code
@elad elad merged commit 3488546 into elad:master Jun 16, 2015
@elad
Copy link
Owner

elad commented Jun 16, 2015

Thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants