Add namespace for short/long count. Also manage conversion configs and defaults.master
@@ -70,7 +70,7 @@ export interface StringifySystem { | |||||
* Finalizes a string. | * Finalizes a string. | ||||
* @param tokens - The tokens to finalize. | * @param tokens - The tokens to finalize. | ||||
*/ | */ | ||||
finalize: (tokens: string[]) => string; | |||||
finalize: <T extends object>(tokens: string[], options?: T) => string; | |||||
/** | /** | ||||
* Tokenizes a string. | * Tokenizes a string. | ||||
* @param value - The string to tokenize. | * @param value - The string to tokenize. | ||||
@@ -34,7 +34,10 @@ type ParseResult = typeof ALLOWED_PARSE_RESULT_TYPES[number]; | |||||
/** | /** | ||||
* Options to use when converting a value to a string. | * Options to use when converting a value to a string. | ||||
*/ | */ | ||||
export interface StringifyOptions<TMakeGroupOptions extends object = object> { | |||||
export interface StringifyOptions< | |||||
TMakeGroupOptions extends object = object, | |||||
TFinalizeOptions extends object = object, | |||||
> { | |||||
/** | /** | ||||
* The system to use when converting a value to a string. | * The system to use when converting a value to a string. | ||||
* | * | ||||
@@ -45,6 +48,7 @@ export interface StringifyOptions<TMakeGroupOptions extends object = object> { | |||||
* Options to use when making a group. This is used to override the default options for a group. | * Options to use when making a group. This is used to override the default options for a group. | ||||
*/ | */ | ||||
makeGroupOptions?: TMakeGroupOptions; | makeGroupOptions?: TMakeGroupOptions; | ||||
finalizeOptions?: TFinalizeOptions; | |||||
} | } | ||||
/** | /** | ||||
@@ -65,7 +69,7 @@ export const stringify = ( | |||||
} | } | ||||
const valueStr = value.toString().replace(/\s/g, ''); | const valueStr = value.toString().replace(/\s/g, ''); | ||||
const { system = enUS, makeGroupOptions } = options; | |||||
const { system = enUS.shortCount, makeGroupOptions, finalizeOptions } = options; | |||||
if (valueStr.startsWith(NEGATIVE_SYMBOL)) { | if (valueStr.startsWith(NEGATIVE_SYMBOL)) { | ||||
return system.makeNegative(stringify(valueStr.slice(NEGATIVE_SYMBOL.length), options)); | return system.makeNegative(stringify(valueStr.slice(NEGATIVE_SYMBOL.length), options)); | ||||
@@ -76,7 +80,7 @@ export const stringify = ( | |||||
groups, | groups, | ||||
makeGroupOptions, | 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. | * @returns AllowedValue The numeric equivalent of the value. | ||||
*/ | */ | ||||
export const parse = (value: string, options = {} as ParseOptions) => { | 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 tokens = system.tokenize(value); | ||||
const groups = system.parseGroups(tokens); | const groups = system.parseGroups(tokens); | ||||
@@ -129,8 +129,8 @@ export const extractExponentialComponents = ( | |||||
// components. | // components. | ||||
const stringValueWithDecimal = forceDecimalPoint(valueWithoutGroupingSymbols, decimalPoint); | const stringValueWithDecimal = forceDecimalPoint(valueWithoutGroupingSymbols, decimalPoint); | ||||
const [integerRaw, fractionalRaw] = stringValueWithDecimal.split(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); | const exponentValue = BigInt(integer.length - 1); | ||||
return { | return { | ||||
integer, | integer, | ||||
@@ -145,10 +145,10 @@ export const extractExponentialComponents = ( | |||||
const [base, exponentRaw] = valueWithoutGroupingSymbols.split(exponentDelimiter); | const [base, exponentRaw] = valueWithoutGroupingSymbols.split(exponentDelimiter); | ||||
const [integerRaw, fractionalRaw = ''] = base.split(decimalPoint); | const [integerRaw, fractionalRaw = ''] = base.split(decimalPoint); | ||||
const integerWithoutZeroes = integerRaw.replace(/^0+/g, ''); | |||||
const integerWithoutZeroes = integerRaw.replace(/^0+/, ''); | |||||
const integer = integerWithoutZeroes[0] ?? '0'; | const integer = integerWithoutZeroes[0] ?? '0'; | ||||
const extraIntegerDigits = integerWithoutZeroes.slice(1); | 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); | const exponentValue = BigInt(exponentRaw) + BigInt(extraIntegerDigits.length); | ||||
return { | return { | ||||
integer, | integer, | ||||
@@ -207,7 +207,7 @@ export const numberToExponential = ( | |||||
} | } | ||||
const significandInteger = significantDigits[0]; | const significandInteger = significantDigits[0]; | ||||
const significandFractional = significantDigits.slice(1).replace(/0+$/g, ''); | |||||
const significandFractional = significantDigits.slice(1).replace(/0+$/, ''); | |||||
if (significandFractional.length === 0) { | if (significandFractional.length === 0) { | ||||
return `${significandInteger}${exponentDelimiter}${exponent}`; | return `${significandInteger}${exponentDelimiter}${exponent}`; | ||||
@@ -250,7 +250,7 @@ export const exponentialToNumberString = ( | |||||
// Error in padEnd(), shadow it. | // Error in padEnd(), shadow it. | ||||
throw new RangeError('Could not represent number in string form.'); | 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) { | if (newFractional.length === 0) { | ||||
return newInteger; | return newInteger; | ||||
} | } | ||||
@@ -1,5 +1,7 @@ | |||||
import { Group } from '../../common'; | import { Group } from '../../common'; | ||||
export const GROUP_SEPARATOR = ' ' as const; | |||||
export const DECIMAL_POINT = '.' as const; | export const DECIMAL_POINT = '.' as const; | ||||
export const GROUPING_SYMBOL = ',' as const; | export const GROUPING_SYMBOL = ',' as const; | ||||
@@ -1,2 +1 @@ | |||||
export * from './parse'; | |||||
export * from './stringify'; | |||||
export * as shortCount from './short-count'; |
@@ -0,0 +1,2 @@ | |||||
export * from './parse'; | |||||
export * from './stringify'; |
@@ -3,7 +3,7 @@ import { | |||||
GROUP_DIGITS_INDEX, | GROUP_DIGITS_INDEX, | ||||
GROUP_PLACE_INDEX, | GROUP_PLACE_INDEX, | ||||
InvalidTokenError, | InvalidTokenError, | ||||
} from '../../common'; | |||||
} from '../../../common'; | |||||
import { | import { | ||||
CENTILLIONS_PREFIXES, | CENTILLIONS_PREFIXES, | ||||
DECILLIONS_PREFIXES, | DECILLIONS_PREFIXES, | ||||
@@ -27,7 +27,7 @@ import { | |||||
TENS_ONES_SEPARATOR, | TENS_ONES_SEPARATOR, | ||||
TensName, | TensName, | ||||
THOUSAND, | THOUSAND, | ||||
} from './common'; | |||||
} from '../common'; | |||||
const FINAL_TOKEN = '' as const; | const FINAL_TOKEN = '' as const; | ||||
@@ -35,7 +35,8 @@ export const tokenize = (stringValue: string) => ( | |||||
stringValue | stringValue | ||||
.toLowerCase() | .toLowerCase() | ||||
.trim() | .trim() | ||||
.replace(/\s+/, ' ') | |||||
.replace(/\n+/gs, ' ') | |||||
.replace(/\s+/g, ' ') | |||||
.replace(new RegExp(`${TENS_ONES_SEPARATOR}`, 'g'), ' ') | .replace(new RegExp(`${TENS_ONES_SEPARATOR}`, 'g'), ' ') | ||||
.split(' ') | .split(' ') | ||||
.filter((maybeToken) => maybeToken.length > 0) | .filter((maybeToken) => maybeToken.length > 0) |
@@ -3,8 +3,8 @@ import { | |||||
GROUP_DIGITS_INDEX, | GROUP_DIGITS_INDEX, | ||||
GROUP_PLACE_INDEX, | GROUP_PLACE_INDEX, | ||||
GroupPlace, | GroupPlace, | ||||
} from '../../common'; | |||||
import { numberToExponential } from '../../exponent'; | |||||
} from '../../../common'; | |||||
import { numberToExponential } from '../../../exponent'; | |||||
import { | import { | ||||
CENTILLIONS_PREFIXES, | CENTILLIONS_PREFIXES, | ||||
CentillionsPrefix, | CentillionsPrefix, | ||||
@@ -12,7 +12,7 @@ import { | |||||
DecillionsPrefix, | DecillionsPrefix, | ||||
DECIMAL_POINT, | DECIMAL_POINT, | ||||
EMPTY_GROUP_DIGITS, | EMPTY_GROUP_DIGITS, | ||||
EXPONENT_DELIMITER, | |||||
EXPONENT_DELIMITER, GROUP_SEPARATOR, | |||||
GROUPING_SYMBOL, | GROUPING_SYMBOL, | ||||
HUNDRED, | HUNDRED, | ||||
ILLION_SUFFIX, | ILLION_SUFFIX, | ||||
@@ -20,7 +20,8 @@ import { | |||||
MILLIONS_PREFIXES, | MILLIONS_PREFIXES, | ||||
MILLIONS_SPECIAL_PREFIXES, | MILLIONS_SPECIAL_PREFIXES, | ||||
MillionsPrefix, | MillionsPrefix, | ||||
MillionsSpecialPrefix, NEGATIVE, | |||||
MillionsSpecialPrefix, | |||||
NEGATIVE, | |||||
ONES, | ONES, | ||||
OnesName, | OnesName, | ||||
SHORT_MILLIA_DELIMITER, | SHORT_MILLIA_DELIMITER, | ||||
@@ -30,7 +31,7 @@ import { | |||||
TENS_ONES_SEPARATOR, | TENS_ONES_SEPARATOR, | ||||
TensName, | TensName, | ||||
THOUSAND, | THOUSAND, | ||||
} from './common'; | |||||
} from '../common'; | |||||
/** | /** | ||||
* Builds a name for numbers in tens and ones. | * 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. | * @param addTensDashes - Whether to add dashes between the tens and ones. | ||||
* @returns string The name for the number. | * @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) { | if (tens === 0) { | ||||
return ONES[ones]; | 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. | * @param addTensDashes - Whether to add dashes between the tens and ones. | ||||
* @returns string The name for the number. | * @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) { | if (hundreds === 0) { | ||||
return makeTensName(tens, ones, addTensDashes); | return makeTensName(tens, ones, addTensDashes); | ||||
} | } | ||||
@@ -226,7 +227,7 @@ export const makeGroups = (groups: Group[], options?: MakeGroupsOptions): string | |||||
const groupDigitsName = makeHundredsName( | const groupDigitsName = makeHundredsName( | ||||
...makeHundredsArgs, | ...makeHundredsArgs, | ||||
options?.addTensDashes ?? false, | |||||
options?.addTensDashes ?? true, | |||||
); | ); | ||||
const groupName = getGroupName(place, options?.shortenMillia ?? false); | const groupName = getGroupName(place, options?.shortenMillia ?? false); | ||||
if (groupName.length > 0) { | if (groupName.length > 0) { | ||||
@@ -252,7 +253,7 @@ export const group = (value: string): Group[] => { | |||||
) | ) | ||||
.split(EXPONENT_DELIMITER); | .split(EXPONENT_DELIMITER); | ||||
const exponent = Number(exponentString); | const exponent = Number(exponentString); | ||||
const significantDigits = significand.replace(DECIMAL_POINT, ''); | |||||
const significantDigits = significand.replace(new RegExp(`\\${DECIMAL_POINT}`, 'g'), ''); | |||||
return significantDigits.split('').reduce<Group[]>( | return significantDigits.split('').reduce<Group[]>( | ||||
(acc, c, i) => { | (acc, c, i) => { | ||||
const currentPlace = BigInt(Math.floor((exponent - i) / 3)); | 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. | * Formats the final tokenized string. | ||||
* @param tokens - The tokens to finalize. | * @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 | tokens | ||||
.map((t) => t.trim()) | .map((t) => t.trim()) | ||||
.join(' ') | |||||
.join(options?.oneGroupPerLine ? '\n' : GROUP_SEPARATOR) | |||||
.trim() | .trim() | ||||
); | ); | ||||
@@ -2,7 +2,16 @@ import { describe, it, expect } from 'vitest'; | |||||
import { parse, stringify, systems } from '../../src'; | import { parse, stringify, systems } from '../../src'; | ||||
import { numberToExponential } from '../../src/exponent'; | 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('numerica', () => { | ||||
describe('group names', () => { | describe('group names', () => { | ||||
@@ -20,7 +29,7 @@ describe('numerica', () => { | |||||
${8} | ${'eight'} | ${8} | ${'eight'} | ||||
${9} | ${'nine'} | ${9} | ${'nine'} | ||||
`('converts $ones to $expected', ({ ones, expected }: { ones: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(ones); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -39,7 +48,7 @@ describe('numerica', () => { | |||||
${18} | ${'eighteen'} | ${18} | ${'eighteen'} | ||||
${19} | ${'nineteen'} | ${19} | ${'nineteen'} | ||||
`('converts $tenPlusOnes to $expected', ({ tenPlusOnes, expected }: { tenPlusOnes: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(tenPlusOnes); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -70,7 +79,7 @@ describe('numerica', () => { | |||||
${tensStart + 8} | ${`${tensBase} eight`} | ${tensStart + 8} | ${`${tensBase} eight`} | ||||
${tensStart + 9} | ${`${tensBase} nine`} | ${tensStart + 9} | ${`${tensBase} nine`} | ||||
`('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(value); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -103,7 +112,7 @@ describe('numerica', () => { | |||||
${hundredsStart + 8} | ${`${hundredsBase} eight`} | ${hundredsStart + 8} | ${`${hundredsBase} eight`} | ||||
${hundredsStart + 9} | ${`${hundredsBase} nine`} | ${hundredsStart + 9} | ${`${hundredsBase} nine`} | ||||
`('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(value); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -122,7 +131,7 @@ describe('numerica', () => { | |||||
${hundredsStart + 18} | ${`${hundredsBase} eighteen`} | ${hundredsStart + 18} | ${`${hundredsBase} eighteen`} | ||||
${hundredsStart + 19} | ${`${hundredsBase} nineteen`} | ${hundredsStart + 19} | ${`${hundredsBase} nineteen`} | ||||
`('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(value); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -153,7 +162,7 @@ describe('numerica', () => { | |||||
${hundredsStart + start + 8} | ${`${hundredsBase} ${base} eight`} | ${hundredsStart + start + 8} | ${`${hundredsBase} ${base} eight`} | ||||
${hundredsStart + start + 9} | ${`${hundredsBase} ${base} nine`} | ${hundredsStart + start + 9} | ${`${hundredsBase} ${base} nine`} | ||||
`('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(value); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -161,22 +170,22 @@ describe('numerica', () => { | |||||
}); | }); | ||||
it('converts 1000 to one thousand', () => { | 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); | expect(parse('one thousand', { ...options, type: 'number' })).toBe(1000); | ||||
}); | }); | ||||
it('converts 10000 to ten thousand', () => { | 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); | expect(parse('ten thousand', { ...options, type: 'number' })).toBe(10000); | ||||
}); | }); | ||||
it('converts 100000 to one hundred thousand', () => { | 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); | expect(parse('one hundred thousand', { ...options, type: 'number' })).toBe(100000); | ||||
}); | }); | ||||
it('converts 123456 to one hundred twenty three thousand four hundred fifty six', () => { | 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); | 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+27} | ${'one octillion'} | ||||
${1e+30} | ${'one nonillion'} | ${1e+30} | ${'one nonillion'} | ||||
`('converts $value to $expected', ({ value, expected }: { value: number, expected: string }) => { | `('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); | expect(parse(expected, { ...options, type: 'number' })).toBe(value); | ||||
}); | }); | ||||
@@ -209,7 +218,7 @@ describe('numerica', () => { | |||||
${'1e+57'} | ${'one octodecillion'} | ${'1e+57'} | ${'one octodecillion'} | ||||
${'1e+60'} | ${'one novemdecillion'} | ${'1e+60'} | ${'one novemdecillion'} | ||||
`('converts $value to $expected', ({ value, expected }: { value: string, expected: string }) => { | `('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); | expect(parse(expected, options)).toBe(value); | ||||
}); | }); | ||||
@@ -226,7 +235,7 @@ describe('numerica', () => { | |||||
${'1e+87'} | ${'one octovigintillion'} | ${'1e+87'} | ${'one octovigintillion'} | ||||
${'1e+90'} | ${'one novemvigintillion'} | ${'1e+90'} | ${'one novemvigintillion'} | ||||
`('converts $value to $expected', ({ value, expected }: { value: string, expected: string }) => { | `('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); | expect(parse(expected, options)).toBe(value); | ||||
}); | }); | ||||
@@ -240,7 +249,7 @@ describe('numerica', () => { | |||||
${'1e+243'} | ${'one octogintillion'} | ${'1e+243'} | ${'one octogintillion'} | ||||
${'1e+273'} | ${'one nonagintillion'} | ${'1e+273'} | ${'one nonagintillion'} | ||||
`('converts $value to $expected', ({ value, expected }: { value: string, expected: string }) => { | `('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); | expect(parse(expected, options)).toBe(value); | ||||
}); | }); | ||||
@@ -2,6 +2,12 @@ import { describe, it, expect } from 'vitest'; | |||||
import { stringify, parse } from '../../../src'; | import { stringify, parse } from '../../../src'; | ||||
import { numberToExponential } from '../../../src/exponent'; | import { numberToExponential } from '../../../src/exponent'; | ||||
const stringifyOptions = { | |||||
makeGroupOptions: { | |||||
addTensDashes: false, | |||||
}, | |||||
}; | |||||
describe('Landon\'s original test cases', () => { | describe('Landon\'s original test cases', () => { | ||||
describe('Basic conversions', () => { | describe('Basic conversions', () => { | ||||
it.each` | it.each` | ||||
@@ -13,15 +19,15 @@ describe('Landon\'s original test cases', () => { | |||||
${1000000000000} | ${'one trillion'} | ${1000000000000} | ${'one trillion'} | ||||
${1000000000000000} | ${'one quadrillion'} | ${1000000000000000} | ${'one quadrillion'} | ||||
${1000000000000000000} | ${'one quintillion'} | ${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); | expect(parse(americanName, { type: 'number' })).toBe(value); | ||||
}); | }); | ||||
it( | it( | ||||
'converts 987654321 to nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one', | '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'); | .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( | 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', | '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( | .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', | '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( | expect(stringify( | ||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) | |||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', | |||||
stringifyOptions, | |||||
)) | |||||
.toBe( | .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', | '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( | expect(stringify( | ||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) | |||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', | |||||
stringifyOptions, | |||||
)) | |||||
.toBe( | .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', | '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( | expect(stringify( | ||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) | |||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', | |||||
stringifyOptions, | |||||
)) | |||||
.toBe( | .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+57'} | ${'octodecillion'} | ||||
${'1e+60'} | ${'novemdecillion'} | ${'1e+60'} | ${'novemdecillion'} | ||||
${'1e+63'} | ${'vigintillion'} | ${'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); | expect(parse(`one ${americanName}`)).toBe(value); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -119,8 +135,8 @@ describe('Landon\'s original test cases', () => { | |||||
${'1e+243'} | ${'octogintillion'} | ${'1e+243'} | ${'octogintillion'} | ||||
${'1e+273'} | ${'nonagintillion'} | ${'1e+273'} | ${'nonagintillion'} | ||||
${'1e+300'} | ${'novemnonagintillion'} | ${'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); | expect(parse(`one ${americanName}`)).toBe(value); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -146,8 +162,8 @@ describe('Landon\'s original test cases', () => { | |||||
${'1e+2103'} | ${'septingentillion'} | ${'1e+2103'} | ${'septingentillion'} | ||||
${'1e+2403'} | ${'octingentillion'} | ${'1e+2403'} | ${'octingentillion'} | ||||
${'1e+2703'} | ${'nongentillion'} | ${'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); | 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', | '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( | expect(stringify( | ||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) | |||||
'123456789246801357000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', | |||||
stringifyOptions, | |||||
)) | |||||
.toBe( | .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+1174743648579'} | ${'one trecenunnonaginmilliamilliamilliaquingenunoctoginmilliamilliaduocensexdecmilliacenduononagintillion'} | ||||
${'1e+3000000000003'} | ${'one milliamilliamilliamilliatillion'} | ${'1e+3000000000003'} | ${'one milliamilliamilliamilliatillion'} | ||||
${'1e+696276510359811'} | ${'one duocenduotriginmilliamilliamilliamilliaduononaginmilliamilliamilliacenseptuaginmilliamilliacennovemdecmillianongensextrigintillion'} | ${'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)); | expect(parse(americanName)).toBe(numberToExponential(value)); | ||||
}); | }); | ||||
}); | }); | ||||
@@ -101,6 +101,9 @@ | |||||
</label><label class="checkbox"> | </label><label class="checkbox"> | ||||
<input type="checkbox" name="addTensDashes" checked> | <input type="checkbox" name="addTensDashes" checked> | ||||
<span>Add dashes to tens</span> | <span>Add dashes to tens</span> | ||||
</label><label class="checkbox"> | |||||
<input type="checkbox" name="oneGroupPerLine" checked> | |||||
<span>One group per line</span> | |||||
</label><!--<label class="checkbox"> | </label><!--<label class="checkbox"> | ||||
<input type="groupDigits" name="groupDigits" checked> | <input type="groupDigits" name="groupDigits" checked> | ||||
<span>Group digits</span> | <span>Group digits</span> | ||||
@@ -120,6 +123,7 @@ | |||||
const nameInput = el.querySelector('[name="name"]'); | const nameInput = el.querySelector('[name="name"]'); | ||||
const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]'); | const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]'); | ||||
const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]'); | const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]'); | ||||
const oneGroupPerLineCheckbox = el.querySelector('[name="oneGroupPerLine"]'); | |||||
const options = { | const options = { | ||||
stringify: { | stringify: { | ||||
@@ -127,6 +131,9 @@ | |||||
shortenMillia: false, | shortenMillia: false, | ||||
addTensDashes: true, | addTensDashes: true, | ||||
}, | }, | ||||
finalizeOptions: { | |||||
oneGroupPerLine: true, | |||||
}, | |||||
}, | }, | ||||
parse: { | parse: { | ||||
type: 'bigint', | type: 'bigint', | ||||
@@ -170,6 +177,11 @@ | |||||
options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked; | options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked; | ||||
numberInput.dispatchEvent(new Event('input')); | numberInput.dispatchEvent(new Event('input')); | ||||
}); | }); | ||||
oneGroupPerLineCheckbox.addEventListener('change', (e) => { | |||||
options.stringify.finalizeOptions.oneGroupPerLine = e.currentTarget.checked; | |||||
numberInput.dispatchEvent(new Event('input')); | |||||
}); | |||||
} | } | ||||
const mainForm = window.document.getElementById('mainForm'); | const mainForm = window.document.getElementById('mainForm'); | ||||
new MainForm(mainForm); | new MainForm(mainForm); | ||||