@syniol/iso-3166
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

ISO-3166

The purpose of ISO 3166 is to define internationally recognized codes of letters and/or numbers that we can use when we refer to countries and their subdivisions. However, it does not define the names of countries – this information comes from United Nations sources (Terminology Bulletin Country Names and the Country and Region Codes for Statistical Use maintained by the United Nations Statistics Divisions).

Background

Using codes saves time and avoids errors as instead of using a country’s name (which will change depending on the language), we can use a combination of letters and/or numbers that are understood all over the world. This library contains both ISO 3166-1 and ISO 3166-2 without the subdivisions in English.

Motivation

Create a light ISO-3166 node package for internal software development at Syniol that is matching our coding standards.

Quick Guide

Below is a quick example of Country ISO-3166 Node library in JavaScript.

    const country = require('iso-3166')
    const britain = country.CreateFromName('britain')

    console.log(britain.name) // Output: United Kingdom of Great Britain and Northern Ireland
    console.log(britain.alpha2Code) // Output: GB
    console.log(britain.alpha3Code) // Output: GBR
    console.log(britain.numeric) // Output: 826
    import { Country } from 'iso-3166'
    const britain = Country.CreateFromName('britain')

    console.log(britain.name) // Output: United Kingdom of Great Britain and Northern Ireland
    console.log(britain.alpha2Code) // Output: GB
    console.log(britain.alpha3Code) // Output: GBR
    console.log(britain.numeric) // Output: 826

API

Available Methods are:

  • CountryList: accepts no parameters, and it returns an array of countries. This could be used for dropdown menu or when you need to display country's name as a title and alpha-2/3 as a value.
const countries = Country.CountryList()

for (const country of countries) {
    console.log(country.name)
    console.log(country.alpha2Code)
    console.log(country.alpha3Code)
    console.log(country.numeric)
}
  • CreateFromName: accept a single parameter, as long as name contains the most relevant name it will find and create a Country object.
let country = Country.CreateFromName('Great Britain')
const country = Country.CreateFromName('Britain')
const countr = Country.CreateFromName('britain')
  • CreateFromAlpha2Code: accepts a single parameter, it should match the alpha-2 code to find and create a Country object.
const country = Country.CreateFromAlpha2Code('GB')
const country = Country.CreateFromAlpha2Code('gb')
  • CreateFromAlpha3Code: accepts a single parameter, it should match the alpha-3 code to find and create a Country object.
const country = Country.CreateFromAlpha3Code('GBR')
const country = Country.CreateFromAlpha3Code('gbr')
  • CreateFromNumeric: accepts a single parameter, it should match the numeric code ( Country Code to find and create a Country object.
const country = Country.CreateFromNumeric(826)

Credits

Built with ❤️ & in Beautiful London. Copyright © Syniol Limited.

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i @syniol/iso-3166

    Weekly Downloads

    0

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    32.8 kB

    Total Files

    11

    Last publish

    Collaborators

    • syniol