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)
- 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
$ npm install countries-3166
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"}, …]
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", …}
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", …}
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"}, …]
All these methods modified this.data and returns this. Allowed to use in chain.
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"}, …}
*/
Modified data to alpha-2 format
Returns: (Object): countries object
countries.alpha2().values();
//object {AF: "Afghanistan", AX: "Åland Islands", …}
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: "Аландські острови", …}
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", …}
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: "Азербайжан", …}
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", …}
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
$ npm test
Package is in development state. Feel free to open pull request and/or propouse new features, bug-fixing, etc. It's under MIT license.