All files / core/src/utility pluralize.ts

100% Statements 112/112
100% Branches 8/8
100% Functions 1/1
100% Lines 112/112

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 13x 100x 100x 100x 100x 100x 100x 13x 13x 208x 208x 208x 208x 208x 208x 13x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * **pluralize**s words of the English language.
 *
 * @param singular - An English word in `singular` form.
 * @returns The **pluralize**d form of `singular`.
 *
 * @example
 * **Pluralize** `'money'`:
 * ```ts
 * import { pluralize } from '@sgrud/core';
 *
 * pluralize('money'); // 'money'
 * ```
 *
 * @example
 * **Pluralize** `'thesis'`:
 * ```ts
 * import { pluralize } from '@sgrud/core';
 *
 * pluralize('thesis'); // 'theses'
 * ```
 */
export function pluralize(singular: string): string {
  if (!uncountables.includes(singular.toLowerCase())) {
    for (const word in irregulars) {
      const pattern = new RegExp(`${word}$`, 'i');
 
      if (pattern.test(singular)) {
        return singular.replace(pattern, irregulars[word]);
      }
    }
 
    for (const regex in regulars) {
      const pattern = new RegExp(regex, 'i');
 
      if (pattern.test(singular)) {
        return singular.replace(pattern, regulars[regex]);
      }
    }
  }
 
  return singular;
}
 
/**
 * Regex mapping of singular words to their regular plural forms.
 */
const regulars = {
  '(quiz)$'                    : '$1zes',
  '^(ox)$'                     : '$1en',
  '([m|l])ouse$'               : '$1ice',
  '(matr|vert|ind)ix|ex$'      : '$1ices',
  '(x|ch|ss|sh)$'              : '$1es',
  '([^aeiouy]|qu)y$'           : '$1ies',
  '(hive)$'                    : '$1s',
  '(?:([^f])fe|([lr])f)$'      : '$1$2ves',
  '(shea|lea|loa|thie)f$'      : '$1ves',
  'sis$'                       : 'ses',
  '([ti])um$'                  : '$1a',
  '(tomat|potat|ech|her|vet)o$': '$1oes',
  '(bu)s$'                     : '$1ses',
  '(alias)$'                   : '$1es',
  '(octop)us$'                 : '$1i',
  '(ax|test)is$'               : '$1es',
  '(us)$'                      : '$1es',
  '([^s]+)$'                   : '$1s'
} as Record<string, string>;
 
/**
 * Mapping of singular words to their irregular plural forms.
 */
const irregulars = {
  child : 'children',
  foot  : 'feet',
  goose : 'geese',
  man   : 'men',
  move  : 'moves',
  person: 'people',
  sex   : 'sexes',
  tooth : 'teeth'
} as Record<string, string>;
 
/**
 * List of uncountable singular words.
 */
const uncountables = [
  'aircraft',
  'bison',
  'cod',
  'deer',
  'equipment',
  'fish',
  'hovercraft',
  'information',
  'money',
  'moose',
  'offspring',
  'pike',
  'rice',
  'salmon',
  'series',
  'sheep',
  'shrimp',
  'spacecraft',
  'species',
  'sugar',
  'swine',
  'trout',
  'tuna',
  'wood',
  'you'
];