aboutsummaryrefslogtreecommitdiff
path: root/extension/lib/util.js
diff options
context:
space:
mode:
authortg(x) <*@tg-x.net>2015-12-04 22:47:44 +0100
committertg(x) <*@tg-x.net>2015-12-04 22:47:44 +0100
commit1089b40b1c03fa69c0e73dd43e4281aa96388171 (patch)
tree0cdffcbad05041659dc826db9f5dbf1f47de47e1 /extension/lib/util.js
parent5243a707260af9de820ad02b1972d3280164cf19 (diff)
DB structure, emscripten interface
Diffstat (limited to 'extension/lib/util.js')
-rw-r--r--extension/lib/util.js91
1 files changed, 91 insertions, 0 deletions
diff --git a/extension/lib/util.js b/extension/lib/util.js
new file mode 100644
index 000000000..9d83f7e74
--- /dev/null
+++ b/extension/lib/util.js
@@ -0,0 +1,91 @@
+'use strict';
+
+/**
+ * Format amount as String.
+ *
+ * @param amount
+ * Amount to be formatted.
+ *
+ * @return String, e.g. "1.23"
+ */
+function amount_format (amount)
+{
+ let separator = "." // FIXME: depends on locale
+ return amount.value + separator + amount.fraction.toString().replace(/0+$/, "");
+}
+
+
+/**
+ * Format amount with currency as String.
+ *
+ * @param amount
+ * Amount to be formatted.
+ *
+ * @return String, e.g. "1.23 EUR"
+ */
+function amount_format_currency (amount)
+{
+ return amount_format(amount) + " " + amount.currency;
+}
+
+
+/**
+ * Convert Date to String.
+ *
+ * Format: YYYY-MM-DD HH:mm
+ *
+ * @param date
+ * Date to be converted.
+ *
+ * @return String
+ */
+function date_format (date)
+{
+ function pad (number) {
+ if (number < 10) {
+ return '0' + number;
+ }
+ return number;
+ }
+
+ return date.getUTCFullYear() +
+ '-' + pad(date.getUTCMonth() + 1) +
+ '-' + pad(date.getUTCDate()) +
+ ' ' + pad(date.getUTCHours()) +
+ ':' + pad(date.getUTCMinutes());
+ //':' + pad(date.getUTCSeconds());
+}
+
+
+/**
+ * Send HTTP request.
+ *
+ * @param method
+ * HTTP method.
+ * @param url
+ * URL to send to.
+ * @param content
+ * Content of request.
+ * @param content_type
+ * Content-Type HTTP header.
+ * @param onsuccess
+ * Function called by XMLHttpRequest on success.
+ * @param onerror
+ * Function called by XMLHttpRequest on error.
+ *
+ */
+function http_req (method, url, content, content_type, onsuccess, onerror) {
+ var req = new XMLHttpRequest();
+
+ req.onload = function(mintEvt) {
+ if (req.readyState == 4)
+ onsuccess(req.status, req.responseText);
+ };
+
+ req.onerror = onerror;
+ req.open(method, url, true);
+ req.setRequestHeader('Content-Type', content_type);
+ req.send(content);
+
+ return req;
+}