Browse Source

fix parseValue

patch-2
Manuel Araoz 10 years ago
parent
commit
aae2e9a654
  1. 17
      test/mute.js
  2. 4
      test/test.examples.js
  3. 45
      test/test.util.js
  4. 19
      util/util.js

17
test/mute.js

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

4
test/test.examples.js

@ -2,8 +2,6 @@
var chai = chai || require('chai'); var chai = chai || require('chai');
var should = chai.should(); var should = chai.should();
var mute = require('./mute').mute;
var unmute = require('./mute').unmute;
var examples = [ var examples = [
'Address', 'Address',
@ -22,8 +20,6 @@ var examples = [
]; ];
describe('Examples', function() { describe('Examples', function() {
before(mute);
after(unmute);
examples.forEach(function(example) { examples.forEach(function(example) {
it('valid '+example, function() { it('valid '+example, function() {
var ex = require('../examples/'+example); var ex = require('../examples/'+example);

45
test/test.util.js

@ -28,25 +28,32 @@ describe('util', function() {
}); });
}); });
describe('#parseValue', function() { describe('#parseValue', function() {
it('should convert floating points to satoshis correctly', function() { function test_value(datum) {
function test_value(datum) { var decimal = datum[0];
var decimal = datum[0]; var intStr = datum[1];
var intStr = datum[1]; var bn = coinUtil.parseValue(decimal);
var bn = coinUtil.parseValue(decimal); should.exist(bn);
should.exist(bn); bn.toString().should.equal(intStr);
bn.toString().should.equal(intStr); }
} var dataValues = [
var dataValues = [ ['0', '0'],
['0', '0'], ['1.0', '100000000'],
['1.0', '100000000'], ['0.1', '10000000'],
['0.1', '10000000'], ['.1', '10000000'],
['.1', '10000000'], ['0.0005', '50000'],
['0.0005', '50000'], ['.000000001', '0'],
['.000000001', '0'], ['.000000009', '0'],
['.000000009', '0'], ['.00000000000000001', '0'],
['.00000000000000001', '0'] ['1e-7', '10'],
]; ['1e-8', '1'],
dataValues.forEach(function(datum) { ['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); test_value(datum);
}); });
}); });

19
util/util.js

@ -269,7 +269,8 @@ var formatValue = exports.formatValue = function(valueBuffer) {
var reFullVal = /^\s*(\d+)\.(\d+)/; var reFullVal = /^\s*(\d+)\.(\d+)/;
var reFracVal = /^\s*\.(\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) { function padFrac(frac) {
frac = frac.substr(0, 8); //truncate to 8 decimal places frac = frac.substr(0, 8); //truncate to 8 decimal places
@ -294,18 +295,26 @@ exports.parseValue = function parseValue(valueStr) {
if (typeof valueStr !== 'string') if (typeof valueStr !== 'string')
valueStr = valueStr.toString(); valueStr = valueStr.toString();
var res = valueStr.match(reFullVal); var res;
if (res)
return parseFullValue(res);
res = valueStr.match(reFracVal); res = valueStr.match(reFracVal);
if (res) if (res)
return parseFracValue(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); res = valueStr.match(reWholeVal);
if (res) if (res)
return parseWholeValue(res); return parseWholeValue(res);
return undefined; return undefined;
}; };

Loading…
Cancel
Save