diff options
author | Florian Dold <florian@dold.me> | 2021-12-01 18:07:27 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-12-01 18:07:27 +0100 |
commit | 2dc876d6d6e766eeeda4efdb2c4e1d0ac19fa524 (patch) | |
tree | 4ad4ccd1a50b55e134b3ea88016b54caefe34654 /packages/taler-util/src | |
parent | dbbe1b0a6173f70d59a5a2165632170cc1d90e03 (diff) | |
download | wallet-core-2dc876d6d6e766eeeda4efdb2c4e1d0ac19fa524.tar.xz |
taler-util: amount currency normalization
Diffstat (limited to 'packages/taler-util/src')
-rw-r--r-- | packages/taler-util/src/amounts.ts | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts index 41fd14bee..25b3a2c8b 100644 --- a/packages/taler-util/src/amounts.ts +++ b/packages/taler-util/src/amounts.ts @@ -151,7 +151,7 @@ export class Amounts { } let fraction = first.fraction % amountFractionalBase; for (const x of rest) { - if (x.currency !== currency) { + if (x.currency.toUpperCase() !== currency.toUpperCase()) { throw Error(`Mismatched currency: ${x.currency} and ${currency}`); } @@ -187,7 +187,7 @@ export class Amounts { let fraction = a.fraction; for (const b of rest) { - if (b.currency !== currency) { + if (b.currency.toUpperCase() !== a.currency.toUpperCase()) { throw Error(`Mismatched currency: ${b.currency} and ${currency}`); } if (fraction < b.fraction) { @@ -299,7 +299,7 @@ export class Amounts { return undefined; } return { - currency: res[1], + currency: res[1].toUpperCase(), fraction: Math.round(amountFractionalBase * Number.parseFloat(tail)), value, }; @@ -403,11 +403,17 @@ export class Amounts { */ static stringify(a: AmountLike): string { a = Amounts.jsonifyAmount(a); - const s = this.stringifyValue(a) + const s = this.stringifyValue(a); return `${a.currency}:${s}`; } + static isSameCurrency(a1: AmountLike, a2: AmountLike): boolean { + const x1 = this.jsonifyAmount(a1); + const x2 = this.jsonifyAmount(a2); + return x1.currency.toUpperCase() === x2.currency.toUpperCase(); + } + static stringifyValue(a: AmountJson, minFractional: number = 0): string { const av = a.value + Math.floor(a.fraction / amountFractionalBase); const af = a.fraction % amountFractionalBase; @@ -424,6 +430,6 @@ export class Amounts { n = (n * 10) % amountFractionalBase; } } - return s + return s; } } |