|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- import { describe, it, expect } from 'vitest';
- import {
- AllowedValue,
- parse,
- stringify,
- systems,
- } from '../../../src';
-
- const options = {
- system: systems.enUK.longCount,
- };
-
- const doExpect = (
- value: AllowedValue,
- stringified: string,
- parsedValue: AllowedValue = value,
- ) => {
- const stringifyOptions = {
- ...options,
- };
-
- const parseOptions = {
- ...options,
- type: typeof value as ('number' | 'string' | 'bigint'),
- };
-
- expect(stringify(value, stringifyOptions)).toBe(stringified);
- expect(parse(stringified, parseOptions)).toBe(parsedValue);
- };
-
- describe('British long count', () => {
- it('returns empty string for empty input', () => {
- expect(parse('')).toBe('');
- });
-
- it('formats doubly negative numbers to be positive', () => {
- expect(stringify('--1')).toBe('one');
- expect(stringify('----1')).toBe('one');
- });
-
- describe('error handling', () => {
- it('throws an error when given fully unparseable input', () => {
- expect(() => parse('foo')).toThrow();
- });
-
- it('throws an error when given partially unparseable input', () => {
- expect(() => parse('one milliamilliafootillion')).toThrow();
- expect(() => parse('one millia^foo-tillion')).toThrow();
- expect(() => parse('one millia^0-tillion')).toThrow();
- });
- });
-
- describe('options', () => {
- it('formats one group per line', () => {
- expect(stringify(1234567, { mergeTokensOptions: { oneGroupPerLine: true } })).toBe(`one million
- two hundred thirty-four thousand
- five hundred sixty-seven`);
- });
- });
-
- describe('individual cases', () => {
- const longNumberValue1 = '123,456,789,012,345,678,901,234,567,890';
- const longNumberStringified1 = [
- 'one hundred twenty-three thousand quadrillion',
- 'four hundred fifty-six quadrillion',
- 'seven hundred eighty-nine thousand trillion',
- 'twelve trillion',
- 'three hundred forty-five thousand billion',
- 'six hundred seventy-eight billion',
- 'nine hundred one thousand million',
- 'two hundred thirty-four million',
- 'five hundred sixty-seven thousand',
- 'eight hundred ninety',
- ].join(' ');
-
- const longNumberParsedValue = '1.2345678901234567890123456789e+29';
-
- it.each`
- value | stringified | parsedValue
- ${1000} | ${'one thousand'} | ${1000}
- ${10000} | ${'ten thousand'} | ${10000}
- ${12012} | ${'twelve thousand twelve'} | ${12012}
- ${12020} | ${'twelve thousand twenty'} | ${12020}
- ${100000} | ${'one hundred thousand'} | ${100000}
- ${123456} | ${'one hundred twenty-three thousand four hundred fifty-six'} | ${123456}
- ${'1000005000000'} | ${'one billion five million'} | ${'1.000005e+12'}
- ${longNumberValue1} | ${longNumberStringified1} | ${longNumberParsedValue}
- ${'1e6006'} | ${'one milliauntillion'} | ${'1e+6006'}
- `('converts $value to $stringified', ({
- value,
- stringified,
- parsedValue,
- }: { value: AllowedValue, stringified: string, parsedValue: AllowedValue }) => {
- doExpect(value, stringified, parsedValue);
- });
-
- it('converts one millia^1-tillion to 1e+3003', () => {
- expect(parse('one millia^1-tillion')).toBe('1e+3003');
- });
- });
-
- describe('blanket cases', () => {
- it('converts one millia^2-tillion', () => {
- const value = '1e+3000003';
- const stringified = 'one millia^2-tillion';
- expect(parse(stringified)).toBe(value);
- expect(stringify(value, { stringifyGroupsOptions: { shortenMillia: true } }))
- .toBe(stringified);
- });
-
- it('converts one millia^2-unmilliatillion', () => {
- const value = '1e+3003003';
- const stringified = 'one millia^2-unmilliatillion';
- expect(stringify(value, { stringifyGroupsOptions: { shortenMillia: true } }))
- .toBe(stringified);
- expect(parse(stringified)).toBe(value);
- });
-
- it('converts one milliamilliaunmilliatillion', () => {
- const value = '1e+3003003';
- const stringified = 'one milliamilliaunmilliatillion';
- expect(stringify(value)).toBe(stringified);
- expect(parse(stringified)).toBe('1e+3003003');
- });
- });
-
- describe('general', () => {
- describe('0-9', () => {
- it.each`
- value | numberName
- ${0} | ${'zero'}
- ${1} | ${'one'}
- ${2} | ${'two'}
- ${3} | ${'three'}
- ${4} | ${'four'}
- ${5} | ${'five'}
- ${6} | ${'six'}
- ${7} | ${'seven'}
- ${8} | ${'eight'}
- ${9} | ${'nine'}
- `('converts $ones to $numberName', ({ value, numberName }: { value: number, numberName: string }) => {
- doExpect(value, numberName);
- });
- });
-
- describe('10-19', () => {
- it.each`
- value | numberName
- ${10} | ${'ten'}
- ${11} | ${'eleven'}
- ${12} | ${'twelve'}
- ${13} | ${'thirteen'}
- ${14} | ${'fourteen'}
- ${15} | ${'fifteen'}
- ${16} | ${'sixteen'}
- ${17} | ${'seventeen'}
- ${18} | ${'eighteen'}
- ${19} | ${'nineteen'}
- `('converts $tenPlusOnes to $numberName', ({ value, numberName }: { value: number, numberName: string }) => {
- doExpect(value, numberName);
- });
- });
-
- describe.each`
- tensStart | tensEnd | tensBase
- ${20} | ${29} | ${'twenty'}
- ${30} | ${39} | ${'thirty'}
- ${40} | ${49} | ${'forty'}
- ${50} | ${59} | ${'fifty'}
- ${60} | ${69} | ${'sixty'}
- ${70} | ${79} | ${'seventy'}
- ${80} | ${89} | ${'eighty'}
- ${90} | ${99} | ${'ninety'}
- `('$tensStart-$tensEnd', ({
- tensStart, tensBase,
- }: { tensStart: number, tensBase: string }) => {
- it.each`
- value | numberName
- ${tensStart} | ${tensBase}
- ${tensStart + 1} | ${`${tensBase}-one`}
- ${tensStart + 2} | ${`${tensBase}-two`}
- ${tensStart + 3} | ${`${tensBase}-three`}
- ${tensStart + 4} | ${`${tensBase}-four`}
- ${tensStart + 5} | ${`${tensBase}-five`}
- ${tensStart + 6} | ${`${tensBase}-six`}
- ${tensStart + 7} | ${`${tensBase}-seven`}
- ${tensStart + 8} | ${`${tensBase}-eight`}
- ${tensStart + 9} | ${`${tensBase}-nine`}
- `('converts $value to $numberName', ({ value, numberName }: { value: number, numberName: string }) => {
- doExpect(value, numberName);
- });
- });
-
- describe.each`
- hundredsStart | hundredsEnd | hundredsBase
- ${100} | ${199} | ${'one hundred'}
- ${200} | ${299} | ${'two hundred'}
- ${300} | ${399} | ${'three hundred'}
- ${400} | ${499} | ${'four hundred'}
- ${500} | ${599} | ${'five hundred'}
- ${600} | ${699} | ${'six hundred'}
- ${700} | ${799} | ${'seven hundred'}
- ${800} | ${899} | ${'eight hundred'}
- ${900} | ${999} | ${'nine hundred'}
- `('$hundredsStart-$hundredsEnd', ({
- hundredsStart, hundredsBase,
- }: { hundredsStart: number, hundredsBase: string }) => {
- describe(`${hundredsStart}-${hundredsStart + 9}`, () => {
- it.each`
- value | numberName
- ${hundredsStart} | ${hundredsBase}
- ${hundredsStart + 1} | ${`${hundredsBase} one`}
- ${hundredsStart + 2} | ${`${hundredsBase} two`}
- ${hundredsStart + 3} | ${`${hundredsBase} three`}
- ${hundredsStart + 4} | ${`${hundredsBase} four`}
- ${hundredsStart + 5} | ${`${hundredsBase} five`}
- ${hundredsStart + 6} | ${`${hundredsBase} six`}
- ${hundredsStart + 7} | ${`${hundredsBase} seven`}
- ${hundredsStart + 8} | ${`${hundredsBase} eight`}
- ${hundredsStart + 9} | ${`${hundredsBase} nine`}
- `('converts $value to $numberName', ({ value, numberName }: { value: number, numberName: string }) => {
- doExpect(value, numberName);
- });
- });
-
- describe(`${hundredsStart + 10}-${hundredsStart + 19}`, () => {
- it.each`
- value | numberName
- ${hundredsStart + 10} | ${`${hundredsBase} ten`}
- ${hundredsStart + 11} | ${`${hundredsBase} eleven`}
- ${hundredsStart + 12} | ${`${hundredsBase} twelve`}
- ${hundredsStart + 13} | ${`${hundredsBase} thirteen`}
- ${hundredsStart + 14} | ${`${hundredsBase} fourteen`}
- ${hundredsStart + 15} | ${`${hundredsBase} fifteen`}
- ${hundredsStart + 16} | ${`${hundredsBase} sixteen`}
- ${hundredsStart + 17} | ${`${hundredsBase} seventeen`}
- ${hundredsStart + 18} | ${`${hundredsBase} eighteen`}
- ${hundredsStart + 19} | ${`${hundredsBase} nineteen`}
- `('converts $value to $numberName', ({ value, numberName }: { value: number, numberName: string }) => {
- doExpect(value, numberName);
- });
- });
-
- describe.each`
- start | end | base
- ${20} | ${29} | ${'twenty'}
- ${30} | ${39} | ${'thirty'}
- ${40} | ${49} | ${'forty'}
- ${50} | ${59} | ${'fifty'}
- ${60} | ${69} | ${'sixty'}
- ${70} | ${79} | ${'seventy'}
- ${80} | ${89} | ${'eighty'}
- ${90} | ${99} | ${'ninety'}
- `('$start-$end', ({
- start, base,
- }: { start: number, base: string }) => {
- it.each`
- value | numberName
- ${hundredsStart + start} | ${`${hundredsBase} ${base}`}
- ${hundredsStart + start + 1} | ${`${hundredsBase} ${base}-one`}
- ${hundredsStart + start + 2} | ${`${hundredsBase} ${base}-two`}
- ${hundredsStart + start + 3} | ${`${hundredsBase} ${base}-three`}
- ${hundredsStart + start + 4} | ${`${hundredsBase} ${base}-four`}
- ${hundredsStart + start + 5} | ${`${hundredsBase} ${base}-five`}
- ${hundredsStart + start + 6} | ${`${hundredsBase} ${base}-six`}
- ${hundredsStart + start + 7} | ${`${hundredsBase} ${base}-seven`}
- ${hundredsStart + start + 8} | ${`${hundredsBase} ${base}-eight`}
- ${hundredsStart + start + 9} | ${`${hundredsBase} ${base}-nine`}
- `('converts $value to $numberName', ({ value, numberName }: { value: number, numberName: string }) => {
- doExpect(value, numberName);
- });
- });
- });
- });
-
- it.each`
- value | numberName
- ${'1e+6'} | ${'one million'}
- ${'1e+9'} | ${'one thousand million'}
- ${'1e+12'} | ${'one billion'}
- ${'1e+15'} | ${'one thousand billion'}
- ${'1e+18'} | ${'one trillion'}
- ${'1e+21'} | ${'one thousand trillion'}
- ${'1e+24'} | ${'one quadrillion'}
- ${'1e+27'} | ${'one thousand quadrillion'}
- ${'1e+30'} | ${'one quintillion'}
- ${'1e+33'} | ${'one thousand quintillion'}
- ${'1e+36'} | ${'one sextillion'}
- ${'1e+39'} | ${'one thousand sextillion'}
- ${'1e+42'} | ${'one septillion'}
- ${'1e+45'} | ${'one thousand septillion'}
- ${'1e+48'} | ${'one octillion'}
- ${'1e+51'} | ${'one thousand octillion'}
- ${'1e+54'} | ${'one nonillion'}
- ${'1e+57'} | ${'one thousand nonillion'}
- `('converts $value to $numberName', ({ value, numberName }: { value: string, numberName: string }) => {
- doExpect(value, numberName);
- });
-
- it.each`
- value | numberName
- ${'1e+60'} | ${'one decillion'}
- ${'1e+66'} | ${'one undecillion'}
- ${'1e+72'} | ${'one duodecillion'}
- ${'1e+78'} | ${'one tredecillion'}
- ${'1e+84'} | ${'one quattuordecillion'}
- ${'1e+90'} | ${'one quindecillion'}
- ${'1e+96'} | ${'one sexdecillion'}
- ${'1e+102'} | ${'one septendecillion'}
- ${'1e+108'} | ${'one octodecillion'}
- ${'1e+114'} | ${'one novemdecillion'}
- `('converts $value to $numberName', ({ value, numberName }: { value: string, numberName: string }) => {
- doExpect(value, numberName);
- });
-
- it.each`
- value | numberName
- ${'1e+120'} | ${'one vigintillion'}
- ${'1e+126'} | ${'one unvigintillion'}
- ${'1e+132'} | ${'one duovigintillion'}
- ${'1e+138'} | ${'one trevigintillion'}
- ${'1e+144'} | ${'one quattuorvigintillion'}
- ${'1e+150'} | ${'one quinvigintillion'}
- ${'1e+156'} | ${'one sexvigintillion'}
- ${'1e+162'} | ${'one septenvigintillion'}
- ${'1e+168'} | ${'one octovigintillion'}
- ${'1e+174'} | ${'one novemvigintillion'}
- `('converts $value to $numberName', ({ value, numberName }: { value: string, numberName: string }) => {
- doExpect(value, numberName);
- });
-
- it.each`
- value | numberName
- ${'1e+180'} | ${'one trigintillion'}
- ${'1e+240'} | ${'one quadragintillion'}
- ${'1e+300'} | ${'one quinquagintillion'}
- ${'1e+360'} | ${'one sexagintillion'}
- ${'1e+420'} | ${'one septuagintillion'}
- ${'1e+480'} | ${'one octogintillion'}
- ${'1e+540'} | ${'one nonagintillion'}
- `('converts $value to $numberName', ({ value, numberName }: { value: string, numberName: string }) => {
- doExpect(value, numberName);
- });
-
- it.each`
- value | numberName
- ${'1e+600'} | ${'one centillion'}
- ${'1e+606'} | ${'one cenuntillion'}
- ${'1e+612'} | ${'one cenduotillion'}
- ${'1e+618'} | ${'one centretillion'}
- ${'1e+624'} | ${'one cenquattuortillion'}
- ${'1e+630'} | ${'one cenquintillion'}
- ${'1e+636'} | ${'one censextillion'}
- ${'1e+642'} | ${'one censeptentillion'}
- ${'1e+648'} | ${'one cenoctotillion'}
- ${'1e+654'} | ${'one cennovemtillion'}
- ${'1e+1200'} | ${'one duocentillion'}
- ${'1e+1800'} | ${'one trecentillion'}
- ${'1e+2400'} | ${'one quadringentillion'}
- ${'1e+3000'} | ${'one quingentillion'}
- ${'1e+3600'} | ${'one sescentillion'}
- ${'1e+4200'} | ${'one septingentillion'}
- ${'1e+4800'} | ${'one octingentillion'}
- ${'1e+5400'} | ${'one nongentillion'}
- `('converts $value to $numberName', ({ value, numberName }: { value: string, numberName: string }) => {
- doExpect(value, numberName);
- });
-
- it('converts \'1e+6000\' to \'one milliatillion\'', () => {
- doExpect('1e+6000', 'one milliatillion');
- });
- });
|