-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquery_params.dart
More file actions
40 lines (33 loc) · 1.17 KB
/
query_params.dart
File metadata and controls
40 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// Helper class to construct uri for the ImageTransformation class
class URLQueryParams {
final Map<String, String> _values = {};
// Appends a parameter to the query with received key.
void append(String key, dynamic value) {
if (value != null && value.toString().isNotEmpty) {
final sanitizedValue = _sanitizeInput(value.toString());
_values[key] = Uri.encodeQueryComponent(sanitizedValue);
}
}
// Removes a parameter from query by key.
void remove(String key) {
_values.remove(key);
}
// Convert to query string like the next example:
// * param1=value1¶m2=value2
@override
String toString() {
return _values.entries
.map((entry) => '${Uri.encodeQueryComponent(entry.key)}=${entry.value}')
.join('&');
}
String toUrl(String url) {
if (url.isEmpty) throw ArgumentError('URL cannot be empty');
final Uri parsedUri = Uri.parse(url);
final String normalizedUrl = parsedUri.normalizePath().toString();
return Uri.parse('$normalizedUrl?${toString()}').toString();
}
String _sanitizeInput(String input) {
final pattern = RegExp(r'[<>\"\;(){}]');
return input.replaceAll(pattern, '');
}
}