index.js 989 B

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