country-fns
Useful country data for forms and stuff.
Install
npm install country-fns --save
Overview
Each country in country-fns 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 'country-fns'
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:
and sms:
<a>
elements - Other annoying stuff I never want to lookup again.
API
getCountry(code: string): Country
Returns a single country by its ISO2 code.
const { getCountry } = require('country-fns')
const hockeyLand = getCountry('ca')
console.log(hockeyLand.name)
console.log(hockeyLand.format)
getCountries(): Country[]
Returns an array of all countries
const { getCountries } = require('country-fns')
const allCountries = getCountries()
console.log(allCountries)
countries = { [code: string]: Country }
An object containing all countries, keyed by lowercase ISO2 code.
const { countries } = require('country-fns')
console.log(countries)
iso2Codes = string[]
An array of all ISO2 Codes (lowercase).
const { iso2Codes } = require('country-fns')
console.log(iso2Codes)
Author
MIT LICENSE.