diff --git a/PROMS/VEPROMS.CSLA.Library/EnumExtensions.cs b/PROMS/VEPROMS.CSLA.Library/EnumExtensions.cs new file mode 100644 index 00000000..d68472e4 --- /dev/null +++ b/PROMS/VEPROMS.CSLA.Library/EnumExtensions.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VEPROMS.CSLA.Library +{ + public static class EnumExtensions + { + // Take a flag enum and extract the cleaned string values. + public static List ToComparableStrings(this Enum eNum) + => eNum.ToString() + .Split(',') + .Select(str => str.ToCleanString()) + .ToList(); + + // Take an individual enum and report the textual value. + public static string ToComparableString(this Enum eNum) + => eNum.ToString() + .ToCleanString(); + + // Take an individual enum and report the integer values as a comma delimited string + public static string ToCommaDelimString(this Enum eNum) + { + string retVal = string.Empty; + + if (!string.IsNullOrEmpty(eNum.ToString())) + { + foreach (Enum f in Enum.GetValues(eNum.GetType())) + { + if (eNum.ToString().Contains(f.ToString())) + { + retVal = (string.IsNullOrEmpty(retVal)) ? Convert.ToInt32(f).ToString() : string.Concat(retVal, ",", Convert.ToInt32(f)); + } + } + } + + return retVal; + } + + // Remove any spaces due to split and if `_` found change it to space. + public static string ToCleanString(this string str) + => str.Replace(" ", string.Empty) + .Replace('_', ' '); + } +}