1
- import 'dart:convert' ;
2
-
3
1
import '../../codelessly_api.dart' ;
4
2
5
3
/// Declares extensions [String] .
6
4
extension StringExtensions on String {
7
- /// Converts given string to a typed value if possible.
8
- /// Returns [defaultValue] or null otherwise.
9
- R ? typedValue <R >({R ? defaultValue}) {
10
- final value = this ;
11
- if (R == String ) {
12
- return value as R ;
13
- } else if (R == int ) {
14
- return (int .tryParse (value) ?? defaultValue) as R ? ;
15
- } else if (R == double ) {
16
- return (double .tryParse (value) ?? defaultValue) as R ;
17
- } else if (R == num ) {
18
- return (num .tryParse (value) ?? defaultValue) as R ;
19
- } else if (R == bool ) {
20
- final boolValue = bool .tryParse (value, caseSensitive: false );
21
- if (boolValue == null ) return defaultValue;
22
- return (boolValue || value == '1' ) as R ;
23
- } else if (R == Map ) {
24
- try {
25
- return jsonDecode (value) as R ? ;
26
- } catch (e) {
27
- return defaultValue;
28
- }
29
- } else if (R == List ) {
30
- try {
31
- if (! value.startsWith ('[' ) || ! value.endsWith (']' )) return null ;
32
- return value
33
- .substring (1 , value.length - 1 )
34
- .split (',' )
35
- .map ((e) => e.trim ().parsedValue ())
36
- .toList () as R ? ;
37
- } catch (e) {
38
- return defaultValue;
39
- }
40
- } else {
41
- return value as R ? ;
42
- }
43
- }
44
-
45
- /// Parse given string to a compatible dart primitive type.
46
- dynamic parsedValue () {
47
- return num .tryParse (this ) ?? bool .tryParse (this ) ?? this ;
48
- }
49
-
50
5
/// Replaces [oldName] variable name with [newName] if this string happens
51
6
/// to be a variable path.
52
7
/// This string must be wrapped with ${} for this to work.
@@ -60,46 +15,3 @@ extension StringExtensions on String {
60
15
});
61
16
}
62
17
}
63
-
64
- /// Declares extensions on [Object] .
65
- extension ObjectExt on Object ? {
66
- /// Converts given string to a typed value if possible.
67
- /// Returns [defaultValue] or null otherwise.
68
- R ? typedValue <R >({R ? defaultValue}) {
69
- final value = this ;
70
- if (value is R ) return value;
71
-
72
- if (R == String ) {
73
- return value as R ;
74
- } else if (R == int ) {
75
- return (int .tryParse ('$value ' ) ?? defaultValue) as R ? ;
76
- } else if (R == double ) {
77
- return (double .tryParse ('$value ' ) ?? defaultValue) as R ;
78
- } else if (R == num ) {
79
- return (num .tryParse ('$value ' ) ?? defaultValue) as R ;
80
- } else if (R == bool ) {
81
- final boolValue = bool .tryParse ('$value ' , caseSensitive: false );
82
- if (boolValue == null ) return defaultValue;
83
- return (boolValue || value == '1' ) as R ;
84
- } else if (R == Map ) {
85
- try {
86
- return jsonDecode ('$value ' ) as R ? ;
87
- } catch (e) {
88
- return defaultValue;
89
- }
90
- } else if (R == List ) {
91
- try {
92
- if (! '$value ' .startsWith ('[' ) || ! '$value ' .endsWith (']' )) return null ;
93
- return '$value '
94
- .substring (1 , '$value ' .length - 1 )
95
- .split (',' )
96
- .map ((e) => e.trim ().parsedValue ())
97
- .toList () as R ? ;
98
- } catch (e) {
99
- return defaultValue;
100
- }
101
- } else {
102
- return value as R ? ;
103
- }
104
- }
105
- }
0 commit comments