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
4 * <p>General purpose inflector class that {@link #pluralize pluralizes}, {@link #singularize singularizes} and
5 * {@link #ordinalize ordinalizes} words. Sample usage:</p>
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'
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'
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>
24 * <p><u>Customization</u></p>
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>
30 <pre><code>
31 Ext.util.Inflector.plural(/^(ox)$/i, "$1en");
32 </code></pre>
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>
38 <pre><code>
39 Ext.util.Inflector.singular(/^(ox)en$/i, "$1");
40 </code></pre>
42 * <p>Note that the ox/oxen rules are present by default.</p>
47 Ext.define('Ext.util.Inflector', {
49 /* Begin Definitions */
55 <span id='Ext-util.Inflector-property-plurals'> /**
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.
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" ]
86 <span id='Ext-util.Inflector-property-singulars'> /**
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.
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), "" ]
122 <span id='Ext-util.Inflector-property-uncountable'> /**
124 * The registered uncountable words
125 * @property uncountable
135 "information",
136 "equipment",
139 "offspring",
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
149 singular: function(matcher, replacer) {
150 this.singulars.unshift([matcher, replacer]);
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
158 plural: function(matcher, replacer) {
159 this.plurals.unshift([matcher, replacer]);
162 <span id='Ext-util.Inflector-method-clearSingulars'> /**
163 </span> * Removes all registered singularization rules
165 clearSingulars: function() {
169 <span id='Ext-util.Inflector-method-clearPlurals'> /**
170 </span> * Removes all registered pluralization rules
172 clearPlurals: function() {
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
181 isTransnumeral: function(word) {
182 return Ext.Array.indexOf(this.uncountable, word) != -1;
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
190 pluralize: function(word) {
191 if (this.isTransnumeral(word)) {
195 var plurals = this.plurals,
196 length = plurals.length,
199 for (i = 0; i < length; i++) {
203 if (regex == word || (regex.test && regex.test(word))) {
204 return word.replace(regex, tuple[1]);
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
216 singularize: function(word) {
217 if (this.isTransnumeral(word)) {
221 var singulars = this.singulars,
222 length = singulars.length,
225 for (i = 0; i < length; i++) {
226 tuple = singulars[i];
229 if (regex == word || (regex.test && regex.test(word))) {
230 return word.replace(regex, tuple[1]);
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
240 * @param {String} word The word to classify
241 * @return {String} The classified version of the word
243 classify: function(word) {
244 return Ext.String.capitalize(this.singularize(word));
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 -> 21st, 22 -> 22nd, 23 -> 23rd, 24 -> 24th etc
250 * @param {Number} number The number to ordinalize
251 * @return {String} The ordinalized number
253 ordinalize: function(number) {
254 var parsed = parseInt(number, 10),
256 mod100 = parsed % 100;
258 //11 through 13 are a special case
259 if (11 <= mod100 && mod100 <= 13) {
260 return number + "th";
263 case 1 : return number + "st";
264 case 2 : return number + "nd";
265 case 3 : return number + "rd";
266 default: return number + "th";
271 //aside from the rules above, there are a number of words that have irregular pluralization so we add them here
279 ellipsis: 'ellipses',
280 paralysis: 'paralyses',
282 appendix: 'appendices',
291 criterion: 'criteria',
292 curriculum: 'curricula',
294 memorandum: 'memoranda',
295 phenomenon: 'phenomena',
302 vertebra: 'vertebrae',
307 for (singular in irregulars) {
308 this.plural(singular, irregulars[singular]);
309 this.singular(irregulars[singular], singular);
311 });</pre></pre></body></html>