Country Parser
Utility for parsing and guessing official country codes and names
Features
- Get country code from country name, case sensitive or insensitive
- Get country name from country code, case sensitive or insensitive
- Guess country code from a string that best matches a country name
- Guess country name from a string that best matches a country name
Installation
$ npm install @ejarnutowski/country-parser
Examples
Get a country code from country name
Looks for an exact string match against the official country name and common alternative names. Returns null
when no match found.
const countryParser = require('@ejarnutowski/country-parser');
// Case sensitive
let code = countryParser.getCode('United States');
=> "US"
// Case insensitive
let code = countryParser.getCode('uNiTeD sTaTeS', true);
=> "US"
// No matches
let code = countryParser.getCode('uNiTeD sTaTeS');
let code = countryParser.getCode('UnitedStates');
=> null
Get a country name from country code
Looks for an exact string match against the official country code. Returns null
when no match found.
const countryParser = require('@ejarnutowski/country-parser');
// Case sensitive
let name = countryParser.getName('JP');
=> "Japan"
// Case insensitive
let name = countryParser.getName('Jp', true);
=> "Japan"
// No matches
let name = countryParser.getName('Jp');
let name = countryParser.getName('Jap');
=> null
Guess a country code
Looks for the most similar country name match and returns the country code.
const countryParser = require('@ejarnutowski/country-parser');
let code = countryParser.guessCode('southern m Korea');
=> "KR"
let code = countryParser.guessCode('united AMErica');
=> "US"
let code = countryParser.guessCode('The Dominican Repubb');
=> "DO"
Guess an real country name
Looks for the most similar country name match and returns the real country name.
const countryParser = require('@ejarnutowski/country-parser');
let name = countryParser.guessName('Costa Margarita');
=> "Costa Rica"
let name = countryParser.guessName('Indonesianish');
=> "Indonesia"
let name = countryParser.guessName('Lao Peoples Republica');
=> "Lao People's Democratic Republic"
License
MIT