janmarques / IsoCountries

List of ISO country codes, with their 2-character, 3 character and numeric code. Includes English country name, demonym and adjective.
0 stars 1 forks source link

Lookup Extensions #4

Open niemyjski opened 7 years ago

niemyjski commented 7 years ago

I have some lookup extensions.. I could submit a pr but I currently have alias support built in....

        public List<string> GetAllNames() {
            return Country.Countries.Select(c => c.Name).ToList();
        }

        public List<Country> GetCountryByPartialName(string name) {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name));

            return Country.Countries.Where(c => CultureInfo.InvariantCulture.CompareInfo.IndexOf(c.Name, name, CompareOptions.IgnoreCase) >= 0).ToList();
        }

        public Country GetCountryByFullName(string name) {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name));

            return Country.Countries.FirstOrDefault(c => String.Equals(c.Name, name, StringComparison.InvariantCultureIgnoreCase));
        }

        public Country GetCountryByAlpha2(string code) {
            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException(nameof(code));

            return Country.Countries.FirstOrDefault(c => c.Alpha2Code == code);
        }

        public Country GetCountryByAlpha3(string code) {
            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException(nameof(code));

            return Country.Countries.FirstOrDefault(c => c.Alpha3Code == code);
        }

        public static Country GetCountryByNumeric(int code) {
            if (code <= 0 || code > 1000)
                throw new ArgumentOutOfRangeException(nameof(code));

            return Country.Countries.FirstOrDefault(c => c.NumericCode == code);
        }
niemyjski commented 7 years ago

Would you like me to submit a pr for the above minus the alias lookup?