diff --git a/packages/core/src/common.ts b/packages/core/src/common.ts index b49fab0..c3e19aa 100644 --- a/packages/core/src/common.ts +++ b/packages/core/src/common.ts @@ -70,7 +70,7 @@ export interface StringifySystem { * Finalizes a string. * @param tokens - The tokens to finalize. */ - finalize: (tokens: string[]) => string; + finalize: (tokens: string[], options?: T) => string; /** * Tokenizes a string. * @param value - The string to tokenize. diff --git a/packages/core/src/converter.ts b/packages/core/src/converter.ts index 6e16a18..6456c66 100644 --- a/packages/core/src/converter.ts +++ b/packages/core/src/converter.ts @@ -34,7 +34,10 @@ type ParseResult = typeof ALLOWED_PARSE_RESULT_TYPES[number]; /** * Options to use when converting a value to a string. */ -export interface StringifyOptions { +export interface StringifyOptions< + TMakeGroupOptions extends object = object, + TFinalizeOptions extends object = object, +> { /** * The system to use when converting a value to a string. * @@ -45,6 +48,7 @@ export interface StringifyOptions { * Options to use when making a group. This is used to override the default options for a group. */ makeGroupOptions?: TMakeGroupOptions; + finalizeOptions?: TFinalizeOptions; } /** @@ -65,7 +69,7 @@ export const stringify = ( } const valueStr = value.toString().replace(/\s/g, ''); - const { system = enUS, makeGroupOptions } = options; + const { system = enUS.shortCount, makeGroupOptions, finalizeOptions } = options; if (valueStr.startsWith(NEGATIVE_SYMBOL)) { return system.makeNegative(stringify(valueStr.slice(NEGATIVE_SYMBOL.length), options)); @@ -76,7 +80,7 @@ export const stringify = ( groups, makeGroupOptions, ); - return system.finalize(groupNames); + return system.finalize(groupNames, finalizeOptions); }; /** @@ -102,7 +106,7 @@ export interface ParseOptions { * @returns AllowedValue The numeric equivalent of the value. */ export const parse = (value: string, options = {} as ParseOptions) => { - const { system = enUS, type = 'string' } = options; + const { system = enUS.shortCount, type = 'string' } = options; const tokens = system.tokenize(value); const groups = system.parseGroups(tokens); diff --git a/packages/core/src/exponent.ts b/packages/core/src/exponent.ts index c8b4153..0a625a1 100644 --- a/packages/core/src/exponent.ts +++ b/packages/core/src/exponent.ts @@ -129,8 +129,8 @@ export const extractExponentialComponents = ( // components. const stringValueWithDecimal = forceDecimalPoint(valueWithoutGroupingSymbols, decimalPoint); const [integerRaw, fractionalRaw] = stringValueWithDecimal.split(decimalPoint); - const integer = integerRaw.replace(/^0+/g, ''); - const fractional = fractionalRaw.replace(/0+$/g, ''); + const integer = integerRaw.replace(/^0+/, ''); + const fractional = fractionalRaw.replace(/0+$/, ''); const exponentValue = BigInt(integer.length - 1); return { integer, @@ -145,10 +145,10 @@ export const extractExponentialComponents = ( const [base, exponentRaw] = valueWithoutGroupingSymbols.split(exponentDelimiter); const [integerRaw, fractionalRaw = ''] = base.split(decimalPoint); - const integerWithoutZeroes = integerRaw.replace(/^0+/g, ''); + const integerWithoutZeroes = integerRaw.replace(/^0+/, ''); const integer = integerWithoutZeroes[0] ?? '0'; const extraIntegerDigits = integerWithoutZeroes.slice(1); - const fractional = `${extraIntegerDigits}${fractionalRaw.replace(/0+$/g, '')}`; + const fractional = `${extraIntegerDigits}${fractionalRaw.replace(/0+$/, '')}`; const exponentValue = BigInt(exponentRaw) + BigInt(extraIntegerDigits.length); return { integer, @@ -207,7 +207,7 @@ export const numberToExponential = ( } const significandInteger = significantDigits[0]; - const significandFractional = significantDigits.slice(1).replace(/0+$/g, ''); + const significandFractional = significantDigits.slice(1).replace(/0+$/, ''); if (significandFractional.length === 0) { return `${significandInteger}${exponentDelimiter}${exponent}`; @@ -250,7 +250,7 @@ export const exponentialToNumberString = ( // Error in padEnd(), shadow it. throw new RangeError('Could not represent number in string form.'); } - const newFractional = significantDigits.slice(newDecimalPointIndex).replace(/0+$/g, ''); + const newFractional = significantDigits.slice(newDecimalPointIndex).replace(/0+$/, ''); if (newFractional.length === 0) { return newInteger; } diff --git a/packages/core/src/systems/en-US/common.ts b/packages/core/src/systems/en-US/common.ts index 01af3f3..2c7a12e 100644 --- a/packages/core/src/systems/en-US/common.ts +++ b/packages/core/src/systems/en-US/common.ts @@ -1,5 +1,7 @@ import { Group } from '../../common'; +export const GROUP_SEPARATOR = ' ' as const; + export const DECIMAL_POINT = '.' as const; export const GROUPING_SYMBOL = ',' as const; diff --git a/packages/core/src/systems/en-US/index.ts b/packages/core/src/systems/en-US/index.ts index 7344573..d0b43a5 100644 --- a/packages/core/src/systems/en-US/index.ts +++ b/packages/core/src/systems/en-US/index.ts @@ -1,2 +1 @@ -export * from './parse'; -export * from './stringify'; +export * as shortCount from './short-count'; diff --git a/packages/core/src/systems/en-US/short-count/index.ts b/packages/core/src/systems/en-US/short-count/index.ts new file mode 100644 index 0000000..7344573 --- /dev/null +++ b/packages/core/src/systems/en-US/short-count/index.ts @@ -0,0 +1,2 @@ +export * from './parse'; +export * from './stringify'; diff --git a/packages/core/src/systems/en-US/parse.ts b/packages/core/src/systems/en-US/short-count/parse.ts similarity index 99% rename from packages/core/src/systems/en-US/parse.ts rename to packages/core/src/systems/en-US/short-count/parse.ts index 506f47c..d290aab 100644 --- a/packages/core/src/systems/en-US/parse.ts +++ b/packages/core/src/systems/en-US/short-count/parse.ts @@ -3,7 +3,7 @@ import { GROUP_DIGITS_INDEX, GROUP_PLACE_INDEX, InvalidTokenError, -} from '../../common'; +} from '../../../common'; import { CENTILLIONS_PREFIXES, DECILLIONS_PREFIXES, @@ -27,7 +27,7 @@ import { TENS_ONES_SEPARATOR, TensName, THOUSAND, -} from './common'; +} from '../common'; const FINAL_TOKEN = '' as const; @@ -35,7 +35,8 @@ export const tokenize = (stringValue: string) => ( stringValue .toLowerCase() .trim() - .replace(/\s+/, ' ') + .replace(/\n+/gs, ' ') + .replace(/\s+/g, ' ') .replace(new RegExp(`${TENS_ONES_SEPARATOR}`, 'g'), ' ') .split(' ') .filter((maybeToken) => maybeToken.length > 0) diff --git a/packages/core/src/systems/en-US/stringify.ts b/packages/core/src/systems/en-US/short-count/stringify.ts similarity index 91% rename from packages/core/src/systems/en-US/stringify.ts rename to packages/core/src/systems/en-US/short-count/stringify.ts index 3d102b5..0fc4da4 100644 --- a/packages/core/src/systems/en-US/stringify.ts +++ b/packages/core/src/systems/en-US/short-count/stringify.ts @@ -3,8 +3,8 @@ import { GROUP_DIGITS_INDEX, GROUP_PLACE_INDEX, GroupPlace, -} from '../../common'; -import { numberToExponential } from '../../exponent'; +} from '../../../common'; +import { numberToExponential } from '../../../exponent'; import { CENTILLIONS_PREFIXES, CentillionsPrefix, @@ -12,7 +12,7 @@ import { DecillionsPrefix, DECIMAL_POINT, EMPTY_GROUP_DIGITS, - EXPONENT_DELIMITER, + EXPONENT_DELIMITER, GROUP_SEPARATOR, GROUPING_SYMBOL, HUNDRED, ILLION_SUFFIX, @@ -20,7 +20,8 @@ import { MILLIONS_PREFIXES, MILLIONS_SPECIAL_PREFIXES, MillionsPrefix, - MillionsSpecialPrefix, NEGATIVE, + MillionsSpecialPrefix, + NEGATIVE, ONES, OnesName, SHORT_MILLIA_DELIMITER, @@ -30,7 +31,7 @@ import { TENS_ONES_SEPARATOR, TensName, THOUSAND, -} from './common'; +} from '../common'; /** * Builds a name for numbers in tens and ones. @@ -39,7 +40,7 @@ import { * @param addTensDashes - Whether to add dashes between the tens and ones. * @returns string The name for the number. */ -const makeTensName = (tens: number, ones: number, addTensDashes = false) => { +const makeTensName = (tens: number, ones: number, addTensDashes: boolean) => { if (tens === 0) { return ONES[ones]; } @@ -63,7 +64,7 @@ const makeTensName = (tens: number, ones: number, addTensDashes = false) => { * @param addTensDashes - Whether to add dashes between the tens and ones. * @returns string The name for the number. */ -const makeHundredsName = (hundreds: number, tens: number, ones: number, addTensDashes = false) => { +const makeHundredsName = (hundreds: number, tens: number, ones: number, addTensDashes: boolean) => { if (hundreds === 0) { return makeTensName(tens, ones, addTensDashes); } @@ -226,7 +227,7 @@ export const makeGroups = (groups: Group[], options?: MakeGroupsOptions): string const groupDigitsName = makeHundredsName( ...makeHundredsArgs, - options?.addTensDashes ?? false, + options?.addTensDashes ?? true, ); const groupName = getGroupName(place, options?.shortenMillia ?? false); if (groupName.length > 0) { @@ -252,7 +253,7 @@ export const group = (value: string): Group[] => { ) .split(EXPONENT_DELIMITER); const exponent = Number(exponentString); - const significantDigits = significand.replace(DECIMAL_POINT, ''); + const significantDigits = significand.replace(new RegExp(`\\${DECIMAL_POINT}`, 'g'), ''); return significantDigits.split('').reduce( (acc, c, i) => { const currentPlace = BigInt(Math.floor((exponent - i) / 3)); @@ -272,14 +273,19 @@ export const group = (value: string): Group[] => { ); }; +export interface FinalizeOptions { + oneGroupPerLine?: boolean; +} + /** * Formats the final tokenized string. * @param tokens - The tokens to finalize. + * @param options - The options to use. */ -export const finalize = (tokens: string[]) => ( +export const finalize = (tokens: string[], options?: FinalizeOptions) => ( tokens .map((t) => t.trim()) - .join(' ') + .join(options?.oneGroupPerLine ? '\n' : GROUP_SEPARATOR) .trim() ); diff --git a/packages/core/test/systems/en-US.test.ts b/packages/core/test/systems/en-US.test.ts index e01d6ab..e43276f 100644 --- a/packages/core/test/systems/en-US.test.ts +++ b/packages/core/test/systems/en-US.test.ts @@ -2,7 +2,16 @@ import { describe, it, expect } from 'vitest'; import { parse, stringify, systems } from '../../src'; import { numberToExponential } from '../../src/exponent'; -const options = { system: systems.enUS }; +const options = { + system: systems.enUS.shortCount, +}; + +const stringifyOptions = { + ...options, + makeGroupOptions: { + addTensDashes: false, + }, +}; describe('numerica', () => { describe('group names', () => { @@ -20,7 +29,7 @@ describe('numerica', () => { ${8} | ${'eight'} ${9} | ${'nine'} `('converts $ones to $expected', ({ ones, expected }: { ones: number, expected: string }) => { - expect(stringify(ones, options)).toBe(expected); + expect(stringify(ones, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(ones); }); }); @@ -39,7 +48,7 @@ describe('numerica', () => { ${18} | ${'eighteen'} ${19} | ${'nineteen'} `('converts $tenPlusOnes to $expected', ({ tenPlusOnes, expected }: { tenPlusOnes: number, expected: string }) => { - expect(stringify(tenPlusOnes, options)).toBe(expected); + expect(stringify(tenPlusOnes, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(tenPlusOnes); }); }); @@ -70,7 +79,7 @@ describe('numerica', () => { ${tensStart + 8} | ${`${tensBase} eight`} ${tensStart + 9} | ${`${tensBase} nine`} `('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(value); }); }); @@ -103,7 +112,7 @@ describe('numerica', () => { ${hundredsStart + 8} | ${`${hundredsBase} eight`} ${hundredsStart + 9} | ${`${hundredsBase} nine`} `('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(value); }); }); @@ -122,7 +131,7 @@ describe('numerica', () => { ${hundredsStart + 18} | ${`${hundredsBase} eighteen`} ${hundredsStart + 19} | ${`${hundredsBase} nineteen`} `('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(value); }); }); @@ -153,7 +162,7 @@ describe('numerica', () => { ${hundredsStart + start + 8} | ${`${hundredsBase} ${base} eight`} ${hundredsStart + start + 9} | ${`${hundredsBase} ${base} nine`} `('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(value); }); }); @@ -161,22 +170,22 @@ describe('numerica', () => { }); it('converts 1000 to one thousand', () => { - expect(stringify(1000, options)).toBe('one thousand'); + expect(stringify(1000, stringifyOptions)).toBe('one thousand'); expect(parse('one thousand', { ...options, type: 'number' })).toBe(1000); }); it('converts 10000 to ten thousand', () => { - expect(stringify(10000, options)).toBe('ten thousand'); + expect(stringify(10000, stringifyOptions)).toBe('ten thousand'); expect(parse('ten thousand', { ...options, type: 'number' })).toBe(10000); }); it('converts 100000 to one hundred thousand', () => { - expect(stringify(100000, options)).toBe('one hundred thousand'); + expect(stringify(100000, stringifyOptions)).toBe('one hundred thousand'); expect(parse('one hundred thousand', { ...options, type: 'number' })).toBe(100000); }); it('converts 123456 to one hundred twenty three thousand four hundred fifty six', () => { - expect(stringify(123456, options)).toBe('one hundred twenty three thousand four hundred fifty six'); + expect(stringify(123456, stringifyOptions)).toBe('one hundred twenty three thousand four hundred fifty six'); expect(parse('one hundred twenty three thousand four hundred fifty six', { ...options, type: 'number' })).toBe(123456); }); @@ -192,7 +201,7 @@ describe('numerica', () => { ${1e+27} | ${'one octillion'} ${1e+30} | ${'one nonillion'} `('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, { ...options, type: 'number' })).toBe(value); }); @@ -209,7 +218,7 @@ describe('numerica', () => { ${'1e+57'} | ${'one octodecillion'} ${'1e+60'} | ${'one novemdecillion'} `('converts $value to $expected', ({ value, expected }: { value: string, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, options)).toBe(value); }); @@ -226,7 +235,7 @@ describe('numerica', () => { ${'1e+87'} | ${'one octovigintillion'} ${'1e+90'} | ${'one novemvigintillion'} `('converts $value to $expected', ({ value, expected }: { value: string, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, options)).toBe(value); }); @@ -240,7 +249,7 @@ describe('numerica', () => { ${'1e+243'} | ${'one octogintillion'} ${'1e+273'} | ${'one nonagintillion'} `('converts $value to $expected', ({ value, expected }: { value: string, expected: string }) => { - expect(stringify(value, options)).toBe(expected); + expect(stringify(value, stringifyOptions)).toBe(expected); expect(parse(expected, options)).toBe(value); }); diff --git a/packages/core/test/systems/en-US/chongo.test.ts b/packages/core/test/systems/en-US/chongo.test.ts index 093ef8c..0d7e505 100644 --- a/packages/core/test/systems/en-US/chongo.test.ts +++ b/packages/core/test/systems/en-US/chongo.test.ts @@ -2,6 +2,12 @@ import { describe, it, expect } from 'vitest'; import { stringify, parse } from '../../../src'; import { numberToExponential } from '../../../src/exponent'; +const stringifyOptions = { + makeGroupOptions: { + addTensDashes: false, + }, +}; + describe('Landon\'s original test cases', () => { describe('Basic conversions', () => { it.each` @@ -13,15 +19,15 @@ describe('Landon\'s original test cases', () => { ${1000000000000} | ${'one trillion'} ${1000000000000000} | ${'one quadrillion'} ${1000000000000000000} | ${'one quintillion'} - `('converts $value to $americanName', ({value, americanName}) => { - expect(stringify(value)).toBe(americanName); + `('converts $value to $americanName', ({ value, americanName }: { value: number, americanName: string }) => { + expect(stringify(value, stringifyOptions)).toBe(americanName); expect(parse(americanName, { type: 'number' })).toBe(value); }); it( 'converts 987654321 to nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one', () => { - expect(stringify(987654321)) + expect(stringify(987654321, stringifyOptions)) .toBe('nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one'); }, ); @@ -29,9 +35,10 @@ describe('Landon\'s original test cases', () => { it( 'converts 123456789246801357 to 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', () => { - expect(stringify('123456789246801357')) + expect(stringify('123456789246801357', stringifyOptions)) .toBe( - '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'); + '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', + ); }, ); @@ -39,9 +46,12 @@ describe('Landon\'s original test cases', () => { 'converts 123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 to 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', () => { expect(stringify( - '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) + '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', + stringifyOptions, + )) .toBe( - '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'); + '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', + ); }, ); @@ -49,9 +59,12 @@ describe('Landon\'s original test cases', () => { 'converts 123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 to 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', () => { expect(stringify( - '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) + '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', + stringifyOptions, + )) .toBe( - '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'); + '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', + ); }, ); @@ -59,9 +72,12 @@ describe('Landon\'s original test cases', () => { 'converts 123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 to 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', () => { expect(stringify( - '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) + '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', + stringifyOptions, + )) .toBe( - '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'); + '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', + ); }, ); }); @@ -89,8 +105,8 @@ describe('Landon\'s original test cases', () => { ${'1e+57'} | ${'octodecillion'} ${'1e+60'} | ${'novemdecillion'} ${'1e+63'} | ${'vigintillion'} - `('converts $value to $americanName', ({value, americanName}) => { - expect(stringify(value)).toBe(`one ${americanName}`); + `('converts $value to $americanName', ({ value, americanName }: { value: string, americanName: string }) => { + expect(stringify(value, stringifyOptions)).toBe(`one ${americanName}`); expect(parse(`one ${americanName}`)).toBe(value); }); }); @@ -119,8 +135,8 @@ describe('Landon\'s original test cases', () => { ${'1e+243'} | ${'octogintillion'} ${'1e+273'} | ${'nonagintillion'} ${'1e+300'} | ${'novemnonagintillion'} - `('converts $value to $americanName', ({value, americanName}) => { - expect(stringify(value)).toBe(`one ${americanName}`); + `('converts $value to $americanName', ({ value, americanName }: { value: string, americanName: string }) => { + expect(stringify(value, stringifyOptions)).toBe(`one ${americanName}`); expect(parse(`one ${americanName}`)).toBe(value); }); }); @@ -146,8 +162,8 @@ describe('Landon\'s original test cases', () => { ${'1e+2103'} | ${'septingentillion'} ${'1e+2403'} | ${'octingentillion'} ${'1e+2703'} | ${'nongentillion'} - `('converts $value to $americanName', ({value, americanName}) => { - expect(stringify(value)).toBe(`one ${americanName}`); + `('converts $value to $americanName', ({ value, americanName }: { value: string, americanName: string }) => { + expect(stringify(value, stringifyOptions)).toBe(`one ${americanName}`); expect(parse(`one ${americanName}`)).toBe(value); }); }); @@ -157,9 +173,12 @@ describe('Landon\'s original test cases', () => { 'converts 123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 to 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', () => { expect(stringify( - '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) + '123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', + stringifyOptions, + )) .toBe( - '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'); + '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', + ); }, ); @@ -176,8 +195,8 @@ describe('Landon\'s original test cases', () => { ${'1e+1174743648579'} | ${'one trecenunnonaginmilliamilliamilliaquingenunoctoginmilliamilliaduocensexdecmilliacenduononagintillion'} ${'1e+3000000000003'} | ${'one milliamilliamilliamilliatillion'} ${'1e+696276510359811'} | ${'one duocenduotriginmilliamilliamilliamilliaduononaginmilliamilliamilliacenseptuaginmilliamilliacennovemdecmillianongensextrigintillion'} - `('converts $value to $americanName', ({value, americanName}) => { - expect(stringify(value)).toBe(americanName); + `('converts $value to $americanName', ({ value, americanName }: { value: string, americanName: string }) => { + expect(stringify(value, stringifyOptions)).toBe(americanName); expect(parse(americanName)).toBe(numberToExponential(value)); }); }); diff --git a/packages/example-web/index.html b/packages/example-web/index.html index 24277ac..f9ac1a8 100644 --- a/packages/example-web/index.html +++ b/packages/example-web/index.html @@ -101,6 +101,9 @@