toWords function

List<String> toWords(
  1. String input, [
  2. CaseStyle? caseStyle = defaultCaseStyle
])

Converts input of certain caseStyle to List of words

Implementation

List<String> toWords(String input, [CaseStyle? caseStyle = defaultCaseStyle]) {
  final effectiveCaseStyle = caseStyle ?? defaultCaseStyle;
  switch (effectiveCaseStyle) {
    case CaseStyle.snake:
    case CaseStyle.snakeAllCaps:
      return input.split('_');
    case CaseStyle.kebab:
      return input.split('-');
    case CaseStyle.pascal:
      return deCapitalize(input)
          .replaceAllMapped(RegExp('([a-z0-9])([A-Z])'),
              (match) => '${match.group(1)} ${match.group(2)}')
          .replaceAllMapped(RegExp('([A-Z])([A-Z])(?=[a-z])'),
              (match) => '${match.group(1)} ${match.group(2)}')
          .toLowerCase()
          .split(' ');
    case CaseStyle.camel:
      return input
          .replaceAllMapped(RegExp('([a-z0-9])([A-Z])'),
              (match) => '${match.group(1)} ${match.group(2)}')
          .replaceAllMapped(RegExp('([A-Z])([A-Z])(?=[a-z])'),
              (match) => '${match.group(1)} ${match.group(2)}')
          .toLowerCase()
          .split(' ');
    default:
      return input.split(' ');
  }
}