Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove underscore #4069

Merged
merged 21 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/web3-bzz/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"use strict";

var _ = require('underscore');

luu-alex marked this conversation as resolved.
Show resolved Hide resolved
var swarm = require("swarm-js");


Expand Down Expand Up @@ -52,7 +52,7 @@ if (typeof ethereum !== 'undefined' && ethereum.bzz) {

Bzz.prototype.setProvider = function(provider) {
// is ethereum provider
if(_.isObject(provider) && _.isString(provider.bzz)) {
if(typeof provider === 'object' && !!provider && typeof provider.bzz === 'string') {
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
provider = provider.bzz;
// is no string, set default
}
Expand All @@ -61,7 +61,7 @@ Bzz.prototype.setProvider = function(provider) {
// }


if(_.isString(provider)) {
if(typeof provider === 'string') {
this.currentProvider = provider;
} else {
this.currentProvider = null;
Expand Down
19 changes: 9 additions & 10 deletions packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

"use strict";

var _ = require('underscore');
var utils = require('web3-utils');
var Iban = require('web3-eth-iban');

Expand Down Expand Up @@ -121,7 +120,7 @@ var inputBlockNumberFormatter = function (blockNumber) {
return '0x0';
}

return (utils.isHexStrict(blockNumber)) ? ((_.isString(blockNumber)) ? blockNumber.toLowerCase() : blockNumber) : utils.numberToHex(blockNumber);
return (utils.isHexStrict(blockNumber)) ? ((typeof blockNumber === 'string') ? blockNumber.toLowerCase() : blockNumber) : utils.numberToHex(blockNumber);
};

/**
Expand Down Expand Up @@ -201,10 +200,10 @@ var inputTransactionFormatter = function (options) {
options = _txInputFormatter(options);

// check from, only if not number, or object
if (!_.isNumber(options.from) && !_.isObject(options.from)) {
if (!(typeof options.from === 'number') && !(typeof options.from === 'object' && !!options.from)) {
options.from = options.from || (this ? this.defaultAccount : null);

if (!options.from && !_.isNumber(options.from)) {
if (!options.from && !(typeof options.from === 'number')) {
throw new Error('The send transactions "from" field must be defined!');
}

Expand Down Expand Up @@ -274,7 +273,7 @@ var outputTransactionReceiptFormatter = function (receipt) {
receipt.cumulativeGasUsed = utils.hexToNumber(receipt.cumulativeGasUsed);
receipt.gasUsed = utils.hexToNumber(receipt.gasUsed);

if (_.isArray(receipt.logs)) {
if (Array.isArray(receipt.logs)) {
receipt.logs = receipt.logs.map(outputLogFormatter);
}

Expand Down Expand Up @@ -311,9 +310,9 @@ var outputBlockFormatter = function (block) {
if (block.totalDifficulty)
block.totalDifficulty = outputBigNumberFormatter(block.totalDifficulty);

if (_.isArray(block.transactions)) {
if (Array.isArray(block.transactions)) {
block.transactions.forEach(function (item) {
if (!_.isString(item))
if (!(typeof item === 'string'))
return outputTransactionFormatter(item);
});
}
Expand Down Expand Up @@ -358,13 +357,13 @@ var inputLogFormatter = function (options) {
// make sure topics, get converted to hex
options.topics = options.topics || [];
options.topics = options.topics.map(function (topic) {
return (_.isArray(topic)) ? topic.map(toTopic) : toTopic(topic);
return (Array.isArray(topic)) ? topic.map(toTopic) : toTopic(topic);
});

toTopic = null;

if (options.address) {
options.address = (_.isArray(options.address)) ? options.address.map(function (addr) {
options.address = (Array.isArray(options.address)) ? options.address.map(function (addr) {
return inputAddressFormatter(addr);
}) : inputAddressFormatter(options.address);
}
Expand Down Expand Up @@ -424,7 +423,7 @@ var inputPostFormatter = function (post) {
post.priority = utils.numberToHex(post.priority);

// fallback
if (!_.isArray(post.topics)) {
if (!Array.isArray(post.topics)) {
post.topics = post.topics ? [post.topics] : [];
}

Expand Down
34 changes: 18 additions & 16 deletions packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

'use strict';

var _ = require('underscore');
var errors = require('web3-core-helpers').errors;
var formatters = require('web3-core-helpers').formatters;
var utils = require('web3-utils');
Expand Down Expand Up @@ -102,7 +101,7 @@ Method.prototype.attachToObject = function (obj) {
* @return {String} name of jsonrpc method
*/
Method.prototype.getCall = function (args) {
return _.isFunction(this.call) ? this.call(args) : this.call;
return typeof this.call === 'function' ? this.call(args) : this.call;
};

/**
Expand All @@ -113,7 +112,7 @@ Method.prototype.getCall = function (args) {
* @return {Function|Null} callback, if exists
*/
Method.prototype.extractCallback = function (args) {
if (_.isFunction(args[args.length - 1])) {
if (typeof (args[args.length - 1]) === 'function') {
return args.pop(); // modify the args array!
}
};
Expand Down Expand Up @@ -161,7 +160,7 @@ Method.prototype.formatInput = function (args) {
Method.prototype.formatOutput = function (result) {
var _this = this;

if (_.isArray(result)) {
if (Array.isArray(result)) {
return result.map(function (res) {
return _this.outputFormatter && res ? _this.outputFormatter(res) : res;
});
Expand All @@ -180,6 +179,8 @@ Method.prototype.formatOutput = function (result) {
Method.prototype.toPayload = function (args) {
var call = this.getCall(args);
var callback = this.extractCallback(args);
//check if last args is a function, if so pop it out.

luu-alex marked this conversation as resolved.
Show resolved Hide resolved
var params = this.formatInput(args);
this.validateArgs(params);

Expand All @@ -206,8 +207,8 @@ Method.prototype._confirmTransaction = function (defer, result, payload) {
intervalId = null,
lastBlock = null,
receiptJSON = '',
gasProvided = (_.isObject(payload.params[0]) && payload.params[0].gas) ? payload.params[0].gas : null,
isContractDeployment = _.isObject(payload.params[0]) &&
gasProvided = ((typeof payload.params[0] === 'object' && !!payload.params[0]) && payload.params[0].gas) ? payload.params[0].gas : null,
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
isContractDeployment = (typeof payload.params[0] === 'object' && !!payload.params[0]) &&
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
payload.params[0].data &&
payload.params[0].from &&
!payload.params[0].to,
Expand Down Expand Up @@ -258,7 +259,7 @@ Method.prototype._confirmTransaction = function (defer, result, payload) {
];
// attach methods to this._ethereumCall
var _ethereumCall = {};
_.each(_ethereumCalls, function (mthd) {
_ethereumCalls.forEach(mthd => {
mthd.attachToObject(_ethereumCall);
mthd.requestManager = method.requestManager; // assign rather than call setRequestManager()
});
Expand Down Expand Up @@ -591,11 +592,11 @@ var getWallet = function (from, accounts) {
var wallet = null;

// is index given
if (_.isNumber(from)) {
if (typeof from === 'number') {
wallet = accounts.wallet[from];

// is account given
} else if (_.isObject(from) && from.address && from.privateKey) {
} else if (typeof from === 'object' && !!from && from.address && from.privateKey) {
wallet = from;

// search in wallet for address
Expand Down Expand Up @@ -689,10 +690,10 @@ Method.prototype.buildCall = function () {
// SENDS the SIGNED SIGNATURE
var sendSignedTx = function (sign) {

var signedPayload = _.extend({}, payload, {
var signedPayload = { ... payload,
method: 'eth_sendRawTransaction',
params: [sign.rawTransaction]
});
};

method.requestManager.send(signedPayload, sendTxCallback);
};
Expand All @@ -706,12 +707,13 @@ Method.prototype.buildCall = function () {
// ETH_SENDTRANSACTION
if (payload.method === 'eth_sendTransaction') {
var tx = payload.params[0];
wallet = getWallet((_.isObject(tx)) ? tx.from : null, method.accounts);
wallet = getWallet((typeof tx === 'object' && !!tx) ? tx.from : null, method.accounts);


// If wallet was found, sign tx, and send using sendRawTransaction
if (wallet && wallet.privateKey) {
var txOptions = _.omit(tx, 'from');
var txOptions = JSON.parse(JSON.stringify(tx));
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
delete txOptions['from'];
luu-alex marked this conversation as resolved.
Show resolved Hide resolved

if (method.defaultChain && !txOptions.chain) {
txOptions.chain = method.defaultChain;
Expand All @@ -728,7 +730,7 @@ Method.prototype.buildCall = function () {
method.accounts.signTransaction(txOptions, wallet.privateKey)
.then(sendSignedTx)
.catch(function (err) {
if (_.isFunction(defer.eventEmitter.listeners) && defer.eventEmitter.listeners('error').length) {
if (typeof defer.eventEmitter.listeners === 'function' && defer.eventEmitter.listeners('error').length) {
try {
defer.eventEmitter.emit('error', err);
} catch (err) {
Expand Down Expand Up @@ -769,7 +771,7 @@ Method.prototype.buildCall = function () {
};

// Send the actual transaction
if (isSendTx && _.isObject(payload.params[0]) && typeof payload.params[0].gasPrice === 'undefined') {
if (isSendTx && typeof payload.params[0] === 'object' && !!payload.params[0] && typeof payload.params[0].gasPrice === 'undefined') {

var getGasPrice = (new Method({
name: 'getGasPrice',
Expand Down Expand Up @@ -866,7 +868,7 @@ Method.prototype.getRevertReason = function (txOptions, blockNumber) {
* @returns {Boolean}
*/
Method.prototype.isRevertReasonString = function (data) {
return _.isString(data) && ((data.length - 2) / 2) % 32 === 4 && data.substring(0, 10) === '0x08c379a0';
return typeof data === 'string' && ((data.length - 2) / 2) % 32 === 4 && data.substring(0, 10) === '0x08c379a0';
};

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


const { callbackify } = require('util');
var _ = require('underscore');
var errors = require('web3-core-helpers').errors;
var Jsonrpc = require('./jsonrpc.js');
var BatchManager = require('./batch.js');
Expand Down Expand Up @@ -207,7 +206,7 @@ RequestManager.prototype.sendBatch = function (data, callback) {
return callback(err);
}

if (!_.isArray(results)) {
if (!Array.isArray(results)) {
return callback(errors.InvalidResponse(results));
}

Expand Down
21 changes: 12 additions & 9 deletions packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@

"use strict";

var _ = require('underscore');
var errors = require('web3-core-helpers').errors;
var EventEmitter = require('eventemitter3');
var formatters = require('web3-core-helpers').formatters;

function identity(value) {
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
return value;
}

function Subscription(options) {
EventEmitter.call(this);

this.id = null;
this.callback = _.identity;
this.callback = identity;
this.arguments = null;
this.lastBlock = null; // "from" block tracker for backfilling events on reconnection

Expand All @@ -56,7 +59,7 @@ Subscription.prototype.constructor = Subscription;
*/

Subscription.prototype._extractCallback = function (args) {
if (_.isFunction(args[args.length - 1])) {
if (typeof args[args.length - 1] === 'function') {
return args.pop(); // modify the args array!
}
};
Expand Down Expand Up @@ -136,7 +139,7 @@ Subscription.prototype._formatOutput = function (result) {
*/
Subscription.prototype._toPayload = function (args) {
var params = [];
this.callback = this._extractCallback(args) || _.identity;
this.callback = this._extractCallback(args) || identity;

if (!this.subscriptionMethod) {
this.subscriptionMethod = args.shift();
Expand Down Expand Up @@ -226,7 +229,7 @@ Subscription.prototype.subscribe = function() {

// Re-subscription only: continue fetching from the last block we received.
// a dropped connection may have resulted in gaps in the logs...
if (this.lastBlock && _.isObject(this.options.params)){
if (this.lastBlock && typeof this.options.params === 'object' && !!this.options.params ){
payload.params[1] = this.options.params;
payload.params[1].fromBlock = formatters.inputBlockNumberFormatter(this.lastBlock + 1);
}
Expand All @@ -240,7 +243,7 @@ Subscription.prototype.subscribe = function() {
this.options.params = payload.params[1];

// get past logs, if fromBlock is available
if(payload.params[0] === 'logs' && _.isObject(payload.params[1]) && payload.params[1].hasOwnProperty('fromBlock') && isFinite(payload.params[1].fromBlock)) {
if(payload.params[0] === 'logs' && typeof payload.params[1] === 'object' && !!payload.params[1] && payload.params[1].hasOwnProperty('fromBlock') && isFinite(payload.params[1].fromBlock)) {
// send the subscription request

// copy the params to avoid race-condition with deletion below this block
Expand Down Expand Up @@ -283,17 +286,17 @@ Subscription.prototype.subscribe = function() {
// call callback on notifications
_this.options.requestManager.addSubscription(_this, function(error, result) {
if (!error) {
if (!_.isArray(result)) {
if (!Array.isArray(result)) {
result = [result];
}

result.forEach(function(resultItem) {
var output = _this._formatOutput(resultItem);

// Track current block (for gaps introduced by dropped connections)
_this.lastBlock = _.isObject(output) ? output.blockNumber : null;
_this.lastBlock = typeof output === 'object' && !!output ? output.blockNumber : null;

if (_.isFunction(_this.options.subscription.subscriptionHandler)) {
if (typeof _this.options.subscription.subscriptionHandler === 'function' ) {
return _this.options.subscription.subscriptionHandler.call(_this, output);
} else {
_this.emit('data', output);
Expand Down
11 changes: 5 additions & 6 deletions packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
*/

var Buffer = require('buffer').Buffer;
var _ = require('underscore');
var utils = require('web3-utils');

var EthersAbiCoder = require('@ethersproject/abi').AbiCoder;
var ParamType = require('@ethersproject/abi').ParamType;
var ethersAbiCoder = new EthersAbiCoder(function (type, value) {
if (type.match(/^u?int/) && !_.isArray(value) && (!_.isObject(value) || value.constructor.name !== 'BN')) {
if (type.match(/^u?int/) && !Array.isArray(value) && (!(typeof value === 'object' && !!value) || value.constructor.name !== 'BN')) {
return value.toString();
}
return value;
Expand All @@ -52,7 +51,7 @@ var ABICoder = function () {
* @return {String} encoded function name
*/
ABICoder.prototype.encodeFunctionSignature = function (functionName) {
if (_.isObject(functionName)) {
if (typeof functionName === 'function' || typeof functionName === 'object' && functionName) {
functionName = utils._jsonInterfaceMethodToString(functionName);
}

Expand All @@ -67,7 +66,7 @@ ABICoder.prototype.encodeFunctionSignature = function (functionName) {
* @return {String} encoded function name
*/
ABICoder.prototype.encodeEventSignature = function (functionName) {
if (_.isObject(functionName)) {
if (typeof functionName === 'function' || typeof functionName === 'object' && functionName) {
functionName = utils._jsonInterfaceMethodToString(functionName);
}

Expand Down Expand Up @@ -367,7 +366,7 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) {

returnValue[i] = decodedValue;

if (_.isObject(output) && output.name) {
if ((typeof output === 'function' || typeof output === 'object' && !!output) && output.name) {
returnValue[output.name] = decodedValue;
}

Expand All @@ -388,7 +387,7 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) {
*/
ABICoder.prototype.decodeLog = function (inputs, data, topics) {
var _this = this;
topics = _.isArray(topics) ? topics : [topics];
topics = Array.isArray(topics) ? topics : [topics];

data = data || '';

Expand Down
Loading