tomSlugify function

String tomSlugify(
  1. String text, {
  2. String fallback = 'section',
})

Lowercase ASCII slug of text: runs of anything but a-z0-9 become one -, with none leading or trailing. An empty result becomes fallback.

Implementation

String tomSlugify(String text, {String fallback = 'section'}) {
  final slug = text
      .toLowerCase()
      .replaceAll(RegExp(r'[^a-z0-9]+'), '-')
      .replaceAll(RegExp(r'-+'), '-')
      .replaceAll(RegExp(r'^-|-$'), '');
  return slug.isEmpty ? fallback : slug;
}