Skip to content

Commit f0646a6

Browse files
committed
Improve type conversion.
1 parent b548b60 commit f0646a6

File tree

4 files changed

+326
-89
lines changed

4 files changed

+326
-89
lines changed

lib/codelessly_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export 'src/api/models/models.dart';
1414
export 'src/api/node_json_converter.dart';
1515
export 'src/api/nodes/nodes.dart';
1616
export 'src/api/regexes.dart';
17+
export 'src/api/typed_value.dart';
1718
export 'src/api/utils.dart';

lib/src/api/extensions.dart

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,7 @@
1-
import 'dart:convert';
2-
31
import '../../codelessly_api.dart';
42

53
/// Declares extensions [String].
64
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-
505
/// Replaces [oldName] variable name with [newName] if this string happens
516
/// to be a variable path.
527
/// This string must be wrapped with ${} for this to work.
@@ -60,46 +15,3 @@ extension StringExtensions on String {
6015
});
6116
}
6217
}
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-
}

lib/src/api/models/variables_model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class VariableData
115115

116116
/// Converts [value] to given type [T]. This will fail if type conversion
117117
/// fails.
118-
R? typedValue<R>({R? defaultValue}) =>
118+
R? typedValue<R extends Object>({R? defaultValue}) =>
119119
value.typedValue<R>(defaultValue: defaultValue);
120120
}
121121

0 commit comments

Comments
 (0)