Browse Source

Organize system

Add namespace for short/long count. Also manage conversion configs and defaults.
master
TheoryOfNekomata 1 year ago
parent
commit
6c81723b78
11 changed files with 117 additions and 63 deletions
  1. +1
    -1
      packages/core/src/common.ts
  2. +8
    -4
      packages/core/src/converter.ts
  3. +6
    -6
      packages/core/src/exponent.ts
  4. +2
    -0
      packages/core/src/systems/en-US/common.ts
  5. +1
    -2
      packages/core/src/systems/en-US/index.ts
  6. +2
    -0
      packages/core/src/systems/en-US/short-count/index.ts
  7. +4
    -3
      packages/core/src/systems/en-US/short-count/parse.ts
  8. +17
    -11
      packages/core/src/systems/en-US/short-count/stringify.ts
  9. +24
    -15
      packages/core/test/systems/en-US.test.ts
  10. +40
    -21
      packages/core/test/systems/en-US/chongo.test.ts
  11. +12
    -0
      packages/example-web/index.html

+ 1
- 1
packages/core/src/common.ts View File

@@ -70,7 +70,7 @@ export interface StringifySystem {
* Finalizes a string.
* @param tokens - The tokens to finalize.
*/
finalize: (tokens: string[]) => string;
finalize: <T extends object>(tokens: string[], options?: T) => string;
/**
* Tokenizes a string.
* @param value - The string to tokenize.


+ 8
- 4
packages/core/src/converter.ts View File

@@ -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<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.
*
@@ -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.
*/
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);


+ 6
- 6
packages/core/src/exponent.ts View File

@@ -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;
}


+ 2
- 0
packages/core/src/systems/en-US/common.ts View File

@@ -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;


+ 1
- 2
packages/core/src/systems/en-US/index.ts View File

@@ -1,2 +1 @@
export * from './parse';
export * from './stringify';
export * as shortCount from './short-count';

+ 2
- 0
packages/core/src/systems/en-US/short-count/index.ts View File

@@ -0,0 +1,2 @@
export * from './parse';
export * from './stringify';

packages/core/src/systems/en-US/parse.ts → packages/core/src/systems/en-US/short-count/parse.ts View File

@@ -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)

packages/core/src/systems/en-US/stringify.ts → packages/core/src/systems/en-US/short-count/stringify.ts View File

@@ -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<Group[]>(
(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()
);


+ 24
- 15
packages/core/test/systems/en-US.test.ts View File

@@ -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);
});



+ 40
- 21
packages/core/test/systems/en-US/chongo.test.ts View File

@@ -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));
});
});


+ 12
- 0
packages/example-web/index.html View File

@@ -101,6 +101,9 @@
</label><label class="checkbox">
<input type="checkbox" name="addTensDashes" checked>
<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">
<input type="groupDigits" name="groupDigits" checked>
<span>Group digits</span>
@@ -120,6 +123,7 @@
const nameInput = el.querySelector('[name="name"]');
const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]');
const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]');
const oneGroupPerLineCheckbox = el.querySelector('[name="oneGroupPerLine"]');

const options = {
stringify: {
@@ -127,6 +131,9 @@
shortenMillia: false,
addTensDashes: true,
},
finalizeOptions: {
oneGroupPerLine: true,
},
},
parse: {
type: 'bigint',
@@ -170,6 +177,11 @@
options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked;
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');
new MainForm(mainForm);


Loading…
Cancel
Save