File tree Expand file tree Collapse file tree 3 files changed +13
-17
lines changed Expand file tree Collapse file tree 3 files changed +13
-17
lines changed Original file line number Diff line number Diff line change @@ -44,30 +44,24 @@ public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviatio
44
44
[ Fact ]
45
45
public void Parse_AbbreviationCaseInsensitive_Lowercase_years ( )
46
46
{
47
- // Arrange
48
47
var abbreviation = "years" ;
49
48
var expected = DurationUnit . Year365 ;
50
49
var parser = UnitParser . Default ;
51
50
52
- // Act
53
51
var actual = parser . Parse < DurationUnit > ( abbreviation ) ;
54
52
55
- // Assert
56
53
Assert . Equal ( expected , actual ) ;
57
54
}
58
55
59
56
[ Fact ]
60
57
public void Parse_AbbreviationCaseInsensitive_Uppercase_Years ( )
61
58
{
62
- // Arrange
63
59
var abbreviation = "Years" ;
64
60
var expected = DurationUnit . Year365 ;
65
61
var parser = UnitParser . Default ;
66
62
67
- // Act
68
63
var actual = parser . Parse < DurationUnit > ( abbreviation ) ;
69
64
70
- // Assert
71
65
Assert . Equal ( expected , actual ) ;
72
66
}
73
67
Original file line number Diff line number Diff line change @@ -63,20 +63,22 @@ internal List<string> GetAbbreviationsForUnit(int unit)
63
63
64
64
internal List < int > GetUnitsForAbbreviation ( string abbreviation )
65
65
{
66
- return abbreviationToUnitMap
67
- . Where ( x => x . Key . Equals ( abbreviation , StringComparison . OrdinalIgnoreCase ) )
68
- . SelectMany ( x => x . Value )
69
- . Distinct ( )
70
- . ToList ( ) ;
66
+ abbreviation = abbreviation . ToLower ( ) ;
67
+ if ( ! abbreviationToUnitMap . TryGetValue ( abbreviation , out var units ) )
68
+ abbreviationToUnitMap [ abbreviation ] = units = new List < int > ( ) ;
69
+
70
+ return units . Distinct ( ) . ToList ( ) ;
71
71
}
72
72
73
73
internal void Add ( int unit , string abbreviation , bool setAsDefault = false )
74
74
{
75
+ // abbreviation = abbreviation.ToLower();
76
+ var lower = abbreviation . ToLower ( ) ;
75
77
if ( ! unitToAbbreviationMap . TryGetValue ( unit , out var abbreviationsForUnit ) )
76
78
abbreviationsForUnit = unitToAbbreviationMap [ unit ] = new List < string > ( ) ;
77
79
78
- if ( ! abbreviationToUnitMap . TryGetValue ( abbreviation , out var unitsForAbbreviation ) )
79
- abbreviationToUnitMap [ abbreviation ] = unitsForAbbreviation = new List < int > ( ) ;
80
+ if ( ! abbreviationToUnitMap . TryGetValue ( lower , out var unitsForAbbreviation ) )
81
+ abbreviationToUnitMap [ lower ] = unitsForAbbreviation = new List < int > ( ) ;
80
82
81
83
abbreviationsForUnit . Remove ( abbreviation ) ;
82
84
unitsForAbbreviation . Remove ( unit ) ;
Original file line number Diff line number Diff line change @@ -45,13 +45,13 @@ public static class UnitConverter
45
45
private static readonly string UnitTypeNamespace = typeof ( LengthUnit ) . Namespace ;
46
46
private static readonly Assembly UnitsNetAssembly = typeof ( Length ) . GetAssembly ( ) ;
47
47
48
- private static readonly List < Type > QuantityTypes = UnitsNetAssembly . GetTypes ( )
48
+ private static readonly Type [ ] QuantityTypes = UnitsNetAssembly . GetTypes ( )
49
49
. Where ( x => x . FullName . StartsWith ( QuantityNamespace ) )
50
- . ToList ( ) ;
50
+ . ToArray ( ) ;
51
51
52
- private static readonly List < Type > UnitTypes = UnitsNetAssembly . GetTypes ( )
52
+ private static readonly Type [ ] UnitTypes = UnitsNetAssembly . GetTypes ( )
53
53
. Where ( x => x . FullName . StartsWith ( UnitTypeNamespace ) )
54
- . ToList ( ) ;
54
+ . ToArray ( ) ;
55
55
56
56
/// <summary>
57
57
/// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter".
You can’t perform that action at this time.
0 commit comments