pluralize.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* global define */
  2. (function (root, pluralize) {
  3. /* istanbul ignore else */
  4. if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
  5. // Node.
  6. module.exports = pluralize();
  7. } else if (typeof define === 'function' && define.amd) {
  8. // AMD, registers as an anonymous module.
  9. define(function () {
  10. return pluralize();
  11. });
  12. } else {
  13. // Browser global.
  14. root.pluralize = pluralize();
  15. }
  16. })(this, function () {
  17. // Rule storage - pluralize and singularize need to be run sequentially,
  18. // while other rules can be optimized using an object for instant lookups.
  19. var pluralRules = [];
  20. var singularRules = [];
  21. var uncountables = {};
  22. var irregularPlurals = {};
  23. var irregularSingles = {};
  24. /**
  25. * Sanitize a pluralization rule to a usable regular expression.
  26. *
  27. * @param {(RegExp|string)} rule
  28. * @return {RegExp}
  29. */
  30. function sanitizeRule (rule) {
  31. if (typeof rule === 'string') {
  32. return new RegExp('^' + rule + '$', 'i');
  33. }
  34. return rule;
  35. }
  36. /**
  37. * Pass in a word token to produce a function that can replicate the case on
  38. * another word.
  39. *
  40. * @param {string} word
  41. * @param {string} token
  42. * @return {Function}
  43. */
  44. function restoreCase (word, token) {
  45. // Tokens are an exact match.
  46. if (word === token) return token;
  47. // Lower cased words. E.g. "hello".
  48. if (word === word.toLowerCase()) return token.toLowerCase();
  49. // Upper cased words. E.g. "WHISKY".
  50. if (word === word.toUpperCase()) return token.toUpperCase();
  51. // Title cased words. E.g. "Title".
  52. if (word[0] === word[0].toUpperCase()) {
  53. return token.charAt(0).toUpperCase() + token.substr(1).toLowerCase();
  54. }
  55. // Lower cased words. E.g. "test".
  56. return token.toLowerCase();
  57. }
  58. /**
  59. * Interpolate a regexp string.
  60. *
  61. * @param {string} str
  62. * @param {Array} args
  63. * @return {string}
  64. */
  65. function interpolate (str, args) {
  66. return str.replace(/\$(\d{1,2})/g, function (match, index) {
  67. return args[index] || '';
  68. });
  69. }
  70. /**
  71. * Replace a word using a rule.
  72. *
  73. * @param {string} word
  74. * @param {Array} rule
  75. * @return {string}
  76. */
  77. function replace (word, rule) {
  78. return word.replace(rule[0], function (match, index) {
  79. var result = interpolate(rule[1], arguments);
  80. if (match === '') {
  81. return restoreCase(word[index - 1], result);
  82. }
  83. return restoreCase(match, result);
  84. });
  85. }
  86. /**
  87. * Sanitize a word by passing in the word and sanitization rules.
  88. *
  89. * @param {string} token
  90. * @param {string} word
  91. * @param {Array} rules
  92. * @return {string}
  93. */
  94. function sanitizeWord (token, word, rules) {
  95. // Empty string or doesn't need fixing.
  96. if (!token.length || uncountables.hasOwnProperty(token)) {
  97. return word;
  98. }
  99. var len = rules.length;
  100. // Iterate over the sanitization rules and use the first one to match.
  101. while (len--) {
  102. var rule = rules[len];
  103. if (rule[0].test(word)) return replace(word, rule);
  104. }
  105. return word;
  106. }
  107. /**
  108. * Replace a word with the updated word.
  109. *
  110. * @param {Object} replaceMap
  111. * @param {Object} keepMap
  112. * @param {Array} rules
  113. * @return {Function}
  114. */
  115. function replaceWord (replaceMap, keepMap, rules) {
  116. return function (word) {
  117. // Get the correct token and case restoration functions.
  118. var token = word.toLowerCase();
  119. // Check against the keep object map.
  120. if (keepMap.hasOwnProperty(token)) {
  121. return restoreCase(word, token);
  122. }
  123. // Check against the replacement map for a direct word replacement.
  124. if (replaceMap.hasOwnProperty(token)) {
  125. return restoreCase(word, replaceMap[token]);
  126. }
  127. // Run all the rules against the word.
  128. return sanitizeWord(token, word, rules);
  129. };
  130. }
  131. /**
  132. * Check if a word is part of the map.
  133. */
  134. function checkWord (replaceMap, keepMap, rules, bool) {
  135. return function (word) {
  136. var token = word.toLowerCase();
  137. if (keepMap.hasOwnProperty(token)) return true;
  138. if (replaceMap.hasOwnProperty(token)) return false;
  139. return sanitizeWord(token, token, rules) === token;
  140. };
  141. }
  142. /**
  143. * Pluralize or singularize a word based on the passed in count.
  144. *
  145. * @param {string} word The word to pluralize
  146. * @param {number} count How many of the word exist
  147. * @param {boolean} inclusive Whether to prefix with the number (e.g. 3 ducks)
  148. * @return {string}
  149. */
  150. function pluralize (word, count, inclusive) {
  151. var pluralized = count === 1
  152. ? pluralize.singular(word) : pluralize.plural(word);
  153. return (inclusive ? count + ' ' : '') + pluralized;
  154. }
  155. /**
  156. * Pluralize a word.
  157. *
  158. * @type {Function}
  159. */
  160. pluralize.plural = replaceWord(
  161. irregularSingles, irregularPlurals, pluralRules
  162. );
  163. /**
  164. * Check if a word is plural.
  165. *
  166. * @type {Function}
  167. */
  168. pluralize.isPlural = checkWord(
  169. irregularSingles, irregularPlurals, pluralRules
  170. );
  171. /**
  172. * Singularize a word.
  173. *
  174. * @type {Function}
  175. */
  176. pluralize.singular = replaceWord(
  177. irregularPlurals, irregularSingles, singularRules
  178. );
  179. /**
  180. * Check if a word is singular.
  181. *
  182. * @type {Function}
  183. */
  184. pluralize.isSingular = checkWord(
  185. irregularPlurals, irregularSingles, singularRules
  186. );
  187. /**
  188. * Add a pluralization rule to the collection.
  189. *
  190. * @param {(string|RegExp)} rule
  191. * @param {string} replacement
  192. */
  193. pluralize.addPluralRule = function (rule, replacement) {
  194. pluralRules.push([sanitizeRule(rule), replacement]);
  195. };
  196. /**
  197. * Add a singularization rule to the collection.
  198. *
  199. * @param {(string|RegExp)} rule
  200. * @param {string} replacement
  201. */
  202. pluralize.addSingularRule = function (rule, replacement) {
  203. singularRules.push([sanitizeRule(rule), replacement]);
  204. };
  205. /**
  206. * Add an uncountable word rule.
  207. *
  208. * @param {(string|RegExp)} word
  209. */
  210. pluralize.addUncountableRule = function (word) {
  211. if (typeof word === 'string') {
  212. uncountables[word.toLowerCase()] = true;
  213. return;
  214. }
  215. // Set singular and plural references for the word.
  216. pluralize.addPluralRule(word, '$0');
  217. pluralize.addSingularRule(word, '$0');
  218. };
  219. /**
  220. * Add an irregular word definition.
  221. *
  222. * @param {string} single
  223. * @param {string} plural
  224. */
  225. pluralize.addIrregularRule = function (single, plural) {
  226. plural = plural.toLowerCase();
  227. single = single.toLowerCase();
  228. irregularSingles[single] = plural;
  229. irregularPlurals[plural] = single;
  230. };
  231. /**
  232. * Irregular rules.
  233. */
  234. [
  235. // Pronouns.
  236. ['I', 'we'],
  237. ['me', 'us'],
  238. ['he', 'they'],
  239. ['she', 'they'],
  240. ['them', 'them'],
  241. ['myself', 'ourselves'],
  242. ['yourself', 'yourselves'],
  243. ['itself', 'themselves'],
  244. ['herself', 'themselves'],
  245. ['himself', 'themselves'],
  246. ['themself', 'themselves'],
  247. ['is', 'are'],
  248. ['was', 'were'],
  249. ['has', 'have'],
  250. ['this', 'these'],
  251. ['that', 'those'],
  252. // Words ending in with a consonant and `o`.
  253. ['echo', 'echoes'],
  254. ['dingo', 'dingoes'],
  255. ['volcano', 'volcanoes'],
  256. ['tornado', 'tornadoes'],
  257. ['torpedo', 'torpedoes'],
  258. // Ends with `us`.
  259. ['genus', 'genera'],
  260. ['viscus', 'viscera'],
  261. // Ends with `ma`.
  262. ['stigma', 'stigmata'],
  263. ['stoma', 'stomata'],
  264. ['dogma', 'dogmata'],
  265. ['lemma', 'lemmata'],
  266. ['schema', 'schemata'],
  267. ['anathema', 'anathemata'],
  268. // Other irregular rules.
  269. ['ox', 'oxen'],
  270. ['axe', 'axes'],
  271. ['die', 'dice'],
  272. ['yes', 'yeses'],
  273. ['foot', 'feet'],
  274. ['eave', 'eaves'],
  275. ['goose', 'geese'],
  276. ['tooth', 'teeth'],
  277. ['quiz', 'quizzes'],
  278. ['human', 'humans'],
  279. ['proof', 'proofs'],
  280. ['carve', 'carves'],
  281. ['valve', 'valves'],
  282. ['looey', 'looies'],
  283. ['thief', 'thieves'],
  284. ['groove', 'grooves'],
  285. ['pickaxe', 'pickaxes'],
  286. ['passerby', 'passersby']
  287. ].forEach(function (rule) {
  288. return pluralize.addIrregularRule(rule[0], rule[1]);
  289. });
  290. /**
  291. * Pluralization rules.
  292. */
  293. [
  294. [/s?$/i, 's'],
  295. [/[^\u0000-\u007F]$/i, '$0'],
  296. [/([^aeiou]ese)$/i, '$1'],
  297. [/(ax|test)is$/i, '$1es'],
  298. [/(alias|[^aou]us|t[lm]as|gas|ris)$/i, '$1es'],
  299. [/(e[mn]u)s?$/i, '$1s'],
  300. [/([^l]ias|[aeiou]las|[ejzr]as|[iu]am)$/i, '$1'],
  301. [/(alumn|syllab|vir|radi|nucle|fung|cact|stimul|termin|bacill|foc|uter|loc|strat)(?:us|i)$/i, '$1i'],
  302. [/(alumn|alg|vertebr)(?:a|ae)$/i, '$1ae'],
  303. [/(seraph|cherub)(?:im)?$/i, '$1im'],
  304. [/(her|at|gr)o$/i, '$1oes'],
  305. [/(agend|addend|millenni|dat|extrem|bacteri|desiderat|strat|candelabr|errat|ov|symposi|curricul|automat|quor)(?:a|um)$/i, '$1a'],
  306. [/(apheli|hyperbat|periheli|asyndet|noumen|phenomen|criteri|organ|prolegomen|hedr|automat)(?:a|on)$/i, '$1a'],
  307. [/sis$/i, 'ses'],
  308. [/(?:(kni|wi|li)fe|(ar|l|ea|eo|oa|hoo)f)$/i, '$1$2ves'],
  309. [/([^aeiouy]|qu)y$/i, '$1ies'],
  310. [/([^ch][ieo][ln])ey$/i, '$1ies'],
  311. [/(x|ch|ss|sh|zz)$/i, '$1es'],
  312. [/(matr|cod|mur|sil|vert|ind|append)(?:ix|ex)$/i, '$1ices'],
  313. [/\b((?:tit)?m|l)(?:ice|ouse)$/i, '$1ice'],
  314. [/(pe)(?:rson|ople)$/i, '$1ople'],
  315. [/(child)(?:ren)?$/i, '$1ren'],
  316. [/eaux$/i, '$0'],
  317. [/m[ae]n$/i, 'men'],
  318. ['thou', 'you']
  319. ].forEach(function (rule) {
  320. return pluralize.addPluralRule(rule[0], rule[1]);
  321. });
  322. /**
  323. * Singularization rules.
  324. */
  325. [
  326. [/s$/i, ''],
  327. [/(ss)$/i, '$1'],
  328. [/(wi|kni|(?:after|half|high|low|mid|non|night|[^\w]|^)li)ves$/i, '$1fe'],
  329. [/(ar|(?:wo|[ae])l|[eo][ao])ves$/i, '$1f'],
  330. [/ies$/i, 'y'],
  331. [/\b([pl]|zomb|(?:neck|cross)?t|coll|faer|food|gen|goon|group|lass|talk|goal|cut)ies$/i, '$1ie'],
  332. [/\b(mon|smil)ies$/i, '$1ey'],
  333. [/\b((?:tit)?m|l)ice$/i, '$1ouse'],
  334. [/(seraph|cherub)im$/i, '$1'],
  335. [/(x|ch|ss|sh|zz|tto|go|cho|alias|[^aou]us|t[lm]as|gas|(?:her|at|gr)o|[aeiou]ris)(?:es)?$/i, '$1'],
  336. [/(analy|diagno|parenthe|progno|synop|the|empha|cri|ne)(?:sis|ses)$/i, '$1sis'],
  337. [/(movie|twelve|abuse|e[mn]u)s$/i, '$1'],
  338. [/(test)(?:is|es)$/i, '$1is'],
  339. [/(alumn|syllab|vir|radi|nucle|fung|cact|stimul|termin|bacill|foc|uter|loc|strat)(?:us|i)$/i, '$1us'],
  340. [/(agend|addend|millenni|dat|extrem|bacteri|desiderat|strat|candelabr|errat|ov|symposi|curricul|quor)a$/i, '$1um'],
  341. [/(apheli|hyperbat|periheli|asyndet|noumen|phenomen|criteri|organ|prolegomen|hedr|automat)a$/i, '$1on'],
  342. [/(alumn|alg|vertebr)ae$/i, '$1a'],
  343. [/(cod|mur|sil|vert|ind)ices$/i, '$1ex'],
  344. [/(matr|append)ices$/i, '$1ix'],
  345. [/(pe)(rson|ople)$/i, '$1rson'],
  346. [/(child)ren$/i, '$1'],
  347. [/(eau)x?$/i, '$1'],
  348. [/men$/i, 'man']
  349. ].forEach(function (rule) {
  350. return pluralize.addSingularRule(rule[0], rule[1]);
  351. });
  352. /**
  353. * Uncountable rules.
  354. */
  355. [
  356. // Singular words with no plurals.
  357. 'adulthood',
  358. 'advice',
  359. 'agenda',
  360. 'aid',
  361. 'aircraft',
  362. 'alcohol',
  363. 'ammo',
  364. 'analytics',
  365. 'anime',
  366. 'athletics',
  367. 'audio',
  368. 'bison',
  369. 'blood',
  370. 'bream',
  371. 'buffalo',
  372. 'butter',
  373. 'carp',
  374. 'cash',
  375. 'chassis',
  376. 'chess',
  377. 'clothing',
  378. 'cod',
  379. 'commerce',
  380. 'cooperation',
  381. 'corps',
  382. 'debris',
  383. 'diabetes',
  384. 'digestion',
  385. 'elk',
  386. 'energy',
  387. 'equipment',
  388. 'excretion',
  389. 'expertise',
  390. 'firmware',
  391. 'flounder',
  392. 'fun',
  393. 'gallows',
  394. 'garbage',
  395. 'graffiti',
  396. 'hardware',
  397. 'headquarters',
  398. 'health',
  399. 'herpes',
  400. 'highjinks',
  401. 'homework',
  402. 'housework',
  403. 'information',
  404. 'jeans',
  405. 'justice',
  406. 'kudos',
  407. 'labour',
  408. 'literature',
  409. 'machinery',
  410. 'mackerel',
  411. 'mail',
  412. 'media',
  413. 'mews',
  414. 'moose',
  415. 'music',
  416. 'mud',
  417. 'manga',
  418. 'news',
  419. 'only',
  420. 'personnel',
  421. 'pike',
  422. 'plankton',
  423. 'pliers',
  424. 'police',
  425. 'pollution',
  426. 'premises',
  427. 'rain',
  428. 'research',
  429. 'rice',
  430. 'salmon',
  431. 'scissors',
  432. 'series',
  433. 'sewage',
  434. 'shambles',
  435. 'shrimp',
  436. 'software',
  437. 'species',
  438. 'staff',
  439. 'swine',
  440. 'tennis',
  441. 'traffic',
  442. 'transportation',
  443. 'trout',
  444. 'tuna',
  445. 'wealth',
  446. 'welfare',
  447. 'whiting',
  448. 'wildebeest',
  449. 'wildlife',
  450. 'you',
  451. /pok[eé]mon$/i,
  452. // Regexes.
  453. /[^aeiou]ese$/i, // "chinese", "japanese"
  454. /deer$/i, // "deer", "reindeer"
  455. /fish$/i, // "fish", "blowfish", "angelfish"
  456. /measles$/i,
  457. /o[iu]s$/i, // "carnivorous"
  458. /pox$/i, // "chickpox", "smallpox"
  459. /sheep$/i
  460. ].forEach(pluralize.addUncountableRule);
  461. return pluralize;
  462. });