normalizePath method
Normalizes a string path
This method will:
- make the path component lowercase
- sort the query parameters
If the string is not a valid URI, it returns itself without change.
Implementation
String normalizePath() {
Uri uri;
try {
uri = Uri.parse(this);
} on FormatException {
return this;
}
final Map<String, String> queryParams = uri.queryParameters;
final sortedQueryParams = queryParams.entries.toList()
..sort((a, b) => a.key.compareTo(b.key));
final sortedQueryString = sortedQueryParams
.map(
(entry) =>
'${Uri.encodeQueryComponent(entry.key)}=${Uri.encodeQueryComponent(entry.value)}',
)
.join('&');
return sortedQueryString.isEmpty
? uri.path.toLowerCase()
: '${uri.path}?$sortedQueryString';
}