npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

countries-3166

v0.0.26

Published

Country codes library (ISO 3166)

Downloads

9

Readme

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.