aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2015-12-05 17:58:41 +0100
committerFlorian Dold <florian.dold@gmail.com>2015-12-05 17:58:41 +0100
commitaeac228df7121dada4827ac248e61ba8b1db7318 (patch)
tree839bb89b1d8eff7a3fd9d3b05c538e3cf79048e7
parenta69cccca470753c96ef070ee09e531ed760b4e67 (diff)
downloadwallet-core-aeac228df7121dada4827ac248e61ba8b1db7318.tar.xz
Taler amount parsing.
-rw-r--r--extension/lib/util.js21
1 files changed, 21 insertions, 0 deletions
diff --git a/extension/lib/util.js b/extension/lib/util.js
index 9d83f7e74..d364d9593 100644
--- a/extension/lib/util.js
+++ b/extension/lib/util.js
@@ -16,6 +16,27 @@ function amount_format (amount)
/**
+ * Parse an amount that is specified like '5.42 EUR'.
+ * Returns a {currency,value,fraction} object or null
+ * if the input is invalid.
+ */
+function amount_parse_pretty(s) {
+ let pattern = /(\d+)(.\d+)?\s*([a-zA-Z]+)/;
+ let matches = pattern.exec(s);
+ if (null == matches) {
+ return null;
+ }
+ return {
+ // Always succeeds due to regex
+ value: parseInt(matches[1]),
+ // Should we warn / fail on lost precision?
+ fraction: Math.round(parseFloat(matches[2] || 0) * 1000000),
+ currency: matches[3],
+ };
+}
+
+
+/**
* Format amount with currency as String.
*
* @param amount