4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-Inflector'>/**
19 </span> * @class Ext.util.Inflector
21 * <p>General purpose inflector class that {@link #pluralize pluralizes}, {@link #singularize singularizes} and
22 * {@link #ordinalize ordinalizes} words. Sample usage:</p>
24 <pre><code>
25 //turning singular words into plurals
26 Ext.util.Inflector.pluralize('word'); //'words'
27 Ext.util.Inflector.pluralize('person'); //'people'
28 Ext.util.Inflector.pluralize('sheep'); //'sheep'
30 //turning plurals into singulars
31 Ext.util.Inflector.singularize('words'); //'word'
32 Ext.util.Inflector.singularize('people'); //'person'
33 Ext.util.Inflector.singularize('sheep'); //'sheep'
35 //ordinalizing numbers
36 Ext.util.Inflector.ordinalize(11); //"11th"
37 Ext.util.Inflector.ordinalize(21); //"21th"
38 Ext.util.Inflector.ordinalize(1043); //"1043rd"
39 </code></pre>
41 * <p><u>Customization</u></p>
43 * <p>The Inflector comes with a default set of US English pluralization rules. These can be augmented with additional
44 * rules if the default rules do not meet your application's requirements, or swapped out entirely for other languages.
45 * Here is how we might add a rule that pluralizes "ox" to "oxen":</p>
47 <pre><code>
48 Ext.util.Inflector.plural(/^(ox)$/i, "$1en");
49 </code></pre>
51 * <p>Each rule consists of two items - a regular expression that matches one or more rules, and a replacement string.
52 * In this case, the regular expression will only match the string "ox", and will replace that match with "oxen".
53 * Here's how we could add the inverse rule:</p>
55 <pre><code>
56 Ext.util.Inflector.singular(/^(ox)en$/i, "$1");
57 </code></pre>
59 * <p>Note that the ox/oxen rules are present by default.</p>
64 Ext.define('Ext.util.Inflector', {
66 /* Begin Definitions */
72 <span id='Ext-util-Inflector-property-plurals'> /**
74 * The registered plural tuples. Each item in the array should contain two items - the first must be a regular
75 * expression that matchers the singular form of a word, the second must be a String that replaces the matched
76 * part of the regular expression. This is managed by the {@link #plural} method.
81 [(/(quiz)$/i), "$1zes" ],
82 [(/^(ox)$/i), "$1en" ],
83 [(/([m|l])ouse$/i), "$1ice" ],
84 [(/(matr|vert|ind)ix|ex$/i), "$1ices" ],
85 [(/(x|ch|ss|sh)$/i), "$1es" ],
86 [(/([^aeiouy]|qu)y$/i), "$1ies" ],
87 [(/(hive)$/i), "$1s" ],
88 [(/(?:([^f])fe|([lr])f)$/i), "$1$2ves"],
89 [(/sis$/i), "ses" ],
90 [(/([ti])um$/i), "$1a" ],
91 [(/(buffal|tomat|potat)o$/i), "$1oes" ],
92 [(/(bu)s$/i), "$1ses" ],
93 [(/(alias|status|sex)$/i), "$1es" ],
94 [(/(octop|vir)us$/i), "$1i" ],
95 [(/(ax|test)is$/i), "$1es" ],
96 [(/^person$/), "people" ],
97 [(/^man$/), "men" ],
98 [(/^(child)$/), "$1ren" ],
99 [(/s$/i), "s" ],
100 [(/$/), "s" ]
103 <span id='Ext-util-Inflector-property-singulars'> /**
105 * The set of registered singular matchers. Each item in the array should contain two items - the first must be a
106 * regular expression that matches the plural form of a word, the second must be a String that replaces the
107 * matched part of the regular expression. This is managed by the {@link #singular} method.
108 * @property singulars
112 [(/(quiz)zes$/i), "$1" ],
113 [(/(matr)ices$/i), "$1ix" ],
114 [(/(vert|ind)ices$/i), "$1ex" ],
115 [(/^(ox)en/i), "$1" ],
116 [(/(alias|status)es$/i), "$1" ],
117 [(/(octop|vir)i$/i), "$1us" ],
118 [(/(cris|ax|test)es$/i), "$1is" ],
119 [(/(shoe)s$/i), "$1" ],
120 [(/(o)es$/i), "$1" ],
121 [(/(bus)es$/i), "$1" ],
122 [(/([m|l])ice$/i), "$1ouse" ],
123 [(/(x|ch|ss|sh)es$/i), "$1" ],
124 [(/(m)ovies$/i), "$1ovie" ],
125 [(/(s)eries$/i), "$1eries"],
126 [(/([^aeiouy]|qu)ies$/i), "$1y" ],
127 [(/([lr])ves$/i), "$1f" ],
128 [(/(tive)s$/i), "$1" ],
129 [(/(hive)s$/i), "$1" ],
130 [(/([^f])ves$/i), "$1fe" ],
131 [(/(^analy)ses$/i), "$1sis" ],
132 [(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i), "$1$2sis"],
133 [(/([ti])a$/i), "$1um" ],
134 [(/(n)ews$/i), "$1ews" ],
135 [(/people$/i), "person" ],
136 [(/s$/i), "" ]
139 <span id='Ext-util-Inflector-property-uncountable'> /**
141 * The registered uncountable words
142 * @property uncountable
152 "information",
153 "equipment",
156 "offspring",
161 <span id='Ext-util-Inflector-method-singular'> /**
162 </span> * Adds a new singularization rule to the Inflector. See the intro docs for more information
163 * @param {RegExp} matcher The matcher regex
164 * @param {String} replacer The replacement string, which can reference matches from the matcher argument
166 singular: function(matcher, replacer) {
167 this.singulars.unshift([matcher, replacer]);
170 <span id='Ext-util-Inflector-method-plural'> /**
171 </span> * Adds a new pluralization rule to the Inflector. See the intro docs for more information
172 * @param {RegExp} matcher The matcher regex
173 * @param {String} replacer The replacement string, which can reference matches from the matcher argument
175 plural: function(matcher, replacer) {
176 this.plurals.unshift([matcher, replacer]);
179 <span id='Ext-util-Inflector-method-clearSingulars'> /**
180 </span> * Removes all registered singularization rules
182 clearSingulars: function() {
186 <span id='Ext-util-Inflector-method-clearPlurals'> /**
187 </span> * Removes all registered pluralization rules
189 clearPlurals: function() {
193 <span id='Ext-util-Inflector-method-isTransnumeral'> /**
194 </span> * Returns true if the given word is transnumeral (the word is its own singular and plural form - e.g. sheep, fish)
195 * @param {String} word The word to test
196 * @return {Boolean} True if the word is transnumeral
198 isTransnumeral: function(word) {
199 return Ext.Array.indexOf(this.uncountable, word) != -1;
202 <span id='Ext-util-Inflector-method-pluralize'> /**
203 </span> * Returns the pluralized form of a word (e.g. Ext.util.Inflector.pluralize('word') returns 'words')
204 * @param {String} word The word to pluralize
205 * @return {String} The pluralized form of the word
207 pluralize: function(word) {
208 if (this.isTransnumeral(word)) {
212 var plurals = this.plurals,
213 length = plurals.length,
216 for (i = 0; i < length; i++) {
220 if (regex == word || (regex.test && regex.test(word))) {
221 return word.replace(regex, tuple[1]);
228 <span id='Ext-util-Inflector-method-singularize'> /**
229 </span> * Returns the singularized form of a word (e.g. Ext.util.Inflector.singularize('words') returns 'word')
230 * @param {String} word The word to singularize
231 * @return {String} The singularized form of the word
233 singularize: function(word) {
234 if (this.isTransnumeral(word)) {
238 var singulars = this.singulars,
239 length = singulars.length,
242 for (i = 0; i < length; i++) {
243 tuple = singulars[i];
246 if (regex == word || (regex.test && regex.test(word))) {
247 return word.replace(regex, tuple[1]);
254 <span id='Ext-util-Inflector-method-classify'> /**
255 </span> * Returns the correct {@link Ext.data.Model Model} name for a given string. Mostly used internally by the data
257 * @param {String} word The word to classify
258 * @return {String} The classified version of the word
260 classify: function(word) {
261 return Ext.String.capitalize(this.singularize(word));
264 <span id='Ext-util-Inflector-method-ordinalize'> /**
265 </span> * Ordinalizes a given number by adding a prefix such as 'st', 'nd', 'rd' or 'th' based on the last digit of the
266 * number. 21 -> 21st, 22 -> 22nd, 23 -> 23rd, 24 -> 24th etc
267 * @param {Number} number The number to ordinalize
268 * @return {String} The ordinalized number
270 ordinalize: function(number) {
271 var parsed = parseInt(number, 10),
273 mod100 = parsed % 100;
275 //11 through 13 are a special case
276 if (11 <= mod100 && mod100 <= 13) {
277 return number + "th";
280 case 1 : return number + "st";
281 case 2 : return number + "nd";
282 case 3 : return number + "rd";
283 default: return number + "th";
288 //aside from the rules above, there are a number of words that have irregular pluralization so we add them here
296 ellipsis: 'ellipses',
297 paralysis: 'paralyses',
299 appendix: 'appendices',
308 criterion: 'criteria',
309 curriculum: 'curricula',
311 memorandum: 'memoranda',
312 phenomenon: 'phenomena',
319 vertebra: 'vertebrae',
324 for (singular in irregulars) {
325 this.plural(singular, irregulars[singular]);
326 this.singular(irregulars[singular], singular);