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
22 changes: 15 additions & 7 deletions country/code_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,25 @@ func (icf *IndexedCodeFetcher) Search(searchTerm string, operator SearchOperator
}

func (icf *IndexedCodeFetcher) prepareIndex() {
ccs := icf.CountryCodes
codes := icf.CountryCodes

if ccs == nil {
ccs = DefaultCountryCodes
if codes == nil {
codes = DefaultCountryCodes
}

icf.indexedCountryCodes = make(map[string]CountryCode, len(ccs))
icf.indexedCountryCodes = make(map[string]CountryCode, len(codes))

for _, cc := range ccs {
for _, k := range icf.ExtractKeys(cc) {
icf.indexedCountryCodes[icf.NormalizeKey(k)] = cc
for _, code := range codes {
for _, k := range icf.ExtractKeys(code) {
normalizedKey := icf.NormalizeKey(k)

// prevent codes with assignments with smaller priority to overwrite
// those with higher priority
if cc, ok := icf.indexedCountryCodes[normalizedKey]; ok && cc.Assignment < code.Assignment {
continue
}

icf.indexedCountryCodes[normalizedKey] = code
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion country/code_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestCodeFetcher_Search(t *testing.T) {
mustFetch("UM"),
mustFetch("AE"),
mustFetch("TZ"),
mustFetch("UK"),
mustFetch("GB"),
},
},
{
Expand Down
Loading