A Node.js module for returning data about countries, ISO info and states/provinces within them.
npm install countryjs
To run in-browser, you'll need the bulkify
transform for browserify
, as it pairs with bulk-require
, to staticly resolve each country JSON.
npm install bulkify
var bulkify = require('bulkify')
var bundle = browserify({
entries: [...],
})
.transform(bulkify, {
global: true
})
To access one of the country properties available, you'll need to use one of the API methods listed below and pass a country in either way:
- Using the ISO-alpha2 code:
country.population('US', 'ISO2')
orcountry.area('JP')
(defaults) - Using the ISO-alpha3 code:
country.capital('GBR', 'ISO3)
- Using the country name:
country.wiki('france', 'name')
. The matching is case-insensitive, against the native name, alternative spellings and available transalations.
Returns all available information for a specified country.
var country = require('countryjs');
country.info('US', 'ISO2'); // 'ISO2', 'ISO3', 'name'
country.info('US'); // Defaults to ISO2
// returns object,
// {
// "name": "United States",
// "altSpellings": ["US", "USA", "United States of America"],
// "area": 9629091,
// "borders": ["CAN", "MEX"],
// "callingCodes": ["1"],
// "capital": "Washington D.C.",
// "currencies": ["USD", "USN", "USS"],
// "demonym": "American",
// "flag": "", // Flag API is incomplete
// "geoJSON": {} //GeoJSON data,
// "ISO": {
// "alpha2": "US",
// "alpha3": "USA"
// },
// "languages": ["en"],
// "latlng": [38, -97],
// "nativeName": "United States",
// "population": 319259000,
// "provinces": [], // State/Province list
// "region": "Americas",
// "subregion": "Northern America",
// "timezones": [], // TimeZone list
// "tld": [".us"],
// "translations": {
// "de": "Vereinigte Staaten von Amerika",
// "es": "Estados Unidos",
// "fr": "États-Unis",
// "ja": "アメリカ合衆国",
// "it": "Stati Uniti D'America"
// },
// "wiki": "http://en.wikipedia.org/wiki/united_states_of_america"
// }
Returns all states/provinces for a specified country.
var country = require('countryjs');
country.states('USA', 'ISO3'); // 'ISO2', 'ISO3', 'name'
country.states('US'); // Defaults to ISO2
// returns array of states / provinces,
// [
// "Alabama",
// ...
// ]
Alias of .states()
Returns name for a specified country
var country = require('countryjs');
country.name('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.name('US') // Defaults to 'ISO2'
// returns string
// "United States"
Returns alternate spellings for the name of a specified country
var country = require('countryjs');
country.altSpellings('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.altSpellings('US') // Defaults to 'ISO2'
// returns array of strings, alternate names
// ["US", "USA", "United States of America"]
Returns area (km²) for a specified country
var country = require('countryjs');
country.area('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.area('US') // Defaults to 'ISO2'
// returns number of square kilometer area
// 9629091
Returns bordering countries (ISO3) for a specified country
var country = require('countryjs');
country.borders('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.borders('US') // Defaults to 'ISO2'
// returns array of strings, ISO3 codes of countries that border the given country
// ["CAN", "MEX"]
Returns international calling codes for a specified country
var country = require('countryjs');
country.callingCodes('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.callingCodes('US') // Defaults to 'ISO2'
// returns array of calling code strings
// ["1"]
Returns capital city for a specified country
var country = require('countryjs');
country.capital('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.capital('US') // Defaults to 'ISO2'
// returns string
// "Washington D.C."
Returns official currencies for a specified country
var country = require('countryjs');
country.currencies('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.currencies('US') // Defaults to 'ISO2'
// returns array of strings, currencies
// ["USD", "USN", "USS"]
Returns the demonyms for a specified country
var country = require('countryjs');
country.demonym('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.demonym('US') // Defaults to 'ISO2'
// returns string, name of residents
// "American"
Returns SVG link of the official flag for a specified country
var country = require('countryjs');
country.flag('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.flag('US') // Defaults to 'ISO2'
// returns string URL of CC licensed svg flag
Returns geoJSON for a specified country
var country = require('countryjs');
country.geoJSON('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.geoJSON('US') // Defaults to 'ISO2'
// returns object of GeoJSON data
Returns ISO codes for a specified country
var country = require('countryjs');
country.ISOcodes('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.ISOcodes('US') // Defaults to 'ISO2'
// returns object of ISO codes
// {
// "alpha2": "US",
// "alpha3": "USA"
// }
Returns official languages for a specified country
var country = require('countryjs');
country.languages('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.languages('US') // Defaults to 'ISO2'
// returns array of language codes
// ["en"]
Returns approx latitude and longitude for a specified country
var country = require('countryjs');
country.latlng('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.latlng('US') // Defaults to 'ISO2'
// returns array, approx latitude and longitude for country
// [38, -97]
Returns the name of the country in its native tongue
var country = require('countryjs');
country.nativeName('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.nativeName('US') // Defaults to 'ISO2'
// returns string, name of country in native language
// "United States"
Returns approximate population for a specified country
var country = require('countryjs');
country.population('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.population('US') // Defaults to 'ISO2'
// returns number, approx population
// 319259000
Returns general region for a specified country
var country = require('countryjs');
country.region('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.region('US') // Defaults to 'ISO2'
// returns string
// "Americas"
Returns a more specific region for a specified country
var country = require('countryjs');
country.subregion('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.subregion('US') // Defaults to 'ISO2'
// returns string
// "Northern America"
Returns all timezones for a specified country
var country = require('countryjs');
country.timezones('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.timezones('US') // Defaults to 'ISO2'
// returns array of timezones
Returns official top level domains for a specified country
var country = require('countryjs');
country.tld('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.tld('US') // Defaults to 'ISO2'
// returns array of top level domains specific to the country
// [".us"]
Returns translations for a specified country name in popular languages
var country = require('countryjs');
country.translations('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.translations('US') // Defaults to 'ISO2'
// returns object of translations of country name in major languages
// {
// "de": "Vereinigte Staaten von Amerika",
// "es": "Estados Unidos",
// "fr": "États-Unis",
// "ja": "アメリカ合衆国",
// "it": "Stati Uniti D'America"
// }
Returns link to wikipedia page for a specified country
var country = require('countryjs');
country.wiki('USA','ISO3'); // 'ISO2', 'ISO3', 'name'
country.wiki('US') // Defaults to 'ISO2'
// returns string URL of wikipedia article on country
// "http://en.wikipedia.org/wiki/united_states_of_america"
Returns array of objects containing all available data for all countries. This will be super big. Not recommended.
var country = require('countryjs');
country.all();
// returns array of objects,
// [{
// "name": "United States",
// "altSpellings": ["US", "USA", "United States of America"],
// "area": 9629091,
// "borders": ["CAN", "MEX"],
// "callingCodes": ["1"],
// "capital": "Washington D.C.",
// "currencies": ["USD", "USN", "USS"],
// "demonym": "American",
// "flag": "", // Flag API is incomplete
// "geoJSON": {} //GeoJSON data,
// "ISO": {
// "alpha2": "US",
// "alpha3": "USA"
// },
// "languages": ["en"],
// "latlng": [38, -97],
// "nativeName": "United States",
// "population": 319259000,
// "provinces": [], // State/Province list
// "region": "Americas",
// "subregion": "Northern America",
// "timezones": [], // TimeZone list
// "tld": [".us"],
// "translations": {
// "de": "Vereinigte Staaten von Amerika",
// "es": "Estados Unidos",
// "fr": "États-Unis",
// "ja": "アメリカ合衆国",
// "it": "Stati Uniti D'America"
// },
// "wiki": "http://en.wikipedia.org/wiki/united_states_of_america"
// }...]
Special thanks to johan for his work on johan/world.geo.json, who made the geojson portion of this build possible.
This project adheres to Javascript Standard style
To contribute code to this module, please follow this workflow:
- fork the repo
- make sure to install dev dependencies using
npm install --dev
- Make the changes you desire
- Ensure all changes have a new test in the
test/
folder, and run:
npm test
This will check do the following:
- Check your code against feross/standard style and notify of any issues.
- Run all mocha tests listed in
test/
- Run all code through istanbul's code coverage runner. You can check the coverage afterwards the coverage report page:
coverage/lcov-report/index.html
- After making changes in your fork, open a pull request.
Please note that if your code updates do not pass JS Standard style, mocha tests and code coverage, your PR may be rejected and you'll need to fix any issues listed in it.
- Trent Oswald trentoswald@therebelrobot.com - /therebelrobot - therebelrobot.com
- Scott Hillman hillmanov@gmail.com - /hillmanov
- Michael Scott Hertzberg mshertzberg@gmail.com - /moimikey - michael.hertzberg.co
- Loris Guignard loris.guignard@gmail.com - /loris
All notable changes to this project will be documented in this file.
This project adheres to Semantic Versioning and Keep A Changelog.
bulk-require
to deps
- Browserify compatibility
glob
dep (replaced bybulk-require
)
- Contributor section to README
- @moimikey to contributor list (
package.json
andREADME
)
- Undefined ISO tags from certain countries (refer #19)
- Yugoslavia from country list, as it's not a country anymore
mocha.opts
- Added standard dev dep
- Added Changelog to README
.DS_STORE
to.gitignore
- Updated
test
script - Updated Contributing Doc
This is being maintained in the contributor's free time, and as such, may contain minor errors in regards to some countries. Most of the information included in this library is what is listed on Wikipedia. If there is an error, please let me know and I will do my best to correct it.
Copyright (c) 2015, Trent Oswald trentoswald@therebelrobot.com
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.