UNPKG

5.29 kBJavaScriptView Raw
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
16var util = require('./util');
17
18var Inflections = function () {
19 this.plurals = [];
20 this.singulars = [];
21 this.uncountables = [];
22 this.humans = [];
23 require('./defaults')(this);
24 return this;
25};
26
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.
29Inflections.prototype.plural = function (rule, replacement) {
30 if (typeof rule == 'string') {
31 this.uncountables = util.array.del(this.uncountables, rule);
32 }
33 this.uncountables = util.array.del(this.uncountables, replacement);
34 this.plurals.unshift([rule, replacement]);
35};
36
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.
39Inflections.prototype.singular = function (rule, replacement) {
40 if (typeof rule == 'string') {
41 this.uncountables = util.array.del(this.uncountables, rule);
42 }
43 this.uncountables = util.array.del(this.uncountables, replacement);
44 this.singulars.unshift([rule, replacement]);
45};
46
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.
49//
50// irregular 'octopus', 'octopi'
51// irregular 'person', 'people'
52Inflections.prototype.irregular = function (singular, plural, fullMatchRequired) {
53 this.uncountables = util.array.del(this.uncountables, singular);
54 this.uncountables = util.array.del(this.uncountables, plural);
55 var prefix = "";
56 if (fullMatchRequired) {
57 prefix = "^";
58 }
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));
63 } else {
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));
70 }
71};
72
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')
76//
77// human /(.*)_cnt$/i, '$1_count'
78// human "legacy_col_person_name", "Name"
79Inflections.prototype.human = function (rule, replacement) {
80 this.humans.unshift([rule, replacement]);
81}
82
83// Add uncountable words that shouldn't be attempted inflected.
84//
85// uncountable "money"
86// uncountable ["money", "information"]
87Inflections.prototype.uncountable = function (words) {
88 this.uncountables = this.uncountables.concat(words);
89}
90
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'_.
94//
95// clear 'all'
96// clear 'plurals'
97Inflections.prototype.clear = function (scope) {
98 if (scope == null) scope = 'all';
99 switch (scope) {
100 case 'all':
101 this.plurals = [];
102 this.singulars = [];
103 this.uncountables = [];
104 this.humans = [];
105 default:
106 this[scope] = [];
107 }
108}
109
110// Clears the loaded inflections and initializes them to [default](../inflections.html)
111Inflections.prototype.default = function () {
112 this.plurals = [];
113 this.singulars = [];
114 this.uncountables = [];
115 this.humans = [];
116 require('./defaults')(this);
117 return this;
118};
119
120module.exports = new Inflections();