1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-03-24 18:28:49 +00:00
Artemis/src/Artemis.Core/Utilities/EnumUtilities.cs
Robert 55dca6cebb Upgrade packages
Update namespaces
Clean up unused imports
Replace Avalonia.ReactiveUI with ReactiveUI.Avalonia 🙄
2025-12-16 22:28:16 +01: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, HumanizeValue(e))).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.ToString().Humanize();
}
}