Get the name of a number, even if it's stupidly big.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

200 行
15 KiB

  1. import fs from 'fs'
  2. import Converter from '../src'
  3. import English from '../src/systems/en'
  4. import { Scale } from '../src/common'
  5. describe.each`
  6. locale | name | system | scaleRaw
  7. ${'en'} | ${'English'} | ${English} | ${'short'}
  8. ${'en'} | ${'English'} | ${English} | ${'long'}
  9. ${'en'} | ${'English'} | ${English} | ${'europeanLong'}
  10. `('$name ($scaleRaw count)', ({ locale, system, scaleRaw, }) => {
  11. const scale = scaleRaw as Scale
  12. let converter: Converter
  13. beforeAll(() => {
  14. converter = new Converter(system, { scale, })
  15. })
  16. describe('on values', () => {
  17. const expected = [
  18. {
  19. value: 1000000,
  20. conversions: {
  21. short: 'one million',
  22. long: 'one million',
  23. europeanLong: 'one million',
  24. },
  25. },
  26. {
  27. value: 123456789,
  28. conversions: {
  29. short: 'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine',
  30. long: 'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine',
  31. europeanLong: 'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine',
  32. },
  33. },
  34. {
  35. value: '1e+6',
  36. conversions: {
  37. short: 'one million',
  38. long: 'one million',
  39. europeanLong: 'one million',
  40. },
  41. },
  42. {
  43. value: '123e+3000003',
  44. conversions: {
  45. short: 'one hundred twenty three milliamilliatillion',
  46. long: 'one hundred twenty three thousand quingenmilliatillion',
  47. europeanLong: 'one hundred twenty three quingenmilliatilliard',
  48. },
  49. },
  50. {
  51. value: '123e+3003003',
  52. conversions: {
  53. short: 'one hundred twenty three milliamilliaunmilliatillion',
  54. long: 'one hundred twenty three thousand quingenmilliaquingentillion',
  55. europeanLong: 'one hundred twenty three quingenmilliaquingentilliard',
  56. },
  57. },
  58. ]
  59. test.each`
  60. value | conversions
  61. ${expected[0].value} | ${expected[0].conversions}
  62. ${expected[1].value} | ${expected[1].conversions}
  63. ${expected[2].value} | ${expected[2].conversions}
  64. ${expected[3].value} | ${expected[3].conversions}
  65. ${expected[4].value} | ${expected[4].conversions}
  66. `('should parse $value', ({ value, conversions, }) => {
  67. expect(converter.convert(value)).toBe(conversions[scale])
  68. })
  69. })
  70. describe('on streams', () => {
  71. const ENCODING = 'utf8'
  72. const expected: Record<string, Record<Scale, string>> = {
  73. 'million': {
  74. short: 'one million',
  75. long: 'one million',
  76. europeanLong: 'one million',
  77. },
  78. 'example1': {
  79. short: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
  80. long: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
  81. europeanLong: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
  82. },
  83. 'example2': {
  84. short: 'one hundred twenty three quadrillion four hundred fifty six trillion seven hundred eighty nine billion two hundred forty six million eight hundred one thousand three hundred fifty seven',
  85. long: 'one hundred twenty three thousand billion four hundred fifty six billion seven hundred eighty nine thousand million two hundred forty six million eight hundred one thousand three hundred fifty seven',
  86. europeanLong: 'one hundred twenty three billiard four hundred fifty six billion seven hundred eighty nine milliard two hundred forty six million eight hundred one thousand three hundred fifty seven',
  87. },
  88. 'example3': {
  89. short: 'one hundred twenty three duoseptuagintillion four hundred fifty six unseptuagintillion seven hundred eighty nine septuagintillion two hundred forty six novemsexagintillion eight hundred one octosexagintillion three hundred fifty seven septensexagintillion',
  90. long: 'one hundred twenty three thousand sextrigintillion four hundred fifty six sextrigintillion seven hundred eighty nine thousand quintrigintillion two hundred forty six quintrigintillion eight hundred one thousand quattuortrigintillion three hundred fifty seven quattuortrigintillion',
  91. europeanLong: 'one hundred twenty three sextrigintilliard four hundred fifty six sextrigintillion seven hundred eighty nine quintrigintilliard two hundred forty six quintrigintillion eight hundred one quattuortrigintilliard three hundred fifty seven quattuortrigintillion',
  92. },
  93. 'example4': {
  94. short: 'one hundred twenty three cenduoseptuagintillion four hundred fifty six cenunseptuagintillion seven hundred eighty nine censeptuagintillion two hundred forty six cennovemsexagintillion eight hundred one cenoctosexagintillion three hundred fifty seven censeptensexagintillion',
  95. long: 'one hundred twenty three thousand sexoctogintillion four hundred fifty six sexoctogintillion seven hundred eighty nine thousand quinoctogintillion two hundred forty six quinoctogintillion eight hundred one thousand quattuoroctogintillion three hundred fifty seven quattuoroctogintillion',
  96. europeanLong: 'one hundred twenty three sexoctogintilliard four hundred fifty six sexoctogintillion seven hundred eighty nine quinoctogintilliard two hundred forty six quinoctogintillion eight hundred one quattuoroctogintilliard three hundred fifty seven quattuoroctogintillion',
  97. },
  98. 'example5': {
  99. short: 'one hundred twenty three trecenduoseptuagintillion four hundred fifty six trecenunseptuagintillion seven hundred eighty nine trecenseptuagintillion two hundred forty six trecennovemsexagintillion eight hundred one trecenoctosexagintillion three hundred fifty seven trecenseptensexagintillion',
  100. long: 'one hundred twenty three thousand censexoctogintillion four hundred fifty six censexoctogintillion seven hundred eighty nine thousand cenquinoctogintillion two hundred forty six cenquinoctogintillion eight hundred one thousand cenquattuoroctogintillion three hundred fifty seven cenquattuoroctogintillion',
  101. europeanLong: 'one hundred twenty three censexoctogintilliard four hundred fifty six censexoctogintillion seven hundred eighty nine cenquinoctogintilliard two hundred forty six cenquinoctogintillion eight hundred one cenquattuoroctogintilliard three hundred fifty seven cenquattuoroctogintillion',
  102. },
  103. 'example6': {
  104. short: 'one hundred twenty three milliaduoseptuagintillion four hundred fifty six milliaunseptuagintillion seven hundred eighty nine milliaseptuagintillion two hundred forty six millianovemsexagintillion eight hundred one milliaoctosexagintillion three hundred fifty seven milliaseptensexagintillion',
  105. long: 'one hundred twenty three thousand quingensextrigintillion four hundred fifty six quingensextrigintillion seven hundred eighty nine thousand quingenquintrigintillion two hundred forty six quingenquintrigintillion eight hundred one thousand quingenquattuortrigintillion three hundred fifty seven quingenquattuortrigintillion',
  106. europeanLong: 'one hundred twenty three quingensextrigintilliard four hundred fifty six quingensextrigintillion seven hundred eighty nine quingenquintrigintilliard two hundred forty six quingenquintrigintillion eight hundred one quingenquattuortrigintilliard three hundred fifty seven quingenquattuortrigintillion',
  107. },
  108. 'example7': {
  109. short: 'one hundred twenty three duomilliaduoseptuagintillion four hundred fifty six duomilliaunseptuagintillion seven hundred eighty nine duomilliaseptuagintillion two hundred forty six duomillianovemsexagintillion eight hundred one duomilliaoctosexagintillion three hundred fifty seven duomilliaseptensexagintillion',
  110. long: 'one hundred twenty three thousand milliasextrigintillion four hundred fifty six milliasextrigintillion seven hundred eighty nine thousand milliaquintrigintillion two hundred forty six milliaquintrigintillion eight hundred one thousand milliaquattuortrigintillion three hundred fifty seven milliaquattuortrigintillion',
  111. europeanLong: 'one hundred twenty three milliasextrigintilliard four hundred fifty six milliasextrigintillion seven hundred eighty nine milliaquintrigintilliard two hundred forty six milliaquintrigintillion eight hundred one milliaquattuortrigintilliard three hundred fifty seven milliaquattuortrigintillion',
  112. },
  113. 'example8': {
  114. short: 'one hundred twenty three nongenseptenoctoginmilliaduoseptuagintillion four hundred fifty six nongenseptenoctoginmilliaunseptuagintillion seven hundred eighty nine nongenseptenoctoginmilliaseptuagintillion two hundred forty six nongenseptenoctoginmillianovemsexagintillion eight hundred one nongenseptenoctoginmilliaoctosexagintillion three hundred fifty seven nongenseptenoctoginmilliaseptensexagintillion',
  115. long: 'one hundred twenty three thousand quadringentrenonaginmilliaquingensextrigintillion four hundred fifty six quadringentrenonaginmilliaquingensextrigintillion seven hundred eighty nine thousand quadringentrenonaginmilliaquingenquintrigintillion two hundred forty six quadringentrenonaginmilliaquingenquintrigintillion eight hundred one thousand quadringentrenonaginmilliaquingenquattuortrigintillion three hundred fifty seven quadringentrenonaginmilliaquingenquattuortrigintillion',
  116. europeanLong: 'one hundred twenty three quadringentrenonaginmilliaquingensextrigintilliard four hundred fifty six quadringentrenonaginmilliaquingensextrigintillion seven hundred eighty nine quadringentrenonaginmilliaquingenquintrigintilliard two hundred forty six quadringentrenonaginmilliaquingenquintrigintillion eight hundred one quadringentrenonaginmilliaquingenquattuortrigintilliard three hundred fifty seven quadringentrenonaginmilliaquingenquattuortrigintillion',
  117. },
  118. 'example9': {
  119. short: 'one hundred twenty three nongenseptenoctoginmilliamilliamilliaduoseptuagintillion four hundred fifty six nongenseptenoctoginmilliamilliamilliaunseptuagintillion seven hundred eighty nine nongenseptenoctoginmilliamilliamilliaseptuagintillion two hundred forty six nongenseptenoctoginmilliamilliamillianovemsexagintillion eight hundred one nongenseptenoctoginmilliamilliamilliaoctosexagintillion three hundred fifty seven nongenseptenoctoginmilliamilliamilliaseptensexagintillion',
  120. long: 'one hundred twenty three thousand quadringentrenonaginmilliamilliamilliaquingenmilliamilliasextrigintillion four hundred fifty six quadringentrenonaginmilliamilliamilliaquingenmilliamilliasextrigintillion seven hundred eighty nine thousand quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquintrigintillion two hundred forty six quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquintrigintillion eight hundred one thousand quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquattuortrigintillion three hundred fifty seven quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquattuortrigintillion',
  121. europeanLong: 'one hundred twenty three quadringentrenonaginmilliamilliamilliaquingenmilliamilliasextrigintilliard four hundred fifty six quadringentrenonaginmilliamilliamilliaquingenmilliamilliasextrigintillion seven hundred eighty nine quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquintrigintilliard two hundred forty six quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquintrigintillion eight hundred one quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquattuortrigintilliard three hundred fifty seven quadringentrenonaginmilliamilliamilliaquingenmilliamilliaquattuortrigintillion',
  122. },
  123. 'example10': {
  124. short: 'one milliatillion',
  125. long: 'one thousand quingentillion',
  126. europeanLong: 'one quingentilliard',
  127. },
  128. 'example11': {
  129. short: 'one duotriginmilliasescenquattuorseptuagintillion',
  130. long: 'one thousand sexdecmilliatrecenseptentrigintillion',
  131. europeanLong: 'one sexdecmilliatrecenseptentrigintilliard',
  132. },
  133. 'example12': {
  134. short: 'one milliamilliatillion',
  135. long: 'one thousand quingenmilliatillion',
  136. europeanLong: 'one quingenmilliatilliard',
  137. },
  138. 'example13': {
  139. short: 'one octingensexquinquaginmilliamilliaseptingensexseptuaginmilliaquingenundecillion',
  140. long: 'one quadringenoctoviginmilliamilliatrecenoctooctoginmilliaduocensexquinquagintillion',
  141. europeanLong: 'one quadringenoctoviginmilliamilliatrecenoctooctoginmilliaduocensexquinquagintillion',
  142. },
  143. 'example14': {
  144. short: 'one milliamilliamilliatillion',
  145. long: 'one thousand quingenmilliamilliatillion',
  146. europeanLong: 'one quingenmilliamilliatilliard',
  147. },
  148. 'example15': {
  149. short: 'one trecenunnonaginmilliamilliamilliaquingenunoctoginmilliamilliaduocensexdecmilliacenduononagintillion',
  150. long: 'one thousand cenquinnonaginmilliamilliamilliaseptingennonaginmilliamilliasescenoctomilliasexnonagintillion',
  151. europeanLong: 'one cenquinnonaginmilliamilliamilliaseptingennonaginmilliamilliasescenoctomilliasexnonagintilliard',
  152. },
  153. 'example16': {
  154. short: 'one milliamilliamilliamilliatillion',
  155. long: 'one thousand quingenmilliamilliamilliatillion',
  156. europeanLong: 'one quingenmilliamilliamilliatilliard',
  157. },
  158. 'example17': {
  159. short: 'one duocenduotriginmilliamilliamilliamilliaduononaginmilliamilliamilliacenseptuaginmilliamilliacennovemdecmillianongensextrigintillion',
  160. long: 'one thousand censexdecmilliamilliamilliamilliasexquadraginmilliamilliamilliaquinoctoginmilliamillianovemquinquaginmillianongenoctosexagintillion',
  161. europeanLong: 'one censexdecmilliamilliamilliamilliasexquadraginmilliamilliamilliaquinoctoginmilliamillianovemquinquaginmillianongenoctosexagintilliard',
  162. },
  163. }
  164. beforeAll(() => {
  165. try { fs.mkdirSync('./tests/output') } catch {}
  166. try { fs.mkdirSync(`./tests/output/${locale}`) } catch {}
  167. })
  168. test.each(Object.keys(expected))(`should correctly parse %s.txt`, async (filename) => {
  169. const inputPath = `./tests/input/${filename}.txt`
  170. const outputPath = `./tests/output/${locale}/${filename}.${scale}.txt`
  171. const inputStream = fs.createReadStream(inputPath, { encoding: ENCODING, })
  172. const outputStream = fs.createWriteStream(outputPath, { encoding: ENCODING, })
  173. const readStream = converter.readStream({
  174. encoding: ENCODING
  175. })
  176. inputStream
  177. .pipe(readStream)
  178. .pipe(outputStream)
  179. return new Promise((resolve) => {
  180. outputStream.on('close', () => {
  181. const output = fs.readFileSync(outputPath).toString(ENCODING)
  182. expect(output).toBe(expected[filename][scale])
  183. resolve()
  184. })
  185. })
  186. })
  187. })
  188. })