toCamelCase method

String toCamelCase()

Converts a string from snake_case or kebab-case to camelCase.

Example:

'hello_world'.toCamelCase( ) ;  // 'helloWorld'
'hello world'.toCamelCase( ) ;  // 'helloWorld'
'user_profile_data'. toCamelCase( ) ;  // 'userProfileData'
'HelloWorld'.toCamelCase( ) ;  // 'helloWorld'

Implementation

String toCamelCase() {
  return this
      .split(RegExp(r'[_\s]'))
      .map((word) => word.isEmpty
          ? ''
          : word[0].toUpperCase() + word.substring(1).toLowerCase())
      .join()
      .replaceFirstMapped(
          RegExp(r'^[A-Z]'), (match) => match.group(0)!.toLowerCase());
}