toCamelCase static method

String toCamelCase(
  1. String text
)

Convert a string to camelCase

Implementation

static String toCamelCase(String text) {
  final parts = text.replaceAll(RegExp(r'[^A-Za-z0-9]'), '_').split('_');
  if (parts.isEmpty) return '';

  var camel = parts.first.toLowerCase() +
      parts
          .skip(1)
          .map((e) => e.isEmpty ? '' : '${e[0].toUpperCase()}${e.substring(1).toLowerCase()}')
          .join();

  // If the result starts with a number, prefix with `k`
  if (RegExp(r'^\d+').hasMatch(camel)) {
    camel = 'k$camel';
  }

  return camel;
}