Usage

import CountryList from 'country-list-with-dial-code-and-flag'

CountryList.getAll() 
// Response => Array<Country>

CountryList.findOneByCountryCode('MM') 
// Response => Country

CountryList.findOneByDialCode('+95') 
// Response => Country

CountryList.findByDialCode('+95') 
// Response => Array<Country>

CountryList.findByKeyword('united') 
// Response => Array<Country>

Country Flag SVG (optional)

import CountryFlagSvg from 'country-list-with-dial-code-and-flag/dist/flag-svg'

const myanmar = CountryList.findOneByCountryCode('mm')
if (myanmar) {
  const flagSvg = CountryFlagSvg[myanmar.code]
  console.log(flagSvg) // This will return svg string
}

Example response

[
 { "name": "Afghanistan", "dial_code": "+93", "code": "AF", "flag": "🇦🇫" },
 { "name": "Myanmar", "dial_code": "+95", "code": "MM", "flag": "🇲🇲" }
]

Available Functions

FunctionDescription

getAll

get all available countries

findOneByCountryCode

find country by ISO 3166-1 alpha-2

findByCountryCode

find countries by ISO 3166-1 alpha-2

findOneByDialCode

find country by dial code Eg.

findByDialCode

find countries by dial code Eg.

findByKeyword

find countries with keyword Eg.

groupCountriesByFirstLetter

group countries by the first letter of their name

getAll(), findByKeyword(), findByCountryCode() functions support filter option for secondary dial codes. As a default withSecondary is true. withSecondary with true option will return list of dial codes available for specific country.

Example.

const list = CountryList.findByCountryCode('DO', 
    { withSecondary: false }
)
console.log(list.length) // 1


const list = CountryList.findByCountryCode('DO', 
    { withSecondary: true }
)
console.log(list.length) // 3

Country

Function / AttributeDescription

formatPhoneNumber(phone, format?)

format phone number with dial code, second param accept PhoneNumberFormat

name

get name attr from country

dialCode or dial_code

get name dial code from country

code

get code attr from country

flag

get flag emoji attr from country

preferred

get preferred attr from country

Sample Usage of formatPhoneNumber()

$ npm i google-libphonenumber 
$ npm i --save-dev @types/google-libphonenumber  (required on typescript)

From version 5.0, you will need to pass PhoneNumberUtil instance to Country list instance using setPhoneNumberUtil

import CountryList , { PhoneNumberFormat } from 'country-list-with-dial-code-and-flag'
import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber'

// only need to do version 5.0 due to performance issue
CountryList.setPhoneNumberUtil(PhoneNumberUtil.getInstance()) 

const mm = CountryList.findOneByCountryCode('mm')
if (mm) {
  mm.formatPhoneNumber('0888888888') // +95888888888
  mm.formatPhoneNumber('+95888888888') // +95888888888
  mm.formatPhoneNumber('888888888') // +95888888888
  mm.formatPhoneNumber('088-888-888-8') // +95888888888
}
const us = CountryList.findOneByCountryCode('us')
if (us) {
  us.formatPhoneNumber('2124567890', PhoneNumberFormat.E164) 
  // +12124567890
  
  us.formatPhoneNumber('2124567890', PhoneNumberFormat.INTERNATIONAL) 
  // +1 212-456-7890
  
  us.formatPhoneNumber('2124567890', PhoneNumberFormat.NATIONAL) 
  // (212) 456-7890
  
  us.formatPhoneNumber('2124567890', PhoneNumberFormat.RFC3966) 
  // tel:+1-212-456-7890
}

formatPhoneNumber() use google-libphonenumber package. For more information about the package, please visit to original documentation.

Last updated