Skip to content

Commit

Permalink
Replace deprecated String.prototype.substr() (#4855)
Browse files Browse the repository at this point in the history
* Replace deprecated String.prototype.substr()

String.prototype.substr() is deprecated (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) so we replace it with slice() which works similarily but isn't deprecated.
Signed-off-by: Tobias Speicher <rootcommander@gmail.com>

* Update CHANGELOG.md
  • Loading branch information
CommanderRoot authored Apr 13, 2022
1 parent a1a7679 commit 8371fa0
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,6 @@ Released with 1.0.0-beta.37 code base.
- Fix web3-core-method throws on `f.call = this.call` when intrinsic is frozen (#4918) (#4938)
- Fix static tuple encoding (#4673) (#4884)
- Fix bug in handleRevert logic for eth_sendRawTransaction (#4902)

### Changed
- Replace deprecated String.prototype.substr() (#4855)
4 changes: 2 additions & 2 deletions packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ var _txInputFormatter = function (options) {
if (options.gas || options.gasLimit) {
options.gas = options.gas || options.gasLimit;
}

if (options.maxPriorityFeePerGas || options.maxFeePerGas) {
delete options.gasPrice;
}
Expand Down Expand Up @@ -402,7 +402,7 @@ var outputLogFormatter = function (log) {
typeof log.transactionHash === 'string' &&
typeof log.logIndex === 'string') {
var shaId = utils.sha3(log.blockHash.replace('0x', '') + log.transactionHash.replace('0x', '') + log.logIndex.replace('0x', ''));
log.id = 'log_' + shaId.replace('0x', '').substr(0, 8);
log.id = 'log_' + shaId.replace('0x', '').slice(0, 8);
} else if (!log.id) {
log.id = null;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/web3-eth-iban/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const iso13616Prepare = function (iban) {
const Z = 'Z'.charCodeAt(0);

iban = iban.toUpperCase();
iban = iban.substr(4) + iban.substr(0,4);
iban = iban.slice(4) + iban.slice(0, 4);

return iban.split('').map(function(n) {
const code = n.charCodeAt(0);
Expand Down Expand Up @@ -220,7 +220,7 @@ class Iban {
* @returns {String} checksum
*/
checksum () {
return this._iban.substr(2, 2);
return this._iban.slice(2, 4);
};

/**
Expand All @@ -231,7 +231,7 @@ class Iban {
* @returns {String} institution identifier
*/
institution () {
return this.isIndirect() ? this._iban.substr(7, 4) : '';
return this.isIndirect() ? this._iban.slice(7, 11) : '';
};

/**
Expand All @@ -242,7 +242,7 @@ class Iban {
* @returns {String} client identifier
*/
client () {
return this.isIndirect() ? this._iban.substr(11) : '';
return this.isIndirect() ? this._iban.slice(11) : '';
};

/**
Expand All @@ -253,7 +253,7 @@ class Iban {
*/
toAddress () {
if (this.isDirect()) {
const base36 = this._iban.substr(4);
const base36 = this._iban.slice(4);
const asBn = new BigNumber(base36, 36);
return utils.toChecksumAddress(asBn.toString(16, 20));
}
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var hexToAscii = function(hex) {
i = 2;
}
for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16);
var code = parseInt(hex.slice(i, i + 2), 16);
str += String.fromCharCode(code);
}

Expand Down Expand Up @@ -350,7 +350,7 @@ var compareBlockNumbers = function(a, b) {
return 1;
} else {
// b !== ("pending" OR "latest"), thus a > b
return -1
return -1
}
} else if (a == "pending") {
// b (== OR <) "latest", thus a > b
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ var hexToUtf8 = function(hex) {
var l = hex.length;

for (var i=0; i < l; i+=2) {
code = parseInt(hex.substr(i, 2), 16);
code = parseInt(hex.slice(i, i + 2), 16);
// if (code !== 0) {
str += String.fromCharCode(code);
// }
Expand Down Expand Up @@ -273,7 +273,7 @@ var numberToHex = function (value) {
var number = toBN(value);
var result = number.toString(16);

return number.lt(new BN(0)) ? '-0x' + result.substr(1) : '0x' + result;
return number.lt(new BN(0)) ? '-0x' + result.slice(1) : '0x' + result;
};


Expand Down Expand Up @@ -315,7 +315,7 @@ var hexToBytes = function(hex) {
hex = hex.replace(/^0x/i,'');

for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
bytes.push(parseInt(hex.slice(c, c + 2), 16));
return bytes;
};

Expand Down
2 changes: 1 addition & 1 deletion test/eth.ens.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function prepareProviderForSetter(provider, signature, types, params, error) {
gas: '0x64',
gasPrice: '0x64',
nonce: '0x1',
data: sha3(signature).slice(0, 10) + abiCoder.encodeParameters(types, params).substr(2),
data: sha3(signature).slice(0, 10) + abiCoder.encodeParameters(types, params).slice(2),
to: '0x00000000000c2e074ec69a0dfb2997ba6c7d2e1e'
}]
);
Expand Down
4 changes: 2 additions & 2 deletions test/utils.sha3.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('web3.sha3', function () {
});
it('should return sha3 with hex prefix when hex input', function() {
var sha3Hex = function(value){
if (value.length > 2 && value.substr(0, 2) === '0x') {
value = value.substr(2);
if (value.length > 2 && value.startsWith('0x')) {
value = value.slice(2);
}
value = CryptoJS.enc.Hex.parse(value);

Expand Down

0 comments on commit 8371fa0

Please sign in to comment.