1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Artemis/src/Artemis.Core/Utilities/EnumUtilities.cs
Robert f6090dc296 Code style - Use file scoped namespaces
Code style - Ran code cleanup
2022-08-21 11:36:15 +02:00

45 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Humanizer;
namespace Artemis.Core;
/// <summary>
/// Provides utilities for display enums in a human readable form
/// </summary>
public static class EnumUtilities
{
/// <summary>
/// Creates a list containing a tuple for each value in the enum type
/// </summary>
/// <typeparam name="T">The enum type to create value descriptions for</typeparam>
/// <returns>A list containing a value-description tuple for each value in the enum type</returns>
public static List<(T, string)> GetAllValuesAndDescriptions<T>() where T : struct, Enum
{
return Enum.GetValues<T>().Select(e => (e, e.Humanize())).ToList();
}
/// <summary>
/// Creates a list containing a tuple for each value in the enum type
/// </summary>
/// <param name="t">The enum type to create value descriptions for</param>
/// <returns>A list containing a value-description tuple for each value in the enum type</returns>
public static List<(Enum, string)> GetAllValuesAndDescriptions(Type t)
{
if (!t.IsEnum)
throw new ArgumentException($"{t} must be an enum type");
return Enum.GetValues(t).Cast<Enum>().Select(e => (e, e.Humanize())).ToList();
}
/// <summary>
/// Humanizes the given enum value using the Humanizer library
/// </summary>
/// <param name="value">The enum value to humanize</param>
/// <returns>A humanized string describing the given value</returns>
public static string HumanizeValue(Enum value)
{
return value.Humanize();
}
}