Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / Inflector.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.Inflector'>/**
2 </span> * @class Ext.util.Inflector
3  * @extends Object
4  * &lt;p&gt;General purpose inflector class that {@link #pluralize pluralizes}, {@link #singularize singularizes} and 
5  * {@link #ordinalize ordinalizes} words. Sample usage:&lt;/p&gt;
6  * 
7 &lt;pre&gt;&lt;code&gt;
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); //&quot;11th&quot;
20 Ext.util.Inflector.ordinalize(21); //&quot;21th&quot;
21 Ext.util.Inflector.ordinalize(1043); //&quot;1043rd&quot;
22 &lt;/code&gt;&lt;/pre&gt;
23  * 
24  * &lt;p&gt;&lt;u&gt;Customization&lt;/u&gt;&lt;/p&gt;
25  * 
26  * &lt;p&gt;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 &quot;ox&quot; to &quot;oxen&quot;:&lt;/p&gt;
29  * 
30 &lt;pre&gt;&lt;code&gt;
31 Ext.util.Inflector.plural(/^(ox)$/i, &quot;$1en&quot;);
32 &lt;/code&gt;&lt;/pre&gt;
33  * 
34  * &lt;p&gt;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 &quot;ox&quot;, and will replace that match with &quot;oxen&quot;. 
36  * Here's how we could add the inverse rule:&lt;/p&gt;
37  * 
38 &lt;pre&gt;&lt;code&gt;
39 Ext.util.Inflector.singular(/^(ox)en$/i, &quot;$1&quot;);
40 &lt;/code&gt;&lt;/pre&gt;
41  * 
42  * &lt;p&gt;Note that the ox/oxen rules are present by default.&lt;/p&gt;
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 <span id='Ext-util.Inflector-property-plurals'>    /**
56 </span>     * @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),                &quot;$1zes&quot;  ],
65         [(/^(ox)$/i),                 &quot;$1en&quot;   ],
66         [(/([m|l])ouse$/i),           &quot;$1ice&quot;  ],
67         [(/(matr|vert|ind)ix|ex$/i),  &quot;$1ices&quot; ],
68         [(/(x|ch|ss|sh)$/i),          &quot;$1es&quot;   ],
69         [(/([^aeiouy]|qu)y$/i),       &quot;$1ies&quot;  ],
70         [(/(hive)$/i),                &quot;$1s&quot;    ],
71         [(/(?:([^f])fe|([lr])f)$/i),  &quot;$1$2ves&quot;],
72         [(/sis$/i),                   &quot;ses&quot;    ],
73         [(/([ti])um$/i),              &quot;$1a&quot;    ],
74         [(/(buffal|tomat|potat)o$/i), &quot;$1oes&quot;  ],
75         [(/(bu)s$/i),                 &quot;$1ses&quot;  ],
76         [(/(alias|status|sex)$/i),    &quot;$1es&quot;   ],
77         [(/(octop|vir)us$/i),         &quot;$1i&quot;    ],
78         [(/(ax|test)is$/i),           &quot;$1es&quot;   ],
79         [(/^person$/),                &quot;people&quot; ],
80         [(/^man$/),                   &quot;men&quot;    ],
81         [(/^(child)$/),               &quot;$1ren&quot;  ],
82         [(/s$/i),                     &quot;s&quot;      ],
83         [(/$/),                       &quot;s&quot;      ]
84     ],
85     
86 <span id='Ext-util.Inflector-property-singulars'>    /**
87 </span>     * @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),                                                    &quot;$1&quot;     ],
96       [(/(matr)ices$/i),                                                   &quot;$1ix&quot;   ],
97       [(/(vert|ind)ices$/i),                                               &quot;$1ex&quot;   ],
98       [(/^(ox)en/i),                                                       &quot;$1&quot;     ],
99       [(/(alias|status)es$/i),                                             &quot;$1&quot;     ],
100       [(/(octop|vir)i$/i),                                                 &quot;$1us&quot;   ],
101       [(/(cris|ax|test)es$/i),                                             &quot;$1is&quot;   ],
102       [(/(shoe)s$/i),                                                      &quot;$1&quot;     ],
103       [(/(o)es$/i),                                                        &quot;$1&quot;     ],
104       [(/(bus)es$/i),                                                      &quot;$1&quot;     ],
105       [(/([m|l])ice$/i),                                                   &quot;$1ouse&quot; ],
106       [(/(x|ch|ss|sh)es$/i),                                               &quot;$1&quot;     ],
107       [(/(m)ovies$/i),                                                     &quot;$1ovie&quot; ],
108       [(/(s)eries$/i),                                                     &quot;$1eries&quot;],
109       [(/([^aeiouy]|qu)ies$/i),                                            &quot;$1y&quot;    ],
110       [(/([lr])ves$/i),                                                    &quot;$1f&quot;    ],
111       [(/(tive)s$/i),                                                      &quot;$1&quot;     ],
112       [(/(hive)s$/i),                                                      &quot;$1&quot;     ],
113       [(/([^f])ves$/i),                                                    &quot;$1fe&quot;   ],
114       [(/(^analy)ses$/i),                                                  &quot;$1sis&quot;  ],
115       [(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i), &quot;$1$2sis&quot;],
116       [(/([ti])a$/i),                                                      &quot;$1um&quot;   ],
117       [(/(n)ews$/i),                                                       &quot;$1ews&quot;  ],
118       [(/people$/i),                                                       &quot;person&quot; ],
119       [(/s$/i),                                                            &quot;&quot;       ]
120     ],
121     
122 <span id='Ext-util.Inflector-property-uncountable'>    /**
123 </span>     * @private
124      * The registered uncountable words
125      * @property uncountable
126      * @type Array
127      */
128      uncountable: [
129         &quot;sheep&quot;,
130         &quot;fish&quot;,
131         &quot;series&quot;,
132         &quot;species&quot;,
133         &quot;money&quot;,
134         &quot;rice&quot;,
135         &quot;information&quot;,
136         &quot;equipment&quot;,
137         &quot;grass&quot;,
138         &quot;mud&quot;,
139         &quot;offspring&quot;,
140         &quot;deer&quot;,
141         &quot;means&quot;
142     ],
143     
144 <span id='Ext-util.Inflector-method-singular'>    /**
145 </span>     * 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 <span id='Ext-util.Inflector-method-plural'>    /**
154 </span>     * 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 <span id='Ext-util.Inflector-method-clearSingulars'>    /**
163 </span>     * Removes all registered singularization rules
164      */
165     clearSingulars: function() {
166         this.singulars = [];
167     },
168     
169 <span id='Ext-util.Inflector-method-clearPlurals'>    /**
170 </span>     * Removes all registered pluralization rules
171      */
172     clearPlurals: function() {
173         this.plurals = [];
174     },
175     
176 <span id='Ext-util.Inflector-method-isTransnumeral'>    /**
177 </span>     * 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 <span id='Ext-util.Inflector-method-pluralize'>    /**
186 </span>     * 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 &lt; length; i++) {
200             tuple = plurals[i];
201             regex = tuple[0];
202             
203             if (regex == word || (regex.test &amp;&amp; regex.test(word))) {
204                 return word.replace(regex, tuple[1]);
205             }
206         }
207         
208         return word;
209     },
210     
211 <span id='Ext-util.Inflector-method-singularize'>    /**
212 </span>     * 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 &lt; length; i++) {
226             tuple = singulars[i];
227             regex = tuple[0];
228             
229             if (regex == word || (regex.test &amp;&amp; regex.test(word))) {
230                 return word.replace(regex, tuple[1]);
231             }
232         }
233         
234         return word;
235     },
236     
237 <span id='Ext-util.Inflector-method-classify'>    /**
238 </span>     * 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 <span id='Ext-util.Inflector-method-ordinalize'>    /**
248 </span>     * 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 -&gt; 21st, 22 -&gt; 22nd, 23 -&gt; 23rd, 24 -&gt; 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 &lt;= mod100 &amp;&amp; mod100 &lt;= 13) {
260             return number + &quot;th&quot;;
261         } else {
262             switch(mod10) {
263                 case 1 : return number + &quot;st&quot;;
264                 case 2 : return number + &quot;nd&quot;;
265                 case 3 : return number + &quot;rd&quot;;
266                 default: return number + &quot;th&quot;;
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 });</pre></pre></body></html>