Merge pull request #517 from maraoz/fix/parseValue

Fix parseValue
This commit is contained in:
Matias Alejo Garcia 2014-09-30 13:24:23 -03:00
commit ce9faf3e61
4 changed files with 40 additions and 45 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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);
});
});

View File

@ -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;
};