All files / src/utils pluralize.ts

100% Statements 27/27
100% Branches 15/15
100% Functions 1/1
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 421x   1x 21x 21x 21x 21x   21x 2x 2x   21x 1x 1x   18x     21x 1x 1x     21x 1x 1x     21x 1x 1x     21x 1x 1x     14x 14x  
import { PLURALIZATION_RULES } from '@/constants';
 
export const pluralize = (
  count: number,
  singular: string,
  plural?: string,
): string => {
  // Explicit plural always wins
  if (plural) {
    return count === 1 ? singular : plural;
  }
 
  if (count === 1) {
    return singular;
  }
 
  const lower = singular.toLowerCase();
 
  // Words ending in -s, -ss, -sh, -ch, -x, -z → add "es"
  if (PLURALIZATION_RULES.ES_ENDINGS.test(lower)) {
    return `${singular}es`;
  }
 
  // Consonant + y → replace y with "ies"
  if (PLURALIZATION_RULES.CONSONANT_Y.test(lower)) {
    return singular.replace(/y$/, 'ies');
  }
 
  // Words ending in -f or -fe → replace with "ves"
  if (PLURALIZATION_RULES.F_OR_FE.test(lower)) {
    return singular.replace(/(f|fe)$/, 'ves');
  }
 
  // Words ending in -o → often add "es"
  if (PLURALIZATION_RULES.O_ENDING.test(lower)) {
    return `${singular}es`;
  }
 
  // Default rule
  return `${singular}s`;
};