enum_to_string.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. library enum_to_string;
  2. import 'camel_case_to_words.dart';
  3. class EnumToString {
  4. static bool _isEnumItem(enumItem) {
  5. final split_enum = enumItem.toString().split('.');
  6. return split_enum.length > 1 &&
  7. split_enum[0] == enumItem.runtimeType.toString();
  8. }
  9. /// Convert an enum to a string
  10. ///
  11. /// Pass in the enum value, so TestEnum.valueOne into [enumItem]
  12. /// It will return the striped off value so "valueOne".
  13. ///
  14. /// If you pass in the option [camelCase]=true it will convert it to words
  15. /// So TestEnum.valueOne will become Value One
  16. static String convertToString(dynamic enumItem, {bool camelCase = false}) {
  17. assert(enumItem != null);
  18. assert(_isEnumItem(enumItem),
  19. '$enumItem of type ${enumItem.runtimeType.toString()} is not an enum item');
  20. final _tmp = enumItem.toString().split('.')[1];
  21. return !camelCase ? _tmp : camelCaseToWords(_tmp);
  22. }
  23. @Deprecated(
  24. 'Renamed function to EnumToString.convertToString to make it clearer')
  25. static String parse(enumItem, {bool camelCase = false}) =>
  26. convertToString(enumItem, camelCase: camelCase);
  27. /// An alias for parse(item, camelCase: true)
  28. ///
  29. @Deprecated(
  30. 'Deprecated in favour of using convertToString(item, camelCase: true)')
  31. static String parseCamelCase(enumItem) {
  32. return EnumToString.convertToString(enumItem, camelCase: true);
  33. }
  34. /// Given a string, find and return its matching enum value
  35. ///
  36. /// You need to pass in the values of the enum object. So TestEnum.values
  37. /// in the first argument. The matching value is the second argument.
  38. ///
  39. /// Example final result = EnumToString.fromString(TestEnum.values, "valueOne")
  40. /// result == TestEnum.valueOne //true
  41. ///
  42. static T? fromString<T>(List<T> enumValues, String value,
  43. {bool camelCase = false}) {
  44. try {
  45. return enumValues.singleWhere((enumItem) =>
  46. EnumToString.convertToString(enumItem, camelCase: camelCase)
  47. .toLowerCase() ==
  48. value.toLowerCase());
  49. } on StateError catch (_) {
  50. return null;
  51. }
  52. }
  53. /// Get the index of the enum value
  54. ///
  55. /// Pass in the enum values to argument one, so TestEnum.values
  56. /// Pass in the matching string to argument 2, so "valueOne"
  57. ///
  58. /// Eg. final index = EnumToString.indexOf(TestEnum.values, "valueOne")
  59. /// index == 0 //true
  60. static int indexOf<T>(List<T> enumValues, String value) {
  61. final fromStringResult = fromString<T>(enumValues, value);
  62. if (fromStringResult == null) {
  63. return -1;
  64. } else {
  65. return enumValues.indexOf(fromStringResult);
  66. }
  67. }
  68. /// Bulk convert enum values to a list
  69. ///
  70. static List<String> toList<T>(List<T> enumValues, {bool camelCase = false}) {
  71. final _enumList = enumValues
  72. .map((t) => !camelCase
  73. ? EnumToString.convertToString(t)
  74. : EnumToString.convertToString(t, camelCase: true))
  75. .toList();
  76. // I am sure there is a better way to convert a nullable list to a
  77. // non-nullable one, but this will do until I find out how. Happy if
  78. // someone want to do a PR in the meantime to correct this.
  79. var output = <String>[];
  80. for (var value in _enumList) {
  81. output.add(value);
  82. }
  83. return output;
  84. }
  85. /// Get a list of enums given a list of strings.
  86. /// Basically just EnumToString.fromString, but using lists
  87. ///
  88. /// As with fromString it is not case sensitive
  89. ///
  90. /// Eg. EnumToString.fromList(TestEnum.values, ["valueOne", "value2"]
  91. static List<T?> fromList<T>(List<T> enumValues, List valueList) {
  92. return List<T?>.from(
  93. valueList.map<T?>((item) => fromString(enumValues, item)));
  94. }
  95. }