Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some switch statements to switch expressions #1466

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions src/Humanizer.Tests/DateHumanize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,24 @@ 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)
{
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)
{
Expand Down
27 changes: 8 additions & 19 deletions src/Humanizer/CasingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@ public static class CasingExtensions
/// <summary>
/// Changes the casing of the provided input
/// </summary>
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))
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,38 +162,16 @@ static List<int> SplitEveryThreeDigits(int number)
/// to use for the next three-digit set.
/// </summary>
/// <returns>The next conversion function to use.</returns>
Func<int, GrammaticalGender, string>? GetNextPartConverter(ThreeDigitSets currentSet)
{
Func<int, GrammaticalGender, string>? converter;

switch (currentSet)
Func<int, GrammaticalGender, string>? 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)
};

/// <summary>
/// Converts a three-digit set to text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 6 additions & 13 deletions src/Humanizer/Localisation/Ordinalizers/EnglishOrdinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
}
}
Loading