countries-3166
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:
; /*Modified data to alpha-2 format, then sort them by country name,and returns data array*/countries; //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;
countriesdata; //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; //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; //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; /*Returns (Object) countriesModified countries.data to {{AFG: "Afghanistan"}, {ALA: "Åland Islands"}, …}*/
alpha2()
Modified data to alpha-2 format
Returns: (Object): countries object
countries; //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; //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; //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; //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; //object {UKR: "Ukraine", FRA: "France", AFG: "Afghanistan", …}
Examples
; /* Alpha-3 (default) */ countries; //returns countries data (object)countries; //returns countries data (array) /* Alpha-2 */ countries; //returns countries alpha-2 data (object)countries; //returns countries alpha-2 data (array) /* Translations */ countries; //returns translated countries data (object)countries; //returns countries translated alpha-2 data (array)countries; //returns countries translated alpha-2 data (object) /* Sorting */ countries; //returns sorted by key countries data (object)countries; //returns sorted by name translated countries data (object)//NOTE: Object's order may displays wrong in console /* Favorite Countries */ countries; //returns sorted by countries data with Ukraine country at the first place (object) /* Set default data */ countries; //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.