Get the name of a number, even if it's stupidly big.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

106 line
7.8 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 ($scale 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. test.each`
  18. value | name
  19. ${1000000} | ${'one million'}
  20. ${123456789} | ${'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine'}
  21. ${'1e+6'} | ${'one million'}
  22. `('should parse $value as $name', ({ value, name, }) => {
  23. expect(converter.convert(value)).toBe(name)
  24. })
  25. })
  26. describe('on streams', () => {
  27. const ENCODING = 'utf-8'
  28. const expected: Record<string, Record<Scale, string>> = {
  29. 'million': {
  30. short: 'one million',
  31. long: 'one million',
  32. europeanLong: 'one million',
  33. },
  34. 'example1': {
  35. short: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
  36. long: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
  37. europeanLong: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
  38. },
  39. 'example2': {
  40. 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',
  41. 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',
  42. 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',
  43. },
  44. 'example3': {
  45. 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',
  46. 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',
  47. 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',
  48. },
  49. 'example4': {
  50. 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',
  51. 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',
  52. 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',
  53. },
  54. 'example5': {
  55. 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',
  56. 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',
  57. 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',
  58. },
  59. 'example6': {
  60. 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',
  61. 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',
  62. 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',
  63. },
  64. 'example7': {
  65. 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',
  66. 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',
  67. 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',
  68. },
  69. }
  70. beforeAll(() => {
  71. try { fs.mkdirSync('./tests/output') } catch {}
  72. try { fs.mkdirSync(`./tests/output/${locale}`) } catch {}
  73. })
  74. test.each(Object.keys(expected))('should correctly parse %s.txt', async (filename) => {
  75. const inputPath = `./tests/input/${filename}.txt`
  76. const outputPath = `./tests/output/${locale}/${filename}.${scale}.txt`
  77. const inputStream = fs.createReadStream(inputPath, { encoding: ENCODING, })
  78. const outputStream = fs.createWriteStream(outputPath, { encoding: ENCODING, })
  79. const readStream = converter.readStream({
  80. encoding: ENCODING
  81. })
  82. inputStream
  83. .pipe(readStream)
  84. .pipe(outputStream)
  85. return new Promise((resolve) => {
  86. outputStream.on('close', () => {
  87. const output = fs.readFileSync(outputPath).toString(ENCODING)
  88. expect(output).toBe(expected[filename][scale])
  89. resolve()
  90. })
  91. })
  92. })
  93. })
  94. })