Skip to content

Commit

Permalink
remove extra expression from enum humanize (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 17, 2024
1 parent c693000 commit 36e603c
Showing 1 changed file with 11 additions and 14 deletions.
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

0 comments on commit 36e603c

Please sign in to comment.