mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 19:48:13 +00:00
common changes in utils, fixing code complexity
This commit is contained in:
parent
3f631094af
commit
0a995e1d1f
184
dist/ethereum.js
vendored
184
dist/ethereum.js
vendored
@ -1993,75 +1993,55 @@ var toHex = function (val) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Takes a number of wei and converts it to any other ether unit.
|
* Takes a number of wei and converts it to any other ether unit.
|
||||||
|
*
|
||||||
Possible units are:
|
* Possible units are:
|
||||||
|
* - kwei/ada
|
||||||
- kwei/ada
|
* - mwei/babbage
|
||||||
- mwei/babbage
|
* - gwei/shannon
|
||||||
- gwei/shannon
|
* - szabo
|
||||||
- szabo
|
* - finney
|
||||||
- finney
|
* - ether
|
||||||
- ether
|
* - kether/grand/einstein
|
||||||
- kether/grand/einstein
|
* - mether
|
||||||
- mether
|
* - gether
|
||||||
- gether
|
* - tether
|
||||||
- tether
|
*
|
||||||
|
* @method fromWei
|
||||||
@method fromWei
|
* @param {Number|String} number can be a number, number string or a HEX of a decimal
|
||||||
@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
|
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||||
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
|
||||||
*/
|
*/
|
||||||
var fromWei = function(number, unit) {
|
var fromWei = function(number, unit) {
|
||||||
/*jshint maxcomplexity: 6 */
|
unit = unit ? unit.toLowerCase() : 'ether';
|
||||||
unit = unit.toLowerCase();
|
var unitValue = unitMap[unit];
|
||||||
|
|
||||||
var isBigNumber = true;
|
if (unitValue === undefined) {
|
||||||
|
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
|
||||||
if(!unitMap[unit]) {
|
|
||||||
console.warn('This unit doesn\'t exists, please use the one of the following units' , unitMap);
|
|
||||||
return number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!number)
|
return toBigNumber(number).dividedBy(new BigNumber(unitValue, 10)).toString(10);
|
||||||
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.dividedBy(new BigNumber(unitMap[unit], 10));
|
|
||||||
|
|
||||||
return (isBigNumber) ? number : number.toString(10);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Takes a number of a unit and converts it to wei.
|
* Takes a number of a unit and converts it to wei.
|
||||||
|
*
|
||||||
Possible units are:
|
* Possible units are:
|
||||||
|
* - kwei/ada
|
||||||
- kwei/ada
|
* - mwei/babbage
|
||||||
- mwei/babbage
|
* - gwei/shannon
|
||||||
- gwei/shannon
|
* - szabo
|
||||||
- szabo
|
* - finney
|
||||||
- finney
|
* - ether
|
||||||
- ether
|
* - kether/grand/einstein
|
||||||
- kether/grand/einstein
|
* - mether
|
||||||
- mether
|
* - gether
|
||||||
- gether
|
* - tether
|
||||||
- tether
|
*
|
||||||
|
* @method toWei
|
||||||
@method toWei
|
* @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal
|
||||||
@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 to
|
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||||
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
|
||||||
*/
|
*/
|
||||||
var toWei = function(number, unit) {
|
var toWei = function(number, unit) {
|
||||||
/*jshint maxcomplexity: 6 */
|
/*jshint maxcomplexity: 6 */
|
||||||
@ -2093,50 +2073,58 @@ var toWei = function(number, unit) {
|
|||||||
return (isBigNumber) ? number : number.toString(10);
|
return (isBigNumber) ? number : number.toString(10);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Checks if the given string is a valid ethereum HEX address.
|
* Takes an input and transforms it into an bignumber
|
||||||
|
*
|
||||||
@method isAddress
|
* @method toBigNumber
|
||||||
@param {String} address the given HEX adress
|
* @param {Number|String|BigNumber} a number, string, HEX string or BigNumber
|
||||||
@return {Boolean}
|
* @return {Object} BigNumber
|
||||||
*/
|
|
||||||
var isAddress = function(address) {
|
|
||||||
if(address.indexOf('0x') === 0 && address.length !== 42)
|
|
||||||
return false;
|
|
||||||
if(address.indexOf('0x') === -1 && address.length !== 40)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return /^\w+$/.test(address);
|
|
||||||
};
|
|
||||||
|
|
||||||
var isBigNumber = function (number) {
|
|
||||||
return number instanceof BigNumber ||
|
|
||||||
(number && number.constructor && number.constructor.name === 'BigNumber');
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
Takes an input and transforms it into an bignumber
|
|
||||||
|
|
||||||
@method toBigNumber
|
|
||||||
@param {Number|String|BigNumber} a number, string, HEX string or BigNumber
|
|
||||||
@return {Object} BigNumber
|
|
||||||
*/
|
*/
|
||||||
var toBigNumber = function(number) {
|
var toBigNumber = function(number) {
|
||||||
|
number = number || 0;
|
||||||
if (isBigNumber(number))
|
if (isBigNumber(number))
|
||||||
return number;
|
return number;
|
||||||
|
|
||||||
if (number) {
|
if (isString(number) && number.indexOf('0x') === 0)
|
||||||
if(typeof number === 'string' && number.indexOf('0x') === 0)
|
return new BigNumber(number, 16);
|
||||||
number = new BigNumber(number, 16);
|
|
||||||
else
|
|
||||||
number = new BigNumber(number.toString(10), 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
return number;
|
return new BigNumber(number.toString(10), 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given string has proper length
|
||||||
|
*
|
||||||
|
* @method isAddress
|
||||||
|
* @param {String} address the given HEX adress
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isAddress = function(address) {
|
||||||
|
return ((address.indexOf('0x') === 0 && address.length === 42) ||
|
||||||
|
(address.indexOf('0x') === -1 && address.length === 40));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if object is BigNumber, otherwise false
|
||||||
|
*
|
||||||
|
* @method isBigNumber
|
||||||
|
* @param {Object}
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isBigNumber = function (object) {
|
||||||
|
return object instanceof BigNumber ||
|
||||||
|
(object && object.constructor && object.constructor.name === 'BigNumber');
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if object is string, otherwise false
|
||||||
|
*
|
||||||
|
* @method isString
|
||||||
|
* @param {Object}
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isString = function (object) {
|
||||||
|
return typeof object === 'string';
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
findIndex: findIndex,
|
findIndex: findIndex,
|
||||||
@ -2153,8 +2141,8 @@ module.exports = {
|
|||||||
toWei: toWei,
|
toWei: toWei,
|
||||||
fromWei: fromWei,
|
fromWei: fromWei,
|
||||||
toBigNumber: toBigNumber,
|
toBigNumber: toBigNumber,
|
||||||
isAddress: isAddress,
|
isBigNumber: isBigNumber,
|
||||||
isBigNumber: isBigNumber
|
isAddress: isAddress
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
4
dist/ethereum.js.map
vendored
4
dist/ethereum.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/ethereum.min.js
vendored
2
dist/ethereum.min.js
vendored
File diff suppressed because one or more lines are too long
184
lib/utils.js
184
lib/utils.js
@ -194,75 +194,55 @@ var toHex = function (val) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Takes a number of wei and converts it to any other ether unit.
|
* Takes a number of wei and converts it to any other ether unit.
|
||||||
|
*
|
||||||
Possible units are:
|
* Possible units are:
|
||||||
|
* - kwei/ada
|
||||||
- kwei/ada
|
* - mwei/babbage
|
||||||
- mwei/babbage
|
* - gwei/shannon
|
||||||
- gwei/shannon
|
* - szabo
|
||||||
- szabo
|
* - finney
|
||||||
- finney
|
* - ether
|
||||||
- ether
|
* - kether/grand/einstein
|
||||||
- kether/grand/einstein
|
* - mether
|
||||||
- mether
|
* - gether
|
||||||
- gether
|
* - tether
|
||||||
- tether
|
*
|
||||||
|
* @method fromWei
|
||||||
@method fromWei
|
* @param {Number|String} number can be a number, number string or a HEX of a decimal
|
||||||
@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
|
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||||
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
|
||||||
*/
|
*/
|
||||||
var fromWei = function(number, unit) {
|
var fromWei = function(number, unit) {
|
||||||
/*jshint maxcomplexity: 6 */
|
unit = unit ? unit.toLowerCase() : 'ether';
|
||||||
unit = unit.toLowerCase();
|
var unitValue = unitMap[unit];
|
||||||
|
|
||||||
var isBigNumber = true;
|
if (unitValue === undefined) {
|
||||||
|
throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2));
|
||||||
if(!unitMap[unit]) {
|
|
||||||
console.warn('This unit doesn\'t exists, please use the one of the following units' , unitMap);
|
|
||||||
return number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!number)
|
return toBigNumber(number).dividedBy(new BigNumber(unitValue, 10)).toString(10);
|
||||||
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.dividedBy(new BigNumber(unitMap[unit], 10));
|
|
||||||
|
|
||||||
return (isBigNumber) ? number : number.toString(10);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Takes a number of a unit and converts it to wei.
|
* Takes a number of a unit and converts it to wei.
|
||||||
|
*
|
||||||
Possible units are:
|
* Possible units are:
|
||||||
|
* - kwei/ada
|
||||||
- kwei/ada
|
* - mwei/babbage
|
||||||
- mwei/babbage
|
* - gwei/shannon
|
||||||
- gwei/shannon
|
* - szabo
|
||||||
- szabo
|
* - finney
|
||||||
- finney
|
* - ether
|
||||||
- ether
|
* - kether/grand/einstein
|
||||||
- kether/grand/einstein
|
* - mether
|
||||||
- mether
|
* - gether
|
||||||
- gether
|
* - tether
|
||||||
- tether
|
*
|
||||||
|
* @method toWei
|
||||||
@method toWei
|
* @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal
|
||||||
@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 to
|
* @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
||||||
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
|
|
||||||
*/
|
*/
|
||||||
var toWei = function(number, unit) {
|
var toWei = function(number, unit) {
|
||||||
/*jshint maxcomplexity: 6 */
|
/*jshint maxcomplexity: 6 */
|
||||||
@ -294,50 +274,58 @@ var toWei = function(number, unit) {
|
|||||||
return (isBigNumber) ? number : number.toString(10);
|
return (isBigNumber) ? number : number.toString(10);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Checks if the given string is a valid ethereum HEX address.
|
* Takes an input and transforms it into an bignumber
|
||||||
|
*
|
||||||
@method isAddress
|
* @method toBigNumber
|
||||||
@param {String} address the given HEX adress
|
* @param {Number|String|BigNumber} a number, string, HEX string or BigNumber
|
||||||
@return {Boolean}
|
* @return {Object} BigNumber
|
||||||
*/
|
|
||||||
var isAddress = function(address) {
|
|
||||||
if(address.indexOf('0x') === 0 && address.length !== 42)
|
|
||||||
return false;
|
|
||||||
if(address.indexOf('0x') === -1 && address.length !== 40)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return /^\w+$/.test(address);
|
|
||||||
};
|
|
||||||
|
|
||||||
var isBigNumber = function (number) {
|
|
||||||
return number instanceof BigNumber ||
|
|
||||||
(number && number.constructor && number.constructor.name === 'BigNumber');
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
Takes an input and transforms it into an bignumber
|
|
||||||
|
|
||||||
@method toBigNumber
|
|
||||||
@param {Number|String|BigNumber} a number, string, HEX string or BigNumber
|
|
||||||
@return {Object} BigNumber
|
|
||||||
*/
|
*/
|
||||||
var toBigNumber = function(number) {
|
var toBigNumber = function(number) {
|
||||||
|
number = number || 0;
|
||||||
if (isBigNumber(number))
|
if (isBigNumber(number))
|
||||||
return number;
|
return number;
|
||||||
|
|
||||||
if (number) {
|
if (isString(number) && number.indexOf('0x') === 0)
|
||||||
if(typeof number === 'string' && number.indexOf('0x') === 0)
|
return new BigNumber(number, 16);
|
||||||
number = new BigNumber(number, 16);
|
|
||||||
else
|
|
||||||
number = new BigNumber(number.toString(10), 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
return number;
|
return new BigNumber(number.toString(10), 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given string has proper length
|
||||||
|
*
|
||||||
|
* @method isAddress
|
||||||
|
* @param {String} address the given HEX adress
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isAddress = function(address) {
|
||||||
|
return ((address.indexOf('0x') === 0 && address.length === 42) ||
|
||||||
|
(address.indexOf('0x') === -1 && address.length === 40));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if object is BigNumber, otherwise false
|
||||||
|
*
|
||||||
|
* @method isBigNumber
|
||||||
|
* @param {Object}
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isBigNumber = function (object) {
|
||||||
|
return object instanceof BigNumber ||
|
||||||
|
(object && object.constructor && object.constructor.name === 'BigNumber');
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if object is string, otherwise false
|
||||||
|
*
|
||||||
|
* @method isString
|
||||||
|
* @param {Object}
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
var isString = function (object) {
|
||||||
|
return typeof object === 'string';
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
findIndex: findIndex,
|
findIndex: findIndex,
|
||||||
@ -354,7 +342,7 @@ module.exports = {
|
|||||||
toWei: toWei,
|
toWei: toWei,
|
||||||
fromWei: fromWei,
|
fromWei: fromWei,
|
||||||
toBigNumber: toBigNumber,
|
toBigNumber: toBigNumber,
|
||||||
isAddress: isAddress,
|
isBigNumber: isBigNumber,
|
||||||
isBigNumber: isBigNumber
|
isAddress: isAddress
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ var assert = require('assert');
|
|||||||
var utils = require('../lib/utils.js');
|
var utils = require('../lib/utils.js');
|
||||||
|
|
||||||
describe('utils', function () {
|
describe('utils', function () {
|
||||||
describe('toWei', function () {
|
describe('fromWei', function () {
|
||||||
it('should return the correct value', function () {
|
it('should return the correct value', function () {
|
||||||
|
|
||||||
assert.equal(utils.fromWei(1000000000000000000, 'wei'), '1000000000000000000');
|
assert.equal(utils.fromWei(1000000000000000000, 'wei'), '1000000000000000000');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user