normalizePath static method
Normalizes a raw path by:
- Reducing multiple slashes (
//
) to one (/
) - Ensuring a leading slash
- Removing trailing slash if path is not the root
Useful for consistent internal comparisons or storage.
Example:
final normalized = UriTemplate.normalizePath('///api//v1/users/');
print(normalized); // /api/v1/users
Implementation
static String normalizePath(String path) {
String normalized = path.replaceAll(RegExp(r'/{2,}'), '/'); // Replace multiple slashes with single
if (!normalized.startsWith('/')) {
normalized = '/$normalized';
}
if (normalized.length > 1 && normalized.endsWith('/')) {
normalized = normalized.substring(0, normalized.length - 1);
}
return normalized;
}