diff --git a/countries.go b/countries.go index c2c9347..3c42943 100644 --- a/countries.go +++ b/countries.go @@ -3,12 +3,15 @@ package sanja import ( _ "embed" "encoding/json" + "sync" ) //go:embed countries.json var countriesData []byte -func getCountries() []Country { +var getCountries = sync.OnceValue(parseCountries) + +func parseCountries() []Country { var result []Country if err := json.Unmarshal(countriesData, &result); err != nil { panic("sanja: failed to parse countries data: " + err.Error()) diff --git a/country.go b/country.go index 6f953a1..d188fbb 100644 --- a/country.go +++ b/country.go @@ -46,7 +46,6 @@ func (n *Normalizer) ValidatePhoneNumber(phone, countryA2 string) error { return ErrInvalidPhoneNumber } - var matchedCode string for _, code := range splitDialingCodes(country.DialingCode) { if strings.HasPrefix(cleaned, "+"+code) || strings.HasPrefix(cleaned, code) { @@ -59,7 +58,6 @@ func (n *Normalizer) ValidatePhoneNumber(phone, countryA2 string) error { return fmt.Errorf("%w: expected code for %s", ErrPhoneNumberAndCountryCodeMismatch, country.Name) } - withoutPlus := strings.TrimPrefix(cleaned, "+") subscriber := withoutPlus[len(matchedCode):]