pluralize.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. module.exports = pluralize;
  3. /**
  4. * Pluralization rules.
  5. */
  6. exports.pluralization = [
  7. [/(m)an$/gi, '$1en'],
  8. [/(pe)rson$/gi, '$1ople'],
  9. [/(child)$/gi, '$1ren'],
  10. [/^(ox)$/gi, '$1en'],
  11. [/(ax|test)is$/gi, '$1es'],
  12. [/(octop|vir)us$/gi, '$1i'],
  13. [/(alias|status)$/gi, '$1es'],
  14. [/(bu)s$/gi, '$1ses'],
  15. [/(buffal|tomat|potat)o$/gi, '$1oes'],
  16. [/([ti])um$/gi, '$1a'],
  17. [/sis$/gi, 'ses'],
  18. [/(?:([^f])fe|([lr])f)$/gi, '$1$2ves'],
  19. [/(hive)$/gi, '$1s'],
  20. [/([^aeiouy]|qu)y$/gi, '$1ies'],
  21. [/(x|ch|ss|sh)$/gi, '$1es'],
  22. [/(matr|vert|ind)ix|ex$/gi, '$1ices'],
  23. [/([m|l])ouse$/gi, '$1ice'],
  24. [/(kn|w|l)ife$/gi, '$1ives'],
  25. [/(quiz)$/gi, '$1zes'],
  26. [/^goose$/i, 'geese'],
  27. [/s$/gi, 's'],
  28. [/([^a-z])$/, '$1'],
  29. [/$/gi, 's']
  30. ];
  31. const rules = exports.pluralization;
  32. /**
  33. * Uncountable words.
  34. *
  35. * These words are applied while processing the argument to `toCollectionName`.
  36. * @api public
  37. */
  38. exports.uncountables = [
  39. 'advice',
  40. 'energy',
  41. 'excretion',
  42. 'digestion',
  43. 'cooperation',
  44. 'health',
  45. 'justice',
  46. 'labour',
  47. 'machinery',
  48. 'equipment',
  49. 'information',
  50. 'pollution',
  51. 'sewage',
  52. 'paper',
  53. 'money',
  54. 'species',
  55. 'series',
  56. 'rain',
  57. 'rice',
  58. 'fish',
  59. 'sheep',
  60. 'moose',
  61. 'deer',
  62. 'news',
  63. 'expertise',
  64. 'status',
  65. 'media'
  66. ];
  67. const uncountables = exports.uncountables;
  68. /*!
  69. * Pluralize function.
  70. *
  71. * @author TJ Holowaychuk (extracted from _ext.js_)
  72. * @param {String} string to pluralize
  73. * @api private
  74. */
  75. function pluralize(str) {
  76. let found;
  77. str = str.toLowerCase();
  78. if (!~uncountables.indexOf(str)) {
  79. found = rules.filter(function(rule) {
  80. return str.match(rule[0]);
  81. });
  82. if (found[0]) {
  83. return str.replace(found[0][0], found[0][1]);
  84. }
  85. }
  86. return str;
  87. }