diff options
author | Sebastian <sebasjm@gmail.com> | 2024-02-04 17:01:13 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-02-04 17:01:13 -0300 |
commit | b30c7dcf667e3d94a8a3ad170d53ede70e1f0be1 (patch) | |
tree | 5dcec487adb2e2c45cce1d15e3101d05ca1a0fb1 /packages/taler-util/src/amounts.ts | |
parent | 3028ce1b3818b4b740f4343506dac746af0c57f4 (diff) | |
download | wallet-core-b30c7dcf667e3d94a8a3ad170d53ede70e1f0be1.tar.xz |
AcceptTipRequest also accept TxID
Diffstat (limited to 'packages/taler-util/src/amounts.ts')
-rw-r--r-- | packages/taler-util/src/amounts.ts | 86 |
1 files changed, 52 insertions, 34 deletions
diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts index 3750cf61f..c3770a393 100644 --- a/packages/taler-util/src/amounts.ts +++ b/packages/taler-util/src/amounts.ts @@ -53,7 +53,7 @@ export const amountMaxValue = 2 ** 52; /** * Separator character between integer and fractional */ -export const FRAC_SEPARATOR = "." +export const FRAC_SEPARATOR = "."; /** * Non-negative financial amount. Fractional values are expressed as multiples @@ -370,7 +370,7 @@ export class Amounts { /** * Parse an amount like 'EUR:20.5' for 20 Euros and 50 ct. - * + * * Currency name size limit is 11 of ASCII letters * Fraction size limit is 8 */ @@ -379,7 +379,7 @@ export class Amounts { if (!res) { return undefined; } - const tail = res[3] || (FRAC_SEPARATOR + "0"); + const tail = res[3] || FRAC_SEPARATOR + "0"; if (tail.length > amountFractionalLength + 1) { return undefined; } @@ -550,58 +550,76 @@ export class Amounts { return amountFractionalLength - i + 1; } - - static stringifyValueWithSpec(value: AmountJson, spec: CurrencySpecification): { currency: string, normal: string, small?: string } { - const strValue = Amounts.stringifyValue(value) - const pos = strValue.indexOf(FRAC_SEPARATOR) + static stringifyValueWithSpec( + value: AmountJson, + spec: CurrencySpecification, + ): { currency: string; normal: string; small?: string } { + const strValue = Amounts.stringifyValue(value); + const pos = strValue.indexOf(FRAC_SEPARATOR); const originalPosition = pos < 0 ? strValue.length : pos; - let currency = value.currency - const names = Object.keys(spec.alt_unit_names) - let FRAC_POS_NEW_POSITION = originalPosition + let currency = value.currency; + const names = Object.keys(spec.alt_unit_names); + let FRAC_POS_NEW_POSITION = originalPosition; //find symbol //FIXME: this should be based on a cache to speed up if (names.length > 0) { - let unitIndex: string = "0" //default entry by DD51 - names.forEach(index => { - const i = Number.parseInt(index, 10) + let unitIndex: string = "0"; //default entry by DD51 + names.forEach((index) => { + const i = Number.parseInt(index, 10); if (Number.isNaN(i)) return; //skip if (originalPosition - i <= 0) return; //too big if (originalPosition - i < FRAC_POS_NEW_POSITION) { FRAC_POS_NEW_POSITION = originalPosition - i; - unitIndex = index + unitIndex = index; } - }) - currency = spec.alt_unit_names[unitIndex] + }); + currency = spec.alt_unit_names[unitIndex]; } if (originalPosition === FRAC_POS_NEW_POSITION) { - const { normal, small } = splitNormalAndSmall(strValue, originalPosition, spec) - return { currency, normal, small } + const { normal, small } = splitNormalAndSmall( + strValue, + originalPosition, + spec, + ); + return { currency, normal, small }; } - const intPart = strValue.substring(0, originalPosition) - const fracPArt = strValue.substring(originalPosition + 1) + const intPart = strValue.substring(0, originalPosition); + const fracPArt = strValue.substring(originalPosition + 1); //indexSize is always smaller than originalPosition - const newValue = intPart.substring(0, FRAC_POS_NEW_POSITION) + FRAC_SEPARATOR + intPart.substring(FRAC_POS_NEW_POSITION) + fracPArt - const { normal, small } = splitNormalAndSmall(newValue, FRAC_POS_NEW_POSITION, spec) - return { currency, normal, small } + const newValue = + intPart.substring(0, FRAC_POS_NEW_POSITION) + + FRAC_SEPARATOR + + intPart.substring(FRAC_POS_NEW_POSITION) + + fracPArt; + const { normal, small } = splitNormalAndSmall( + newValue, + FRAC_POS_NEW_POSITION, + spec, + ); + return { currency, normal, small }; } - - } -function splitNormalAndSmall(decimal: string, fracSeparatorIndex: number, spec: CurrencySpecification): { normal: string, small?: string } { +function splitNormalAndSmall( + decimal: string, + fracSeparatorIndex: number, + spec: CurrencySpecification, +): { normal: string; small?: string } { let normal: string; let small: string | undefined; - if (decimal.length - fracSeparatorIndex - 1 > spec.num_fractional_normal_digits) { - const limit = fracSeparatorIndex + spec.num_fractional_normal_digits + 1 - normal = decimal.substring(0, limit) - small = decimal.substring(limit) + if ( + decimal.length - fracSeparatorIndex - 1 > + spec.num_fractional_normal_digits + ) { + const limit = fracSeparatorIndex + spec.num_fractional_normal_digits + 1; + normal = decimal.substring(0, limit); + small = decimal.substring(limit); } else { - normal = decimal - small = undefined + normal = decimal; + small = undefined; } - return { normal, small } + return { normal, small }; } - |