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

remove extra expression from enum humanize #1402

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
25 changes: 11 additions & 14 deletions src/Humanizer/EnumHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ public static string Humanize(this Enum input)
{
return Enum.GetValues(type)
.Cast<Enum>()
.Where(e => e.CompareTo(Convert.ChangeType(Enum.ToObject(type, 0), type)) != 0)
.Where(input.HasFlag)
.Select(e => e.Humanize())
.Where(_ => input.HasFlag(_) &&
_.CompareTo(Convert.ChangeType(Enum.ToObject(type, 0), type)) != 0)
.Select(_ => _.Humanize())
.Humanize();
}

var caseName = input.ToString();
var member = type.GetTypeInfo().GetDeclaredField(caseName);
var member = type.GetField(caseName)!;

if (member != null)
{
var description = GetCustomDescription(member);
var description = GetCustomDescription(member);

if (description != null)
{
return description;
}
if (description != null)
{
return description;
}

return caseName.Humanize();
Expand All @@ -48,9 +45,9 @@ public static string Humanize(this Enum input)
static bool IsBitFieldEnum(Type type) =>
type.GetCustomAttribute(typeof(FlagsAttribute)) != null;

static string GetCustomDescription(MemberInfo memberInfo)
static string GetCustomDescription(MemberInfo member)
{
var displayAttribute = memberInfo.GetCustomAttribute<DisplayAttribute>();
var displayAttribute = member.GetCustomAttribute<DisplayAttribute>();
if (displayAttribute != null)
{
var description = displayAttribute.GetDescription();
Expand All @@ -62,7 +59,7 @@ static string GetCustomDescription(MemberInfo memberInfo)
return displayAttribute.GetName();
}

foreach (var attr in memberInfo.GetCustomAttributes())
foreach (var attr in member.GetCustomAttributes())
{
var attrType = attr.GetType();
var descriptionProperty =
Expand Down