globalist
v1.0.2
Published
Useful country data for forms and stuff.
Downloads
7
Maintainers
Readme
Globalist
Useful country data for forms and stuff.
Install
npm install globalist --save
Overview
Each country in Globalist is represented by an object with the following keys:
name
: Common name and native name in parentheses if available.iso2
: 2 character ISO2 code. Lowercase.dial
: International calling code.format
: Telephone format.
Quick Example
Imagine you need to make a "Select Country" input.
import React from 'react'
import { getCountries } from 'globalist'
const SelectCountry = (props) =>
<select {...props}>
{getCountries().map((c) =>
<option value={c.iso2}>{c.name}</option>
)}
</select>
export default SelectCountry
Other use cases
- Intelligent telephone placeholder and input masks
- Telephone number validation
- Localized
tel:
andsms:
<a>
elements - Other annoying stuff I never want to lookup again.
API
Table of Contents
getCountry(code: string): Country
Returns a single country by its ISO2 code.
const { getCountry } = require('globalist')
const hockeyLand = getCountry('ca')
console.log(hockeyLand.name) // => Canada
console.log(hockeyLand.format) // => Canada
getCountries(): Country[]
Returns an array of all countries
const { getCountries } = require('globalist')
const allCountries = getCountries()
console.log(allCountries)
/*
[
{
name: 'Afghanistan (افغانستان)',
iso2: 'af',
dial: '93',
format: '+..-..-...-....',
},
{
name: 'Albania (Shqipëri)',
iso2: 'al',
dial: '355',
format: '+...(...)...-...',
},
{
name: 'Algeria (الجزائر)',
iso2: 'dz',
dial: '213',
format: '+...-..-...-....',
},
...
]
*/
countries = { [code: string]: Country }
An object containing all countries, keyed by lowercase ISO2 code.
const { countries } = require('globalist')
console.log(countries)
/*
{
af: {
name: 'Afghanistan (افغانستان)',
iso2: 'af',
dial: '93',
format: '+..-..-...-....',
},
al: {
name: 'Albania (Shqipëri)',
iso2: 'al',
dial: '355',
format: '+...(...)...-...',
},
dz: {
name: 'Algeria (الجزائر)',
iso2: 'dz',
dial: '213',
format: '+...-..-...-....',
},
...
}
*/
iso2Codes = string[]
An array of all ISO2 Codes (lowercase).
const { iso2Codes } = require('globalist')
console.log(iso2Codes)
/*
['af', 'al', 'dz', 'as', 'ad', 'ao', ... ]
*/
Author
- Jared Palmer (@jaredpalmer)
MIT LICENSE.