inflection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var inflection = require('../lib/inflection')
  19. , assert = require('assert')
  20. , esInflections
  21. , sInflections
  22. , iesInflections
  23. , vesInflections
  24. , icesInflections
  25. , renInflections
  26. , oesInflections
  27. , iInflections
  28. , genInflections
  29. , irregularInflections
  30. , noInflections
  31. , tests;
  32. /**
  33. * Most test inflections are from Ruby on Rails:
  34. * https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb
  35. *
  36. * Ruby on Rails is MIT licensed: http://www.opensource.org/licenses/MIT
  37. */
  38. esInflections = [
  39. ["search", "searches"]
  40. , ["switch", "switches"]
  41. , ["fix", "fixes"]
  42. , ["box", "boxes"]
  43. , ["process", "processes"]
  44. , ["address", "addresses"]
  45. , ["wish", "wishes"]
  46. , ["status", "statuses"]
  47. , ["alias", "aliases"]
  48. , ["basis", "bases"]
  49. , ["diagnosis", "diagnoses"]
  50. , ["bus", "buses"]
  51. ];
  52. sInflections = [
  53. ["stack", "stacks"]
  54. , ["shoe", "shoes"]
  55. , ["status_code", "status_codes"]
  56. , ["case", "cases"]
  57. , ["edge", "edges"]
  58. , ["archive", "archives"]
  59. , ["experience", "experiences"]
  60. , ["day", "days"]
  61. , ["comment", "comments"]
  62. , ["foobar", "foobars"]
  63. , ["newsletter", "newsletters"]
  64. , ["old_news", "old_news"]
  65. , ["perspective", "perspectives"]
  66. , ["diagnosis_a", "diagnosis_as"]
  67. , ["horse", "horses"]
  68. , ["prize", "prizes"]
  69. ];
  70. iesInflections = [
  71. ["category", "categories"]
  72. , ["query", "queries"]
  73. , ["ability", "abilities"]
  74. , ["agency", "agencies"]
  75. ];
  76. vesInflections = [
  77. ["wife", "wives"]
  78. , ["safe", "saves"]
  79. , ["half", "halves"]
  80. , ["elf", "elves"]
  81. , ["dwarf", "dwarves"]
  82. ];
  83. icesInflections = [
  84. ["index", "indices"]
  85. , ["vertex", "vertices"]
  86. , ["matrix", "matrices"]
  87. ];
  88. renInflections = [
  89. ["node_child", "node_children"]
  90. , ["child", "children"]
  91. ];
  92. oesInflections = [
  93. ["buffalo", "buffaloes"]
  94. , ["tomato", "tomatoes"]
  95. ];
  96. iInflections = [
  97. ["octopus", "octopi"]
  98. , ["virus", "viri"]
  99. ];
  100. genInflections = [
  101. ["salesperson", "salespeople"]
  102. , ["person", "people"]
  103. , ["spokesman", "spokesmen"]
  104. , ["man", "men"]
  105. , ["woman", "women"]
  106. ];
  107. irregularInflections = [
  108. ["datum", "data"]
  109. , ["medium", "media"]
  110. , ["ox", "oxen"]
  111. , ["cow", "kine"]
  112. , ["mouse", "mice"]
  113. , ["louse", "lice"]
  114. , ["axis", "axes"]
  115. , ["testis", "testes"]
  116. , ["crisis", "crises"]
  117. , ["analysis", "analyses"]
  118. , ["quiz", "quizzes"]
  119. ];
  120. noInflections = [
  121. ["fish", "fish"]
  122. , ["news", "news"]
  123. , ["series", "series"]
  124. , ["species", "species"]
  125. , ["rice", "rice"]
  126. , ["information", "information"]
  127. , ["equipment", "equipment"]
  128. ];
  129. tests = {
  130. 'test es plural words for inflection': function () {
  131. var i = esInflections.length
  132. , value;
  133. while (--i >= 0) {
  134. value = esInflections[i];
  135. assert.equal(value[1], inflection.pluralize(value[0]))
  136. }
  137. }
  138. , 'test es singular words for inflection': function () {
  139. var i = esInflections.length
  140. , value;
  141. while (--i >= 0) {
  142. value = esInflections[i];
  143. assert.equal(value[0], inflection.singularize(value[1]))
  144. }
  145. }
  146. , 'test es plural words for inflection consistency': function() {
  147. var i = esInflections.length
  148. , value;
  149. while (--i >= 0) {
  150. value = esInflections[i];
  151. assert.equal(value[1], inflection.pluralize(value[1]))
  152. }
  153. }
  154. , 'test es singular words for inflection consistency': function() {
  155. var i = esInflections.length
  156. , value;
  157. while (--i >= 0) {
  158. value = esInflections[i];
  159. assert.equal(value[0], inflection.singularize(value[0]))
  160. }
  161. }
  162. , 'test s plural words for inflection': function () {
  163. var i = sInflections.length
  164. , value;
  165. while (--i >= 0) {
  166. value = sInflections[i];
  167. assert.equal(value[1], inflection.pluralize(value[0]))
  168. }
  169. }
  170. , 'test s singular words for inflection': function () {
  171. var i = sInflections.length
  172. , value;
  173. while (--i >= 0) {
  174. value = sInflections[i];
  175. assert.equal(value[0], inflection.singularize(value[1]))
  176. }
  177. }
  178. , 'test s plural words for inflection consistency': function () {
  179. var i = sInflections.length
  180. , value;
  181. while (--i >= 0) {
  182. value = sInflections[i];
  183. assert.equal(value[1], inflection.pluralize(value[1]))
  184. }
  185. }
  186. , 'test s singular words for inflection consistency': function () {
  187. var i = sInflections.length
  188. , value;
  189. while (--i >= 0) {
  190. value = sInflections[i];
  191. assert.equal(value[0], inflection.singularize(value[0]))
  192. }
  193. }
  194. , 'test ies plural words for inflection': function () {
  195. var i = iesInflections.length
  196. , value;
  197. while (--i >= 0) {
  198. value = iesInflections[i];
  199. assert.equal(value[1], inflection.pluralize(value[0]))
  200. }
  201. }
  202. , 'test ies singular words for inflection': function () {
  203. var i = iesInflections.length
  204. , value;
  205. while (--i >= 0) {
  206. value = iesInflections[i];
  207. assert.equal(value[0], inflection.singularize(value[1]))
  208. }
  209. }
  210. , 'test ies plural words for inflection consistency': function () {
  211. var i = iesInflections.length
  212. , value;
  213. while (--i >= 0) {
  214. value = iesInflections[i];
  215. assert.equal(value[1], inflection.pluralize(value[1]))
  216. }
  217. }
  218. , 'test ies singular words for inflection consistency': function () {
  219. var i = iesInflections.length
  220. , value;
  221. while (--i >= 0) {
  222. value = iesInflections[i];
  223. assert.equal(value[0], inflection.singularize(value[0]))
  224. }
  225. }
  226. , 'test ves plural words for inflection': function () {
  227. var i = vesInflections.length
  228. , value;
  229. while (--i >= 0) {
  230. value = vesInflections[i];
  231. assert.equal(value[1], inflection.pluralize(value[0]))
  232. }
  233. }
  234. , 'test ves singular words for inflection': function () {
  235. var i = vesInflections.length
  236. , value;
  237. while (--i >= 0) {
  238. value = vesInflections[i];
  239. assert.equal(value[0], inflection.singularize(value[1]))
  240. }
  241. }
  242. , 'test ves plural words for inflection consistency': function () {
  243. var i = vesInflections.length
  244. , value;
  245. while (--i >= 0) {
  246. value = vesInflections[i];
  247. assert.equal(value[1], inflection.pluralize(value[1]))
  248. }
  249. }
  250. , 'test ves singular words for inflection consistency': function () {
  251. var i = vesInflections.length
  252. , value;
  253. while (--i >= 0) {
  254. value = vesInflections[i];
  255. assert.equal(value[0], inflection.singularize(value[0]))
  256. }
  257. }
  258. , 'test ices plural words for inflection': function () {
  259. var i = icesInflections.length
  260. , value;
  261. while (--i >= 0) {
  262. value = icesInflections[i];
  263. assert.equal(value[1], inflection.pluralize(value[0]))
  264. }
  265. }
  266. , 'test ices singular words for inflection': function () {
  267. var i = icesInflections.length
  268. , value;
  269. while (--i >= 0) {
  270. value = icesInflections[i];
  271. assert.equal(value[0], inflection.singularize(value[1]))
  272. }
  273. }
  274. , 'test ices plural words for inflection consistency': function () {
  275. var i = icesInflections.length
  276. , value;
  277. while (--i >= 0) {
  278. value = icesInflections[i];
  279. assert.equal(value[1], inflection.pluralize(value[1]))
  280. }
  281. }
  282. , 'test ices singular words for inflection consistency': function () {
  283. var i = icesInflections.length
  284. , value;
  285. while (--i >= 0) {
  286. value = icesInflections[i];
  287. assert.equal(value[0], inflection.singularize(value[0]))
  288. }
  289. }
  290. , 'test ren plural words for inflection': function () {
  291. var i = renInflections.length
  292. , value;
  293. while (--i >= 0) {
  294. value = renInflections[i];
  295. assert.equal(value[1], inflection.pluralize(value[0]))
  296. }
  297. }
  298. , 'test ren singular words for inflection': function () {
  299. var i = renInflections.length
  300. , value;
  301. while (--i >= 0) {
  302. value = renInflections[i];
  303. assert.equal(value[0], inflection.singularize(value[1]))
  304. }
  305. }
  306. , 'test ren plural words for inflection consistency': function () {
  307. var i = renInflections.length
  308. , value;
  309. while (--i >= 0) {
  310. value = renInflections[i];
  311. assert.equal(value[1], inflection.pluralize(value[1]))
  312. }
  313. }
  314. , 'test ren singular words for inflection consistency': function () {
  315. var i = renInflections.length
  316. , value;
  317. while (--i >= 0) {
  318. value = renInflections[i];
  319. assert.equal(value[0], inflection.singularize(value[0]))
  320. }
  321. }
  322. , 'test oes plural words for inflection': function () {
  323. var i = oesInflections.length
  324. , value;
  325. while (--i >= 0) {
  326. value = oesInflections[i];
  327. assert.equal(value[1], inflection.pluralize(value[0]))
  328. }
  329. }
  330. , 'test oes singular words for inflection': function () {
  331. var i = oesInflections.length
  332. , value;
  333. while (--i >= 0) {
  334. value = oesInflections[i];
  335. assert.equal(value[0], inflection.singularize(value[1]))
  336. }
  337. }
  338. , 'test oes plural words for inflection consistency': function () {
  339. var i = oesInflections.length
  340. , value;
  341. while (--i >= 0) {
  342. value = oesInflections[i];
  343. assert.equal(value[1], inflection.pluralize(value[1]))
  344. }
  345. }
  346. , 'test oes singular words for inflection consistency': function () {
  347. var i = oesInflections.length
  348. , value;
  349. while (--i >= 0) {
  350. value = oesInflections[i];
  351. assert.equal(value[0], inflection.singularize(value[0]))
  352. }
  353. }
  354. , 'test i plural words for inflection': function () {
  355. var i = iInflections.length
  356. , value;
  357. while (--i >= 0) {
  358. value = iInflections[i];
  359. assert.equal(value[1], inflection.pluralize(value[0]))
  360. }
  361. }
  362. , 'test i singular words for inflection': function () {
  363. var i = iInflections.length
  364. , value;
  365. while (--i >= 0) {
  366. value = iInflections[i];
  367. assert.equal(value[0], inflection.singularize(value[1]))
  368. }
  369. }
  370. , 'test i plural words for inflection consistency': function () {
  371. var i = iInflections.length
  372. , value;
  373. while (--i >= 0) {
  374. value = iInflections[i];
  375. assert.equal(value[1], inflection.pluralize(value[1]))
  376. }
  377. }
  378. , 'test i singular words for inflection consistency': function () {
  379. var i = iInflections.length
  380. , value;
  381. while (--i >= 0) {
  382. value = iInflections[i];
  383. assert.equal(value[0], inflection.singularize(value[0]))
  384. }
  385. }
  386. , 'test gender and people plural words for inflection': function () {
  387. var i = genInflections.length
  388. , value;
  389. while (--i >= 0) {
  390. value = genInflections[i];
  391. assert.equal(value[1], inflection.pluralize(value[0]))
  392. }
  393. }
  394. , 'test gender and people singular words for inflection': function () {
  395. var i = genInflections.length
  396. , value;
  397. while (--i >= 0) {
  398. value = genInflections[i];
  399. assert.equal(value[0], inflection.singularize(value[1]))
  400. }
  401. }
  402. , 'test gender and people plural words for inflection consistency': function () {
  403. var i = genInflections.length
  404. , value;
  405. while (--i >= 0) {
  406. value = genInflections[i];
  407. assert.equal(value[1], inflection.pluralize(value[1]))
  408. }
  409. }
  410. , 'test gender and people singular words for inflection consistency': function () {
  411. var i = genInflections.length
  412. , value;
  413. while (--i >= 0) {
  414. value = genInflections[i];
  415. assert.equal(value[0], inflection.singularize(value[0]))
  416. }
  417. }
  418. , 'test irregular plural words for inflection': function () {
  419. var i = irregularInflections.length
  420. , value;
  421. while (--i >= 0) {
  422. value = irregularInflections[i];
  423. assert.equal(value[1], inflection.pluralize(value[0]))
  424. }
  425. }
  426. , 'test irregular singular words for inflection': function () {
  427. var i = irregularInflections.length
  428. , value;
  429. while (--i >= 0) {
  430. value = irregularInflections[i];
  431. assert.equal(value[0], inflection.singularize(value[1]))
  432. }
  433. }
  434. , 'test irregular plural words for inflection consistency': function () {
  435. var i = irregularInflections.length
  436. , value;
  437. while (--i >= 0) {
  438. value = irregularInflections[i];
  439. assert.equal(value[1], inflection.pluralize(value[1]))
  440. }
  441. }
  442. , 'test irregular singular words for inflection consistency': function () {
  443. var i = irregularInflections.length
  444. , value;
  445. while (--i >= 0) {
  446. value = irregularInflections[i];
  447. assert.equal(value[0], inflection.singularize(value[0]))
  448. }
  449. }
  450. , 'test no change plural words for inflection': function () {
  451. var i = noInflections.length
  452. , value;
  453. while (--i >= 0) {
  454. value = noInflections[i];
  455. assert.equal(value[1], inflection.pluralize(value[0]))
  456. }
  457. }
  458. , 'test no change singular words for inflection': function () {
  459. var i = noInflections.length
  460. , value;
  461. while (--i >= 0) {
  462. value = noInflections[i];
  463. assert.equal(value[0], inflection.singularize(value[1]))
  464. }
  465. }
  466. , 'test no change plural words for inflection consistency': function () {
  467. var i = noInflections.length
  468. , value;
  469. while (--i >= 0) {
  470. value = noInflections[i];
  471. assert.equal(value[1], inflection.pluralize(value[1]))
  472. }
  473. }
  474. , 'test no change singular words for inflection consistency': function () {
  475. var i = noInflections.length
  476. , value;
  477. while (--i >= 0) {
  478. value = noInflections[i];
  479. assert.equal(value[0], inflection.singularize(value[0]))
  480. }
  481. }
  482. };
  483. module.exports = tests;