All files / web/src/utils pluralization.ts

100% Statements 44/44
100% Branches 13/13
100% Functions 10/10
100% Lines 44/44

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 42 43 44 451x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 12x 12x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { en } from 'make-plural'
 
// Use English pluralization rules from make-plural
const plural = en
 
/**
 * Pluralize a word based on count using make-plural library
 * @param count - The number to base pluralization on
 * @param singular - The singular form of the word
 * @param plural - The plural form of the word (optional, will add 's' by default)
 * @returns The properly pluralized word
 */
export function pluralizeWord(count: number, singular: string, pluralForm?: string): string {
  const category = plural(count)
  if (category === 'one') {
    return singular
  }
  return pluralForm || `${singular}s`
}
 
/**
 * Create a formatted string with count and properly pluralized word
 * @param count - The number to display
 * @param singular - The singular form of the word
 * @param plural - The plural form of the word (optional)
 * @returns Formatted string like "1 pair" or "3 pairs"
 */
export function pluralizeCount(count: number, singular: string, pluralForm?: string): string {
  return `${count} ${pluralizeWord(count, singular, pluralForm)}`
}
 
/**
 * Common game-specific pluralization helpers using make-plural
 */
export const gamePlurals = {
  pair: (count: number) => pluralizeCount(count, 'pair'),
  pairs: (count: number) => pluralizeCount(count, 'pair'),
  move: (count: number) => pluralizeCount(count, 'move'),
  moves: (count: number) => pluralizeCount(count, 'move'),
  match: (count: number) => pluralizeCount(count, 'match', 'matches'),
  matches: (count: number) => pluralizeCount(count, 'match', 'matches'),
  player: (count: number) => pluralizeCount(count, 'player'),
  players: (count: number) => pluralizeCount(count, 'player'),
} as const