Guide for adding a new country to the static Region Identifier dataset. Each supported country needs three JSON files plus a small code change.
- Keys:
zipCodeFormat:"numeric"or"alpha".zipCodeLength: integer, required for numeric formats to pad/compare values (USA=5, AUS=4, DEU=5, etc.). Omit for alpha formats (CAN/GBR/NLD).
- Example:
{ "zipCodeFormat": "numeric", "zipCodeLength": 5 }- Plain object mapping ISO 3166-2 region codes (same codes used in
regions/*.json) to display names in English. - Example (
regionNames/AUS.json):
{
"AU-NSW": "New South Wales",
"AU-VIC": "Victoria"
}Array of objects that map postal codes to ISO 3166-2 region codes. Formats already used in the repo:
- Numeric intervals (inclusive): objects with
low,high,region. Values can be numbers or numeric strings if leading zeros matter. Example (regions/AUS.json):
{ "region": "AU-NSW", "low": 1000, "high": 1999 }- Explicit codes: objects with
low,region, nohigh. Every postal code (or prefix) must be listed. Use strings to preserve zeros. Examples:- Belgium lists every 4-digit code (
regions/BEL.json). - Spain lists 5-digit codes as strings (
regions/ESP.json). - Mexico lists 2-digit prefixes (
regions/MEX.json). - CAN/GBR use alpha prefixes (e.g.,
"low": "X0A"or"low": "AB").
- Belgium lists every 4-digit code (
- Discrete lists: objects with
regionandlist(array of codes). Numeric countries store numbers; matching useszipCodeLengthto pad leading zeros before comparison. Examples: USA (listof 5-digit numbers), RUS (per-region lists).
Additional format notes:
- Files can mix formats if needed. Order matters because the lookup stops at the first match.
- Intervals and lists are treated as inclusive ranges; for alpha codes only exact equality is used.
- Keep codes as strings when leading zeros are significant.
- Add the ISO3 code to
availableCountriesinlib/region.js; otherwise the static data is ignored and Google is used. - If the country needs custom postal-code normalization, extend
validateZipCode(see existing cases for GBR, CAN, NLD, MEX).
- Add test cases to
test/tests.jsundercountriesPostalCodes: include the country ISO/name, a representative zip, expected region code, and setusingGoogle: false. - Run
npm testto execute Mocha tests. - Optional: verify pretty-name lookups via
getNameFromCountryAndRegionusing theregionNamesfile.
- Region codes must be valid ISO 3166-2 for the country.
regionNamesshould be English and cover every region code present inregions/ISO3.json.- Postal-code coverage should include territories and edge prefixes (e.g., US territories, Canadian northern prefixes).
- Prefer ascending ordering of
regionsentries to keep matching predictable. - Avoid empty mappings; placeholders (e.g., empty lists) will yield null regions and fall back to Google.