https://github.com/jaredpalmer/country-fns
🌏 Useful country data for forms and stuff.
https://github.com/jaredpalmer/country-fns
countries country-codes form forms telephone
Last synced: 6 months ago
JSON representation
🌏 Useful country data for forms and stuff.
- Host: GitHub
- URL: https://github.com/jaredpalmer/country-fns
- Owner: jaredpalmer
- License: mit
- Created: 2017-10-14T19:42:39.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-13T17:49:36.000Z (over 7 years ago)
- Last Synced: 2025-03-29T08:22:25.164Z (7 months ago)
- Topics: countries, country-codes, form, forms, telephone
- Language: JavaScript
- Homepage: https://npm.im/country-fns
- Size: 42 KB
- Stars: 33
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
```jsx
import React from 'react'
import { getCountries } from 'country-fns'const SelectCountry = (props) =>
{getCountries().map((c) =>
{c.name}
)}
export default SelectCountry
```#### Other use cases
- Intelligent telephone placeholder and input masks
- Telephone number validation
- Localized `tel:` and `sms:` `` elements
- Other annoying stuff I never want to lookup again.## API
- [`getCountry(code: string): Country`](#getcountrycode-string-country)
- [`getCountries(): Country[]`](#getcountries-country)
- [`countries = { [code: string]: Country }`](#countries---code-string-country-)
- [`iso2Codes = string[]`](#iso2codes--string)### `getCountry(code: string): Country`
Returns a single country by its ISO2 code.
```js
const { getCountry } = require('country-fns')const hockeyLand = getCountry('ca')
console.log(hockeyLand.name) // => Canada
console.log(hockeyLand.format) // => Canada
```### `getCountries(): Country[]`
Returns an array of all countries
```js
const { getCountries } = require('country-fns')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.
```js
const { countries } = require('country-fns')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).
```js
const { iso2Codes } = require('country-fns')
console.log(iso2Codes)
/*
['af', 'al', 'dz', 'as', 'ad', 'ao', ... ]
*/
```## Author
- Jared Palmer ([@jaredpalmer](https://twitter.com/jaredpalmer))
---
MIT LICENSE.