toSlug method

String toSlug()

Converts this string to a URL-friendly slug.

'Hello World! 123'.toSlug() // 'hello-world-123'

Implementation

String toSlug() {
  if (isEmpty) return '';
  return toLowerCase()
      .replaceAll(RegExp(r'[^\w\s\-]'), '')
      .trim()
      .replaceAll(RegExp(r'[\s_]+'), '-');
}