web3-utils
Advanced tools
Comparing version 4.2.4-dev.53e2466.0 to 4.2.4-dev.553f270.0
@@ -473,3 +473,4 @@ "use strict"; | ||
} | ||
return `${integer}.${fraction}`; | ||
const updatedValue = `${integer}.${fraction}`; | ||
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); | ||
}; | ||
@@ -502,3 +503,3 @@ exports.fromWei = fromWei; | ||
} | ||
if (parsedNumber > 1e+20) { | ||
if (parsedNumber > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
@@ -509,3 +510,6 @@ parsedNumber = BigInt(parsedNumber); | ||
// in case there is a decimal point, we need to convert it to string | ||
parsedNumber = parsedNumber.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 20 }); | ||
parsedNumber = parsedNumber.toLocaleString('fullwide', { | ||
useGrouping: false, | ||
maximumFractionDigits: 20, | ||
}); | ||
} | ||
@@ -515,3 +519,5 @@ } | ||
// to avoid `fraction` to be null use `concat` with empty string | ||
const [integer, fraction] = String(typeof parsedNumber === 'string' && !(0, web3_validator_1.isHexStrict)(parsedNumber) ? parsedNumber : (0, exports.toNumber)(parsedNumber)) | ||
const [integer, fraction] = String(typeof parsedNumber === 'string' && !(0, web3_validator_1.isHexStrict)(parsedNumber) | ||
? parsedNumber | ||
: (0, exports.toNumber)(parsedNumber)) | ||
.split('.') | ||
@@ -525,12 +531,9 @@ .concat(''); | ||
const updatedValue = value * denomination; | ||
// count number of zeros in denomination | ||
const numberOfZerosInDenomination = denomination.toString().length - 1; | ||
// check which either `fraction` or `denomination` have lower number of zeros | ||
const decimals = Math.min(fraction.length, numberOfZerosInDenomination); | ||
// check if whole number was passed in | ||
const decimals = fraction.length; | ||
if (decimals === 0) { | ||
return updatedValue.toString(); | ||
} | ||
// Add zeros to make length equal to required decimal points | ||
// If string is larger than decimal points required then remove last zeros | ||
return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals); | ||
// trim the value to remove extra zeros | ||
return updatedValue.toString().slice(0, -decimals); | ||
}; | ||
@@ -537,0 +540,0 @@ exports.toWei = toWei; |
@@ -456,3 +456,4 @@ /* | ||
} | ||
return `${integer}.${fraction}`; | ||
const updatedValue = `${integer}.${fraction}`; | ||
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); | ||
}; | ||
@@ -484,3 +485,3 @@ /** | ||
} | ||
if (parsedNumber > 1e+20) { | ||
if (parsedNumber > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
@@ -491,3 +492,6 @@ parsedNumber = BigInt(parsedNumber); | ||
// in case there is a decimal point, we need to convert it to string | ||
parsedNumber = parsedNumber.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 20 }); | ||
parsedNumber = parsedNumber.toLocaleString('fullwide', { | ||
useGrouping: false, | ||
maximumFractionDigits: 20, | ||
}); | ||
} | ||
@@ -497,3 +501,5 @@ } | ||
// to avoid `fraction` to be null use `concat` with empty string | ||
const [integer, fraction] = String(typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber)) | ||
const [integer, fraction] = String(typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) | ||
? parsedNumber | ||
: toNumber(parsedNumber)) | ||
.split('.') | ||
@@ -507,12 +513,9 @@ .concat(''); | ||
const updatedValue = value * denomination; | ||
// count number of zeros in denomination | ||
const numberOfZerosInDenomination = denomination.toString().length - 1; | ||
// check which either `fraction` or `denomination` have lower number of zeros | ||
const decimals = Math.min(fraction.length, numberOfZerosInDenomination); | ||
// check if whole number was passed in | ||
const decimals = fraction.length; | ||
if (decimals === 0) { | ||
return updatedValue.toString(); | ||
} | ||
// Add zeros to make length equal to required decimal points | ||
// If string is larger than decimal points required then remove last zeros | ||
return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals); | ||
// trim the value to remove extra zeros | ||
return updatedValue.toString().slice(0, -decimals); | ||
}; | ||
@@ -519,0 +522,0 @@ /** |
{ | ||
"name": "web3-utils", | ||
"sideEffects": false, | ||
"version": "4.2.4-dev.53e2466.0+53e2466", | ||
"version": "4.2.4-dev.553f270.0+553f270", | ||
"description": "Collection of utility functions used in web3.js.", | ||
@@ -34,3 +34,3 @@ "main": "./lib/commonjs/index.js", | ||
"build:check": "node -e \"require('./lib')\"", | ||
"lint": "eslint --ext .js,.ts .", | ||
"lint": "eslint --cache --cache-strategy content --ext .ts .", | ||
"lint:fix": "eslint --fix --ext .js,.ts .", | ||
@@ -69,7 +69,7 @@ "format": "prettier --write '**/*'", | ||
"eventemitter3": "^5.0.1", | ||
"web3-errors": "1.1.5-dev.53e2466.0+53e2466", | ||
"web3-types": "1.6.1-dev.53e2466.0+53e2466", | ||
"web3-validator": "2.0.6-dev.53e2466.0+53e2466" | ||
"web3-errors": "1.1.5-dev.553f270.0+553f270", | ||
"web3-types": "1.6.1-dev.553f270.0+553f270", | ||
"web3-validator": "2.0.6-dev.553f270.0+553f270" | ||
}, | ||
"gitHead": "53e24664e4c660826eb10207b480848e677a73e6" | ||
"gitHead": "553f270e49c35b68adec64454db8d32a03646de7" | ||
} |
@@ -535,4 +535,5 @@ /* | ||
} | ||
const updatedValue = `${integer}.${fraction}`; | ||
return `${integer}.${fraction}`; | ||
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); | ||
}; | ||
@@ -563,20 +564,25 @@ | ||
let parsedNumber = number; | ||
if (typeof parsedNumber === 'number'){ | ||
if (parsedNumber < 1e-15){ | ||
console.warn(PrecisionLossWarning) | ||
if (typeof parsedNumber === 'number') { | ||
if (parsedNumber < 1e-15) { | ||
console.warn(PrecisionLossWarning); | ||
} | ||
if (parsedNumber > 1e+20) { | ||
console.warn(PrecisionLossWarning) | ||
if (parsedNumber > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
parsedNumber = BigInt(parsedNumber); | ||
parsedNumber = BigInt(parsedNumber); | ||
} else { | ||
// in case there is a decimal point, we need to convert it to string | ||
parsedNumber = parsedNumber.toLocaleString('fullwide', {useGrouping: false, maximumFractionDigits: 20}) | ||
parsedNumber = parsedNumber.toLocaleString('fullwide', { | ||
useGrouping: false, | ||
maximumFractionDigits: 20, | ||
}); | ||
} | ||
} | ||
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part | ||
// to avoid `fraction` to be null use `concat` with empty string | ||
const [integer, fraction] = String( | ||
typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber), | ||
typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) | ||
? parsedNumber | ||
: toNumber(parsedNumber), | ||
) | ||
@@ -588,3 +594,3 @@ .split('.') | ||
// 24.56 -> 2456 | ||
const value = BigInt(`${integer}${fraction}`); | ||
@@ -596,8 +602,4 @@ | ||
// count number of zeros in denomination | ||
const numberOfZerosInDenomination = denomination.toString().length - 1; | ||
// check which either `fraction` or `denomination` have lower number of zeros | ||
const decimals = Math.min(fraction.length, numberOfZerosInDenomination); | ||
// check if whole number was passed in | ||
const decimals = fraction.length; | ||
if (decimals === 0) { | ||
@@ -607,5 +609,4 @@ return updatedValue.toString(); | ||
// Add zeros to make length equal to required decimal points | ||
// If string is larger than decimal points required then remove last zeros | ||
return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals); | ||
// trim the value to remove extra zeros | ||
return updatedValue.toString().slice(0, -decimals); | ||
}; | ||
@@ -612,0 +613,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
9987
526442