Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ Sanja includes comprehensive country data for **250+ countries and territories**
### Data Source
The country data used in this package was sourced from [Kaggle - Country 2ISO3UN Digit Code and Dialing Code](https://www.kaggle.com/datasets/migeruj/country-2iso3un-digit-code-and-dialing-code). The main country for each shared dialling code (`mainCountryForCode`) and the prefix each country dials to call abroad (`internationalPrefix`) come from Google’s [libphonenumber metadata](https://github.com/google/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml).

### Updating country data

These fields in `countries.json` come from Google’s libphonenumber metadata:

- `internationalPrefix`: what callers dial before a foreign number, such as `00` or `011`
- `nationalPrefix`: what callers dial before a number inside the country, such as `0` in Malawi or `8` in Russia. Some countries, like Italy and Côte d’Ivoire, have none: their leading `0` is part of the number
- `numberPattern`: the shape of a valid number, used to tell a leading `0` or `8` that belongs to the number from one that doesn’t
- `mainCountryForCode`: which country to use when several share a dialling code

To refresh them, pin the libphonenumber release in `internal/cmd/gencountries/main.go` and run this from the repository root:

```bash
go run ./internal/cmd/gencountries
```

Countries libphonenumber doesn’t list keep their current values, and the command prints their codes.

## API Reference

### Types
Expand All @@ -150,6 +167,8 @@ type Country struct {
NumCode int // ISO Numeric code (e.g., 840)
DialingCode string // International dialing code (e.g., "1"), or several separated by commas
InternationalPrefix string // Pattern for what callers dial before a foreign number (e.g., "00", "011")
NationalPrefix string // What callers dial before a number inside the country (e.g., "0"), or "" for none
NumberPattern string // Pattern for a valid number without the dialing code
MainCountryForCode bool // True for the country returned when several share a dialing code
MinLocalDigits int // Fewest digits after the dialing code
MaxLocalDigits int // Most digits after the dialing code
Expand Down
7 changes: 7 additions & 0 deletions countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func parseCountries() []Country {
}

result[i].internationalPrefixPattern = pattern

numberPattern, err := compileNumberPattern(result[i].NumberPattern)
if err != nil {
panic("sanja: number pattern for " + result[i].A2 + ": " + err.Error())
}

result[i].numberPattern = numberPattern
}

return result
Expand Down
Loading
Loading