|
- import fs from 'fs'
- import Converter from '../src'
- import English from '../src/systems/en'
- import { Scale } from '../src/common'
-
- describe.each`
- locale | name | system | scaleRaw
- ${'en'} | ${'English'} | ${English} | ${'short'}
- ${'en'} | ${'English'} | ${English} | ${'long'}
- ${'en'} | ${'English'} | ${English} | ${'europeanLong'}
- `('$name ($scaleRaw count)', ({ locale, system, scaleRaw, }) => {
- const scale = scaleRaw as Scale
- let converter: Converter
-
- beforeAll(() => {
- converter = new Converter(system, { scale, })
- })
-
- describe('on values', () => {
- const expected = [
- {
- value: 1000000,
- conversions: {
- short: 'one million',
- long: 'one million',
- europeanLong: 'one million',
- },
- },
- {
- value: 123456789,
- conversions: {
- short: 'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine',
- long: 'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine',
- europeanLong: 'one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine',
- },
- },
- {
- value: '1e+6',
- conversions: {
- short: 'one million',
- long: 'one million',
- europeanLong: 'one million',
- },
- },
- {
- value: '123e+3000003',
- conversions: {
- short: 'one hundred twenty three milliamilliatillion',
- long: 'one hundred twenty three thousand quingenmilliatillion',
- europeanLong: 'one hundred twenty three quingenmilliatilliard',
- },
- },
- {
- value: '123e+3003003',
- conversions: {
- short: 'one hundred twenty three milliamilliaunmilliatillion',
- long: 'one hundred twenty three thousand quingenmilliaquingentillion',
- europeanLong: 'one hundred twenty three quingenmilliaquingentilliard',
- },
- },
- ]
- test.each`
- value | conversions
- ${expected[0].value} | ${expected[0].conversions}
- ${expected[1].value} | ${expected[1].conversions}
- ${expected[2].value} | ${expected[2].conversions}
- ${expected[3].value} | ${expected[3].conversions}
- ${expected[4].value} | ${expected[4].conversions}
- `('should parse $value', ({ value, conversions, }) => {
- expect(converter.convert(value)).toBe(conversions[scale])
- })
- })
-
- describe('on streams', () => {
- const ENCODING = 'utf8'
-
- const expected: Record<string, Record<Scale, string>> = {
- 'million': {
- short: 'one million',
- long: 'one million',
- europeanLong: 'one million',
- },
- 'example1': {
- short: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
- long: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
- europeanLong: 'nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one',
- },
- 'example2': {
- 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',
- 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',
- 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',
- },
- 'example3': {
- 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',
- 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',
- 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',
- },
- 'example4': {
- 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',
- 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',
- 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',
- },
- 'example5': {
- 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',
- 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',
- 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',
- },
- 'example6': {
- 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',
- 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',
- 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',
- },
- 'example7': {
- 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',
- 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',
- 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',
- },
- 'example8': {
- 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',
- 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',
- 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',
- },
- 'example9': {
- 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',
- 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',
- 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',
- },
- 'example10': {
- short: 'one milliatillion',
- long: 'one thousand quingentillion',
- europeanLong: 'one quingentilliard',
- },
- 'example11': {
- short: 'one duotriginmilliasescenquattuorseptuagintillion',
- long: 'one thousand sexdecmilliatrecenseptentrigintillion',
- europeanLong: 'one sexdecmilliatrecenseptentrigintilliard',
- },
- 'example12': {
- short: 'one milliamilliatillion',
- long: 'one thousand quingenmilliatillion',
- europeanLong: 'one quingenmilliatilliard',
- },
- 'example13': {
- short: 'one octingensexquinquaginmilliamilliaseptingensexseptuaginmilliaquingenundecillion',
- long: 'one quadringenoctoviginmilliamilliatrecenoctooctoginmilliaduocensexquinquagintillion',
- europeanLong: 'one quadringenoctoviginmilliamilliatrecenoctooctoginmilliaduocensexquinquagintillion',
- },
- 'example14': {
- short: 'one milliamilliamilliatillion',
- long: 'one thousand quingenmilliamilliatillion',
- europeanLong: 'one quingenmilliamilliatilliard',
- },
- 'example15': {
- short: 'one trecenunnonaginmilliamilliamilliaquingenunoctoginmilliamilliaduocensexdecmilliacenduononagintillion',
- long: 'one thousand cenquinnonaginmilliamilliamilliaseptingennonaginmilliamilliasescenoctomilliasexnonagintillion',
- europeanLong: 'one cenquinnonaginmilliamilliamilliaseptingennonaginmilliamilliasescenoctomilliasexnonagintilliard',
- },
- 'example16': {
- short: 'one milliamilliamilliamilliatillion',
- long: 'one thousand quingenmilliamilliamilliatillion',
- europeanLong: 'one quingenmilliamilliamilliatilliard',
- },
- 'example17': {
- short: 'one duocenduotriginmilliamilliamilliamilliaduononaginmilliamilliamilliacenseptuaginmilliamilliacennovemdecmillianongensextrigintillion',
- long: 'one thousand censexdecmilliamilliamilliamilliasexquadraginmilliamilliamilliaquinoctoginmilliamillianovemquinquaginmillianongenoctosexagintillion',
- europeanLong: 'one censexdecmilliamilliamilliamilliasexquadraginmilliamilliamilliaquinoctoginmilliamillianovemquinquaginmillianongenoctosexagintilliard',
- },
- }
-
- beforeAll(() => {
- try { fs.mkdirSync('./tests/output') } catch {}
- try { fs.mkdirSync(`./tests/output/${locale}`) } catch {}
- })
-
- test.each(Object.keys(expected))(`should correctly parse %s.txt`, async (filename) => {
- const inputPath = `./tests/input/${filename}.txt`
- const outputPath = `./tests/output/${locale}/${filename}.${scale}.txt`
-
- const inputStream = fs.createReadStream(inputPath, { encoding: ENCODING, })
- const outputStream = fs.createWriteStream(outputPath, { encoding: ENCODING, })
-
- const readStream = converter.readStream({
- encoding: ENCODING
- })
-
- inputStream
- .pipe(readStream)
- .pipe(outputStream)
-
- return new Promise((resolve) => {
- outputStream.on('close', () => {
- const output = fs.readFileSync(outputPath).toString(ENCODING)
- expect(output).toBe(expected[filename][scale])
- resolve()
- })
- })
- })
- })
- })
|