diff options
author | Florian Dold <florian.dold@gmail.com> | 2020-09-01 20:37:50 +0530 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2020-09-01 20:37:50 +0530 |
commit | 044b7236572089b98a9f230499bb4cd9ad0342a3 (patch) | |
tree | f80955c0fdf6e7dc5e836d8b811e43ce637093ab /packages/taler-wallet-core/src/util/amounts.ts | |
parent | 7f4ebca0c4330805ea8f3821dba075b34dd2be58 (diff) | |
download | wallet-core-044b7236572089b98a9f230499bb4cd9ad0342a3.tar.xz |
correct refund amounts and better testing
Diffstat (limited to 'packages/taler-wallet-core/src/util/amounts.ts')
-rw-r--r-- | packages/taler-wallet-core/src/util/amounts.ts | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/packages/taler-wallet-core/src/util/amounts.ts b/packages/taler-wallet-core/src/util/amounts.ts index 2a8c47905..2f912cff4 100644 --- a/packages/taler-wallet-core/src/util/amounts.ts +++ b/packages/taler-wallet-core/src/util/amounts.ts @@ -101,11 +101,21 @@ export function getZero(currency: string): AmountJson { }; } -export function sum(amounts: AmountJson[]): Result { +export type AmountLike = AmountString | AmountJson; + +export function jsonifyAmount(amt: AmountLike): AmountJson { + if (typeof amt === "string") { + return parseOrThrow(amt); + } + return amt; +} + +export function sum(amounts: AmountLike[]): Result { if (amounts.length <= 0) { throw Error("can't sum zero amounts"); } - return add(amounts[0], ...amounts.slice(1)); + const jsonAmounts = amounts.map((x) => jsonifyAmount(x)); + return add(jsonAmounts[0], ...jsonAmounts.slice(1)); } /** |