Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / util / Inflector.js
1 /**
2  * @class Ext.util.Inflector
3  * @extends Object
4  * <p>General purpose inflector class that {@link #pluralize pluralizes}, {@link #singularize singularizes} and 
5  * {@link #ordinalize ordinalizes} words. Sample usage:</p>
6  * 
7 <pre><code>
8 //turning singular words into plurals
9 Ext.util.Inflector.pluralize('word'); //'words'
10 Ext.util.Inflector.pluralize('person'); //'people'
11 Ext.util.Inflector.pluralize('sheep'); //'sheep'
12
13 //turning plurals into singulars
14 Ext.util.Inflector.singularize('words'); //'word'
15 Ext.util.Inflector.singularize('people'); //'person'
16 Ext.util.Inflector.singularize('sheep'); //'sheep'
17
18 //ordinalizing numbers
19 Ext.util.Inflector.ordinalize(11); //"11th"
20 Ext.util.Inflector.ordinalize(21); //"21th"
21 Ext.util.Inflector.ordinalize(1043); //"1043rd"
22 </code></pre>
23  * 
24  * <p><u>Customization</u></p>
25  * 
26  * <p>The Inflector comes with a default set of US English pluralization rules. These can be augmented with additional
27  * rules if the default rules do not meet your application's requirements, or swapped out entirely for other languages.
28  * Here is how we might add a rule that pluralizes "ox" to "oxen":</p>
29  * 
30 <pre><code>
31 Ext.util.Inflector.plural(/^(ox)$/i, "$1en");
32 </code></pre>
33  * 
34  * <p>Each rule consists of two items - a regular expression that matches one or more rules, and a replacement string.
35  * In this case, the regular expression will only match the string "ox", and will replace that match with "oxen". 
36  * Here's how we could add the inverse rule:</p>
37  * 
38 <pre><code>
39 Ext.util.Inflector.singular(/^(ox)en$/i, "$1");
40 </code></pre>
41  * 
42  * <p>Note that the ox/oxen rules are present by default.</p>
43  * 
44  * @singleton
45  */
46
47 Ext.define('Ext.util.Inflector', {
48
49     /* Begin Definitions */
50
51     singleton: true,
52
53     /* End Definitions */
54
55     /**
56      * @private
57      * The registered plural tuples. Each item in the array should contain two items - the first must be a regular
58      * expression that matchers the singular form of a word, the second must be a String that replaces the matched
59      * part of the regular expression. This is managed by the {@link #plural} method.
60      * @property plurals
61      * @type Array
62      */
63     plurals: [
64         [(/(quiz)$/i),                "$1zes"  ],
65         [(/^(ox)$/i),                 "$1en"   ],
66         [(/([m|l])ouse$/i),           "$1ice"  ],
67         [(/(matr|vert|ind)ix|ex$/i),  "$1ices" ],
68         [(/(x|ch|ss|sh)$/i),          "$1es"   ],
69         [(/([^aeiouy]|qu)y$/i),       "$1ies"  ],
70         [(/(hive)$/i),                "$1s"    ],
71         [(/(?:([^f])fe|([lr])f)$/i),  "$1$2ves"],
72         [(/sis$/i),                   "ses"    ],
73         [(/([ti])um$/i),              "$1a"    ],
74         [(/(buffal|tomat|potat)o$/i), "$1oes"  ],
75         [(/(bu)s$/i),                 "$1ses"  ],
76         [(/(alias|status|sex)$/i),    "$1es"   ],
77         [(/(octop|vir)us$/i),         "$1i"    ],
78         [(/(ax|test)is$/i),           "$1es"   ],
79         [(/^person$/),                "people" ],
80         [(/^man$/),                   "men"    ],
81         [(/^(child)$/),               "$1ren"  ],
82         [(/s$/i),                     "s"      ],
83         [(/$/),                       "s"      ]
84     ],
85     
86     /**
87      * @private
88      * The set of registered singular matchers. Each item in the array should contain two items - the first must be a 
89      * regular expression that matches the plural form of a word, the second must be a String that replaces the 
90      * matched part of the regular expression. This is managed by the {@link #singular} method.
91      * @property singulars
92      * @type Array
93      */
94     singulars: [
95       [(/(quiz)zes$/i),                                                    "$1"     ],
96       [(/(matr)ices$/i),                                                   "$1ix"   ],
97       [(/(vert|ind)ices$/i),                                               "$1ex"   ],
98       [(/^(ox)en/i),                                                       "$1"     ],
99       [(/(alias|status)es$/i),                                             "$1"     ],
100       [(/(octop|vir)i$/i),                                                 "$1us"   ],
101       [(/(cris|ax|test)es$/i),                                             "$1is"   ],
102       [(/(shoe)s$/i),                                                      "$1"     ],
103       [(/(o)es$/i),                                                        "$1"     ],
104       [(/(bus)es$/i),                                                      "$1"     ],
105       [(/([m|l])ice$/i),                                                   "$1ouse" ],
106       [(/(x|ch|ss|sh)es$/i),                                               "$1"     ],
107       [(/(m)ovies$/i),                                                     "$1ovie" ],
108       [(/(s)eries$/i),                                                     "$1eries"],
109       [(/([^aeiouy]|qu)ies$/i),                                            "$1y"    ],
110       [(/([lr])ves$/i),                                                    "$1f"    ],
111       [(/(tive)s$/i),                                                      "$1"     ],
112       [(/(hive)s$/i),                                                      "$1"     ],
113       [(/([^f])ves$/i),                                                    "$1fe"   ],
114       [(/(^analy)ses$/i),                                                  "$1sis"  ],
115       [(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i), "$1$2sis"],
116       [(/([ti])a$/i),                                                      "$1um"   ],
117       [(/(n)ews$/i),                                                       "$1ews"  ],
118       [(/people$/i),                                                       "person" ],
119       [(/s$/i),                                                            ""       ]
120     ],
121     
122     /**
123      * @private
124      * The registered uncountable words
125      * @property uncountable
126      * @type Array
127      */
128      uncountable: [
129         "sheep",
130         "fish",
131         "series",
132         "species",
133         "money",
134         "rice",
135         "information",
136         "equipment",
137         "grass",
138         "mud",
139         "offspring",
140         "deer",
141         "means"
142     ],
143     
144     /**
145      * Adds a new singularization rule to the Inflector. See the intro docs for more information
146      * @param {RegExp} matcher The matcher regex
147      * @param {String} replacer The replacement string, which can reference matches from the matcher argument
148      */
149     singular: function(matcher, replacer) {
150         this.singulars.unshift([matcher, replacer]);
151     },
152     
153     /**
154      * Adds a new pluralization rule to the Inflector. See the intro docs for more information
155      * @param {RegExp} matcher The matcher regex
156      * @param {String} replacer The replacement string, which can reference matches from the matcher argument
157      */
158     plural: function(matcher, replacer) {
159         this.plurals.unshift([matcher, replacer]);
160     },
161     
162     /**
163      * Removes all registered singularization rules
164      */
165     clearSingulars: function() {
166         this.singulars = [];
167     },
168     
169     /**
170      * Removes all registered pluralization rules
171      */
172     clearPlurals: function() {
173         this.plurals = [];
174     },
175     
176     /**
177      * Returns true if the given word is transnumeral (the word is its own singular and plural form - e.g. sheep, fish)
178      * @param {String} word The word to test
179      * @return {Boolean} True if the word is transnumeral
180      */
181     isTransnumeral: function(word) {
182         return Ext.Array.indexOf(this.uncountable, word) != -1;
183     },
184
185     /**
186      * Returns the pluralized form of a word (e.g. Ext.util.Inflector.pluralize('word') returns 'words')
187      * @param {String} word The word to pluralize
188      * @return {String} The pluralized form of the word
189      */
190     pluralize: function(word) {
191         if (this.isTransnumeral(word)) {
192             return word;
193         }
194
195         var plurals = this.plurals,
196             length  = plurals.length,
197             tuple, regex, i;
198         
199         for (i = 0; i < length; i++) {
200             tuple = plurals[i];
201             regex = tuple[0];
202             
203             if (regex == word || (regex.test && regex.test(word))) {
204                 return word.replace(regex, tuple[1]);
205             }
206         }
207         
208         return word;
209     },
210     
211     /**
212      * Returns the singularized form of a word (e.g. Ext.util.Inflector.singularize('words') returns 'word')
213      * @param {String} word The word to singularize
214      * @return {String} The singularized form of the word
215      */
216     singularize: function(word) {
217         if (this.isTransnumeral(word)) {
218             return word;
219         }
220
221         var singulars = this.singulars,
222             length    = singulars.length,
223             tuple, regex, i;
224         
225         for (i = 0; i < length; i++) {
226             tuple = singulars[i];
227             regex = tuple[0];
228             
229             if (regex == word || (regex.test && regex.test(word))) {
230                 return word.replace(regex, tuple[1]);
231             }
232         }
233         
234         return word;
235     },
236     
237     /**
238      * Returns the correct {@link Ext.data.Model Model} name for a given string. Mostly used internally by the data 
239      * package
240      * @param {String} word The word to classify
241      * @return {String} The classified version of the word
242      */
243     classify: function(word) {
244         return Ext.String.capitalize(this.singularize(word));
245     },
246     
247     /**
248      * Ordinalizes a given number by adding a prefix such as 'st', 'nd', 'rd' or 'th' based on the last digit of the 
249      * number. 21 -> 21st, 22 -> 22nd, 23 -> 23rd, 24 -> 24th etc
250      * @param {Number} number The number to ordinalize
251      * @return {String} The ordinalized number
252      */
253     ordinalize: function(number) {
254         var parsed = parseInt(number, 10),
255             mod10  = parsed % 10,
256             mod100 = parsed % 100;
257         
258         //11 through 13 are a special case
259         if (11 <= mod100 && mod100 <= 13) {
260             return number + "th";
261         } else {
262             switch(mod10) {
263                 case 1 : return number + "st";
264                 case 2 : return number + "nd";
265                 case 3 : return number + "rd";
266                 default: return number + "th";
267             }
268         }
269     }
270 }, function() {
271     //aside from the rules above, there are a number of words that have irregular pluralization so we add them here
272     var irregulars = {
273             alumnus: 'alumni',
274             cactus : 'cacti',
275             focus  : 'foci',
276             nucleus: 'nuclei',
277             radius: 'radii',
278             stimulus: 'stimuli',
279             ellipsis: 'ellipses',
280             paralysis: 'paralyses',
281             oasis: 'oases',
282             appendix: 'appendices',
283             index: 'indexes',
284             beau: 'beaux',
285             bureau: 'bureaux',
286             tableau: 'tableaux',
287             woman: 'women',
288             child: 'children',
289             man: 'men',
290             corpus:     'corpora',
291             criterion: 'criteria',
292             curriculum: 'curricula',
293             genus: 'genera',
294             memorandum: 'memoranda',
295             phenomenon: 'phenomena',
296             foot: 'feet',
297             goose: 'geese',
298             tooth: 'teeth',
299             antenna: 'antennae',
300             formula: 'formulae',
301             nebula: 'nebulae',
302             vertebra: 'vertebrae',
303             vita: 'vitae'
304         },
305         singular;
306     
307     for (singular in irregulars) {
308         this.plural(singular, irregulars[singular]);
309         this.singular(irregulars[singular], singular);
310     }
311 });