Inflector.html 13 KB

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