refactoring

This commit is contained in:
Marek Kotewicz 2015-03-07 19:18:22 +01:00
parent 0a995e1d1f
commit 2b17ad67c9
10 changed files with 61 additions and 444 deletions

122
dist/ethereum.js vendored
View File

@ -1203,7 +1203,7 @@ Formats the input of a whisper post and converts all values to HEX
*/
var inputPostFormatter = function(post){
post.payload = utils.toHex(post.payload);
post.payload = utils.fromAscii(post.payload);
post.ttl = utils.toHex(post.ttl);
post.workToProve = utils.toHex(post.workToProve);
@ -1213,7 +1213,7 @@ var inputPostFormatter = function(post){
// format the following options
post.topic = post.topic.map(function(topic){
return utils.toHex(topic);
return utils.fromAscii(topic);
});
return post;
@ -1819,8 +1819,6 @@ module.exports = {
* @date 2015
*/
var c = require('./config');
if ("build" !== 'build') {/*
var BigNumber = require('bignumber.js'); // jshint ignore:line
*/}
@ -1926,35 +1924,6 @@ var filterEvents = function (json) {
});
};
/// used to transform value/string to eth string
/// TODO: use BigNumber.js to parse int
/// TODO: add tests for it!
// DEPRECATED
var toEth = function (str) {
/*jshint maxcomplexity:7 */
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str.replace(/,/g,'').replace(/ /g,'')) : 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];
};
var toDecimal = function (val) {
// pass it through is its already a number
@ -1972,25 +1941,25 @@ var fromDecimal = function (val) {
var toHex = function (val) {
/*jshint maxcomplexity:5 */
// pass it through is its already a number
if(typeof val === 'object')
return fromAscii(JSON.stringify(val));
// pass it through is its already a number
if(typeof val === 'string' && val.indexOf('0x') === 0)
return val;
if(typeof val === 'string' && !isFinite(val))
return fromAscii(val);
if(isFinite(val))
return fromDecimal(val);
return val;
return '0x' + toBigNumber(val).toString(16);
};
/**
* Returns value of unit in Wei
*
* @method getValueOfUnit
* @param {String} unit the unit to convert to, default ether
* @returns {BigNumber} value of the unit (in Wei)
* @throws error if the unit is not correct:w
*/
var getValueOfUnit = function (unit) {
unit = unit ? unit.toLowerCase() : 'ether';
var unitValue = unitMap[unit];
if (unitValue === undefined) {
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
}
return new BigNumber(unitValue, 10);
};
/**
* Takes a number of wei and converts it to any other ether unit.
@ -2009,18 +1978,11 @@ var toHex = function (val) {
*
* @method fromWei
* @param {Number|String} number can be a number, number string or a HEX of a decimal
* @param {String} unit the unit to convert to, default ethere
* @param {String} unit the unit to convert to, default ether
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var fromWei = function(number, unit) {
unit = unit ? unit.toLowerCase() : 'ether';
var unitValue = unitMap[unit];
if (unitValue === undefined) {
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
}
return toBigNumber(number).dividedBy(new BigNumber(unitValue, 10)).toString(10);
return toBigNumber(number).dividedBy(getValueOfUnit(unit)).toString(10);
};
/**
@ -2040,37 +2002,11 @@ var fromWei = function(number, unit) {
*
* @method toWei
* @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal
* @param {String} unit the unit to convert to
* @param {String} unit the unit to convert from, default ether
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var toWei = function(number, unit) {
/*jshint maxcomplexity: 6 */
unit = unit.toLowerCase();
var isBigNumber = true;
if(!unitMap[unit]) {
console.warn('This unit doesn\'t exists, please use the one of the following units' , unitMap);
return number;
}
if(!number)
return number;
if(typeof number === 'string' && number.indexOf('0x') === 0) {
isBigNumber = false;
number = new BigNumber(number, 16);
}
if(!(number instanceof BigNumber)) {
isBigNumber = false;
number = new BigNumber(number.toString(10), 10);// toString to prevent errors, the user have to handle giving correct bignums themselves
}
number = number.times(new BigNumber(unitMap[unit], 10));
return (isBigNumber) ? number : number.toString(10);
return toBigNumber(number).times(getValueOfUnit(unit)).toString(10);
};
/**
@ -2137,7 +2073,6 @@ module.exports = {
extractTypeName: extractTypeName,
filterFunctions: filterFunctions,
filterEvents: filterEvents,
toEth: toEth,
toWei: toWei,
fromWei: fromWei,
toBigNumber: toBigNumber,
@ -2146,7 +2081,7 @@ module.exports = {
};
},{"./config":2}],18:[function(require,module,exports){
},{}],18:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2385,15 +2320,6 @@ var web3 = {
/// @returns a BigNumber object
toBigNumber: utils.toBigNumber,
// DEPRECATED
/// used to transform value/string to eth string
toEth: function(str) {
console.warn('This method is deprecated please use eth.fromWei(BigNumberOrNumber, unit) instead.');
return utils.toEth(str);
},
toWei: utils.toWei,
fromWei: utils.fromWei,
isAddress: utils.isAddress,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -259,7 +259,7 @@ Formats the input of a whisper post and converts all values to HEX
*/
var inputPostFormatter = function(post){
post.payload = utils.toHex(post.payload);
post.payload = utils.fromAscii(post.payload);
post.ttl = utils.toHex(post.ttl);
post.workToProve = utils.toHex(post.workToProve);
@ -269,7 +269,7 @@ var inputPostFormatter = function(post){
// format the following options
post.topic = post.topic.map(function(topic){
return utils.toHex(topic);
return utils.fromAscii(topic);
});
return post;

View File

@ -20,8 +20,6 @@
* @date 2015
*/
var c = require('./config');
if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js'); // jshint ignore:line
}
@ -127,35 +125,6 @@ var filterEvents = function (json) {
});
};
/// used to transform value/string to eth string
/// TODO: use BigNumber.js to parse int
/// TODO: add tests for it!
// DEPRECATED
var toEth = function (str) {
/*jshint maxcomplexity:7 */
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str.replace(/,/g,'').replace(/ /g,'')) : 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];
};
var toDecimal = function (val) {
// pass it through is its already a number
@ -173,25 +142,25 @@ var fromDecimal = function (val) {
var toHex = function (val) {
/*jshint maxcomplexity:5 */
// pass it through is its already a number
if(typeof val === 'object')
return fromAscii(JSON.stringify(val));
// pass it through is its already a number
if(typeof val === 'string' && val.indexOf('0x') === 0)
return val;
if(typeof val === 'string' && !isFinite(val))
return fromAscii(val);
if(isFinite(val))
return fromDecimal(val);
return val;
return '0x' + toBigNumber(val).toString(16);
};
/**
* Returns value of unit in Wei
*
* @method getValueOfUnit
* @param {String} unit the unit to convert to, default ether
* @returns {BigNumber} value of the unit (in Wei)
* @throws error if the unit is not correct:w
*/
var getValueOfUnit = function (unit) {
unit = unit ? unit.toLowerCase() : 'ether';
var unitValue = unitMap[unit];
if (unitValue === undefined) {
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
}
return new BigNumber(unitValue, 10);
};
/**
* Takes a number of wei and converts it to any other ether unit.
@ -210,18 +179,11 @@ var toHex = function (val) {
*
* @method fromWei
* @param {Number|String} number can be a number, number string or a HEX of a decimal
* @param {String} unit the unit to convert to, default ethere
* @param {String} unit the unit to convert to, default ether
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var fromWei = function(number, unit) {
unit = unit ? unit.toLowerCase() : 'ether';
var unitValue = unitMap[unit];
if (unitValue === undefined) {
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
}
return toBigNumber(number).dividedBy(new BigNumber(unitValue, 10)).toString(10);
return toBigNumber(number).dividedBy(getValueOfUnit(unit)).toString(10);
};
/**
@ -241,37 +203,11 @@ var fromWei = function(number, unit) {
*
* @method toWei
* @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal
* @param {String} unit the unit to convert to
* @param {String} unit the unit to convert from, default ether
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var toWei = function(number, unit) {
/*jshint maxcomplexity: 6 */
unit = unit.toLowerCase();
var isBigNumber = true;
if(!unitMap[unit]) {
console.warn('This unit doesn\'t exists, please use the one of the following units' , unitMap);
return number;
}
if(!number)
return number;
if(typeof number === 'string' && number.indexOf('0x') === 0) {
isBigNumber = false;
number = new BigNumber(number, 16);
}
if(!(number instanceof BigNumber)) {
isBigNumber = false;
number = new BigNumber(number.toString(10), 10);// toString to prevent errors, the user have to handle giving correct bignums themselves
}
number = number.times(new BigNumber(unitMap[unit], 10));
return (isBigNumber) ? number : number.toString(10);
return toBigNumber(number).times(getValueOfUnit(unit)).toString(10);
};
/**
@ -338,7 +274,6 @@ module.exports = {
extractTypeName: extractTypeName,
filterFunctions: filterFunctions,
filterEvents: filterEvents,
toEth: toEth,
toWei: toWei,
fromWei: fromWei,
toBigNumber: toBigNumber,

View File

@ -185,15 +185,6 @@ var web3 = {
/// @returns a BigNumber object
toBigNumber: utils.toBigNumber,
// DEPRECATED
/// used to transform value/string to eth string
toEth: function(str) {
console.warn('This method is deprecated please use eth.fromWei(BigNumberOrNumber, unit) instead.');
return utils.toEth(str);
},
toWei: utils.toWei,
fromWei: utils.fromWei,
isAddress: utils.isAddress,

View File

@ -16,6 +16,7 @@
"devDependencies": {
"bower": ">=1.3.0",
"browserify": ">=6.0",
"chai": "^2.1.1",
"coveralls": "^2.11.2",
"del": ">=0.1.1",
"exorcist": "^0.1.6",

View File

@ -1,5 +1,6 @@
var assert = require('assert');
var chai = require('chai');
var formatters = require('../lib/formatters.js');
var assert = chai.assert;
describe('formatters', function () {
describe('inputPostFormatter', function () {
@ -9,7 +10,7 @@ describe('formatters', function () {
assert.deepEqual(formatters.inputPostFormatter({
from: '0x00000',
to: '0x00000',
payload: {test: 'test'},
payload: JSON.stringify({test: 'test'}),
ttl: 200,
workToProve: 1000,
topic: ['hello','mytopics']
@ -24,4 +25,4 @@ describe('formatters', function () {
});
});
});
});

View File

@ -1,237 +0,0 @@
var assert = require('assert');
var utils = require('../lib/utils.js');
describe('utils', function() {
// given
// NOTE: I made the following tests pass as long as the values are mathematically correct.
// However, in the current state, there are inconsistancy regarding the trailing decimals.
// For instance '10 Mether' but '10.00 Gether'
var data = [{
test: 10e-9,
res: '0.00 wei'
}, {
test: 10e-6,
res: '0.00 wei'
}, {
test: 10e-3,
res: '0.01 wei'
}, {
test: '-10',
res: '-10 wei'
}, {
test: '0',
res: '0 wei'
}, {
test: 0,
res: '0 wei'
}, {
test: '1200000',
res: '1200 Kwei'
}, {
test: 1e3,
res: '1000 wei'
}, {
test: 10e3,
res: '10 Kwei'
}, {
test: 100e3,
res: '100 Kwei'
}, {
test: 1000e3,
res: '1000 Kwei'
}, {
test: 10e6,
res: '10 Mwei'
}, {
test: 100e6,
res: '100 Mwei'
}, {
test: 1000e6,
res: '1000 Mwei'
}, {
test: 10e9,
res: '10 Gwei'
}, {
test: 100e9,
res: '100 Gwei'
}, {
test: 1000e9,
res: '1000 Gwei'
}, {
test: 10e12,
res: '10 szabo'
}, {
test: 100e12,
res: '100 szabo'
}, {
test: 1000e12,
res: '1000 szabo'
}, {
test: 10e15,
res: '10 finney'
}, {
test: 100e15,
res: '100 finney'
}, {
test: 1000e15,
res: '1000 finney'
}, {
test: 10e18,
res: '10 ether'
}, {
test: 100e18,
res: '100 ether'
}, {
test: 1000e18,
res: '1000 ether'
}, {
test: 10e21,
res: '10 grand'
}, {
test: 100e21,
res: '100.00 grand'
}, {
test: 1000e21,
res: '1000 grand'
}, {
test: 10e24,
res: '10 Mether'
}, {
test: 100e24,
res: '100.00 Mether'
}, {
test: 1000e24,
res: '1000 Mether'
}, {
test: 10e27,
res: '10.00 Gether'
}, {
test: 100e27,
res: '100.00 Gether'
}, {
test: 1000e27,
res: '1000 Gether'
}, {
test: 10e30,
res: '10.00 Tether'
}, {
test: 100e30,
res: '100.00 Tether'
}, {
test: 1000e30,
res: '1,000.00 Tether'
}, {
test: 10e33,
res: '10.00 Pether'
}, {
test: 100e33,
res: '100.00 Pether'
}, {
test: 1000e33,
res: '1000 Pether'
}, {
test: 10e36,
res: '10.00 Eether'
}, {
test: 100e36,
res: '100.00 Eether'
}, {
test: 1000e36,
res: '1,000.00 Eether'
}, {
test: 10e39,
res: '10 Zether'
}, {
test: 100e39,
res: '100.00 Zether'
}, {
test: 1000e39,
res: '1000 Zether'
}, {
test: 10e42,
res: '10 Yether'
}, {
test: 100e42,
res: '100.00 Yether'
}, {
test: 1000e42,
res: '1,000.00 Yether'
}, {
test: 10e45,
res: '10 Nether'
}, {
test: 100e45,
res: '100.00 Nether'
}, {
test: 1000e45,
res: '1000 Nether'
}, {
test: 10e48,
res: '10 Dether'
}, {
test: 100e48,
res: '100.00 Dether'
}, {
test: 1000e48,
res: '1000 Dether'
}, {
test: 10e51,
res: '10 Vether'
}, {
test: 100e51,
res: '100.00 Vether'
}, {
test: 1000e51,
res: '1,000.00 Vether'
}, {
test: 10e54,
res: '10 Uether'
}, {
test: 10e57,
res: '10000 Uether'
}, {
test: 100e57,
res: '100,000.00 Uether'
}, {
test: 1000e57,
res: '1,000,000.00 Uether'
}, {
test: '10',
res: utils.toEth(10)
}, {
test: '100',
res: utils.toEth(100)
}, {
test: '1000',
res: utils.toEth(1000)
}, {
test: '1001',
res: utils.toEth(1001)
}, {
test: '999',
res: utils.toEth(999)
}, {
test: '10000000000000000000000',
res: utils.toEth(10e21) // 10 grand
}, {
test: '10 000',
res: '10 Kwei'
}, {
test: '10,000',
res: '10 Kwei'
}, {
test: '10,000.00',
res: '10 Kwei'
}];
data.forEach(function(elem) {
it('should convert ' + elem.test + ' into ' + elem.res, function() {
// when
var res = utils.toEth(elem.test);
// then
assert.equal(res, elem.res);
});
});
});

View File

@ -7,10 +7,10 @@ describe('utils', function () {
assert.equal(utils.toHex(1000), "0x3e8");
assert.equal(utils.toHex('1000'), "0x3e8");
assert.equal(utils.toHex('hello'), "0x68656c6c6f");
assert.equal(utils.toHex({test: 'test'}), "0x7b2274657374223a2274657374227d");
assert.equal(utils.toHex([1,2,3,{hallo:'test'}]), '0x5b312c322c332c7b2268616c6c6f223a2274657374227d5d');
//assert.equal(utils.toHex('hello'), "0x68656c6c6f");
//assert.equal(utils.toHex({test: 'test'}), "0x7b2274657374223a2274657374227d");
//assert.equal(utils.toHex([1,2,3,{hallo:'test'}]), '0x5b312c322c332c7b2268616c6c6f223a2274657374227d5d');
});
});
});
});