| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const irregularPlurals = require('irregular-plurals'); |
| 3 | |
| 4 | module.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 | }; |