From b09c7c7e8ac4cc9d11b3fe545f8b62c1476d79f1 Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Tue, 27 Feb 2024 09:14:50 +1000 Subject: [PATCH] convert some switch statements to switch expressions --- src/Humanizer.Tests/DateHumanize.cs | 34 ++++--------- src/Humanizer/CasingExtensions.cs | 27 +++------- .../EnglishNumberToWordsConverter.cs | 49 +++++++------------ .../GermanNumberToWordsConverterBase.cs | 20 +++----- .../LatvianNumberToWordsConverter.cs | 21 +++----- .../LithuanianNumberToWordsConverter.cs | 44 +++++------------ .../RomanianCardinalNumberConverter.cs | 40 ++++----------- .../SwedishNumberToWordsConverter.cs | 12 ++--- .../VietnameseNumberToWordsConverter.cs | 20 +++----- .../Ordinalizers/EnglishOrdinalizer.cs | 19 +++---- src/Humanizer/TimeSpanHumanizeExtensions.cs | 31 +++++------- src/Humanizer/TupleizeExtensions.cs | 47 ++++++------------ 12 files changed, 113 insertions(+), 251 deletions(-) diff --git a/src/Humanizer.Tests/DateHumanize.cs b/src/Humanizer.Tests/DateHumanize.cs index 98ecde6d7..114910843 100644 --- a/src/Humanizer.Tests/DateHumanize.cs +++ b/src/Humanizer.Tests/DateHumanize.cs @@ -51,7 +51,6 @@ public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Te Configurator.DateTimeHumanizeStrategy = new DefaultDateTimeHumanizeStrategy(); } - var deltaFromNow = new TimeSpan(); unit = Math.Abs(unit); if (tense == Tense.Past) @@ -59,30 +58,17 @@ public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Te unit = -unit; } - switch (timeUnit) + var deltaFromNow = timeUnit switch { - case TimeUnit.Millisecond: - deltaFromNow = TimeSpan.FromMilliseconds(unit); - break; - case TimeUnit.Second: - deltaFromNow = TimeSpan.FromSeconds(unit); - break; - case TimeUnit.Minute: - deltaFromNow = TimeSpan.FromMinutes(unit); - break; - case TimeUnit.Hour: - deltaFromNow = TimeSpan.FromHours(unit); - break; - case TimeUnit.Day: - deltaFromNow = TimeSpan.FromDays(unit); - break; - case TimeUnit.Month: - deltaFromNow = TimeSpan.FromDays(unit * 31); - break; - case TimeUnit.Year: - deltaFromNow = TimeSpan.FromDays(unit * 366); - break; - } + TimeUnit.Millisecond => TimeSpan.FromMilliseconds(unit), + TimeUnit.Second => TimeSpan.FromSeconds(unit), + TimeUnit.Minute => TimeSpan.FromMinutes(unit), + TimeUnit.Hour => TimeSpan.FromHours(unit), + TimeUnit.Day => TimeSpan.FromDays(unit), + TimeUnit.Month => TimeSpan.FromDays(unit * 31), + TimeUnit.Year => TimeSpan.FromDays(unit * 366), + _ => new() + }; if (baseDate == null) { diff --git a/src/Humanizer/CasingExtensions.cs b/src/Humanizer/CasingExtensions.cs index 1c66e9759..a4d558b1a 100644 --- a/src/Humanizer/CasingExtensions.cs +++ b/src/Humanizer/CasingExtensions.cs @@ -8,24 +8,13 @@ public static class CasingExtensions /// /// Changes the casing of the provided input /// - public static string ApplyCase(this string input, LetterCasing casing) - { - switch (casing) + public static string ApplyCase(this string input, LetterCasing casing) => + casing switch { - case LetterCasing.Title: - return input.Transform(To.TitleCase); - - case LetterCasing.LowerCase: - return input.Transform(To.LowerCase); - - case LetterCasing.AllCaps: - return input.Transform(To.UpperCase); - - case LetterCasing.Sentence: - return input.Transform(To.SentenceCase); - - default: - throw new ArgumentOutOfRangeException(nameof(casing)); - } - } + LetterCasing.Title => input.Transform(To.TitleCase), + LetterCasing.LowerCase => input.Transform(To.LowerCase), + LetterCasing.AllCaps => input.Transform(To.UpperCase), + LetterCasing.Sentence => input.Transform(To.SentenceCase), + _ => throw new ArgumentOutOfRangeException(nameof(casing)) + }; } \ No newline at end of file diff --git a/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs index fa745d70e..597bd7c9c 100644 --- a/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs @@ -144,36 +144,21 @@ static string GetUnitValue(long number, bool isOrdinal) static bool ExceptionNumbersToWords(long number, [NotNullWhen(true)] out string? words) => OrdinalExceptions.TryGetValue(number, out words); - public override string ConvertToTuple(int number) - { - switch (number) - { - case 1: - return "single"; - case 2: - return "double"; - case 3: - return "triple"; - case 4: - return "quadruple"; - case 5: - return "quintuple"; - case 6: - return "sextuple"; - case 7: - return "septuple"; - case 8: - return "octuple"; - case 9: - return "nonuple"; - case 10: - return "decuple"; - case 100: - return "centuple"; - case 1000: - return "milluple"; - default: - return $"{number}-tuple"; - } - } + public override string ConvertToTuple(int number) => + number switch + { + 1 => "single", + 2 => "double", + 3 => "triple", + 4 => "quadruple", + 5 => "quintuple", + 6 => "sextuple", + 7 => "septuple", + 8 => "octuple", + 9 => "nonuple", + 10 => "decuple", + 100 => "centuple", + 1000 => "milluple", + _ => $"{number}-tuple" + }; } \ No newline at end of file diff --git a/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs b/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs index a01b11282..b8f954295 100644 --- a/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs +++ b/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs @@ -134,20 +134,14 @@ string Part(string pluralFormat, string singular, long number) static int NoRestIndex(int number) => number == 0 ? 0 : 1; - static string GetEndingForGender(GrammaticalGender gender) - { - switch (gender) + static string GetEndingForGender(GrammaticalGender gender) => + gender switch { - case GrammaticalGender.Masculine: - return "ter"; - case GrammaticalGender.Feminine: - return "te"; - case GrammaticalGender.Neuter: - return "tes"; - default: - throw new ArgumentOutOfRangeException(nameof(gender)); - } - } + GrammaticalGender.Masculine => "ter", + GrammaticalGender.Feminine => "te", + GrammaticalGender.Neuter => "tes", + _ => throw new ArgumentOutOfRangeException(nameof(gender)) + }; protected virtual string GetTens(long tens) => TensMap[tens]; diff --git a/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs index edebe3633..f8f418adf 100644 --- a/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs @@ -184,22 +184,13 @@ public override string ConvertToOrdinal(int input, GrammaticalGender gender) return string.Join(" ", parts); } - static string GetOrdinalEndingForGender(GrammaticalGender gender) - { - switch (gender) + static string GetOrdinalEndingForGender(GrammaticalGender gender) => + gender switch { - case GrammaticalGender.Masculine: - { - return "ais"; - } - case GrammaticalGender.Feminine: - { - return "ā"; - } - default: - throw new ArgumentOutOfRangeException(nameof(gender)); - } - } + GrammaticalGender.Masculine => "ais", + GrammaticalGender.Feminine => "ā", + _ => throw new ArgumentOutOfRangeException(nameof(gender)) + }; static string GetCardinalEndingForGender(GrammaticalGender gender, long number) { diff --git a/src/Humanizer/Localisation/NumberToWords/LithuanianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/LithuanianNumberToWordsConverter.cs index d8eb45ad0..4be695098 100644 --- a/src/Humanizer/Localisation/NumberToWords/LithuanianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/LithuanianNumberToWordsConverter.cs @@ -172,24 +172,13 @@ static string ChooseCardinalOrOrdinalForm(long number, string ordinalForm, strin static int GetFormIndex(long number) { var form = LithuanianNumberFormDetector.Detect(number); - - switch (form) + return form switch { - case LithuanianNumberForm.Singular: - { - return 0; - } - case LithuanianNumberForm.Plural: - { - return 1; - } - case LithuanianNumberForm.GenitivePlural: - { - return 2; - } - default: - throw new ArgumentOutOfRangeException(nameof(form)); - } + LithuanianNumberForm.Singular => 0, + LithuanianNumberForm.Plural => 1, + LithuanianNumberForm.GenitivePlural => 2, + _ => throw new ArgumentOutOfRangeException(nameof(form)) + }; } static string GetCardinalNumberForGender(string number, GrammaticalGender gender) @@ -222,20 +211,11 @@ static string GetCardinalNumberForGender(string number, GrammaticalGender gender throw new ArgumentOutOfRangeException(nameof(gender)); } - static string GetOrdinalEndingForGender(GrammaticalGender gender) - { - switch (gender) + static string GetOrdinalEndingForGender(GrammaticalGender gender) => + gender switch { - case GrammaticalGender.Masculine: - { - return "as"; - } - case GrammaticalGender.Feminine: - { - return "a"; - } - default: - throw new ArgumentOutOfRangeException(nameof(gender)); - } - } + GrammaticalGender.Masculine => "as", + GrammaticalGender.Feminine => "a", + _ => throw new ArgumentOutOfRangeException(nameof(gender)) + }; } \ No newline at end of file diff --git a/src/Humanizer/Localisation/NumberToWords/Romanian/RomanianCardinalNumberConverter.cs b/src/Humanizer/Localisation/NumberToWords/Romanian/RomanianCardinalNumberConverter.cs index 2a27e4ffd..24808f8fd 100644 --- a/src/Humanizer/Localisation/NumberToWords/Romanian/RomanianCardinalNumberConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/Romanian/RomanianCardinalNumberConverter.cs @@ -162,38 +162,16 @@ static List SplitEveryThreeDigits(int number) /// to use for the next three-digit set. /// /// The next conversion function to use. - Func? GetNextPartConverter(ThreeDigitSets currentSet) - { - Func? converter; - - switch (currentSet) + Func? GetNextPartConverter(ThreeDigitSets currentSet) => + currentSet switch { - case ThreeDigitSets.Units: - converter = UnitsConverter; - break; - - case ThreeDigitSets.Thousands: - converter = ThousandsConverter; - break; - - case ThreeDigitSets.Millions: - converter = MillionsConverter; - break; - - case ThreeDigitSets.Billions: - converter = BillionsConverter; - break; - - case ThreeDigitSets.More: - converter = null; - break; - - default: - throw new ArgumentOutOfRangeException("Unknow ThreeDigitSet: " + currentSet); - } - - return converter; - } + ThreeDigitSets.Units => UnitsConverter, + ThreeDigitSets.Thousands => ThousandsConverter, + ThreeDigitSets.Millions => MillionsConverter, + ThreeDigitSets.Billions => BillionsConverter, + ThreeDigitSets.More => null, + _ => throw new ArgumentOutOfRangeException("Unknow ThreeDigitSet: " + currentSet) + }; /// /// Converts a three-digit set to text. diff --git a/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs index 0b337940c..2abed4ca2 100644 --- a/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs @@ -222,15 +222,11 @@ public override string ConvertToOrdinal(int number) // suffix -de/-te if (number % m.Value == 0) { - switch (number) + word += number switch { - case 1_000_000: - word += "te"; - break; - default: - word += "de"; - break; - } + 1_000_000 => "te", + _ => "de" + }; } number %= m.Value; diff --git a/src/Humanizer/Localisation/NumberToWords/VietnameseNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/VietnameseNumberToWordsConverter.cs index 3c926ccc5..8cf2073d5 100644 --- a/src/Humanizer/Localisation/NumberToWords/VietnameseNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/VietnameseNumberToWordsConverter.cs @@ -18,20 +18,14 @@ public override string Convert(long number) => public override string ConvertToOrdinal(int number) => $"thứ {ConvertToOrdinalImpl(number)}"; - string ConvertToOrdinalImpl(int number) - { - switch (number) + string ConvertToOrdinalImpl(int number) => + number switch { - case 1: - return "nhất"; - case 2: - return "nhì"; - case 4: - return "tư"; - default: - return Convert(number); - } - } + 1 => "nhất", + 2 => "nhì", + 4 => "tư", + _ => Convert(number) + }; static string ConvertImpl(long number, bool hasTens = false, bool isGreaterThanOneHundred = false) { diff --git a/src/Humanizer/Localisation/Ordinalizers/EnglishOrdinalizer.cs b/src/Humanizer/Localisation/Ordinalizers/EnglishOrdinalizer.cs index 2dd0f5dbb..d934f81d8 100644 --- a/src/Humanizer/Localisation/Ordinalizers/EnglishOrdinalizer.cs +++ b/src/Humanizer/Localisation/Ordinalizers/EnglishOrdinalizer.cs @@ -11,19 +11,12 @@ public override string Convert(int number, string numberString) return numberString + "th"; } - switch (number % 10) + return (number % 10) switch { - case 1: - return numberString + "st"; - - case 2: - return numberString + "nd"; - - case 3: - return numberString + "rd"; - - default: - return numberString + "th"; - } + 1 => numberString + "st", + 2 => numberString + "nd", + 3 => numberString + "rd", + _ => numberString + "th" + }; } } \ No newline at end of file diff --git a/src/Humanizer/TimeSpanHumanizeExtensions.cs b/src/Humanizer/TimeSpanHumanizeExtensions.cs index b448710fe..f77dac0df 100644 --- a/src/Humanizer/TimeSpanHumanizeExtensions.cs +++ b/src/Humanizer/TimeSpanHumanizeExtensions.cs @@ -103,27 +103,18 @@ public static string ToAge(this TimeSpan timeSpan, CultureInfo? culture = null, static int GetTimeUnitNumericalValue(TimeUnit timeUnitToGet, TimeSpan timespan, TimeUnit maximumTimeUnit) { var isTimeUnitToGetTheMaximumTimeUnit = timeUnitToGet == maximumTimeUnit; - switch (timeUnitToGet) + return timeUnitToGet switch { - case TimeUnit.Millisecond: - return GetNormalCaseTimeAsInteger(timespan.Milliseconds, timespan.TotalMilliseconds, isTimeUnitToGetTheMaximumTimeUnit); - case TimeUnit.Second: - return GetNormalCaseTimeAsInteger(timespan.Seconds, timespan.TotalSeconds, isTimeUnitToGetTheMaximumTimeUnit); - case TimeUnit.Minute: - return GetNormalCaseTimeAsInteger(timespan.Minutes, timespan.TotalMinutes, isTimeUnitToGetTheMaximumTimeUnit); - case TimeUnit.Hour: - return GetNormalCaseTimeAsInteger(timespan.Hours, timespan.TotalHours, isTimeUnitToGetTheMaximumTimeUnit); - case TimeUnit.Day: - return GetSpecialCaseDaysAsInteger(timespan, maximumTimeUnit); - case TimeUnit.Week: - return GetSpecialCaseWeeksAsInteger(timespan, isTimeUnitToGetTheMaximumTimeUnit); - case TimeUnit.Month: - return GetSpecialCaseMonthAsInteger(timespan, isTimeUnitToGetTheMaximumTimeUnit); - case TimeUnit.Year: - return GetSpecialCaseYearAsInteger(timespan); - default: - return 0; - } + TimeUnit.Millisecond => GetNormalCaseTimeAsInteger(timespan.Milliseconds, timespan.TotalMilliseconds, isTimeUnitToGetTheMaximumTimeUnit), + TimeUnit.Second => GetNormalCaseTimeAsInteger(timespan.Seconds, timespan.TotalSeconds, isTimeUnitToGetTheMaximumTimeUnit), + TimeUnit.Minute => GetNormalCaseTimeAsInteger(timespan.Minutes, timespan.TotalMinutes, isTimeUnitToGetTheMaximumTimeUnit), + TimeUnit.Hour => GetNormalCaseTimeAsInteger(timespan.Hours, timespan.TotalHours, isTimeUnitToGetTheMaximumTimeUnit), + TimeUnit.Day => GetSpecialCaseDaysAsInteger(timespan, maximumTimeUnit), + TimeUnit.Week => GetSpecialCaseWeeksAsInteger(timespan, isTimeUnitToGetTheMaximumTimeUnit), + TimeUnit.Month => GetSpecialCaseMonthAsInteger(timespan, isTimeUnitToGetTheMaximumTimeUnit), + TimeUnit.Year => GetSpecialCaseYearAsInteger(timespan), + _ => 0 + }; } static int GetSpecialCaseMonthAsInteger(TimeSpan timespan, bool isTimeUnitToGetTheMaximumTimeUnit) diff --git a/src/Humanizer/TupleizeExtensions.cs b/src/Humanizer/TupleizeExtensions.cs index e8fa0b1d1..d264dace7 100644 --- a/src/Humanizer/TupleizeExtensions.cs +++ b/src/Humanizer/TupleizeExtensions.cs @@ -13,36 +13,21 @@ public static class TupleizeExtensions /// /// Integer /// Named tuple - public static string Tupleize(this int input) - { - switch (input) + public static string Tupleize(this int input) => + input switch { - case 1: - return "single"; - case 2: - return "double"; - case 3: - return "triple"; - case 4: - return "quadruple"; - case 5: - return "quintuple"; - case 6: - return "sextuple"; - case 7: - return "septuple"; - case 8: - return "octuple"; - case 9: - return "nonuple"; - case 10: - return "decuple"; - case 100: - return "centuple"; - case 1000: - return "milluple"; - default: - return $"{input}-tuple"; - } - } + 1 => "single", + 2 => "double", + 3 => "triple", + 4 => "quadruple", + 5 => "quintuple", + 6 => "sextuple", + 7 => "septuple", + 8 => "octuple", + 9 => "nonuple", + 10 => "decuple", + 100 => "centuple", + 1000 => "milluple", + _ => $"{input}-tuple" + }; } \ No newline at end of file