diff options
author | Sebastian <sebasjm@gmail.com> | 2021-05-27 11:15:55 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-05-27 11:15:55 -0300 |
commit | 3688f7e4d4d2ccd148edd25e0a8eaddbd677b317 (patch) | |
tree | ee9a047fd843d9c5ee513fac737ab9c2294cec91 /packages/taler-util | |
parent | dd8969646be7fa35eccaf89b767798602d19231a (diff) |
Amount.toFloat and hide filesystem requirement
Diffstat (limited to 'packages/taler-util')
-rw-r--r-- | packages/taler-util/src/amounts.ts | 7 | ||||
-rw-r--r-- | packages/taler-util/src/talerconfig.ts | 20 |
2 files changed, 24 insertions, 3 deletions
diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts index e472de503..931bc1dda 100644 --- a/packages/taler-util/src/amounts.ts +++ b/packages/taler-util/src/amounts.ts @@ -331,6 +331,13 @@ export class Amounts { }; } + /** + * Convert a Taler amount to a float + */ + static toFloat(amount: AmountJson): number { + return amount.value + (amount.fraction / amountFractionalBase) + } + static min(a: AmountLike, b: AmountLike): AmountJson { const cr = Amounts.cmp(a, b); if (cr >= 0) { diff --git a/packages/taler-util/src/talerconfig.ts b/packages/taler-util/src/talerconfig.ts index e060afb71..f3c1220e2 100644 --- a/packages/taler-util/src/talerconfig.ts +++ b/packages/taler-util/src/talerconfig.ts @@ -25,7 +25,21 @@ */ import { AmountJson } from "./amounts.js"; import { Amounts } from "./amounts.js"; -import fs from "fs"; + +const nodejs_fs = (function () { + let fs: typeof import("fs"); + return function() { + if (!fs) { + /** + * need to use an expression when doing a require if we want + * webpack not to find out about the requirement + */ + const _r = "require" + fs = module[_r]("fs") + } + return fs + } +})() export class ConfigError extends Error { constructor(message: string) { @@ -293,7 +307,7 @@ export class Configuration { } static load(filename: string): Configuration { - const s = fs.readFileSync(filename, "utf-8"); + const s = nodejs_fs().readFileSync(filename, "utf-8"); const cfg = new Configuration(); cfg.loadFromString(s); return cfg; @@ -313,6 +327,6 @@ export class Configuration { } s += "\n"; } - fs.writeFileSync(filename, s); + nodejs_fs().writeFileSync(filename, s); } } |