expand method
Expands the HREF by replacing URI template variables by the given parameters.
Implementation
String expand(Map<String, String> parameters) {
// `+` is considered like an encoded space, and will not be properly encoded in parameters.
// This is an issue for ISO 8601 date for example.
// As a workaround, we encode manually this character. We don't do it in the full URI,
// because it could contain some legitimate +-as-space characters.
Map<String, String> params = parameters
.map((key, value) => MapEntry(key, value.replaceFirst("+", "~~+~~")));
// Escaping the last } is somehow required, otherwise the regex can't be parsed on a Pixel
// 3a. However, without it works with the unit tests.
String expanded = uri.replaceAllMapped(RegExp("\\{(\\??)([^}]+)\\}"), (it) {
if (it.group(1)?.isEmpty == true) {
return _expandSimpleString(it.group(2)!, params);
} else {
return _expandFormStyle(it.group(2)!, params);
}
});
return Href(expanded)
.percentEncodedString
.replaceAll("~~+~~", "%2B")
.replaceAll("~~%20~~", "%2B");
}