pluralize.js 1.8 KB

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