Skip to content

Commit 6cb14d7

Browse files
committed
Replace lodash isFunction with native code
1 parent ecc4269 commit 6cb14d7

File tree

27 files changed

+124
-128
lines changed

27 files changed

+124
-128
lines changed

.changeset/weak-apes-clap.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"victory-area": patch
3+
"victory-bar": patch
4+
"victory-box-plot": patch
5+
"victory-brush-container": patch
6+
"victory-brush-line": patch
7+
"victory-candlestick": patch
8+
"victory-core": patch
9+
"victory-create-container": patch
10+
"victory-cursor-container": patch
11+
"victory-errorbar": patch
12+
"victory-histogram": patch
13+
"victory-legend": patch
14+
"victory-line": patch
15+
"victory-native": patch
16+
"victory-pie": patch
17+
"victory-scatter": patch
18+
"victory-selection-container": patch
19+
"victory-shared-events": patch
20+
"victory-voronoi": patch
21+
"victory-voronoi-container": patch
22+
"victory-zoom-container": patch
23+
---
24+
25+
Replace lodash isFunction with native code

packages/victory-brush-container/src/brush-helpers.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Selection } from "victory-core";
2-
import { throttle, isFunction, defaults, mapValues } from "lodash";
1+
import { Helpers as CoreHelpers, Selection } from "victory-core";
2+
import { throttle, defaults, mapValues } from "lodash";
33
import isEqual from "react-fast-compare";
44

