makePlural method

String makePlural()

Makes a word plural by adding the correct suffix ("s", "es", or "'s")

Implementation

String makePlural() {
  if (isEmpty) return this;

  final word = this;
  final lowerWord = toLowerCase();

  // For acronyms or all-caps words, use apostrophe + s
  if (word == word.toUpperCase() && word.length > 1) {
    return "$word's";
  }

  // Words ending in s, ss, x, z, ch, sh -> add "es"
  if (lowerWord.endsWith('s') ||
      lowerWord.endsWith('ss') ||
      lowerWord.endsWith('x') ||
      lowerWord.endsWith('z') ||
      lowerWord.endsWith('ch') ||
      lowerWord.endsWith('sh')) {
    return "${word}es";
  }

  // Words ending in consonant + y -> change y to ies
  if (lowerWord.endsWith('y') && lowerWord.length > 1) {
    final beforeY = lowerWord[lowerWord.length - 2];
    if (!_isVowel(beforeY)) {
      return "${word.substring(0, word.length - 1)}ies";
    }
  }

  // Words ending in consonant + o -> add "es"
  if (lowerWord.endsWith('o') && lowerWord.length > 1) {
    final beforeO = lowerWord[lowerWord.length - 2];
    if (!_isVowel(beforeO)) {
      return "${word}es";
    }
  }

  // Words ending in f or fe -> change to ves
  if (lowerWord.endsWith('f')) {
    return "${word.substring(0, word.length - 1)}ves";
  }
  if (lowerWord.endsWith('fe')) {
    return "${word.substring(0, word.length - 2)}ves";
  }

  // Default: just add "s"
  return "${word}s";
}