toKebabCase method
Implementation
String toKebabCase() {
// Insert a space before each uppercase letter that follows a non-uppercase letter
String result = replaceAllMapped(
RegExp('([a-z0-9])([A-Z])'),
(final Match match) {
return '${match.group(1)} ${match.group(2)}';
},
);
// Replace non-alphanumeric characters with a space
result = result.replaceAll(RegExp('[^a-zA-Z0-9]+'), ' ').trim();
if (result.trim().isEmpty) {
return result;
}
// Convert to lowercase and join with hyphens
return result
.split(RegExp(r'\s+'))
.map(
(final String word) {
return word.toLowerCase();
},
)
.join('-');
}