blob: 826de8fedb940a9cb5491bfc788ffe0c436aa696 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const irregularPlurals = require('irregular-plurals');
3
4module.exports = (word, plural, count) => {
5 if (typeof plural === 'number') {
6 count = plural;
7 }
8
9 if (irregularPlurals.has(word.toLowerCase())) {
10 plural = irregularPlurals.get(word.toLowerCase());
11
12 const firstLetter = word.charAt(0);
13 const isFirstLetterUpperCase = firstLetter === firstLetter.toUpperCase();
14 if (isFirstLetterUpperCase) {
15 plural = firstLetter.toUpperCase() + plural.slice(1);
16 }
17
18 const isWholeWordUpperCase = word === word.toUpperCase();
19 if (isWholeWordUpperCase) {
20 plural = plural.toUpperCase();
21 }
22 } else if (typeof plural !== 'string') {
23 plural = (word.replace(/(?:s|x|z|ch|sh)$/i, '$&e').replace(/([^aeiou])y$/i, '$1ie') + 's')
24 .replace(/i?e?s$/i, match => {
25 const isTailLowerCase = word.slice(-1) === word.slice(-1).toLowerCase();
26 return isTailLowerCase ? match.toLowerCase() : match.toUpperCase();
27 });
28 }
29
30 return Math.abs(count) === 1 ? word : plural;
31};