55
const Helpers = {
@@ -370,7 +370,7 @@ const Helpers = {
370370
...constrainedBox,
371371
};
372372

373-
if (isFunction(onBrushDomainChange)) {
373+
if (CoreHelpers.isFunction(onBrushDomainChange)) {
374374
onBrushDomainChange(
375375
currentDomain,
376376
defaults({}, mutatedProps, targetProps),
@@ -400,7 +400,7 @@ const Helpers = {
400400
});
401401

402402
const mutatedProps = { x2, y2, currentDomain, parentSVG };
403-
if (isFunction(onBrushDomainChange)) {
403+
if (CoreHelpers.isFunction(onBrushDomainChange)) {
404404
onBrushDomainChange(
405405
currentDomain,
406406
defaults({}, mutatedProps, targetProps),
@@ -458,23 +458,23 @@ const Helpers = {
458458
evt,
459459
);
460460
mutatedProps.currentDomain = defaultDomain;
461-
if (isFunction(onBrushDomainChange)) {
461+
if (CoreHelpers.isFunction(onBrushDomainChange)) {
462462
onBrushDomainChange(
463463
defaultDomain,
464464
defaults({}, mutatedProps, targetProps),
465465
);
466466
}
467-
if (isFunction(onBrushDomainChangeEnd)) {
467+
if (CoreHelpers.isFunction(onBrushDomainChangeEnd)) {
468468
onBrushDomainChangeEnd(
469469
defaultDomain,
470470
defaults({}, mutatedProps, targetProps),
471471
);
472472
}
473-
if (isFunction(onBrushCleared)) {
473+
if (CoreHelpers.isFunction(onBrushCleared)) {
474474
onBrushCleared(defaultDomain, defaults({}, mutatedProps, targetProps));
475475
}
476476
} else if ((allowDrag && isPanning) || (allowResize && isSelecting)) {
477-
if (isFunction(onBrushDomainChangeEnd)) {
477+
if (CoreHelpers.isFunction(onBrushDomainChangeEnd)) {
478478
onBrushDomainChangeEnd(
479479
currentDomain,
480480
defaults({}, mutatedProps, targetProps),

packages/victory-brush-line/src/victory-brush-line.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
DomainTuple,
1111
VictoryStyleObject,
1212
} from "victory-core";
13-
import { defaults, isFunction, pick } from "lodash";
13+
import { defaults, pick } from "lodash";
1414
import isEqual from "react-fast-compare";
1515

1616
export type VictoryBrushLineTargetType = "data" | "labels" | "parent";
@@ -372,7 +372,7 @@ export class VictoryBrushLine<
372372
parentSVG,
373373
};
374374

375-
if (isFunction(onBrushDomainChange)) {
375+
if (Helpers.isFunction(onBrushDomainChange)) {
376376
onBrushDomainChange(
377377
currentDomain,
378378
defaults({}, mutatedProps, targetProps),
@@ -423,7 +423,7 @@ export class VictoryBrushLine<
423423
maxHandle: activeHandle === "max",
424424
},
425425
};
426-
if (isFunction(onBrushDomainChange)) {
426+
if (Helpers.isFunction(onBrushDomainChange)) {
427427
onBrushDomainChange(
428428
currentDomain,
429429
defaults({}, mutatedProps, targetProps),
@@ -453,7 +453,7 @@ export class VictoryBrushLine<
453453
brushDomain,
454454
activeBrushes,
455455
};
456-
if (allowResize && isFunction(onBrushDomainChange)) {
456+
if (allowResize && Helpers.isFunction(onBrushDomainChange)) {
457457
onBrushDomainChange(
458458
brushDomain,
459459
defaults({}, mutatedProps, targetProps),

packages/victory-candlestick/src/candle.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
VictoryCommonPrimitiveProps,
88
VictoryStyleObject,
99
} from "victory-core";
10-
import { defaults, isFunction } from "lodash";
10+
import { defaults } from "lodash";
1111

1212
export interface CandleProps extends VictoryCommonPrimitiveProps {
1313
candleRatio?: number;
@@ -31,7 +31,7 @@ const getCandleWidth = (
3131
) => {
3232
const { style } = props;
3333
if (candleWidth) {
34-
return isFunction(candleWidth)
34+
return Helpers.isFunction(candleWidth)
3535
? Helpers.evaluateProp(candleWidth, props)
3636
: candleWidth;
3737
} else if (style.width) {

packages/victory-candlestick/src/helper-methods.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, isFunction, isPlainObject } from "lodash";
1+
import { defaults, isPlainObject } from "lodash";
22
import {
33
Helpers,
44
Scale,
@@ -224,7 +224,7 @@ const getText = (props, type) => {
224224
const getCandleWidth = (props, style?: VictoryStyleObject) => {
225225
const { data, candleWidth, scale } = props;
226226
if (candleWidth) {
227-
return isFunction(candleWidth)
227+
return Helpers.isFunction(candleWidth)
228228
? Helpers.evaluateProp(candleWidth, props)
229229
: candleWidth;
230230
} else if (style && style.width) {

packages/victory-core/src/exports.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ describe("victory-core", () => {
309309
"getRadius": [Function],
310310
"getRange": [Function],
311311
"getStyles": [Function],
312+
"isFunction": [Function],
312313
"isHorizontal": [Function],
313314
"isNil": [Function],
314315
"isTooltip": [Function],

packages/victory-core/src/victory-container/victory-container.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { defaults, uniqueId, isObject, isFunction } from "lodash";
2+
import { defaults, uniqueId, isObject } from "lodash";
33
import { Portal } from "../victory-portal/portal";
44
import { PortalContext } from "../victory-portal/portal-context";
55
import TimerContext from "../victory-util/timer-context";
@@ -81,7 +81,7 @@ export class VictoryContainer<
8181
portalDeregister = (key) => this.portalRef.portalDeregister(key);
8282

8383
saveContainerRef = (container: HTMLElement) => {
84-
if (isFunction(this.props.containerRef)) {
84+
if (Helpers.isFunction(this.props.containerRef)) {
8585
this.props.containerRef(container);
8686
}
8787
this.containerRef = container;

packages/victory-core/src/victory-transition/victory-transition.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Collection from "../victory-util/collection";
44
import * as Helpers from "../victory-util/helpers";
55
import TimerContext from "../victory-util/timer-context";
66
import * as Transitions from "../victory-util/transitions";
7-
import { defaults, isFunction, pick } from "lodash";
7+
import { defaults, pick } from "lodash";
88
import isEqual from "react-fast-compare";
99
import Timer from "../victory-util/timer";
1010

@@ -119,7 +119,7 @@ export class VictoryTransition extends React.Component<
119119
) {
120120
const getChildDomains = (children) => {
121121
return children.reduce((memo, child) => {
122-
if (child.type && isFunction(child.type.getDomain)) {
122+
if (child.type && Helpers.isFunction(child.type.getDomain)) {
123123
const childDomain =
124124
child.props && child.type.getDomain(child.props, axis);
125125
return childDomain ? memo.concat(childDomain) : memo;

packages/victory-core/src/victory-util/add-events.tsx

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
import React from "react";
2-
import {
3-
defaults,
4-
difference,
5-
isEmpty,
6-
isFunction,
7-
keys,
8-
pick,
9-
without,
10-
} from "lodash";
2+
import { defaults, difference, isEmpty, keys, pick, without } from "lodash";
113
import type { ComponentEvent } from "./events";
124
import * as Events from "./events";
135
import isEqual from "react-fast-compare";
146
import { VictoryTransition } from "../victory-transition/victory-transition";
157
import { VictoryCommonProps, VictoryDatableProps } from "./common-props";
168
import { VictoryLabelableProps } from "../types/prop-types";
17-
import { isNil } from "./helpers";
9+
import { isFunction, isNil } from "./helpers";
1810

1911
// DISCLAIMER:
2012
// This file is not currently tested, and it is first on the list of files

packages/victory-core/src/victory-util/axis.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from "react";
22
import {
33
defaults,
44
identity,
5-
isFunction,
65
isObject,
76
invert,
87
uniq,
@@ -250,7 +249,7 @@ export function getTickFormat(props, scale) {
250249
// by default. This changed the default formatting for some scale types when
251250
// we upgraded to d3-scale@4..
252251
const scaleTickFormat =
253-
scale.tickFormat && isFunction(scale.tickFormat)
252+
scale.tickFormat && Helpers.isFunction(scale.tickFormat)
254253
? scale.tickFormat()
255254
: (x) => x;
256255
return defaultTickFormat || scaleTickFormat;
@@ -261,7 +260,7 @@ export function getTickFormat(props, scale) {
261260
tickArrayIndices?.includes(index),
262261
);
263262
return (x, index) => filteredTickFormat[index];
264-
} else if (tickFormat && isFunction(tickFormat)) {
263+
} else if (tickFormat && Helpers.isFunction(tickFormat)) {
265264
const applyStringTicks = (tick, index, ticks) => {
266265
const invertedStringMap = invert(stringMap);
267266
const stringTickArray = ticks.map((t) => invertedStringMap[t]);
@@ -291,7 +290,7 @@ export function getTicks(props, scale: D3Scale, filterZero = false) {
291290
const tickValues = tickArray ? tickArray.map((v) => v.value) : undefined;
292291
if (tickValues) {
293292
return downsampleTicks(tickValues, tickCount);
294-
} else if (scale.ticks && isFunction(scale.ticks)) {
293+
} else if (scale.ticks && Helpers.isFunction(scale.ticks)) {
295294
// eslint-disable-next-line no-magic-numbers
296295
const defaultTickCount = tickCount || 5;
297296
const scaleTicks = scale.ticks(defaultTickCount);
@@ -362,7 +361,7 @@ export function getAxisValue(props, axis) {
362361
}
363362
const scaleAxis = axis === "x" ? "y" : "x";
364363
const scale =
365-
isObject(props.scale) && isFunction(props.scale[scaleAxis])
364+
isObject(props.scale) && Helpers.isFunction(props.scale[scaleAxis])
366365
? props.scale[scaleAxis]
367366
: undefined;
368367
if (!scale) {

packages/victory-core/src/victory-util/data.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import React from "react";
33
import {
44
uniq,
55
last,
6-
isFunction,
76
isPlainObject,
87
property,
98
orderBy,
@@ -97,7 +96,7 @@ function cleanData(dataset, props) {
9796
function getEventKey(key) {
9897
// creates a data accessor function
9998
// given a property key, path, array index, or null for identity.
100-
if (isFunction(key)) {
99+
if (Helpers.isFunction(key)) {
101100
return key;
102101
} else if (key === null || key === undefined) {
103102
return () => undefined;

packages/victory-core/src/victory-util/domain.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
/* eslint-disable no-use-before-define */
22
import React from "react";
3-
import {
4-
flatten,
5-
isPlainObject,
6-
sortedUniq,
7-
isFunction,
8-
includes,
9-
isDate,
10-
} from "lodash";
3+
import { flatten, isPlainObject, sortedUniq, includes, isDate } from "lodash";
114
import * as Data from "./data";
125
import * as Scale from "./scale";
136
import * as Helpers from "./helpers";
@@ -183,10 +176,10 @@ export function createDomainFunction(
183176
getDomainFromDataFunction?,
184177
formatDomainFunction?,
185178
) {
186-
const getDomainFromDataFn = isFunction(getDomainFromDataFunction)
179+
const getDomainFromDataFn = Helpers.isFunction(getDomainFromDataFunction)
187180
? getDomainFromDataFunction
188181
: getDomainFromData;
189-
const formatDomainFn = isFunction(formatDomainFunction)
182+
const formatDomainFn = Helpers.isFunction(formatDomainFunction)
190183
? formatDomainFunction
191184
: formatDomain;
192185
return (props, axis) => {

packages/victory-core/src/victory-util/events.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
/* eslint-disable no-use-before-define */
2-
import {
3-
isEmpty,
4-
isFunction,
5-
without,
6-
pickBy,
7-
omitBy,
8-
uniq,
9-
includes,
10-
keys,
11-
} from "lodash";
2+
import { isEmpty, without, pickBy, omitBy, uniq, includes, keys } from "lodash";
123
import type { EventMixinCalculatedValues } from "./add-events";
4+
import { isFunction } from "./helpers";
135

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

packages/victory-core/src/victory-util/helpers.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-use-before-define */
22
import React, { isValidElement } from "react";
3-
import { defaults, isFunction, property, pick, keys } from "lodash";
3+
import { defaults, property, pick, keys } from "lodash";
44
import { CallbackArgs } from "../types/callbacks";
55
import { ValueOrAccessor } from "../types/prop-types";
66

@@ -194,6 +194,17 @@ export function isNil(value: any): boolean {
194194
return value == null;
195195
}
196196

197+
/**
198+
* Checks if `value` is classified as a `Function` object.
199+
*
200+
* @since 0.1.0
201+
* @param {*} value The value to check.
202+
* @returns {boolean} Returns `true` if `value` is a function, else `false`.
203+
*/
204+
export function isFunction(value: any): value is (...args: any[]) => any {
205+
return typeof value === "function";
206+
}
207+
197208
export function createAccessor(key) {
198209
// creates a data accessor function
199210
// given a property key, path, array index, or null for identity.

packages/victory-core/src/victory-util/scale.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-use-before-define */
2-
import { includes, isFunction, isPlainObject } from "lodash";
2+
import { includes, isPlainObject } from "lodash";
33
import * as Helpers from "./helpers";
44
import * as Collection from "./collection";
55
import * as d3Scale from "victory-vendor/d3-scale";
@@ -25,9 +25,9 @@ export function validScale(
2525
): scale is ScaleName | D3Scale {
2626
if (typeof scale === "function") {
2727
return (
28-
isFunction(scale.copy) &&
29-
isFunction(scale.domain) &&
30-
isFunction(scale.range)
28+
Helpers.isFunction(scale.copy) &&
29+
Helpers.isFunction(scale.domain) &&
30+
Helpers.isFunction(scale.range)
3131
);
3232
} else if (typeof scale === "string") {
3333
return includes(supportedScaleStrings, scale);
@@ -110,7 +110,7 @@ export function getScaleFromProps(props, axis): D3Scale | undefined {
110110
}
111111
const scale = props.scale[axis] || props.scale;
112112
if (validScale(scale)) {
113-
return isFunction(scale) ? scale : getScaleFromName(scale);
113+
return Helpers.isFunction(scale) ? scale : getScaleFromName(scale);
114114
}
115115
return undefined;
116116
}

0 commit comments

Comments
 (0)