1 // A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
2 // inflection rules. Examples:
4 // BulletSupport.Inflector.inflect ($) ->
5 // $.plural /^(ox)$/i, '$1en'
6 // $.singular /^(ox)en/i, '$1'
8 // $.irregular 'octopus', 'octopi'
10 // $.uncountable "equipment"
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.
16 var util = require('./util');
18 var Inflections = function () {
21 this.uncountables = [];
23 require('./defaults')(this);
27 // Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
28 // The replacement should always be a string that may include references to the matched data from the rule.
29 Inflections.prototype.plural = function (rule, replacement) {
30 if (typeof rule == 'string') {
31 this.uncountables = util.array.del(this.uncountables, rule);
33 this.uncountables = util.array.del(this.uncountables, replacement);
34 this.plurals.unshift([rule, replacement]);
37 // Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
38 // The replacement should always be a string that may include references to the matched data from the rule.
39 Inflections.prototype.singular = function (rule, replacement) {
40 if (typeof rule == 'string') {
41 this.uncountables = util.array.del(this.uncountables, rule);
43 this.uncountables = util.array.del(this.uncountables, replacement);
44 this.singulars.unshift([rule, replacement]);
47 // Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
48 // for strings, not regular expressions. You simply pass the irregular in singular and plural form.
50 // irregular 'octopus', 'octopi'
51 // irregular 'person', 'people'
52 Inflections.prototype.irregular = function (singular, plural, fullMatchRequired) {
53 this.uncountables = util.array.del(this.uncountables, singular);
54 this.uncountables = util.array.del(this.uncountables, plural);
56 if (fullMatchRequired) {
59 if (singular[0].toUpperCase() == plural[0].toUpperCase()) {
60 this.plural(new RegExp("(" + prefix + singular[0] + ")" + singular.slice(1) + "$", "i"), '$1' + plural.slice(1));
61 this.plural(new RegExp("(" + prefix + plural[0] + ")" + plural.slice(1) + "$", "i"), '$1' + plural.slice(1));
62 this.singular(new RegExp("(" + prefix + plural[0] + ")" + plural.slice(1) + "$", "i"), '$1' + singular.slice(1));
64 this.plural(new RegExp(prefix + (singular[0].toUpperCase()) + singular.slice(1) + "$"), plural[0].toUpperCase() + plural.slice(1));
65 this.plural(new RegExp(prefix + (singular[0].toLowerCase()) + singular.slice(1) + "$"), plural[0].toLowerCase() + plural.slice(1));
66 this.plural(new RegExp(prefix + (plural[0].toUpperCase()) + plural.slice(1) + "$"), plural[0].toUpperCase() + plural.slice(1));
67 this.plural(new RegExp(prefix + (plural[0].toLowerCase()) + plural.slice(1) + "$"), plural[0].toLowerCase() + plural.slice(1));
68 this.singular(new RegExp(prefix + (plural[0].toUpperCase()) + plural.slice(1) + "$"), singular[0].toUpperCase() + singular.slice(1));
69 this.singular(new RegExp(prefix + (plural[0].toLowerCase()) + plural.slice(1) + "$"), singular[0].toLowerCase() + singular.slice(1));
73 // Specifies a humanized form of a string by a regular expression rule or by a string mapping.
74 // When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
75 // When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
77 // human /(.*)_cnt$/i, '$1_count'
78 // human "legacy_col_person_name", "Name"
79 Inflections.prototype.human = function (rule, replacement) {
80 this.humans.unshift([rule, replacement]);
83 // Add uncountable words that shouldn't be attempted inflected.
85 // uncountable "money"
86 // uncountable ["money", "information"]
87 Inflections.prototype.uncountable = function (words) {
88 this.uncountables = this.uncountables.concat(words);
91 // Clears the loaded inflections within a given scope (default is _'all'_).
92 // Give the scope as a symbol of the inflection type, the options are: _'plurals'_,
93 // _'singulars'_, _'uncountables'_, _'humans'_.
97 Inflections.prototype.clear = function (scope) {
98 if (scope == null) scope = 'all';
103 this.uncountables = [];
110 // Clears the loaded inflections and initializes them to [default](../inflections.html)
111 Inflections.prototype.default = function () {
114 this.uncountables = [];
116 require('./defaults')(this);
120 module.exports = new Inflections();