aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js31
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/utils.js b/lib/utils.js
index 5cd6ec8d6..e1265803c 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -20,6 +20,8 @@
* @date 2015
*/
+var c = require('./const');
+
/// Finds first index of array element matching pattern
/// @param array
/// @param callback pattern
@@ -101,6 +103,32 @@ var filterEvents = function (json) {
});
};
+/// used to transform value/string to eth string
+/// TODO: use BigNumber.js to parse int
+/// TODO: add tests for it!
+var toEth = function (str) {
+ var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
+ var unit = 0;
+ var units = c.ETH_UNITS;
+ while (val > 3000 && unit < units.length - 1)
+ {
+ val /= 1000;
+ unit++;
+ }
+ var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
+ var replaceFunction = function($0, $1, $2) {
+ return $1 + ',' + $2;
+ };
+
+ while (true) {
+ var o = s;
+ s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
+ if (o === s)
+ break;
+ }
+ return s + ' ' + units[unit];
+};
+
module.exports = {
findIndex: findIndex,
toAscii: toAscii,
@@ -108,6 +136,7 @@ module.exports = {
extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName,
filterFunctions: filterFunctions,
- filterEvents: filterEvents
+ filterEvents: filterEvents,
+ toEth: toEth
};