From aae2e9a654a845d05eabca4d3b0f6788610537ac Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 29 Sep 2014 17:24:56 -0300 Subject: [PATCH] fix parseValue --- test/mute.js | 17 ---------------- test/test.examples.js | 4 ---- test/test.util.js | 45 +++++++++++++++++++++++++------------------ util/util.js | 19 +++++++++++++----- 4 files changed, 40 insertions(+), 45 deletions(-) delete mode 100644 test/mute.js diff --git a/test/mute.js b/test/mute.js deleted file mode 100644 index 1e1f182..0000000 --- a/test/mute.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -var backup = console.log; -var ebackup = console.error; -var nop = function() {}; -var mute = function() { - console.log = nop; - console.error = nop; -}; - -var unmute = function() { - console.log = backup; - console.error = ebackup; -}; - -module.exports.mute = mute; -module.exports.unmute = unmute; diff --git a/test/test.examples.js b/test/test.examples.js index 51fc04d..93038e9 100644 --- a/test/test.examples.js +++ b/test/test.examples.js @@ -2,8 +2,6 @@ var chai = chai || require('chai'); var should = chai.should(); -var mute = require('./mute').mute; -var unmute = require('./mute').unmute; var examples = [ 'Address', @@ -22,8 +20,6 @@ var examples = [ ]; describe('Examples', function() { - before(mute); - after(unmute); examples.forEach(function(example) { it('valid '+example, function() { var ex = require('../examples/'+example); diff --git a/test/test.util.js b/test/test.util.js index cf64c86..89f6cba 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -28,25 +28,32 @@ describe('util', function() { }); }); describe('#parseValue', function() { - it('should convert floating points to satoshis correctly', function() { - function test_value(datum) { - var decimal = datum[0]; - var intStr = datum[1]; - var bn = coinUtil.parseValue(decimal); - should.exist(bn); - bn.toString().should.equal(intStr); - } - var dataValues = [ - ['0', '0'], - ['1.0', '100000000'], - ['0.1', '10000000'], - ['.1', '10000000'], - ['0.0005', '50000'], - ['.000000001', '0'], - ['.000000009', '0'], - ['.00000000000000001', '0'] - ]; - dataValues.forEach(function(datum) { + function test_value(datum) { + var decimal = datum[0]; + var intStr = datum[1]; + var bn = coinUtil.parseValue(decimal); + should.exist(bn); + bn.toString().should.equal(intStr); + } + var dataValues = [ + ['0', '0'], + ['1.0', '100000000'], + ['0.1', '10000000'], + ['.1', '10000000'], + ['0.0005', '50000'], + ['.000000001', '0'], + ['.000000009', '0'], + ['.00000000000000001', '0'], + ['1e-7', '10'], + ['1e-8', '1'], + ['1e2', '10000000000'], + ['1.5e2', '15000000000'], + ['1e-9', '0'], + ['0.0001', '10000'], + [1e-8, '1'], + ]; + dataValues.forEach(function(datum) { + it('should convert floating points to satoshis correctly for ' + datum[0], function() { test_value(datum); }); }); diff --git a/util/util.js b/util/util.js index a824d9c..efe8a25 100644 --- a/util/util.js +++ b/util/util.js @@ -269,7 +269,8 @@ var formatValue = exports.formatValue = function(valueBuffer) { var reFullVal = /^\s*(\d+)\.(\d+)/; var reFracVal = /^\s*\.(\d+)/; -var reWholeVal = /^\s*(\d+)/; +var reWholeVal = /^\s*(\d+)$/; +var reSciNotation = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/; function padFrac(frac) { frac = frac.substr(0, 8); //truncate to 8 decimal places @@ -294,18 +295,26 @@ exports.parseValue = function parseValue(valueStr) { if (typeof valueStr !== 'string') valueStr = valueStr.toString(); - var res = valueStr.match(reFullVal); - if (res) - return parseFullValue(res); - + var res; res = valueStr.match(reFracVal); if (res) return parseFracValue(res); + res = valueStr.match(reSciNotation); + if (res) { + var f = parseFloat(res[0]); + valueStr = f.toFixed(8).toString(); + } + + res = valueStr.match(reFullVal); + if (res) + return parseFullValue(res); + res = valueStr.match(reWholeVal); if (res) return parseWholeValue(res); + return undefined; };