toCamelCase method

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

Camel-cases this string.

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

'camelCase'.toCamelCase(); // 'camelCase'

'PascalCase'.toCamelCase(); // 'pascalCase'

'SCREAMING_CASE'.toCamelCase(); // 'screamingCase'

'snake_case'.toCamelCase(); // 'snakeCase'

'kebab-case'.toCamelCase(); // 'kebabCase'

'Title Case'.toCamelCase(); // 'titleCase'

'Sentence case'.toCamelCase(); // 'sentenceCase'

Implementation

@useResult String toCamelCase([Pattern? separators]) {
  final words = split(separators ?? wordSeparators).where((e) => e.isNotEmpty);
  if (words.isEmpty) {
    return '';
  }

  final buffer = StringBuffer()..write(words.first.toLowerCase());
  for (final word in words.skip(1)) {
    if (isNotEmpty) {
      buffer..write(word[0].toUpperCase())..write(word.substring(1).toLowerCase());
    }
  }

  return buffer.toString();
}