Add accessors

This commit is contained in:
femsci 2023-12-24 00:57:02 +01:00
parent e0db4a25a0
commit d590c7b8fa
Signed by: femsci
GPG key ID: 08F7911F0E650C67

View file

@ -14,6 +14,46 @@ public readonly partial struct Country
CcTld = tld;
}
public static Country GetFromCode(CountryCode code)
{
return _countries[code];
}
public static Country? GetFromName(string name)
{
name = name.Trim();
return _countries.SingleOrDefault(s => s.Value.OfficialName.Equals(name, StringComparison.OrdinalIgnoreCase) || s.Value.CustomaryName.Equals(name, StringComparison.OrdinalIgnoreCase)).Value;
}
public static IEnumerable<Country> GetByCurrency(string code)
{
code = code.ToUpperInvariant();
return _countries.Where(s => s.Value.CurrencyCode == code).Select(s => s.Value).AsEnumerable();
}
public static IEnumerable<Country> GetByCurrency(int code)
{
return _countries.Where(s => s.Value.CurrencyCodeNumeric == code).Select(s => s.Value).AsEnumerable();
}
public static Country? GetFromCallingCode(int code)
{
return _countries.SingleOrDefault(s => s.Value.CallingCode == code).Value;
}
public static Country? GetFromTld(string ccTld)
{
ccTld = ccTld.Trim();
if (ccTld.Length != 2)
{
throw new ArgumentException($"Invalid ccTLD: {ccTld}");
}
return _countries.SingleOrDefault(s => s.Value.CcTld == ccTld).Value;
}
public static ICollection<Country> GetAll() => _countries.Select(c => c.Value).ToArray();
public readonly CountryCode Code { get; private init; }
public readonly int IsoNumeric => (int)Code;
public readonly string OfficialName { get; private init; }