File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
packages/netglade_utils/lib/src/extensions Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ extension EnumExtensions <T extends Enum > on Iterable <T > {
2
+ /// Finds the enum value in this list with name [name] .
3
+ ///
4
+ /// Goes through this collection looking for an enum with
5
+ /// name [name] , as reported by [EnumName.name] .
6
+ /// Returns the first value with the given name. Such a value must be found.
7
+ T byNameOrDefault (String name, {required T defaultValue}) {
8
+ try {
9
+ return byName (name);
10
+ // ignore: avoid_catching_errors, byName throws it
11
+ } on ArgumentError catch (_) {
12
+ return defaultValue;
13
+ }
14
+ }
15
+
16
+ T ? byNameOrNull (String name) {
17
+ try {
18
+ return byName (name);
19
+ // ignore: avoid_catching_errors, byName throws it
20
+ } on ArgumentError catch (_) {
21
+ return null ;
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change 1
1
export 'date_time_extensions.dart' ;
2
+ export 'enum_extensions.dart' ;
2
3
export 'future_extensions.dart' ;
3
4
export 'iterable_extensions.dart' ;
4
5
export 'object_extensions.dart' ;
Original file line number Diff line number Diff line change @@ -32,6 +32,25 @@ extension StringExtensions on String {
32
32
String ? ifBlank ([String ? defaultValue]) {
33
33
return isBlank ? defaultValue : this ;
34
34
}
35
+
36
+ /// Normalize supposedly string containing URL
37
+ ///
38
+ /// Appends https:// when does not start with.
39
+ /// Trims any trailing '/' chars.
40
+ String normalizeUrl () {
41
+ final value = trim ();
42
+
43
+ if (value.startsWith (RegExp (r'http[s]?:\/\/' ))) return value.rtrim ('/' );
44
+
45
+ return 'https://$value ' .rtrim ('/' );
46
+ }
47
+
48
+ /// Removes any trailing char from [chars] .
49
+ String rtrim (String chars) {
50
+ final pattern = RegExp ('[$chars ]+\$ ' );
51
+
52
+ return replaceAll (pattern, '' );
53
+ }
35
54
}
36
55
37
56
extension NullableStringExtensions on String ? {
You can’t perform that action at this time.
0 commit comments