Index: lib/src/utils.dart |
diff --git a/lib/src/utils.dart b/lib/src/utils.dart |
index 0a0e2c68c7ccc323804d8ad8375f307a80a11d2c..f6b0ab1893978b674a773df1b35fad055fc9e54d 100644 |
--- a/lib/src/utils.dart |
+++ b/lib/src/utils.dart |
@@ -433,3 +433,20 @@ bool isDartMathMinMax(Element e) => |
e is FunctionElement && |
e.library.source.uri.toString() == 'dart:math' && |
(e.name == 'min' || e.name == 'max'); |
+ |
+/// Parses an enum value out of a string. |
+// TODO(ochafik): generic signature. |
+dynamic parseEnum(String s, List enumValues) => |
+ enumValues.firstWhere((v) => s == getEnumName(v), |
+ orElse: () => throw new ArgumentError( |
+ 'Unknown enum value: $s ' |
+ '(expected one of ${enumValues.map(getEnumName)})')); |
+ |
+/// Gets the "simple" name of an enum value. |
+getEnumName(v) { |
+ var parts = '$v'.split('.'); |
+ if (parts.length != 2 || !parts.every((p) => p.isNotEmpty)) { |
+ throw new ArgumentError('Invalid enum value: $v'); |
+ } |
+ return parts[1]; |
+} |