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 util type checks #3135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/actions/LNActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getWalletCfg } from "../config";
import { getWalletPath } from "main_dev/paths";
import { getNextAccountAttempt } from "./ControlActions";
import * as cfgConstants from "constants/config";
import { isString, isNumber } from "lodash";

export const CLOSETYPE_COOPERATIVE_CLOSE = 0;
export const CLOSETYPE_LOCAL_FORCE_CLOSE = 1;
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
3 changes: 2 additions & 1 deletion app/components/buttons/EyeFilterMenu/EyeFilterMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useRef } from "react";
import { classNames } from "pi-ui";
import InvisibleButton from "../InvisibleButton";
import { useClickOutside } from "hooks";
import { isArray } from "lodash";
import styles from "./EyeFilterMenu.module.css";

const EyeFilterMenu = ({
Expand Down Expand Up @@ -41,7 +42,7 @@ const EyeFilterMenu = ({
key={i}
className={classNames(
styles.contextMenuItem,
(Array.isArray(selected)
(isArray(selected)
? selected.includes(option.key)
: selected === option.key) && styles.selected
)}
Expand Down
2 changes: 1 addition & 1 deletion app/components/charts/VoteTimeChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
radiusFull,
hoverFill
} from "./Styles";
import { isArray } from "util";
import { isArray } from "lodash";
import { FormattedMessage as T } from "react-intl";
import styles from "./Charts.module.css";
import { useChart } from "./hooks";
Expand Down
3 changes: 2 additions & 1 deletion app/components/inputs/AccountsSelect/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { usePrevious } from "hooks";
import { useIntl } from "react-intl";
import { defineMessages } from "react-intl";
import { useState, useEffect, useCallback } from "react";
import { isArray } from "lodash";
import { isEqual } from "lodash/fp";

const messages = defineMessages({
Expand Down Expand Up @@ -39,7 +40,7 @@ export const useAccountsSelect = ({
let filteredAccounts = accountsPerType[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
5 changes: 3 additions & 2 deletions app/components/layout/TabbedPage/TabbedPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RoutedTabsHeader, RoutedTab } from "shared";
import { getStyles, getMatchedTab, willEnter, willLeave } from "./helpers";
import TabbedPageTab from "./TabbedPageTab";
import styles from "./TabbedPage.module.css";
import { isFunction, isArray } from "lodash";

// returns the state.styles in a static container, without animations.
const staticStyles = (stylesObj, contentClassName) => (
Expand Down Expand Up @@ -84,7 +85,7 @@ const TabbedPage = ({

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
Expand All @@ -100,7 +101,7 @@ const TabbedPage = ({
setMatchedTab(matchedTab);
}, [location, previous, children, onChange]);

if (!Array.isArray(children)) children = [children];
if (!isArray(children)) children = [children];

const tabs = children.filter(
({ type, props: { disabled } }) => type === TabbedPageTab && !disabled
Expand Down
3 changes: 2 additions & 1 deletion app/components/layout/TabbedPage/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createElement as h, cloneElement as k } from "react";
import { matchPath } from "react-router-dom";
import { isArray } from "lodash";
import { spring } from "react-motion";
import theme from "theme";
import TabbedPageTab from "./TabbedPageTab";

const getTabs = (children) => {
if (!Array.isArray(children)) children = [children];
if (!isArray(children)) children = [children];
return children
.filter((c) => c.type === TabbedPageTab)
.map((c, i) => ({ index: i, tab: c }));
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/TransitionMotionWrapper.jsx
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
@@ -1,4 +1,5 @@
import { FormattedMessage as T } from "react-intl";
import { isArray } from "lodash";
import { Tooltip } from "pi-ui";
import { SeedHexEntry } from "inputs";
import { TextToggle } from "buttons";
Expand Down Expand Up @@ -57,7 +58,7 @@ const ExistingSeedForm = ({
<Section className={styles.confirmSeedLabel}>
<ConfirmSeedMsg />
</Section>
{seedType === WORDS && Array.isArray(seedWords) ? (
{seedType === WORDS && isArray(seedWords) ? (
<SeedArea>
{seedWords.map((seedWord, index) => (
<SeedWord
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 @@ -23,6 +23,7 @@ import { useMachine } from "@xstate/react";
import { getStartedMachine } from "stateMachines/GetStartedStateMachine";
import { AdvancedStartupBody } from "./AdvancedStartup/AdvancedStartup";
import styles from "./GetStarted.module.css";
import { isObject } from "lodash";

export const useGetStarted = () => {
const {
Expand Down Expand Up @@ -232,7 +233,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 @@ -3,11 +3,12 @@ import { useState, useCallback, useMemo } from "react";
import { useMachine } from "@xstate/react";
import { fetchMachine } from "stateMachines/FetchStateMachine";
import * as gov from "actions/GovernanceActions";
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 @@ -3,7 +3,7 @@ import { spring } from "react-motion";
import { PurchaseTicketsForm } from "shared";
import PurchaseTicketsAdvanced from "./PurchaseTicketsAdvanced";
import PurchaseTicketsQuickBar from "./PurchaseTicketsQuickBar";
import { isNullOrUndefined } from "util";
import { isNil } from "lodash";
import { usePurchaseTickets } from "./hooks";
import { MAX_POSSIBLE_FEE_INPUT } from "constants";

Expand Down Expand Up @@ -206,7 +206,7 @@ const PurchaseTickets = ({

const onChangeExpiry = (expiry) => {
const expiryError =
isNaN(expiry) || expiry < 0 || isNullOrUndefined(expiry) || expiry === "";
isNaN(expiry) || expiry < 0 || isNil(expiry) || expiry === "";
setExpiry(expiry.replace(/[^\d.]/g, ""));
setExpiryError(expiryError);
};
Expand Down
3 changes: 2 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Store from "electron-store";
import ini from "ini";
import path from "path";
import { stakePoolInfo } from "./middleware/vspapi";
import { isArray } from "lodash";
import {
getGlobalCfgPath,
getWalletPath,
Expand Down Expand Up @@ -135,7 +136,7 @@ export function getDcrdCert(dcrdCertPath) {
export function updateStakePoolConfig(config, foundStakePoolConfigs) {
const currentStakePoolConfigs =
config.has(cfgConstants.STAKEPOOLS) &&
Array.isArray(config.get(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 @@ -13,9 +13,9 @@ export {
} from "lodash/fp";
export { concat, isString, cloneDeep } from "lodash";

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

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
4 changes: 2 additions & 2 deletions 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 util from "util";
import { isRegExp } from "lodash";
import { getGlobalCfg } from "config";
import {
POLITEIA_URL_TESTNET,
Expand Down Expand Up @@ -144,7 +144,7 @@ export const installSessionHandlers = (mainLogger) => {
};

const addAllowedURL = (url) => {
if (!util.types.isRegExp(url)) url = new RegExp(url);
if (!isRegExp(url)) url = new RegExp(url);
allowedURLs.push(url);
};

Expand Down
18 changes: 9 additions & 9 deletions app/main_dev/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 @@ -389,7 +389,7 @@ export const launchDCRD = (reactIPC, testnet, appdata) =>
try {
const win32ipc = require("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 @@ -667,10 +667,10 @@ export const launchDCRWallet = (
try {
const win32ipc = require("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 @@ -707,16 +707,16 @@ export const launchDCRWallet = (
// We're doing this after logger to avoid user/pass being logged. It's randomly
// set each start, but better to be safe.
if (rpcUser) {
args.push(util.format("--username=%s", rpcUser));
args.push(format("--username=%s", rpcUser));
}
if (rpcPass) {
args.push(util.format("--password=%s", rpcPass));
args.push(format("--password=%s", rpcPass));
}
if (rpcListen) {
args.push(util.format("--rpclisten=%s", rpcListen));
args.push(format("--rpclisten=%s", rpcListen));
}
if (rpcCert) {
args.push(util.format("--rpccert=%s", rpcCert));
args.push(format("--rpccert=%s", rpcCert));
}

const dcrwallet = spawn(dcrwExe, args, {
Expand Down Expand Up @@ -830,7 +830,7 @@ export const launchDCRLnd = (
try {
const win32ipc = require("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
8 changes: 3 additions & 5 deletions app/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
VSP_FEE_PROCESS_ERRORED
} from "constants";
import * as wallet from "wallet";
import { isFunction, isArray } from "lodash";

const EMPTY_ARRAY = []; // Maintaining identity (will) improve performance;

Expand Down Expand Up @@ -883,7 +884,7 @@ export const lastVotedTicket = createSelector(
[]
);

return Array.isArray(lastVotedTicket) ? null : lastVotedTicket;
return isArray(lastVotedTicket) ? null : lastVotedTicket;
}
);

Expand Down Expand Up @@ -1667,10 +1668,7 @@ const normalizeAgenda = createSelector([currentAgenda], (currentAgenda) => {
// When agenda has getId function (this happens when dcrdata privacy is disabled
// or a possible dcrdata crash) or the agenda is the same for dcrwallet and dcrdata.
// We use the information from our dcrwallet grpc request.
if (
typeof agenda.getId === "function" ||
currentAgenda.getId() === agenda.name
) {
if (isFunction(agenda.getId) || currentAgenda.getId() === agenda.name) {
currentAgenda.isCurrent = true;
const agendaObj = {};
agendaObj.name = currentAgenda.getId();
Expand Down
3 changes: 2 additions & 1 deletion app/stateMachines/GetStartedStateMachine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Machine, assign, spawn } from "xstate";
import { CreateWalletMachine } from "stateMachines/CreateWalletStateMachine";
import { SetupWalletConfigMachine } from "stateMachines/SetupWalletConfigMachine";
import { isUndefined } from "lodash";

export const getStartedMachine = Machine({
id: "getStarted",
Expand Down Expand Up @@ -190,7 +191,7 @@ export const getStartedMachine = Machine({
target: "preCreateWallet",
actions: assign({
isCreateNewWallet: (context, event) =>
typeof event.isNew !== "undefined"
!isUndefined(event.isNew)
? event.isNew
: context.isCreateNewWallet
})
Expand Down
10 changes: 8 additions & 2 deletions app/wallet/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ipcRenderer } from "electron";
import { isObject, isString, isNumber, isUndefined, isNull } from "util";
import { isFunction } from "util";
import {
isObject,
isString,
isNumber,
isUndefined,
isNull,
isFunction
} from "lodash";

export const onAppReloadRequested = (cb) =>
ipcRenderer.on("app-reload-requested", cb);
Expand Down
3 changes: 2 additions & 1 deletion app/wallet/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
VSP_FEE_PROCESS_PAID,
VSP_FEE_PROCESS_ERRORED
} from "constants";
import { isUndefined } from "lodash";

const hexToBytes = (hex) => {
const bytes = [];
Expand Down Expand Up @@ -517,7 +518,7 @@ export const startTicketAutoBuyerV3 = (
!changeAccount ||
!csppServer ||
!csppPort ||
typeof mixedAcctBranch === "undefined"
isUndefined(mixedAcctBranch)
) {
throw "missing cspp argument";
}
Expand Down