toSnakeCase method

  1. @useResult
String toSnakeCase([
  1. Pattern? separators
])

Snake-cases this string.

wordSeparators is used to separate words if separators is not given.

'camelCase'.toSnakeCase(); // 'camel_case'

'PascalCase'.toSnakeCase(); // 'pascal_case'

'SCREAMING_CASE'.toSnakeCase(); // 'screaming_case'

'snake_case'.toSnakeCase(); // 'snake_case'

'kebab-case'.toSnakeCase(); // 'kebab_case'

'Title Case'.toSnakeCase(); // 'title_case'

'Sentence case'.toSnakeCase(); // 'sentence_case'

Implementation

@useResult String toSnakeCase([Pattern? separators]) => split(separators ?? wordSeparators)
  .where((e) => e.isNotEmpty)
  .join('_')
  .toLowerCase();