Skip to content

Commit

Permalink
Fix output sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Oct 24, 2022
1 parent 644238b commit b383a4e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/TimeZoneNames.DataBuilder/DataExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ private void LoadDisplayNames()
foreach (var item in languages.AsArray())
{
var locale = item["Locale"]!.GetValue<string>().Replace("-", "_");
var timeZones = item["TimeZones"]!.AsObject().ToDictionary(o=> o.Key, o=> (string)o.Value);
var timeZones = item["TimeZones"]!.AsObject()
.ToOrderedDictionary(o => o.Key, o => (string) o.Value);

_data.DisplayNames.Add(locale, timeZones);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\TimeZoneNames\OrderedDictionary.cs" Link="OrderedDictionary.cs" />
<Compile Include="..\TimeZoneNames\TimeZoneData.cs" Link="TimeZoneData.cs" />
<Compile Include="..\TimeZoneNames\TimeZoneValues.cs" Link="TimeZoneValues.cs" />
</ItemGroup>
Expand Down
46 changes: 45 additions & 1 deletion src/TimeZoneNames/OrderedDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
Expand All @@ -11,7 +12,7 @@ internal class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
// We might only need an IReadOnlyDictionary<TKey, TValue> but we want to preserve insertion order.
private readonly OrderedDictionary _dictionary;

public OrderedDictionary(int capacity, IEqualityComparer<TKey> equalityComparer)
public OrderedDictionary(int capacity = 0, IEqualityComparer<TKey> equalityComparer = default)
{
_dictionary = new OrderedDictionary(capacity, (IEqualityComparer) equalityComparer);
}
Expand Down Expand Up @@ -86,4 +87,47 @@ public TValue this[TKey key]
ICollection<TValue> IDictionary<TKey, TValue>.Values => _dictionary.Values.Cast<TValue>().ToList();

ICollection<TKey> IDictionary<TKey, TValue>.Keys => _dictionary.Keys.Cast<TKey>().ToList();
}

internal static class OrderedDictionaryExtensions
{
public static OrderedDictionary<TKey, TValue> ToOrderedDictionary<TKey, TValue>(
this ICollection<KeyValuePair<TKey, TValue>> items,
IEqualityComparer<TKey> comparer = default)
{
var result = new OrderedDictionary<TKey, TValue>(items.Count, comparer);
foreach (var item in items)
{
result.Add(item);
}
return result;
}

public static OrderedDictionary<TKey, TValue> ToOrderedDictionary<TSourceKey, TSourceValue, TKey, TValue>(
this ICollection<KeyValuePair<TSourceKey, TSourceValue>> items,
Func<KeyValuePair<TSourceKey, TSourceValue>, TKey> keySelector,
Func<KeyValuePair<TSourceKey, TSourceValue>, TValue> valueSelector,
IEqualityComparer<TKey> comparer = default)
{
var result = new OrderedDictionary<TKey, TValue>(items.Count, comparer);
foreach (var item in items)
{
var key = keySelector(item);
var value = valueSelector(item);
result.Add(key, value);
}
return result;
}

public static OrderedDictionary<TKey, TValue> ToOrderedDictionary<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> items,
IEqualityComparer<TKey> comparer = default) =>
items.ToList().ToOrderedDictionary(comparer);

public static OrderedDictionary<TKey, TValue> ToOrderedDictionary<TSourceKey, TSourceValue, TKey, TValue>(
this IEnumerable<KeyValuePair<TSourceKey, TSourceValue>> items,
Func<KeyValuePair<TSourceKey, TSourceValue>, TKey> keySelector,
Func<KeyValuePair<TSourceKey, TSourceValue>, TValue> valueSelector,
IEqualityComparer<TKey> comparer = default) =>
items.ToList().ToOrderedDictionary(keySelector, valueSelector, comparer);
}
21 changes: 9 additions & 12 deletions src/TimeZoneNames/TZNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ private static IDictionary<string, string> GetFixedTimeZoneNames(string language
results.Add(zone, name);
}


return results;
}

Expand Down Expand Up @@ -292,25 +291,23 @@ public static IDictionary<string, string> GetDisplayNames(string languageCode, b
throw new ArgumentException("Invalid Language Code", nameof(languageCode));
}

var displayNames = Data.DisplayNames[langKey];
var displayNames = Data.DisplayNames[langKey]
.Where(x => !TimeZoneData.ObsoleteWindowsZones.Contains(x.Key))
.ToList();

if (!useIanaZoneIds)
{
return displayNames;
return displayNames.ToOrderedDictionary(StringComparer.OrdinalIgnoreCase);
}

var languageCodeParts = languageCode.Split('_', '-');
var territoryCode = languageCodeParts.Length < 2 ? "001" : languageCodeParts[1];
return displayNames
.Where(x => !TimeZoneData.ObsoleteWindowsZones.Contains(x.Key))
.ToDictionary(
x => TZConvert.WindowsToIana(x.Key, territoryCode),
x => x.Value,
StringComparer.OrdinalIgnoreCase);
return displayNames.ToOrderedDictionary(
x => TZConvert.WindowsToIana(x.Key, territoryCode),
x => x.Value,
StringComparer.OrdinalIgnoreCase);
}



/// <summary>
/// Gets a list of all language codes supported by this library.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/TimeZoneNames/TimeZoneData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class TimeZoneData

public List<TimeZoneSelectionData> SelectionZones { get; set; } = new();

public Dictionary<string, Dictionary<string, string>> DisplayNames { get; set; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, IDictionary<string, string>> DisplayNames { get; set; } = new(StringComparer.OrdinalIgnoreCase);

[SecuritySafeCritical]
public static TimeZoneData Load()
Expand Down
4 changes: 4 additions & 0 deletions test/TimeZoneNames.Tests/TimeZoneNames.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions test/TimeZoneNames.Tests/TimeZoneNamesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,17 @@ public void Can_Get_English_Names_For_Alias_Cuba()
Assert.Equal(namesForZone.Standard, namesForAlias.Standard);
Assert.Equal(namesForZone.Daylight, namesForAlias.Daylight);
}

[Theory]
[MemberData(nameof(TestData.GetLanguages), MemberType = typeof(TestData))]
public Task CanGetNamesForTimeZone(string language)
{
var results = new List<KeyValuePair<string, TimeZoneValues>>();
foreach (var zone in TZConvert.KnownIanaTimeZoneNames.OrderBy(x => x))
{
results.Add(new KeyValuePair<string, TimeZoneValues>(zone, TZNames.GetNamesForTimeZone(zone, language)));
}
return Verifier.Verify(results).UseParameters(language).AutoVerify();
var results = TZConvert.KnownIanaTimeZoneNames
.ToDictionary(
zone => zone,
zone => TZNames.GetNamesForTimeZone(zone, language),
StringComparer.InvariantCulture);

return Verifier.Verify(results).UseParameters(language).DontSortDictionaries();
}
}

0 comments on commit b383a4e

Please sign in to comment.