aboutsummaryrefslogtreecommitdiff
path: root/src/amounts.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-04-09 00:41:14 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-04-09 00:41:14 +0200
commita75ef403acf80685c560aed09c544f0a272c666f (patch)
treebcfae61fe2c03beac71dbc6fc8b10b2f4f2b7393 /src/amounts.ts
parent6533716fac07e4988ef94231a0c0c8aba68e0d5e (diff)
downloadwallet-core-a75ef403acf80685c560aed09c544f0a272c666f.tar.xz
make linter happy
Diffstat (limited to 'src/amounts.ts')
-rw-r--r--src/amounts.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/amounts.ts b/src/amounts.ts
index fafbcb3ef..280cd636c 100644
--- a/src/amounts.ts
+++ b/src/amounts.ts
@@ -19,16 +19,19 @@
* Types and helper functions for dealing with Taler amounts.
*/
+
/**
* Imports.
*/
import { Checkable } from "./checkable";
+
/**
* Number of fractional units that one value unit represents.
*/
export const fractionalBase = 1e8;
+
/**
* Non-negative financial amount. Fractional values are expressed as multiples
* of 1e-8.
@@ -60,6 +63,7 @@ export class AmountJson {
static checked: (obj: any) => AmountJson;
}
+
/**
* Result of a possibly overflowing operation.
*/
@@ -74,6 +78,7 @@ export interface Result {
saturated: boolean;
}
+
/**
* Get the largest amount that is safely representable.
*/
@@ -85,6 +90,7 @@ export function getMaxAmount(currency: string): AmountJson {
};
}
+
/**
* Get an amount that represents zero units of a currency.
*/
@@ -96,6 +102,7 @@ export function getZero(currency: string): AmountJson {
};
}
+
/**
* Add two amounts. Return the result and whether
* the addition overflowed. The overflow is always handled
@@ -124,6 +131,7 @@ export function add(first: AmountJson, ...rest: AmountJson[]): Result {
return { amount: { currency, value, fraction }, saturated: false };
}
+
/**
* Subtract two amounts. Return the result and whether
* the subtraction overflowed. The overflow is always handled
@@ -158,6 +166,7 @@ export function sub(a: AmountJson, ...rest: AmountJson[]): Result {
return { amount: { currency, value, fraction }, saturated: false };
}
+
/**
* Compare two amounts. Returns 0 when equal, -1 when a < b
* and +1 when a > b. Throws when currencies don't match.
@@ -186,6 +195,7 @@ export function cmp(a: AmountJson, b: AmountJson): number {
}
}
+
/**
* Create a copy of an amount.
*/
@@ -197,6 +207,7 @@ export function copy(a: AmountJson): AmountJson {
};
}
+
/**
* Divide an amount. Throws on division by zero.
*/
@@ -215,6 +226,7 @@ export function divide(a: AmountJson, n: number): AmountJson {
};
}
+
/**
* Check if an amount is non-zero.
*/
@@ -222,6 +234,7 @@ export function isNonZero(a: AmountJson): boolean {
return a.value > 0 || a.fraction > 0;
}
+
/**
* Parse an amount like 'EUR:20.5' for 20 Euros and 50 ct.
*/
@@ -237,6 +250,11 @@ export function parse(s: string): AmountJson|undefined {
};
}
+
+/**
+ * Parse amount in standard string form (like 'EUR:20.5'),
+ * throw if the input is not a valid amount.
+ */
export function parseOrThrow(s: string): AmountJson {
const res = parse(s);
if (!res) {
@@ -245,6 +263,7 @@ export function parseOrThrow(s: string): AmountJson {
return res;
}
+
/**
* Convert the amount to a float.
*/
@@ -252,6 +271,7 @@ export function toFloat(a: AmountJson): number {
return a.value + (a.fraction / fractionalBase);
}
+
/**
* Convert a float to a Taler amount.
* Loss of precision possible.
@@ -264,6 +284,7 @@ export function fromFloat(floatVal: number, currency: string) {
};
}
+
/**
* Convert to standard human-readable string representation that's
* also used in JSON formats.
@@ -272,6 +293,10 @@ export function toString(a: AmountJson) {
return `${a.currency}:${a.value + (a.fraction / fractionalBase)}`;
}
+
+/**
+ * Check if the argument is a valid amount in string form.
+ */
export function check(a: any) {
if (typeof a !== "string") {
return false;