4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * General purpose inflector class that {@link #pluralize pluralizes}, {@link #singularize singularizes} and
20 * {@link #ordinalize ordinalizes} words. Sample usage:
22 * //turning singular words into plurals
23 * Ext.util.Inflector.pluralize('word'); //'words'
24 * Ext.util.Inflector.pluralize('person'); //'people'
25 * Ext.util.Inflector.pluralize('sheep'); //'sheep'
27 * //turning plurals into singulars
28 * Ext.util.Inflector.singularize('words'); //'word'
29 * Ext.util.Inflector.singularize('people'); //'person'
30 * Ext.util.Inflector.singularize('sheep'); //'sheep'
32 * //ordinalizing numbers
33 * Ext.util.Inflector.ordinalize(11); //"11th"
34 * Ext.util.Inflector.ordinalize(21); //"21th"
35 * Ext.util.Inflector.ordinalize(1043); //"1043rd"
39 * The Inflector comes with a default set of US English pluralization rules. These can be augmented with additional
40 * rules if the default rules do not meet your application's requirements, or swapped out entirely for other languages.
41 * Here is how we might add a rule that pluralizes "ox" to "oxen":
43 * Ext.util.Inflector.plural(/^(ox)$/i, "$1en");
45 * Each rule consists of two items - a regular expression that matches one or more rules, and a replacement string. In
46 * this case, the regular expression will only match the string "ox", and will replace that match with "oxen". Here's
47 * how we could add the inverse rule:
49 * Ext.util.Inflector.singular(/^(ox)en$/i, "$1");
51 * Note that the ox/oxen rules are present by default.
53 Ext.define('Ext.util.Inflector', {
55 /* Begin Definitions */
61 <span id='Ext-util-Inflector-property-plurals'> /**
63 * The registered plural tuples. Each item in the array should contain two items - the first must be a regular
64 * expression that matchers the singular form of a word, the second must be a String that replaces the matched
65 * part of the regular expression. This is managed by the {@link #plural} method.
66 * @property {Array} plurals
69 [(/(quiz)$/i), "$1zes" ],
70 [(/^(ox)$/i), "$1en" ],
71 [(/([m|l])ouse$/i), "$1ice" ],
72 [(/(matr|vert|ind)ix|ex$/i), "$1ices" ],
73 [(/(x|ch|ss|sh)$/i), "$1es" ],
74 [(/([^aeiouy]|qu)y$/i), "$1ies" ],
75 [(/(hive)$/i), "$1s" ],
76 [(/(?:([^f])fe|([lr])f)$/i), "$1$2ves"],
77 [(/sis$/i), "ses" ],
78 [(/([ti])um$/i), "$1a" ],
79 [(/(buffal|tomat|potat)o$/i), "$1oes" ],
80 [(/(bu)s$/i), "$1ses" ],
81 [(/(alias|status|sex)$/i), "$1es" ],
82 [(/(octop|vir)us$/i), "$1i" ],
83 [(/(ax|test)is$/i), "$1es" ],
84 [(/^person$/), "people" ],
85 [(/^man$/), "men" ],
86 [(/^(child)$/), "$1ren" ],
87 [(/s$/i), "s" ],
88 [(/$/), "s" ]
91 <span id='Ext-util-Inflector-property-singulars'> /**
93 * The set of registered singular matchers. Each item in the array should contain two items - the first must be a
94 * regular expression that matches the plural form of a word, the second must be a String that replaces the
95 * matched part of the regular expression. This is managed by the {@link #singular} method.
96 * @property {Array} singulars
99 [(/(quiz)zes$/i), "$1" ],
100 [(/(matr)ices$/i), "$1ix" ],
101 [(/(vert|ind)ices$/i), "$1ex" ],
102 [(/^(ox)en/i), "$1" ],
103 [(/(alias|status)es$/i), "$1" ],
104 [(/(octop|vir)i$/i), "$1us" ],
105 [(/(cris|ax|test)es$/i), "$1is" ],
106 [(/(shoe)s$/i), "$1" ],
107 [(/(o)es$/i), "$1" ],
108 [(/(bus)es$/i), "$1" ],
109 [(/([m|l])ice$/i), "$1ouse" ],
110 [(/(x|ch|ss|sh)es$/i), "$1" ],
111 [(/(m)ovies$/i), "$1ovie" ],
112 [(/(s)eries$/i), "$1eries"],
113 [(/([^aeiouy]|qu)ies$/i), "$1y" ],
114 [(/([lr])ves$/i), "$1f" ],
115 [(/(tive)s$/i), "$1" ],
116 [(/(hive)s$/i), "$1" ],
117 [(/([^f])ves$/i), "$1fe" ],
118 [(/(^analy)ses$/i), "$1sis" ],
119 [(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i), "$1$2sis"],
120 [(/([ti])a$/i), "$1um" ],
121 [(/(n)ews$/i), "$1ews" ],
122 [(/people$/i), "person" ],
123 [(/s$/i), "" ]
126 <span id='Ext-util-Inflector-property-uncountable'> /**
128 * The registered uncountable words
129 * @property {String[]} uncountable
138 "information",
139 "equipment",
142 "offspring",
147 <span id='Ext-util-Inflector-method-singular'> /**
148 </span> * Adds a new singularization rule to the Inflector. See the intro docs for more information
149 * @param {RegExp} matcher The matcher regex
150 * @param {String} replacer The replacement string, which can reference matches from the matcher argument
152 singular: function(matcher, replacer) {
153 this.singulars.unshift([matcher, replacer]);
156 <span id='Ext-util-Inflector-method-plural'> /**
157 </span> * Adds a new pluralization rule to the Inflector. See the intro docs for more information
158 * @param {RegExp} matcher The matcher regex
159 * @param {String} replacer The replacement string, which can reference matches from the matcher argument
161 plural: function(matcher, replacer) {
162 this.plurals.unshift([matcher, replacer]);
165 <span id='Ext-util-Inflector-method-clearSingulars'> /**
166 </span> * Removes all registered singularization rules
168 clearSingulars: function() {
172 <span id='Ext-util-Inflector-method-clearPlurals'> /**
173 </span> * Removes all registered pluralization rules
175 clearPlurals: function() {
179 <span id='Ext-util-Inflector-method-isTransnumeral'> /**
180 </span> * Returns true if the given word is transnumeral (the word is its own singular and plural form - e.g. sheep, fish)
181 * @param {String} word The word to test
182 * @return {Boolean} True if the word is transnumeral
184 isTransnumeral: function(word) {
185 return Ext.Array.indexOf(this.uncountable, word) != -1;
188 <span id='Ext-util-Inflector-method-pluralize'> /**
189 </span> * Returns the pluralized form of a word (e.g. Ext.util.Inflector.pluralize('word') returns 'words')
190 * @param {String} word The word to pluralize
191 * @return {String} The pluralized form of the word
193 pluralize: function(word) {
194 if (this.isTransnumeral(word)) {
198 var plurals = this.plurals,
199 length = plurals.length,
202 for (i = 0; i < length; i++) {
206 if (regex == word || (regex.test && regex.test(word))) {
207 return word.replace(regex, tuple[1]);
214 <span id='Ext-util-Inflector-method-singularize'> /**
215 </span> * Returns the singularized form of a word (e.g. Ext.util.Inflector.singularize('words') returns 'word')
216 * @param {String} word The word to singularize
217 * @return {String} The singularized form of the word
219 singularize: function(word) {
220 if (this.isTransnumeral(word)) {
224 var singulars = this.singulars,
225 length = singulars.length,
228 for (i = 0; i < length; i++) {
229 tuple = singulars[i];
232 if (regex == word || (regex.test && regex.test(word))) {
233 return word.replace(regex, tuple[1]);
240 <span id='Ext-util-Inflector-method-classify'> /**
241 </span> * Returns the correct {@link Ext.data.Model Model} name for a given string. Mostly used internally by the data
243 * @param {String} word The word to classify
244 * @return {String} The classified version of the word
246 classify: function(word) {
247 return Ext.String.capitalize(this.singularize(word));
250 <span id='Ext-util-Inflector-method-ordinalize'> /**
251 </span> * Ordinalizes a given number by adding a prefix such as 'st', 'nd', 'rd' or 'th' based on the last digit of the
252 * number. 21 -> 21st, 22 -> 22nd, 23 -> 23rd, 24 -> 24th etc
253 * @param {Number} number The number to ordinalize
254 * @return {String} The ordinalized number
256 ordinalize: function(number) {
257 var parsed = parseInt(number, 10),
259 mod100 = parsed % 100;
261 //11 through 13 are a special case
262 if (11 <= mod100 && mod100 <= 13) {
263 return number + "th";
266 case 1 : return number + "st";
267 case 2 : return number + "nd";
268 case 3 : return number + "rd";
269 default: return number + "th";
274 //aside from the rules above, there are a number of words that have irregular pluralization so we add them here
282 ellipsis: 'ellipses',
283 paralysis: 'paralyses',
285 appendix: 'appendices',
294 criterion: 'criteria',
295 curriculum: 'curricula',
297 memorandum: 'memoranda',
298 phenomenon: 'phenomena',
305 vertebra: 'vertebrae',
310 for (singular in irregulars) {
311 this.plural(singular, irregulars[singular]);
312 this.singular(irregulars[singular], singular);