Skip to content

Commit

Permalink
Replace lodash isFunction with native code
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot committed Feb 12, 2024
1 parent ecc4269 commit 6cb14d7
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 128 deletions.
25 changes: 25 additions & 0 deletions .changeset/weak-apes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"victory-area": patch
"victory-bar": patch
"victory-box-plot": patch
"victory-brush-container": patch
"victory-brush-line": patch
"victory-candlestick": patch
"victory-core": patch
"victory-create-container": patch
"victory-cursor-container": patch
"victory-errorbar": patch
"victory-histogram": patch
"victory-legend": patch
"victory-line": patch
"victory-native": patch
"victory-pie": patch
"victory-scatter": patch
"victory-selection-container": patch
"victory-shared-events": patch
"victory-voronoi": patch
"victory-voronoi-container": patch
"victory-zoom-container": patch
---

Replace lodash isFunction with native code
16 changes: 8 additions & 8 deletions packages/victory-brush-container/src/brush-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Selection } from "victory-core";
import { throttle, isFunction, defaults, mapValues } from "lodash";
import { Helpers as CoreHelpers, Selection } from "victory-core";
import { throttle, defaults, mapValues } from "lodash";
import isEqual from "react-fast-compare";

const Helpers = {
Expand Down Expand Up @@ -370,7 +370,7 @@ const Helpers = {
...constrainedBox,
};

if (isFunction(onBrushDomainChange)) {
if (CoreHelpers.isFunction(onBrushDomainChange)) {
onBrushDomainChange(
currentDomain,
defaults({}, mutatedProps, targetProps),
Expand Down Expand Up @@ -400,7 +400,7 @@ const Helpers = {
});

const mutatedProps = { x2, y2, currentDomain, parentSVG };
if (isFunction(onBrushDomainChange)) {
if (CoreHelpers.isFunction(onBrushDomainChange)) {
onBrushDomainChange(
currentDomain,
defaults({}, mutatedProps, targetProps),
Expand Down Expand Up @@ -458,23 +458,23 @@ const Helpers = {
evt,
);
mutatedProps.currentDomain = defaultDomain;
if (isFunction(onBrushDomainChange)) {
if (CoreHelpers.isFunction(onBrushDomainChange)) {
onBrushDomainChange(
defaultDomain,
defaults({}, mutatedProps, targetProps),
);
}
if (isFunction(onBrushDomainChangeEnd)) {
if (CoreHelpers.isFunction(onBrushDomainChangeEnd)) {
onBrushDomainChangeEnd(
defaultDomain,
defaults({}, mutatedProps, targetProps),
);
}
if (isFunction(onBrushCleared)) {
if (CoreHelpers.isFunction(onBrushCleared)) {
onBrushCleared(defaultDomain, defaults({}, mutatedProps, targetProps));
}
} else if ((allowDrag && isPanning) || (allowResize && isSelecting)) {
if (isFunction(onBrushDomainChangeEnd)) {
if (CoreHelpers.isFunction(onBrushDomainChangeEnd)) {
onBrushDomainChangeEnd(
currentDomain,
defaults({}, mutatedProps, targetProps),
Expand Down
8 changes: 4 additions & 4 deletions packages/victory-brush-line/src/victory-brush-line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DomainTuple,
VictoryStyleObject,
} from "victory-core";
import { defaults, isFunction, pick } from "lodash";
import { defaults, pick } from "lodash";
import isEqual from "react-fast-compare";

export type VictoryBrushLineTargetType = "data" | "labels" | "parent";
Expand Down Expand Up @@ -372,7 +372,7 @@ export class VictoryBrushLine<
parentSVG,
};

if (isFunction(onBrushDomainChange)) {
if (Helpers.isFunction(onBrushDomainChange)) {
onBrushDomainChange(
currentDomain,
defaults({}, mutatedProps, targetProps),
Expand Down Expand Up @@ -423,7 +423,7 @@ export class VictoryBrushLine<
maxHandle: activeHandle === "max",
},
};
if (isFunction(onBrushDomainChange)) {
if (Helpers.isFunction(onBrushDomainChange)) {
onBrushDomainChange(
currentDomain,
defaults({}, mutatedProps, targetProps),
Expand Down Expand Up @@ -453,7 +453,7 @@ export class VictoryBrushLine<
brushDomain,
activeBrushes,
};
if (allowResize && isFunction(onBrushDomainChange)) {
if (allowResize && Helpers.isFunction(onBrushDomainChange)) {
onBrushDomainChange(
brushDomain,
defaults({}, mutatedProps, targetProps),
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-candlestick/src/candle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
VictoryCommonPrimitiveProps,
VictoryStyleObject,
} from "victory-core";
import { defaults, isFunction } from "lodash";
import { defaults } from "lodash";

export interface CandleProps extends VictoryCommonPrimitiveProps {
candleRatio?: number;
Expand All @@ -31,7 +31,7 @@ const getCandleWidth = (
) => {
const { style } = props;
if (candleWidth) {
return isFunction(candleWidth)
return Helpers.isFunction(candleWidth)
? Helpers.evaluateProp(candleWidth, props)
: candleWidth;
} else if (style.width) {
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-candlestick/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, isFunction, isPlainObject } from "lodash";
import { defaults, isPlainObject } from "lodash";
import {
Helpers,
Scale,
Expand Down Expand Up @@ -224,7 +224,7 @@ const getText = (props, type) => {
const getCandleWidth = (props, style?: VictoryStyleObject) => {
const { data, candleWidth, scale } = props;
if (candleWidth) {
return isFunction(candleWidth)
return Helpers.isFunction(candleWidth)
? Helpers.evaluateProp(candleWidth, props)
: candleWidth;
} else if (style && style.width) {
Expand Down
1 change: 1 addition & 0 deletions packages/victory-core/src/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ describe("victory-core", () => {
"getRadius": [Function],
"getRange": [Function],
"getStyles": [Function],
"isFunction": [Function],
"isHorizontal": [Function],
"isNil": [Function],
"isTooltip": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { defaults, uniqueId, isObject, isFunction } from "lodash";
import { defaults, uniqueId, isObject } from "lodash";
import { Portal } from "../victory-portal/portal";
import { PortalContext } from "../victory-portal/portal-context";
import TimerContext from "../victory-util/timer-context";
Expand Down Expand Up @@ -81,7 +81,7 @@ export class VictoryContainer<
portalDeregister = (key) => this.portalRef.portalDeregister(key);

saveContainerRef = (container: HTMLElement) => {
if (isFunction(this.props.containerRef)) {
if (Helpers.isFunction(this.props.containerRef)) {
this.props.containerRef(container);
}
this.containerRef = container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Collection from "../victory-util/collection";
import * as Helpers from "../victory-util/helpers";
import TimerContext from "../victory-util/timer-context";
import * as Transitions from "../victory-util/transitions";
import { defaults, isFunction, pick } from "lodash";
import { defaults, pick } from "lodash";
import isEqual from "react-fast-compare";
import Timer from "../victory-util/timer";

Expand Down Expand Up @@ -119,7 +119,7 @@ export class VictoryTransition extends React.Component<
) {
const getChildDomains = (children) => {
return children.reduce((memo, child) => {
if (child.type && isFunction(child.type.getDomain)) {
if (child.type && Helpers.isFunction(child.type.getDomain)) {
const childDomain =
child.props && child.type.getDomain(child.props, axis);
return childDomain ? memo.concat(childDomain) : memo;
Expand Down
12 changes: 2 additions & 10 deletions packages/victory-core/src/victory-util/add-events.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import React from "react";
import {
defaults,
difference,
isEmpty,
isFunction,
keys,
pick,
without,
} from "lodash";
import { defaults, difference, isEmpty, keys, pick, without } from "lodash";
import type { ComponentEvent } from "./events";
import * as Events from "./events";
import isEqual from "react-fast-compare";
import { VictoryTransition } from "../victory-transition/victory-transition";
import { VictoryCommonProps, VictoryDatableProps } from "./common-props";
import { VictoryLabelableProps } from "../types/prop-types";
import { isNil } from "./helpers";
import { isFunction, isNil } from "./helpers";

// DISCLAIMER:
// This file is not currently tested, and it is first on the list of files
Expand Down
9 changes: 4 additions & 5 deletions packages/victory-core/src/victory-util/axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from "react";
import {
defaults,
identity,
isFunction,
isObject,
invert,
uniq,
Expand Down Expand Up @@ -250,7 +249,7 @@ export function getTickFormat(props, scale) {
// by default. This changed the default formatting for some scale types when
// we upgraded to d3-scale@4..
const scaleTickFormat =
scale.tickFormat && isFunction(scale.tickFormat)
scale.tickFormat && Helpers.isFunction(scale.tickFormat)
? scale.tickFormat()
: (x) => x;
return defaultTickFormat || scaleTickFormat;
Expand All @@ -261,7 +260,7 @@ export function getTickFormat(props, scale) {
tickArrayIndices?.includes(index),
);
return (x, index) => filteredTickFormat[index];
} else if (tickFormat && isFunction(tickFormat)) {
} else if (tickFormat && Helpers.isFunction(tickFormat)) {
const applyStringTicks = (tick, index, ticks) => {
const invertedStringMap = invert(stringMap);
const stringTickArray = ticks.map((t) => invertedStringMap[t]);
Expand Down Expand Up @@ -291,7 +290,7 @@ export function getTicks(props, scale: D3Scale, filterZero = false) {
const tickValues = tickArray ? tickArray.map((v) => v.value) : undefined;
if (tickValues) {
return downsampleTicks(tickValues, tickCount);
} else if (scale.ticks && isFunction(scale.ticks)) {
} else if (scale.ticks && Helpers.isFunction(scale.ticks)) {
// eslint-disable-next-line no-magic-numbers
const defaultTickCount = tickCount || 5;
const scaleTicks = scale.ticks(defaultTickCount);
Expand Down Expand Up @@ -362,7 +361,7 @@ export function getAxisValue(props, axis) {
}
const scaleAxis = axis === "x" ? "y" : "x";
const scale =
isObject(props.scale) && isFunction(props.scale[scaleAxis])
isObject(props.scale) && Helpers.isFunction(props.scale[scaleAxis])
? props.scale[scaleAxis]
: undefined;
if (!scale) {
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-core/src/victory-util/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from "react";
import {
uniq,
last,
isFunction,
isPlainObject,
property,
orderBy,
Expand Down Expand Up @@ -97,7 +96,7 @@ function cleanData(dataset, props) {
function getEventKey(key) {
// creates a data accessor function
// given a property key, path, array index, or null for identity.
if (isFunction(key)) {
if (Helpers.isFunction(key)) {
return key;
} else if (key === null || key === undefined) {
return () => undefined;
Expand Down
13 changes: 3 additions & 10 deletions packages/victory-core/src/victory-util/domain.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
/* eslint-disable no-use-before-define */
import React from "react";
import {
flatten,
isPlainObject,
sortedUniq,
isFunction,
includes,
isDate,
} from "lodash";
import { flatten, isPlainObject, sortedUniq, includes, isDate } from "lodash";
import * as Data from "./data";
import * as Scale from "./scale";
import * as Helpers from "./helpers";
Expand Down Expand Up @@ -183,10 +176,10 @@ export function createDomainFunction(
getDomainFromDataFunction?,
formatDomainFunction?,
) {
const getDomainFromDataFn = isFunction(getDomainFromDataFunction)
const getDomainFromDataFn = Helpers.isFunction(getDomainFromDataFunction)
? getDomainFromDataFunction
: getDomainFromData;
const formatDomainFn = isFunction(formatDomainFunction)
const formatDomainFn = Helpers.isFunction(formatDomainFunction)
? formatDomainFunction
: formatDomain;
return (props, axis) => {
Expand Down
12 changes: 2 additions & 10 deletions packages/victory-core/src/victory-util/events.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/* eslint-disable no-use-before-define */
import {
isEmpty,
isFunction,
without,
pickBy,
omitBy,
uniq,
includes,
keys,
} from "lodash";
import { isEmpty, without, pickBy, omitBy, uniq, includes, keys } from "lodash";
import type { EventMixinCalculatedValues } from "./add-events";
import { isFunction } from "./helpers";

const GLOBAL_EVENT_REGEX = /^onGlobal(.*)$/;

Expand Down
13 changes: 12 additions & 1 deletion packages/victory-core/src/victory-util/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-use-before-define */
import React, { isValidElement } from "react";
import { defaults, isFunction, property, pick, keys } from "lodash";
import { defaults, property, pick, keys } from "lodash";
import { CallbackArgs } from "../types/callbacks";
import { ValueOrAccessor } from "../types/prop-types";

Expand Down Expand Up @@ -194,6 +194,17 @@ export function isNil(value: any): boolean {
return value == null;
}

/**
* Checks if `value` is classified as a `Function` object.
*
* @since 0.1.0
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a function, else `false`.
*/
export function isFunction(value: any): value is (...args: any[]) => any {
return typeof value === "function";
}

export function createAccessor(key) {
// creates a data accessor function
// given a property key, path, array index, or null for identity.
Expand Down
10 changes: 5 additions & 5 deletions packages/victory-core/src/victory-util/scale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define */
import { includes, isFunction, isPlainObject } from "lodash";
import { includes, isPlainObject } from "lodash";
import * as Helpers from "./helpers";
import * as Collection from "./collection";
import * as d3Scale from "victory-vendor/d3-scale";
Expand All @@ -25,9 +25,9 @@ export function validScale(
): scale is ScaleName | D3Scale {
if (typeof scale === "function") {
return (
isFunction(scale.copy) &&
isFunction(scale.domain) &&
isFunction(scale.range)
Helpers.isFunction(scale.copy) &&
Helpers.isFunction(scale.domain) &&
Helpers.isFunction(scale.range)
);
} else if (typeof scale === "string") {
return includes(supportedScaleStrings, scale);
Expand Down Expand Up @@ -110,7 +110,7 @@ export function getScaleFromProps(props, axis): D3Scale | undefined {
}
const scale = props.scale[axis] || props.scale;
if (validScale(scale)) {
return isFunction(scale) ? scale : getScaleFromName(scale);
return Helpers.isFunction(scale) ? scale : getScaleFromName(scale);
}
return undefined;
}
Expand Down

0 comments on commit 6cb14d7

Please sign in to comment.