0.1.2 β€’ Published 2 years ago

@opengovsg/convert-nationality-codes v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Convert nationality codes

Coverage Status

A utility for converting between nationality codes. We currently support 6 schemes:

  • 'name' Plaintext English
  • 'icao' International Civil Aviation Organization
  • 'iso2' ISO 3166-1 alpha-2 (link)
  • 'iso3' ISO 3166-1 alpha-3 (link)
  • 'flag' Emojis πŸ‡¦πŸ‡ͺ πŸ‡ͺπŸ‡Έ πŸ‡ΈπŸ‡¬ πŸ‡²πŸ‡΅ πŸ‡³πŸ‡±
  • 'phone' Prefixes for international dialing (link)

Installation

npm install @opengovsg/convert-nationality-codes

Usage

convert-nationality-codes provides a chained API:

import { convert } from '@opengovsg/convert-nationality-codes'

convert('SGP').from('icao').to('iso2') // 'SG'

Schemes don't always have 1-1 mappings. In such cases, we support many-to-one mappings as expected

// United States Minor Outlying Islands
convert('UM').from('iso2').to('icao') // 'USA'

// United States
convert('US').from('iso2').to('icao') // 'USA'

and choose a sensible default for one-to-many mappings. Where ambiguous, we choose the territory with a larger population. The full list of alternates can be seen here

convert('USA').from('icao').to('name') // 'United States'

Where a scheme has no representation for an option provided by another scheme, we throw an error.

// Refugee (Status) exists in icao but not iso2
convert('XXB').from('icao').to('iso2') // throws

Additionally, if you require the full enums, e.g. to use in a validator / to iterate over with your own logic, you can access them as a de-duped, sorted list:

import { schemes } from '@opengovsg/convert-nationality-codes'

console.log(schemes.icao) // ['AFG', 'ALB', 'AZM', ...]
console.log(schemes.iso2) // ['AF', 'AL', 'AS', ...]

Typings

Array items in schemes and return type from convert() are strongly-typed thanks to type-narrowing.

const possiblePhonePrefix: string = '+66'
const wellTypedIso3 = convert(possiblePhonePrefix).from('phone').to('iso3')

if (wellTypedIso3 === 'Thailand') // TS complains - wellTypedIso3 is known to be 'AFG' | 'ALB' | 'AZM' ...
0.1.2

2 years ago

0.1.1

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago