- import {BLANK_DIGIT, createBlankDigits, deconstructNumeric, Group, groupDigits, normalizeNumeric} from '../numeric';
- import BigNumber from 'bignumber.js';
-
- interface Config {
- hundredsLatinNames: string[],
- onesLatinNames: string[],
- onesSpecialLatinNames: string[],
- tensLatinNames: string[],
- illion: string,
- illionth: string,
- illiard?: string,
- illiardth?: string,
- millia: string,
- }
-
- const getLatinPowerGroupDigitsName = (latinRaw: string, special: boolean, config: Config) => {
- const digits = latinRaw.padStart(3, BLANK_DIGIT);
- const [hundreds, tens, ones] = digits.split('').map(s => Number(s));
- const names = [];
-
- if (hundreds > 0) {
- names.push(config.hundredsLatinNames[hundreds]);
- names.push(config.onesLatinNames[ones]);
- names.push(config.tensLatinNames[tens]);
- } else {
- if (tens > 0) {
- names.push(config.onesLatinNames[ones]);
- names.push(config.tensLatinNames[tens]);
- } else {
- if (special) {
- names.push(config.onesSpecialLatinNames[ones]);
- } else if (ones > 1) {
- names.push(config.onesLatinNames[ones]);
- }
- }
- }
- return names.join('');
- };
-
- const getLatinPowerSuffix = (latinRaw: string, isOdd: boolean, special: boolean, ordinal: boolean, config: Config) => {
- const digits = latinRaw.padStart(3, BLANK_DIGIT);
- const [hundreds, tens, ones] = digits.split('').map(s => Number(s));
- const suffix = isOdd ? (ordinal ? config.illiardth : config.illiard) : (ordinal ? config.illionth : config.illion);
-
- if (hundreds > 0) {
- if (tens !== 1) {
- return 't' + suffix;
- }
- return suffix;
- }
- if (tens > 0) {
- switch (tens) {
- case 1:
- return suffix;
- default:
- break;
- }
- return 't' + suffix;
- }
- switch (ones) {
- case 1:
- case 2:
- case 3:
- case 4:
- return special ? suffix : 't' + suffix;
- case 5:
- case 6:
- case 7:
- return 't' + suffix;
- case 8:
- case 9:
- return suffix;
- default:
- break;
- }
- return '';
- };
-
- const getLatinPowerGroupName = (g: Group, config: Config) => {
- const [digits, index] = g;
- if (index.lt(1)) {
- return getLatinPowerGroupDigitsName(digits, index.eq(0), config);
- }
- let milliaSuffix = '';
- for (let i = new BigNumber(0); i.lt(index); i = i.plus(1)) {
- milliaSuffix += config.millia;
- }
- return [getLatinPowerGroupDigitsName(digits, index.eq(0), config), milliaSuffix].join('');
- };
-
- const getLatinPowerName = (latinRaw: BigNumber, isOdd: boolean, ordinal: boolean, config: Config) => {
- const x = normalizeNumeric(latinRaw);
- const {significandDigits, exponent} = deconstructNumeric(x);
- const blankDigits = createBlankDigits(3);
- const groups = groupDigits(significandDigits, exponent, 3);
-
- if (groups.length === 1) {
- return [
- getLatinPowerGroupName(groups[0], config),
- getLatinPowerSuffix(groups[0][0], isOdd, true, ordinal, config),
- ].join('');
- }
-
- const visibleGroups = groups.filter(([digits]) => digits !== blankDigits);
- const [lastVisibleGroup] = visibleGroups.slice(-1);
- return [
- ...visibleGroups.map(g => getLatinPowerGroupName(g, config)),
- getLatinPowerSuffix(lastVisibleGroup[0], isOdd, lastVisibleGroup[1].eq(0), ordinal, config),
- ].join('');
- };
-
- export default getLatinPowerName;
|