pluralize method

  1. @useResult
String pluralize(
  1. num? count, {
  2. bool simple = false,
})

Returns the plural form of this string based on count.

When simple is true, just appends 's'. Otherwise, applies English pluralization rules (e.g., -es, -ies).

Implementation

@useResult
String pluralize(num? count, {bool simple = false}) {
  if (isEmpty || count == 1) {
    return this;
  }

  if (simple) {
    return '${this}s';
  }

  final String lastChar = lastChars(1);
  switch (lastChar) {
    case 's':
    case 'x':
    case 'z':
      return '${this}es';
    case 'y':
      // Vowel + y just adds s ("days"); consonant + y switches y to "ies"
      // ("city" -> "cities"). length > 2 guards against 2-letter words like
      // "by" where there's no consonant to inspect before the y.
      if (length > 2 && this[length - 2].isVowel()) {
        return '${this}s';
      }

      return '${substringSafe(0, length - 1)}ies';
  }

  final String lastTwo = lastChars(2);
  if (lastTwo == 'sh' || lastTwo == 'ch') {
    return '${this}es';
  }

  return '${this}s';
}