country-from-postcode
v1.0.1
Published
Get British country from postcode (mostly accurately)
Downloads
13
Maintainers
Readme
country-from-postcode
A simple package that determines the country (England, Scotland, Wales, Northern Ireland, Channel Islands, Isle of Man) based on a UK postcode. No external dependencies.
Installation
npm install country-from-postcode
Usage
import { getCountryFromPostcode } from 'country-from-postcode';
const country = getCountryFromPostcode('SW1A 1AA');
console.log(country); // Outputs: "England"
The function getCountryFromPostcode
returns one of the following country names:
- England
- Scotland
- Wales
- Northern Ireland
- Channel Islands
- Isle of Man
- “Invalid postcode” (if the postcode format is invalid)
- “Unknown postcode” (if the postcode does not match any known area or district)
Edge Cases
The package handles edge cases where postcodes span across borders:
- Dumfries and Galloway: Postcode DG1 maps to Scotland, but DG16 maps to England.
- Welsh Borders: Postcodes like SY1 (England) and SY10 (Wales) are handled correctly.
UK Postcode Format
UK postcodes follow a specific format:
- The outward code (before the space) consists of the area (1-2 letters) and the district (1-2 digits).
- The inward code (after the space) identifies more specific locations, such as a street.
For more detailed information on UK postcode formats, refer to:
License
This package is licensed under the WTFPL. Do what you want.