countries-3166

0.0.26 • Public • Published

countries-3166

Join the chat at https://gitter.im/gzoreslav/countries-3166

NPM version Download Count

Build Status Test coverage Dependency Status

Country codes library (ISO 3166, alpha2 and alpha3). All data were get from Wikipedia.

Benefits:

  • usefull "chain" using countries.alpha2().translate('UA').sortByName().toArray();
  • output like object or array
  • alpha-2 transformation
  • sorting by code or country name
  • translation
  • favorite countries

Supported languages:

  • english (default)
  • ukrainian (ua)
  • germany (de)

Changelog

  • 0.0.26 - add German language translations (DE)
  • 0.0.25 - increase tests coverage
  • 0.0.24 - modified toArray method to be more useful; add tests
  • 0.0.19 - fix sorting for locales, add favorites method
  • 0.0.18 - add Ukrainian language translations (UA); fix minor bugs
  • 0.0.16 - first working release

Installation

$ npm install countries-3166

How to use?

Library written to use it like chain. All methods (except values() and toArray()) modified this.data attribute and returns this. It allows to use chain for transforming data in what you need. These metods should be the last ones: values() and toArray(). See usage example below:

import countries from 'countries-3166';
 
/*
Modified data to alpha-2 format, then sort them by country name,
and returns data array
*/
countries.alpha2().sortByName().toArray();
 
//array [{code: "AF", name: "Afghanistan"}, {code: "AX", name: "Åland Islands"}, …]

data attribute

Object row data property (by default: alpha-3, english language, no sort). Not recommended to use it directly. Chain methods modified this property. If you need return data to default state after some manipulations, use init() method;

countries.data;
 
//object {AFG: "Afghanistan", ALA: "Åland Islands", …}

Return methods

values()

Returns data object for using. Should be the last method in the chain;

Returns: (Object): countries.data (by default: alpha-3, english language, no sort)

countries.values();
 
//object {AFG: "Afghanistan", ALA: "Åland Islands", …}

toArray()

Returns data arrayt for using. Should be the last method in the chain;

Returns: (Array): countries.data (by default: alpha-3, english language, no sort)

countries.toArray();
 
//array [{code: "AFG", name: "Afghanistan"}, {code: "ALA", name: "Åland Islands"}, …]

Chain methods

All these methods modified this.data and returns this. Allowed to use in chain.

init()

Reset state to default raw data (alpha-3, english language, no sort).

Returns: (Object): countries object

countries.init();
 
/*
Returns (Object) countries
Modified countries.data to {{AFG: "Afghanistan"}, {ALA: "Åland Islands"}, …}
*/

alpha2()

Modified data to alpha-2 format

Returns: (Object): countries object

countries.alpha2().values();
 
//object {AF: "Afghanistan", AX: "Åland Islands", …}

translate(lang_code)

Translate data to appropriate language

Arguments: lang_code: 2 or 3-length language code. Examples: 'FR', 'UKR'

Returns: (Object): countries object

countries.translate('UA').values();
 
//object {AFG: "Афганістан", ALA: "Аландські острови", …}

sortByKey()

Sort data by key. If you need alpha-2 format sorted, use alpha2() method before sort.

Returns: (Object): countries object

countries.sortByKey().values();
 
//object {ABW: "Aruba", AFG: "Afghanistan", AGO: "Angola", AIA: "Anguilla", …}

sortByName()

Sort data by country name. If you need translated format sorted, use translate(lang) method before sort.

Returns: (Object): countries object

countries.translate('UKR').sortByKey().values();
 
//object {AUS: "Австралія", AUT: "Австрія", AZE: "Азербайжан", …}

favorites(countries)

Put your favorite countries at the beginning. Usually uses after sort.

Arguments: countries: array of countries codes or string code if one country needed

Returns: (Object): countries object

countries.favorites(['UA', 'FR']).values();
 
//object {UKR: "Ukraine", FRA: "France", AFG: "Afghanistan", …}

Examples

import countries from 'countries-3166';
 
/* Alpha-3 (default) */
 
countries.values(); //returns countries data (object)
countries.toArray(); //returns countries data (array)
 
/* Alpha-2 */
 
countries.alpha2().values(); //returns countries alpha-2 data (object)
countries.alpha2().toArray(); //returns countries alpha-2 data (array)
 
/* Translations */
 
countries.translate('UKR').values(); //returns translated countries data (object)
countries.translate('UKR').alpha2().toArray(); //returns countries translated alpha-2 data (array)
countries.translate('UA').alpha2().values(); //returns countries translated alpha-2 data (object)
 
/* Sorting */
 
countries.sortByKey().values(); //returns sorted by key countries data (object)
countries.translate('UKR').sortByName().toArray(); //returns sorted by name translated countries data (object)
//NOTE: Object's order may displays wrong in console
 
/* Favorite Countries */
 
countries.translate('UKR').sortByName().favorites('UKR').values(); //returns sorted by countries data with Ukraine country at the first place (object)
 
/* Set default data */
 
countries.init(); //reset countris data to default state

Check example page

More Demos can be found in the examples directory.

Build examples:

$ grunt example

Tests

$ npm test

Welcome

Package is in development state. Feel free to open pull request and/or propouse new features, bug-fixing, etc. It's under MIT license.

Package Sidebar

Install

npm i countries-3166

Weekly Downloads

1

Version

0.0.26

License

MIT

Last publish

Collaborators

  • zoreslavgoral