toSnakeCase method

String toSnakeCase()

Converts input like "meal-plan" or "meal plan" into snake_case.

Implementation

String toSnakeCase() {
  return trim()
      .toLowerCase()
      .replaceAll(
        RegExp(r'[\s\-]+'),
        '_',
      ) // Replace spaces and hyphens with underscores
      .replaceAll(
        RegExp(r'_+'),
        '_',
      ) // Merge multiple consecutive underscores
      .replaceAll(
        RegExp(r'^_+|_+$'),
        '',
      ); // Remove leading and trailing underscores
}