Skip to content

Commit

Permalink
Use lodash solutions instead of typeof operator and Array.isArray method
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermemntt committed Jan 10, 2021
1 parent 60ae633 commit 76ed888
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 91 deletions.
8 changes: 5 additions & 3 deletions app/actions/ControlActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import * as wallet from "wallet";
import * as sel from "selectors";
import { isUndefined } from "lodash";
import { isValidAddress, isValidMasterPubKey } from "helpers";
import {
getStakeInfoAttempt,
Expand Down Expand Up @@ -389,14 +390,15 @@ export const newPurchaseTicketsAttempt = (
// we need at least one 2 utxo for each ticket, one for paying the fee
// and another for the splitTx and ticket purchase.
// Note: at least one of them needs to be big enough for ticket purchase.
if (unspentOutputs.length < numTickets*2) {
if (unspentOutputs.length < numTickets * 2) {
// check if amount is indeed insufficient
const ticketPrice = sel.ticketPrice(getState());
if (accountNum.spendable > ticketPrice * numTickets) {
return dispatch({
error: `Not enough utxo. Need to break the input so one can be reserved
for paying the fee.`,
type: PURCHASETICKETS_FAILED });
type: PURCHASETICKETS_FAILED
});
}
}
}
Expand Down Expand Up @@ -448,7 +450,7 @@ export const startTicketBuyerV3Attempt = (
!changeAccount ||
!csppServer ||
!csppPort ||
typeof mixedAcctBranch === "undefined"
isUndefined(mixedAcctBranch)
) {
throw "missing cspp argument";
}
Expand Down
5 changes: 3 additions & 2 deletions app/actions/LNActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ln from "wallet/ln";
import * as sel from "selectors";
import * as wallet from "wallet";
import { ipcRenderer } from "electron";
import { isNumber, isString } from "lodash";
import { getWalletCfg } from "../config";
import { getWalletPath } from "main_dev/paths";
import { getNextAccountAttempt } from "./ControlActions";
Expand Down Expand Up @@ -80,7 +81,7 @@ export const startDcrlnd = (
dispatch({ type: LNWALLET_STARTUP_FAILED });
throw error;
}
} else if (typeof walletAccount === "number") {
} else if (isNumber(walletAccount)) {
lnAccount = walletAccount;
creating = true;
}
Expand All @@ -105,7 +106,7 @@ export const startDcrlnd = (
isTestnet,
autopilotEnabled
);
if (typeof res === "string" || res instanceof Error) {
if (isString(res) || res instanceof Error) {
throw res;
}
dcrlndCreds = res;
Expand Down
4 changes: 2 additions & 2 deletions app/actions/StatisticsActions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as wallet from "wallet";
import * as sel from "selectors";
import fs from "fs";
import { isNumber, isNullOrUndefined, isUndefined } from "util";
import { isNumber, isNil, isUndefined } from "lodash";
import { endOfDay, formatLocalISODate, isSameDate } from "helpers";
import {
REGULAR,
Expand Down Expand Up @@ -355,7 +355,7 @@ export const exportStatToCSV = (opts) => (dispatch, getState) => {
const quote = (v) => '"' + v.replace('"', '\\"') + '"';
const formatTime = (v) => (v ? formatLocalISODate(v, timezone) : "");
const csvValue = (v) =>
isNullOrUndefined(v) ? "" : isNumber(v) ? v.toFixed(precision) : quote(v);
isNil(v) ? "" : isNumber(v) ? v.toFixed(precision) : quote(v);
const csvLine = (values) => values.map(csvValue).join(vsep);

const seriesValueFormatFunc = (series) => {
Expand Down
2 changes: 1 addition & 1 deletion app/components/charts/VoteTimeChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
radiusFull,
hoverFill
} from "./Styles";
import { isArray } from "util";
import { isArray } from "lodash";
import { FormattedMessage as T } from "react-intl";

const ChartTooltip = ({ payload }) => {
Expand Down
3 changes: 2 additions & 1 deletion app/components/inputs/AccountsSelect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Select from "react-select";
import { accountsSelect } from "connectors";
import { injectIntl, defineMessages } from "react-intl";
import { isArray } from "lodash";
import { Balance, LinkToAccounts } from "shared";

const messages = defineMessages({
Expand Down Expand Up @@ -68,7 +69,7 @@ class AccountsSelect extends React.Component {
let filteredAccounts = accountsPerType[this.props.accountsType || type];
// filterAccounts remove accounts if needed. This is usefull to remove accounts
// which are not supposed to be shown, for example, mixed accounts in privacy wallets
if (Array.isArray(filterAccounts)) {
if (isArray(filterAccounts)) {
filteredAccounts = filteredAccounts.filter(
({ value }) => !filterAccounts.includes(value)
);
Expand Down
4 changes: 2 additions & 2 deletions app/components/inputs/Input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FormattedMessage as T } from "react-intl";
import "style/Input.less";
import { isNullOrUndefined } from "util";
import { isNil } from "lodash";

class Input extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -98,7 +98,7 @@ class Input extends React.Component {
disabled={disabled ? disabled : null}
readOnly={readOnly ? readOnly : null}
placeholder={placeholder}
value={isNullOrUndefined(value) ? "" : value}
value={isNil(value) ? "" : value}
onChange={onChange}
onFocus={this.onInputFocus}
onBlur={this.onInputBlur}
Expand Down
14 changes: 7 additions & 7 deletions app/components/layout/TabbedPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { classNames } from "pi-ui";
import { Switch, Route, matchPath } from "react-router-dom";
import { isArray } from "util";
import { isArray, isFunction } from "lodash";
import { RoutedTabsHeader, RoutedTab } from "shared";
import { TransitionMotion, spring } from "react-motion";
import theme from "theme";
Expand Down Expand Up @@ -42,12 +42,12 @@ function getStyles(matchedTab) {

const element = React.isValidElement(matchedTab.tab.props.component)
? k(matchedTab.tab.props.component, {
...matchedTab.tab.props,
...matchedTab.tab.props.component.props
})
...matchedTab.tab.props,
...matchedTab.tab.props.component.props
})
: // If the component props are needed, it is needed to make it a valid react element
// before send, otherwise they will be undfined.
h(matchedTab.tab.props.component, { ...matchedTab.tab.props }, null);
// before send, otherwise they will be undfined.
h(matchedTab.tab.props.component, { ...matchedTab.tab.props }, null);
return [
{
key: matchedTab.tab.props.path,
Expand Down Expand Up @@ -132,7 +132,7 @@ function TabbedPage({ children, header, className, onChange, caret }) {
}, [children, location]);
useEffect(() => {
if (previous && previous.location.pathname === location.pathname) return;
if (typeof onChange === "function") onChange();
if (isFunction(onChange)) onChange();
const matchedTab = getMatchedTab(location, children);
if (!matchedTab) return;
// if (previous && previous.matchedTab) is false, it probably means it is the first time rendering
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/TransitionMotionWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cloneElement as k, createElement as h } from "react";
import { TransitionMotion } from "react-motion";
import { isFunction } from "util";
import { isFunction } from "lodash";
import { useTheming } from "hooks";

const TransitionMotionWrapper = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SingleSeedWordEntry from "../SingleSeedWordEntry";
import SeedHexEntry from "inputs/SeedHexEntry";
import { TextToggle, KeyBlueButton, InvisibleButton } from "buttons";
import { FormattedMessage as T } from "react-intl";
import { isArray } from "lodash";
import {
ConfirmSeedMsg,
BackBtnMsg,
Expand Down Expand Up @@ -52,7 +53,7 @@ const ExistingSeedForm = ({
<div className={classNames(styles.confirmSeedLabel, styles.seed)}>
<ConfirmSeedMsg />
</div>
{seedType === WORDS && Array.isArray(seedWords) ? (
{seedType === WORDS && isArray(seedWords) ? (
<div className={styles.seedArea}>
{seedWords.map((seedWord, index) => (
<div
Expand Down
3 changes: 2 additions & 1 deletion app/components/views/GetStartedPage/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import WalletPubpassInput from "./OpenWallet/OpenWallet";
import DiscoverAccounts from "./OpenWallet/DiscoverAccounts";
import ReleaseNotes from "./ReleaseNotes/ReleaseNotes";
import { ipcRenderer } from "electron";
import { isObject } from "lodash";
import {
OPENWALLET_INPUT,
OPENWALLET_INPUTPRIVPASS
Expand Down Expand Up @@ -256,7 +257,7 @@ export const useGetStarted = () => {
}
// If the errors is an object but not a react component, we strigfy it so we can
// render.
if (typeof serviceError === "object") {
if (isObject(serviceError)) {
return JSON.stringify(serviceError);
}
return serviceError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import * as gov from "actions/GovernanceActions";
import styles from "../ProposalDetails.module.css";
import ChooseOptions from "./ChooseOptions";
import { FormattedMessage as T } from "react-intl";
import { isString, isObject } from "lodash";

const getError = (error) => {
if (!error) return;
if (typeof error === "string") return error;
if (typeof error === "object") {
if (isString(error)) return error;
if (isObject(error)) {
if (error.message) return error.message;
return JSON.stringify(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import purchaseTickets from "connectors/purchaseTickets";
import PurchaseTicketsAdvanced from "./PurchaseTicketsAdvanced";
import PurchaseTicketsQuickBar from "./PurchaseTicketsQuickBar";
import { injectIntl } from "react-intl";
import { isNullOrUndefined } from "util";
import { isNil } from "lodash";
import { MIN_RELAY_FEE, MAX_POSSIBLE_FEE_INPUT } from "constants";

@autobind
Expand Down Expand Up @@ -252,7 +252,7 @@ class PurchaseTickets extends React.Component {

onChangeExpiry(expiry) {
const expiryError =
isNaN(expiry) || expiry < 0 || isNullOrUndefined(expiry) || expiry === "";
isNaN(expiry) || expiry < 0 || isNil(expiry) || expiry === "";
this.setState({
expiry: expiry.replace(/[^\d.]/g, ""),
expiryError: expiryError
Expand Down
3 changes: 2 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import Store from "electron-store";
import ini from "ini";
import { isArray } from "lodash";
import { stakePoolInfo } from "./middleware/vspapi";
import {
getGlobalCfgPath,
Expand Down Expand Up @@ -132,7 +133,7 @@ export function getDcrdCert(dcrdCertPath) {

export function updateStakePoolConfig(config, foundStakePoolConfigs) {
const currentStakePoolConfigs =
config.has(cfgConstants.STAKEPOOLS) && Array.isArray(config.get(cfgConstants.STAKEPOOLS))
config.has(cfgConstants.STAKEPOOLS) && isArray(config.get(cfgConstants.STAKEPOOLS))
? config.get(cfgConstants.STAKEPOOLS)
: [];

Expand Down
2 changes: 1 addition & 1 deletion app/fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export { concat, isString, cloneDeep } from "lodash";

import compose from "lodash/fp/compose";
import get from "lodash/fp/get";
import { isFunction } from "util";
import { isFunction } from "lodash";

export const not = (fn) => (...args) => !fn(...args);
export const bool = compose(not, not);
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/strings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString } from "util";
import { isString } from "lodash";

// @flow

Expand Down
2 changes: 1 addition & 1 deletion app/main_dev/externalRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* allowed by passing the appropriate url to the ipcMain message "allowURL".
*/
import { session } from "electron";
import { isRegExp } from "util";
import { isRegExp } from "lodash";
import { getGlobalCfg } from "config";
import {
POLITEIA_URL_TESTNET,
Expand Down
14 changes: 7 additions & 7 deletions app/main_dev/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { OPTIONS, UPGD_ELECTRON8 } from "constants";
import * as cfgConstants from "constants/config";
import os from "os";
import fs from "fs-extra";
import util from "util";
import { format } from "util";
import { spawn } from "child_process";
import isRunning from "is-running";
import stringArgv from "string-argv";
Expand Down Expand Up @@ -104,7 +104,7 @@ export const getDcrwalletGrpcKeyCert = () => dcrwalletGrpcKeyCert;
export const setDcrwalletGrpcKeyCert = (grpcKeyCert) => {
if (!Buffer.isBuffer(grpcKeyCert)) {
logger.log("error", "Error getting grpc key and cert from dcrwallet, " +
"grpc key and cert value: " + grpcKeyCert);
"grpc key and cert value: " + grpcKeyCert);
}
dcrwalletGrpcKeyCert = grpcKeyCert;
};
Expand Down Expand Up @@ -314,7 +314,7 @@ export const launchDCRD = (reactIPC, testnet, appdata) =>
try {
const win32ipc = require("../node_modules/win32ipc/build/Release/win32ipc.node");
dcrdPipeRx = win32ipc.createPipe("out");
args.push(util.format("--piperx=%d", dcrdPipeRx.readEnd));
args.push(format("--piperx=%d", dcrdPipeRx.readEnd));
} catch (e) {
logger.log("error", "can't find proper module to launch dcrd: " + e);
}
Expand Down Expand Up @@ -537,7 +537,7 @@ export const launchDCRWallet = (
// When in mainnet, we always include it, because if we doensn't and a user
// sets mixing config, we would need to restart dcrwallet.
const certPath = path.resolve(getCertsPath(), "cspp.decred.org.pem");
!testnet && args.push("--csppserver.ca="+certPath);
!testnet && args.push("--csppserver.ca=" + certPath);
args.push(testnet ? "--csppserver=cspp.decred.org:5760" : "--csppserver=cspp.decred.org:15760");

const dcrwExe = getExecutablePath("dcrwallet", argv.custombinpath);
Expand Down Expand Up @@ -581,10 +581,10 @@ export const launchDCRWallet = (
try {
const win32ipc = require("../node_modules/win32ipc/build/Release/win32ipc.node");
dcrwPipeRx = win32ipc.createPipe("out");
args.push(util.format("--piperx=%d", dcrwPipeRx.readEnd));
args.push(format("--piperx=%d", dcrwPipeRx.readEnd));

dcrwPipeTx = win32ipc.createPipe("in");
args.push(util.format("--pipetx=%d", dcrwPipeTx.writeEnd));
args.push(format("--pipetx=%d", dcrwPipeTx.writeEnd));
args.push("--rpclistenerevents");
const pipeTxReadFd = win32ipc.getPipeEndFd(dcrwPipeTx.readEnd);
dcrwPipeTx.readEnd = -1; // -1 == INVALID_HANDLE_VALUE
Expand Down Expand Up @@ -728,7 +728,7 @@ export const launchDCRLnd = (
try {
const win32ipc = require("../node_modules/win32ipc/build/Release/win32ipc.node");
dcrlndPipeRx = win32ipc.createPipe("out");
args.push(util.format("--piperx=%d", dcrlndPipeRx.readEnd));
args.push(format("--piperx=%d", dcrlndPipeRx.readEnd));
} catch (e) {
logger.log("error", "can't find proper module to launch dcrlnd: " + e);
}
Expand Down

0 comments on commit 76ed888

Please sign in to comment.