mobven / CountryPicker

Country Picker with unicode flags and country codes.
MIT License
67 stars 19 forks source link

Improve filter function #29

Open yousifalraheem opened 6 months ago

yousifalraheem commented 6 months ago

Description

The filter/search function is lacking a lot. The current implementation filters only by the country name, and even then. It could be much better if users can also filter by the phoneCode. For example, if I want to lookup Germany I can either type "Germany" or "+49".

Another improvement that you can make is to run the comparison while reseting base diacritic characters. For example, Austria is written in German as "Österreich", or in some accents "Osterreich". Notice there is a difference in the first letter. With your current text matching, the if I try to search for Austria with the second accent I will get false:

// Current implementation
let text1 = "Österreich"
let text2 = "Osterreich"

let result = text1.localizedLowercase.contains(text2.localizedLowercase) // false
// Improved implementation
let text1 = "Österreich"
let text2 = "Osterreich"

let options: String.CompareOptions = [.diacriticInsensitive, .caseInsensitive, .widthInsensitive]
let first = text1.folding(options: options, locale: .current)
let second = text2.folding(options: options, locale: .current)

let result = first.contains(second) // true
ucotta-at-wwm commented 2 months ago

We have the same problem in Spanish: in search fields, we never use special characters like "é" in "Bélgica" (Belgium). I found another problem with spaces; they should be trimmed after the search to avoid things like "Bélgica ". My dictionary added it before replacing "belgica" with "Bélgica".