inflections.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
  2. // inflection rules. Examples:
  3. //
  4. // BulletSupport.Inflector.inflect ($) ->
  5. // $.plural /^(ox)$/i, '$1en'
  6. // $.singular /^(ox)en/i, '$1'
  7. //
  8. // $.irregular 'octopus', 'octopi'
  9. //
  10. // $.uncountable "equipment"
  11. //
  12. // New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
  13. // pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
  14. // already have been loaded.
  15. var util = require('./util');
  16. var Inflections = function () {
  17. this.plurals = [];
  18. this.singulars = [];
  19. this.uncountables = [];
  20. this.humans = [];
  21. require('./defaults')(this);
  22. return this;
  23. };
  24. // Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
  25. // The replacement should always be a string that may include references to the matched data from the rule.
  26. Inflections.prototype.plural = function (rule, replacement) {
  27. if (typeof rule == 'string') {
  28. this.uncountables = util.array.del(this.uncountables, rule);
  29. }
  30. this.uncountables = util.array.del(this.uncountables, replacement);
  31. this.plurals.unshift([rule, replacement]);
  32. };
  33. // Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
  34. // The replacement should always be a string that may include references to the matched data from the rule.
  35. Inflections.prototype.singular = function (rule, replacement) {
  36. if (typeof rule == 'string') {
  37. this.uncountables = util.array.del(this.uncountables, rule);
  38. }
  39. this.uncountables = util.array.del(this.uncountables, replacement);
  40. this.singulars.unshift([rule, replacement]);
  41. };
  42. // Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
  43. // for strings, not regular expressions. You simply pass the irregular in singular and plural form.
  44. //
  45. // irregular 'octopus', 'octopi'
  46. // irregular 'person', 'people'
  47. Inflections.prototype.irregular = function (singular, plural, fullMatchRequired) {
  48. this.uncountables = util.array.del(this.uncountables, singular);
  49. this.uncountables = util.array.del(this.uncountables, plural);
  50. var prefix = '';
  51. if (fullMatchRequired) {
  52. prefix = '^';
  53. }
  54. if (singular[0].toUpperCase() == plural[0].toUpperCase()) {
  55. this.plural(new RegExp('(' + prefix + singular[0] + ')' + singular.slice(1) + '$', 'i'), '$1' + plural.slice(1));
  56. this.plural(new RegExp('(' + prefix + plural[0] + ')' + plural.slice(1) + '$', 'i'), '$1' + plural.slice(1));
  57. this.singular(new RegExp('(' + prefix + plural[0] + ')' + plural.slice(1) + '$', 'i'), '$1' + singular.slice(1));
  58. } else {
  59. this.plural(
  60. new RegExp(prefix + singular[0].toUpperCase() + singular.slice(1) + '$'),
  61. plural[0].toUpperCase() + plural.slice(1)
  62. );
  63. this.plural(
  64. new RegExp(prefix + singular[0].toLowerCase() + singular.slice(1) + '$'),
  65. plural[0].toLowerCase() + plural.slice(1)
  66. );
  67. this.plural(
  68. new RegExp(prefix + plural[0].toUpperCase() + plural.slice(1) + '$'),
  69. plural[0].toUpperCase() + plural.slice(1)
  70. );
  71. this.plural(
  72. new RegExp(prefix + plural[0].toLowerCase() + plural.slice(1) + '$'),
  73. plural[0].toLowerCase() + plural.slice(1)
  74. );
  75. this.singular(
  76. new RegExp(prefix + plural[0].toUpperCase() + plural.slice(1) + '$'),
  77. singular[0].toUpperCase() + singular.slice(1)
  78. );
  79. this.singular(
  80. new RegExp(prefix + plural[0].toLowerCase() + plural.slice(1) + '$'),
  81. singular[0].toLowerCase() + singular.slice(1)
  82. );
  83. }
  84. };
  85. // Specifies a humanized form of a string by a regular expression rule or by a string mapping.
  86. // When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
  87. // When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
  88. //
  89. // human /(.*)_cnt$/i, '$1_count'
  90. // human "legacy_col_person_name", "Name"
  91. Inflections.prototype.human = function (rule, replacement) {
  92. this.humans.unshift([rule, replacement]);
  93. };
  94. // Add uncountable words that shouldn't be attempted inflected.
  95. //
  96. // uncountable "money"
  97. // uncountable ["money", "information"]
  98. Inflections.prototype.uncountable = function (words) {
  99. this.uncountables = this.uncountables.concat(words);
  100. };
  101. // Clears the loaded inflections within a given scope (default is _'all'_).
  102. // Give the scope as a symbol of the inflection type, the options are: _'plurals'_,
  103. // _'singulars'_, _'uncountables'_, _'humans'_.
  104. //
  105. // clear 'all'
  106. // clear 'plurals'
  107. Inflections.prototype.clear = function (scope) {
  108. if (scope == null) scope = 'all';
  109. switch (scope) {
  110. case 'all':
  111. this.plurals = [];
  112. this.singulars = [];
  113. this.uncountables = [];
  114. this.humans = [];
  115. default:
  116. this[scope] = [];
  117. }
  118. };
  119. // Clears the loaded inflections and initializes them to [default](../inflections.html)
  120. Inflections.prototype.default = function () {
  121. this.plurals = [];
  122. this.singulars = [];
  123. this.uncountables = [];
  124. this.humans = [];
  125. require('./defaults')(this);
  126. return this;
  127. };
  128. module.exports = new Inflections();