Add accessors
This commit is contained in:
parent
e0db4a25a0
commit
d590c7b8fa
1 changed files with 40 additions and 0 deletions
|
@ -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; }
|
||||
|
|
Loading…
Reference in a new issue