Skip to content

Commit 7efd582

Browse files
authored
Replace lodash array utils with native code (#2810)
1 parent 9cdaf2c commit 7efd582

File tree

14 files changed

+59
-61
lines changed

14 files changed

+59
-61
lines changed

.changeset/proud-islands-smoke.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"victory-box-plot": patch
3+
"victory-brush-container": patch
4+
"victory-core": patch
5+
"victory-create-container": patch
6+
"victory-cursor-container": patch
7+
"victory-scatter": patch
8+
"victory-selection-container": patch
9+
"victory-voronoi": patch
10+
"victory-voronoi-container": patch
11+
---
12+
13+
Replace lodash array utils with native code

packages/victory-box-plot/src/victory-box-plot.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from "react";
2-
import { flatten } from "lodash";
32
import {
43
Helpers,
54
VictoryLabel,
@@ -182,8 +181,8 @@ class VictoryBoxPlotBase extends React.Component<VictoryBoxPlotProps> {
182181

183182
renderBoxPlot(props) {
184183
const types = ["q1", "q3", "max", "min", "median"];
185-
const dataComponents = flatten(
186-
types.map((type) => {
184+
const dataComponents = types
185+
.map((type) => {
187186
return this.dataKeys.reduce((validDataComponents, _key, index) => {
188187
const baseComponent = props[`${type}Component`];
189188
const componentProps = this.getComponentProps(
@@ -198,11 +197,11 @@ class VictoryBoxPlotBase extends React.Component<VictoryBoxPlotProps> {
198197
}
199198
return validDataComponents;
200199
}, [] as React.ReactElement[]);
201-
}),
202-
);
200+
})
201+
.flat();
203202

204-
const labelComponents = flatten(
205-
types.map((type) => {
203+
const labelComponents = types
204+
.map((type) => {
206205
const components = this.dataKeys.reduce(
207206
(validComponents, _key, index) => {
208207
const name = `${type}Labels`;
@@ -222,8 +221,9 @@ class VictoryBoxPlotBase extends React.Component<VictoryBoxPlotProps> {
222221
[] as React.ReactElement[],
223222
);
224223
return components.filter(Boolean);
225-
}),
226-
);
224+
})
225+
.flat();
226+
227227
const children = [...dataComponents, ...labelComponents];
228228
return this.renderContainer(props.groupComponent, children);
229229
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { defaults, difference, isEmpty, keys, pick, without } from "lodash";
2+
import { defaults, difference, isEmpty, keys, pick } from "lodash";
33
import type { ComponentEvent } from "./events";
44
import * as Events from "./events";
55
import isEqual from "react-fast-compare";
@@ -424,7 +424,7 @@ export function addEvents<
424424
// Used by `VictoryLine` and `VictoryArea`
425425
renderContinuousData(props: TProps) {
426426
const { dataComponent, labelComponent, groupComponent } = props;
427-
const dataKeys = without(this.dataKeys, "all");
427+
const dataKeys = this.dataKeys.filter((value) => value !== "all");
428428
const labelComponents = dataKeys.reduce((memo, key) => {
429429
let newMemo = memo;
430430
const labelProps = this.getComponentProps(

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import React from "react";
2-
import {
3-
defaults,
4-
identity,
5-
isObject,
6-
invert,
7-
uniq,
8-
orderBy,
9-
includes,
10-
without,
11-
} from "lodash";
2+
import { defaults, identity, isObject, invert, uniq, orderBy } from "lodash";
123
import * as Collection from "./collection";
134
import * as Domain from "./domain";
145
import * as Helpers from "./helpers";
@@ -299,7 +290,7 @@ export function getTicks(props, scale: D3Scale, filterZero = false) {
299290
: scale.domain();
300291
const ticks = downsampleTicks(scaledTickArray, tickCount);
301292
if (filterZero) {
302-
const filteredTicks = includes(ticks, 0) ? without(ticks, 0) : ticks;
293+
const filteredTicks = ticks.filter((value) => value !== 0);
303294
return filteredTicks.length ? filteredTicks : ticks;
304295
}
305296
return ticks;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
import React from "react";
33
import {
44
uniq,
5-
last,
65
isPlainObject,
76
property,
87
orderBy,
98
isEmpty,
109
isEqual,
11-
includes,
1210
isUndefined,
1311
omitBy,
1412
} from "lodash";
@@ -45,7 +43,9 @@ function generateDataArray(props, axis) {
4543
const domainMin = Math.min(...domain);
4644
const step = (domainMax - domainMin) / samples;
4745
const values = Helpers.range(domainMin, domainMax, step);
48-
return last(values) === domainMax ? values : values.concat(domainMax);
46+
return values[values.length - 1] === domainMax
47+
? values
48+
: values.concat(domainMax);
4949
}
5050

5151
// Returns sorted data. If no sort keys are provided, data is returned unaltered.
@@ -454,5 +454,5 @@ export function isDataComponent(component) {
454454
"stack",
455455
"voronoi",
456456
];
457-
return includes(whitelist, role);
457+
return whitelist.includes(role);
458458
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-use-before-define */
22
import React from "react";
3-
import { flatten, isPlainObject, sortedUniq, includes, isDate } from "lodash";
3+
import { isPlainObject, sortedUniq, isDate } from "lodash";
44
import * as Data from "./data";
55
import * as Scale from "./scale";
66
import * as Helpers from "./helpers";
@@ -42,7 +42,7 @@ function getDomainPadding(props, axis) {
4242

4343
function getFlatData(dataset, axis: "x" | "y") {
4444
const axisKey = `_${axis}`;
45-
return flatten(dataset).map((datum: any) => {
45+
return dataset.flat().map((datum: any) => {
4646
return datum[axisKey] && datum[axisKey][1] !== undefined
4747
? datum[axisKey][1]
4848
: datum[axisKey];
@@ -54,7 +54,7 @@ function getExtremeFromData(dataset, axis, type = "min") {
5454
type === "max" ? Math.max(...arr) : Math.min(...arr);
5555
const initialValue = type === "max" ? -Infinity : Infinity;
5656
let containsDate = false;
57-
const result = flatten(dataset).reduce((memo: number, datum: any) => {
57+
const result = dataset.flat().reduce((memo: number, datum: any) => {
5858
const current0 =
5959
datum[`_${axis}0`] !== undefined ? datum[`_${axis}0`] : datum[`_${axis}`];
6060
const current1 =
@@ -459,5 +459,5 @@ export function isDomainComponent(component) {
459459
"stack",
460460
"voronoi",
461461
];
462-
return includes(whitelist, role);
462+
return whitelist.includes(role);
463463
}

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

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

@@ -50,7 +50,7 @@ export function getEvents(
5050
const targetEvents = events.reduce((memo, event) => {
5151
if (event.target !== undefined) {
5252
const matchesTarget = Array.isArray(event.target)
53-
? includes(event.target, target)
53+
? event.target.includes(target)
5454
: `${event.target}` === `${target}`;
5555
return matchesTarget ? memo.concat(event) : memo;
5656
}
@@ -157,8 +157,8 @@ export function getScopedEvents(
157157
}
158158
if (eventReturn.eventKey === "all") {
159159
return newBaseProps[childName]
160-
? without(keys(newBaseProps[childName]), "parent")
161-
: without(keys(newBaseProps), "parent");
160+
? keys(newBaseProps[childName]).filter((value) => value !== "parent")
161+
: keys(newBaseProps).filter((value) => value !== "parent");
162162
} else if (eventReturn.eventKey === undefined && eventKey === "parent") {
163163
return newBaseProps[childName]
164164
? keys(newBaseProps[childName])
@@ -233,7 +233,9 @@ export function getScopedEvents(
233233

234234
// returns an entire mutated state for all children
235235
const allChildNames =
236-
childNames === "all" ? without(keys(newBaseProps), "parent") : childNames;
236+
childNames === "all"
237+
? keys(newBaseProps).filter((value) => value !== "parent")
238+
: childNames;
237239
return Array.isArray(allChildNames)
238240
? allChildNames.reduce((memo, childName) => {
239241
return Object.assign(memo, getReturnByChild(childName));
@@ -438,7 +440,7 @@ export function getExternalMutation(
438440
} else if (Array.isArray(mutation[type])) {
439441
// coerce arrays to strings before matching
440442
const stringArray = mutation[type].map((m) => `${m}`);
441-
return includes(stringArray, identifier[type]);
443+
return stringArray.includes(identifier[type]);
442444
}
443445
return false;
444446
};

packages/victory-core/src/victory-util/hooks/use-animation-state.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { defaults, some } from "lodash";
2+
import { defaults } from "lodash";
33

44
import * as Collection from "../collection";
55
import * as Transitions from "../transitions";
@@ -91,15 +91,15 @@ export const useAnimationState = (initialState = INITIAL_STATE) => {
9191
const nextChildren = React.Children.toArray(nextProps.children);
9292
const isContinuous = (child) => {
9393
const check = (c) => c.type && c.type.continuous;
94-
return Array.isArray(child) ? some(child, check) : check(child);
94+
return Array.isArray(child) ? child.some(check) : check(child);
9595
};
9696

9797
const continuous =
9898
!props.polar &&
99-
some(oldChildren, (child: React.ReactElement) => {
99+
oldChildren.some((child: any) => {
100100
return (
101101
isContinuous(child) ||
102-
(child.props.children && isContinuous(child.props.children))
102+
(child?.props?.children && isContinuous(child.props.children))
103103
);
104104
});
105105
const {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-use-before-define */
2-
import { includes, isPlainObject } from "lodash";
2+
import { isPlainObject } from "lodash";
33
import * as Helpers from "./helpers";
44
import * as Collection from "./collection";
55
import * as d3Scale from "victory-vendor/d3-scale";
@@ -30,7 +30,7 @@ export function validScale(
3030
Helpers.isFunction(scale.range)
3131
);
3232
} else if (typeof scale === "string") {
33-
return includes(supportedScaleStrings, scale);
33+
return (supportedScaleStrings as ReadonlyArray<string>).includes(scale);
3434
}
3535
return false;
3636
}

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

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
defaults,
3-
flatten,
4-
uniq,
5-
groupBy,
6-
uniqBy,
7-
isPlainObject,
8-
} from "lodash";
1+
import { defaults, uniq, groupBy, uniqBy, isPlainObject } from "lodash";
92
import React from "react";
103
import * as Axis from "./axis";
114
import * as Style from "./style";
@@ -428,7 +421,7 @@ export function getCategoryAndAxisStringsFromChildren(
428421
: [];
429422
const categoryStrings =
430423
categories || getStringsFromCategories(childComponents, axis);
431-
return uniq(flatten([...categoryStrings, ...axisStrings]));
424+
return uniq([...categoryStrings, ...axisStrings].flat());
432425
}
433426

434427
export function getStringsFromChildren(props, childComponents) {
@@ -440,8 +433,8 @@ export function getStringsFromChildren(props, childComponents) {
440433
const dataStrings = getStringsFromData(children);
441434

442435
return {
443-
x: uniq(flatten([...xStrings, ...dataStrings.x])),
444-
y: uniq(flatten([...yStrings, ...dataStrings.y])),
436+
x: uniq([...xStrings, ...dataStrings.x].flat()),
437+
y: uniq([...yStrings, ...dataStrings.y].flat()),
445438
};
446439
}
447440

packages/victory-create-container/src/create-container.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { toPairs, groupBy, forOwn, includes, flow, isEmpty } from "lodash";
2+
import { toPairs, groupBy, forOwn, flow, isEmpty } from "lodash";
33
import { Helpers, VictoryContainer, Log } from "victory-core";
44
import { voronoiContainerMixin } from "victory-voronoi-container";
55
import { zoomContainerMixin } from "victory-zoom-container";
@@ -121,7 +121,7 @@ const checkBehaviorName = (
121121
behavior: ContainerType,
122122
behaviors: ContainerType[],
123123
) => {
124-
if (behavior && !includes(behaviors, behavior)) {
124+
if (behavior && !behaviors.includes(behavior)) {
125125
Log.warn(
126126
`"${behavior}" is not a valid behavior. Choose from [${behaviors.join(
127127
", ",

packages/victory-selection-container/src/selection-helpers.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Selection, Data, Helpers, Datum } from "victory-core";
2-
import { defaults, throttle, includes } from "lodash";
2+
import { defaults, throttle } from "lodash";
33
import React from "react";
44

55
const ON_MOUSE_MOVE_THROTTLE_MS = 16;
@@ -26,7 +26,7 @@ class SelectionHelpersClass {
2626
const iteratee = (child, childName, parent) => {
2727
const blacklist = props.selectionBlacklist || [];
2828
let childElement;
29-
if (!Data.isDataComponent(child) || includes(blacklist, childName)) {
29+
if (!Data.isDataComponent(child) || blacklist.includes(childName)) {
3030
return null;
3131
} else if (child.type && Helpers.isFunction(child.type.getData)) {
3232
childElement = parent ? React.cloneElement(child, parent.props) : child;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Collection, Selection, Data, Helpers } from "victory-core";
2-
import { isEmpty, includes, isString, isRegExp, throttle } from "lodash";
2+
import { isEmpty, isString, isRegExp, throttle } from "lodash";
33
import isEqual from "react-fast-compare";
44
import Delaunay from "delaunay-find/lib/index.js";
55
import React from "react";
@@ -71,7 +71,7 @@ class VoronoiHelpersClass {
7171
const isRegExpMatch = blacklistRegExp.some((regExp) => regExp.test(name));
7272
if (
7373
!Data.isDataComponent(child) ||
74-
includes(blacklistStr, name) ||
74+
blacklistStr.includes(name) ||
7575
isRegExpMatch
7676
) {
7777
return null;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { without } from "lodash";
21
// victory-vendor note: This module is still CommonJS, so not part of victory-vendor.
32
import { voronoi as d3Voronoi } from "d3-voronoi";
43
import { Helpers, LabelHelpers, Scale, Domain, Data } from "victory-core";
@@ -122,7 +121,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
122121
};
123122

124123
return data.reduce((childProps, datum, index) => {
125-
const polygon = without(polygons[index], "data");
124+
const polygon = polygons[index]?.filter((value) => value !== "data");
126125
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
127126
const { x, y } = Helpers.scalePoint(props, datum);
128127
const dataProps = {

0 commit comments

Comments
 (0)