Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

246 рядки
8.9 KiB

  1. import { describe, it, expect } from 'vitest';
  2. import {parse, stringify, systems} from '../../src';
  3. const options = { system: systems.enUS };
  4. describe('numerica', () => {
  5. describe('group names', () => {
  6. describe('0-9', () => {
  7. it.each`
  8. ones | expected
  9. ${0} | ${'zero'}
  10. ${1} | ${'one'}
  11. ${2} | ${'two'}
  12. ${3} | ${'three'}
  13. ${4} | ${'four'}
  14. ${5} | ${'five'}
  15. ${6} | ${'six'}
  16. ${7} | ${'seven'}
  17. ${8} | ${'eight'}
  18. ${9} | ${'nine'}
  19. `('converts $ones to $expected', ({ ones, expected }) => {
  20. expect(stringify(ones, options)).toBe(expected)
  21. expect(parse(expected, { ...options, type: 'number' })).toBe(ones);
  22. });
  23. });
  24. describe('10-19', () => {
  25. it.each`
  26. tenPlusOnes | expected
  27. ${10} | ${'ten'}
  28. ${11} | ${'eleven'}
  29. ${12} | ${'twelve'}
  30. ${13} | ${'thirteen'}
  31. ${14} | ${'fourteen'}
  32. ${15} | ${'fifteen'}
  33. ${16} | ${'sixteen'}
  34. ${17} | ${'seventeen'}
  35. ${18} | ${'eighteen'}
  36. ${19} | ${'nineteen'}
  37. `('converts $tenPlusOnes to $expected', ({ tenPlusOnes, expected }) => {
  38. expect(stringify(tenPlusOnes, options)).toBe(expected)
  39. expect(parse(expected, { ...options, type: 'number' })).toBe(tenPlusOnes);
  40. });
  41. });
  42. describe.each`
  43. tensStart | tensEnd | tensBase
  44. ${20} | ${29} | ${'twenty'}
  45. ${30} | ${39} | ${'thirty'}
  46. ${40} | ${49} | ${'forty'}
  47. ${50} | ${59} | ${'fifty'}
  48. ${60} | ${69} | ${'sixty'}
  49. ${70} | ${79} | ${'seventy'}
  50. ${80} | ${89} | ${'eighty'}
  51. ${90} | ${99} | ${'ninety'}
  52. `('$tensStart-$tensEnd', ({
  53. tensStart, tensBase,
  54. }) => {
  55. it.each`
  56. value | expected
  57. ${tensStart + 0} | ${tensBase}
  58. ${tensStart + 1} | ${`${tensBase} one`}
  59. ${tensStart + 2} | ${`${tensBase} two`}
  60. ${tensStart + 3} | ${`${tensBase} three`}
  61. ${tensStart + 4} | ${`${tensBase} four`}
  62. ${tensStart + 5} | ${`${tensBase} five`}
  63. ${tensStart + 6} | ${`${tensBase} six`}
  64. ${tensStart + 7} | ${`${tensBase} seven`}
  65. ${tensStart + 8} | ${`${tensBase} eight`}
  66. ${tensStart + 9} | ${`${tensBase} nine`}
  67. `('converts $value to $expected', ({ value, expected }) => {
  68. expect(stringify(value, options)).toBe(expected);
  69. expect(parse(expected, { ...options, type: 'number' })).toBe(value);
  70. });
  71. });
  72. describe.each`
  73. hundredsStart | hundredsEnd | hundredsBase
  74. ${100} | ${199} | ${'one hundred'}
  75. ${200} | ${299} | ${'two hundred'}
  76. ${300} | ${399} | ${'three hundred'}
  77. ${400} | ${499} | ${'four hundred'}
  78. ${500} | ${599} | ${'five hundred'}
  79. ${600} | ${699} | ${'six hundred'}
  80. ${700} | ${799} | ${'seven hundred'}
  81. ${800} | ${899} | ${'eight hundred'}
  82. ${900} | ${999} | ${'nine hundred'}
  83. `('$hundredsStart-$hundredsEnd', ({
  84. hundredsStart, hundredsBase,
  85. }) => {
  86. describe(`${hundredsStart}-${hundredsStart + 9}`, () => {
  87. it.each`
  88. value | expected
  89. ${hundredsStart + 0} | ${hundredsBase}
  90. ${hundredsStart + 1} | ${`${hundredsBase} one`}
  91. ${hundredsStart + 2} | ${`${hundredsBase} two`}
  92. ${hundredsStart + 3} | ${`${hundredsBase} three`}
  93. ${hundredsStart + 4} | ${`${hundredsBase} four`}
  94. ${hundredsStart + 5} | ${`${hundredsBase} five`}
  95. ${hundredsStart + 6} | ${`${hundredsBase} six`}
  96. ${hundredsStart + 7} | ${`${hundredsBase} seven`}
  97. ${hundredsStart + 8} | ${`${hundredsBase} eight`}
  98. ${hundredsStart + 9} | ${`${hundredsBase} nine`}
  99. `('converts $value to $expected', ({ value, expected }) => {
  100. expect(stringify(value, options)).toBe(expected)
  101. expect(parse(expected, { ...options, type: 'number' })).toBe(value)
  102. });
  103. });
  104. describe(`${hundredsStart + 10}-${hundredsStart + 19}`, () => {
  105. it.each`
  106. value | expected
  107. ${hundredsStart + 10} | ${`${hundredsBase} ten`}
  108. ${hundredsStart + 11} | ${`${hundredsBase} eleven`}
  109. ${hundredsStart + 12} | ${`${hundredsBase} twelve`}
  110. ${hundredsStart + 13} | ${`${hundredsBase} thirteen`}
  111. ${hundredsStart + 14} | ${`${hundredsBase} fourteen`}
  112. ${hundredsStart + 15} | ${`${hundredsBase} fifteen`}
  113. ${hundredsStart + 16} | ${`${hundredsBase} sixteen`}
  114. ${hundredsStart + 17} | ${`${hundredsBase} seventeen`}
  115. ${hundredsStart + 18} | ${`${hundredsBase} eighteen`}
  116. ${hundredsStart + 19} | ${`${hundredsBase} nineteen`}
  117. `('converts $value to $expected', ({ value, expected }) => {
  118. expect(stringify(value, options)).toBe(expected)
  119. expect(parse(expected, { ...options, type: 'number' })).toBe(value)
  120. });
  121. });
  122. describe.each`
  123. start | end | base
  124. ${20} | ${29} | ${'twenty'}
  125. ${30} | ${39} | ${'thirty'}
  126. ${40} | ${49} | ${'forty'}
  127. ${50} | ${59} | ${'fifty'}
  128. ${60} | ${69} | ${'sixty'}
  129. ${70} | ${79} | ${'seventy'}
  130. ${80} | ${89} | ${'eighty'}
  131. ${90} | ${99} | ${'ninety'}
  132. `('$start-$end', ({
  133. start, base,
  134. }) => {
  135. it.each`
  136. value | expected
  137. ${hundredsStart + start + 0} | ${`${hundredsBase} ${base}`}
  138. ${hundredsStart + start + 1} | ${`${hundredsBase} ${base} one`}
  139. ${hundredsStart + start + 2} | ${`${hundredsBase} ${base} two`}
  140. ${hundredsStart + start + 3} | ${`${hundredsBase} ${base} three`}
  141. ${hundredsStart + start + 4} | ${`${hundredsBase} ${base} four`}
  142. ${hundredsStart + start + 5} | ${`${hundredsBase} ${base} five`}
  143. ${hundredsStart + start + 6} | ${`${hundredsBase} ${base} six`}
  144. ${hundredsStart + start + 7} | ${`${hundredsBase} ${base} seven`}
  145. ${hundredsStart + start + 8} | ${`${hundredsBase} ${base} eight`}
  146. ${hundredsStart + start + 9} | ${`${hundredsBase} ${base} nine`}
  147. `('converts $value to $expected', ({ value, expected }) => {
  148. expect(stringify(value, options)).toBe(expected);
  149. expect(parse(expected, { ...options, type: 'number' })).toBe(value);
  150. });
  151. });
  152. });
  153. });
  154. it('converts 1000 to one thousand', () => {
  155. expect(stringify(1000, options)).toBe('one thousand');
  156. expect(parse('one thousand', { ...options, type: 'number' })).toBe(1000);
  157. });
  158. it('converts 10000 to ten thousand', () => {
  159. expect(stringify(10000, options)).toBe('ten thousand');
  160. expect(parse('ten thousand', { ...options, type: 'number' })).toBe(10000);
  161. });
  162. it('converts 100000 to one hundred thousand', () => {
  163. expect(stringify(100000, options)).toBe('one hundred thousand');
  164. expect(parse('one hundred thousand', { ...options, type: 'number' })).toBe(100000);
  165. });
  166. it('converts 123456 to one hundred twenty three thousand four hundred fifty six', () => {
  167. expect(stringify(123456, options)).toBe('one hundred twenty three thousand four hundred fifty six');
  168. expect(parse('one hundred twenty three thousand four hundred fifty six', { ...options, type: 'number' })).toBe(123456);
  169. });
  170. it.each`
  171. value | expected
  172. ${1e+6} | ${'one million'}
  173. ${1e+9} | ${'one billion'}
  174. ${1e+12} | ${'one trillion'}
  175. ${1e+15} | ${'one quadrillion'}
  176. ${1e+18} | ${'one quintillion'}
  177. ${1e+21} | ${'one sextillion'}
  178. ${1e+24} | ${'one septillion'}
  179. ${1e+27} | ${'one octillion'}
  180. ${1e+30} | ${'one nonillion'}
  181. `('converts $value to $expected', ({ value, expected }) => {
  182. expect(stringify(value, options)).toBe(expected);
  183. expect(parse(expected, { ...options, type: 'number' })).toBe(value);
  184. });
  185. it.each`
  186. value | expected
  187. ${'1e+33'} | ${'one decillion'}
  188. ${'1e+36'} | ${'one undecillion'}
  189. ${'1e+39'} | ${'one duodecillion'}
  190. ${'1e+42'} | ${'one tredecillion'}
  191. ${'1e+45'} | ${'one quattuordecillion'}
  192. ${'1e+48'} | ${'one quindecillion'}
  193. ${'1e+51'} | ${'one sexdecillion'}
  194. ${'1e+54'} | ${'one septendecillion'}
  195. ${'1e+57'} | ${'one octodecillion'}
  196. ${'1e+60'} | ${'one novemdecillion'}
  197. `('converts $value to $expected', ({ value, expected }) => {
  198. expect(stringify(value, options)).toBe(expected);
  199. expect(parse(expected, options)).toBe(value);
  200. });
  201. it.each`
  202. value | expected
  203. ${'1e+63'} | ${'one vigintillion'}
  204. ${'1e+66'} | ${'one unvigintillion'}
  205. ${'1e+69'} | ${'one duovigintillion'}
  206. ${'1e+72'} | ${'one trevigintillion'}
  207. ${'1e+75'} | ${'one quattuorvigintillion'}
  208. ${'1e+78'} | ${'one quinvigintillion'}
  209. ${'1e+81'} | ${'one sexvigintillion'}
  210. ${'1e+84'} | ${'one septenvigintillion'}
  211. ${'1e+87'} | ${'one octovigintillion'}
  212. ${'1e+90'} | ${'one novemvigintillion'}
  213. `('converts $value to $expected', ({ value, expected }) => {
  214. expect(stringify(value, options)).toBe(expected);
  215. expect(parse(expected, options)).toBe(value);
  216. });
  217. it.each`
  218. value | expected
  219. ${'1e+93'} | ${'one trigintillion'}
  220. ${'1e+123'} | ${'one quadragintillion'}
  221. ${'1e+153'} | ${'one quinquagintillion'}
  222. ${'1e+183'} | ${'one sexagintillion'}
  223. ${'1e+213'} | ${'one septuagintillion'}
  224. ${'1e+243'} | ${'one octogintillion'}
  225. ${'1e+273'} | ${'one nonagintillion'}
  226. `('converts $value to $expected', ({ value, expected }) => {
  227. expect(stringify(value, options)).toBe(expected);
  228. expect(parse(expected, options)).toBe(value);
  229. });
  230. });