Skip to content

Commit

Permalink
Replace lodash keys with native code (#2811)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot authored Feb 16, 2024
1 parent 7efd582 commit 7da7909
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 35 deletions.
13 changes: 13 additions & 0 deletions .changeset/curvy-socks-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"victory-box-plot": patch
"victory-core": patch
"victory-create-container": patch
"victory-legend": patch
"victory-selection-container": patch
"victory-shared-events": patch
"victory-stack": patch
"victory-voronoi": patch
"victory-voronoi-container": patch
---

Replace lodash keys with native code
4 changes: 2 additions & 2 deletions packages/victory-box-plot/src/helper-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { orderBy, defaults, uniq, groupBy, keys } from "lodash";
import { orderBy, defaults, uniq, groupBy } from "lodash";
import { Helpers, Scale, Domain, Data, Collection } from "victory-core";
import {
min as d3Min,
Expand Down Expand Up @@ -72,7 +72,7 @@ const processData = (data) => {
} else {
/* Group data by independent variable and generate summary statistics for each group */
const groupedData = groupBy(data, groupKey);
return keys(groupedData).map((key) => {
return Object.keys(groupedData).map((key) => {
const datum = groupedData[key];
const sortedData = orderBy(datum, sortKey);
return getSummaryStatistics(sortedData);
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-core/src/victory-portal/portal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
import { keys } from "lodash";
import { PortalContextValue } from "./portal-context";

export interface PortalProps {
Expand Down Expand Up @@ -40,7 +39,7 @@ export class Portal
};

public getChildren() {
return keys(this.map).map((key) => {
return Object.keys(this.map).map((key) => {
const el = this.map[key];
return el ? React.cloneElement(el, { key }) : el;
});
Expand Down
10 changes: 5 additions & 5 deletions packages/victory-core/src/victory-util/add-events.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { defaults, difference, isEmpty, keys, pick } from "lodash";
import { defaults, difference, isEmpty, pick } from "lodash";
import type { ComponentEvent } from "./events";
import * as Events from "./events";
import isEqual from "react-fast-compare";
Expand Down Expand Up @@ -180,15 +180,15 @@ export function addEvents<
}

componentDidMount() {
const globalEventKeys = keys(this.globalEvents);
const globalEventKeys = Object.keys(this.globalEvents);
globalEventKeys.forEach((key) => this.addGlobalListener(key));
this.prevGlobalEventKeys = globalEventKeys;
}

componentDidUpdate(prevProps) {
const calculatedState = this.getStateChanges(prevProps);
this.calculatedState = calculatedState;
const globalEventKeys = keys(this.globalEvents);
const globalEventKeys = Object.keys(this.globalEvents);
const removedGlobalEventKeys = difference(
this.prevGlobalEventKeys,
globalEventKeys,
Expand Down Expand Up @@ -285,7 +285,7 @@ export function addEvents<
? sharedEvents.getEventState
: () => undefined;
const baseProps = this.getBaseProps(props, getSharedEventState);
const dataKeys = keys(baseProps).filter((key) => key !== "parent");
const dataKeys = Object.keys(baseProps).filter((key) => key !== "parent");
const hasEvents = props.events || props.sharedEvents || componentEvents;
const events = this.getAllEvents(props);
return {
Expand All @@ -310,7 +310,7 @@ export function addEvents<
}

cacheValues(obj) {
keys(obj).forEach((key) => {
Object.keys(obj).forEach((key) => {
this[key] = obj[key];
});
}
Expand Down
24 changes: 13 additions & 11 deletions packages/victory-core/src/victory-util/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define */
import { isEmpty, pickBy, omitBy, uniq, keys } from "lodash";
import { isEmpty, pickBy, omitBy, uniq } from "lodash";
import type { EventMixinCalculatedValues } from "./add-events";
import { isFunction } from "./helpers";

Expand Down Expand Up @@ -157,12 +157,14 @@ export function getScopedEvents(
}
if (eventReturn.eventKey === "all") {
return newBaseProps[childName]
? keys(newBaseProps[childName]).filter((value) => value !== "parent")
: keys(newBaseProps).filter((value) => value !== "parent");
? Object.keys(newBaseProps[childName]).filter(
(value) => value !== "parent",
)
: Object.keys(newBaseProps).filter((value) => value !== "parent");
} else if (eventReturn.eventKey === undefined && eventKey === "parent") {
return newBaseProps[childName]
? keys(newBaseProps[childName])
: keys(newBaseProps);
? Object.keys(newBaseProps[childName])
: Object.keys(newBaseProps);
}
return eventReturn.eventKey !== undefined
? eventReturn.eventKey
Expand Down Expand Up @@ -194,7 +196,7 @@ export function getScopedEvents(
if (state[key] && state[key][target]) {
delete state[key][target];
}
if (state[key] && !keys(state[key]).length) {
if (state[key] && !Object.keys(state[key]).length) {
delete state[key];
}
return state;
Expand Down Expand Up @@ -234,7 +236,7 @@ export function getScopedEvents(
// returns an entire mutated state for all children
const allChildNames =
childNames === "all"
? keys(newBaseProps).filter((value) => value !== "parent")
? Object.keys(newBaseProps).filter((value) => value !== "parent")
: childNames;
return Array.isArray(allChildNames)
? allChildNames.reduce((memo, childName) => {
Expand Down Expand Up @@ -278,7 +280,7 @@ export function getScopedEvents(
};

// returns a new events object with enhanced event handlers
return keys(events).reduce((memo, event) => {
return Object.keys(events).reduce((memo, event) => {
memo[event] = onEvent;
return memo;
}, {});
Expand All @@ -295,7 +297,7 @@ export function getPartialEvents(
): PartialEvents {
if (!events) return {};

return keys(events).reduce((memo, eventName) => {
return Object.keys(events).reduce((memo, eventName) => {
const appliedEvent = (evt) =>
events[eventName](evt, childProps, eventKey, eventName);
memo[eventName] = appliedEvent;
Expand Down Expand Up @@ -378,7 +380,7 @@ export function getExternalMutations(
baseState = {},
childName?,
) {
const eventKeys = keys(baseProps);
const eventKeys = Object.keys(baseProps);
return eventKeys.reduce((memo, eventKey) => {
const keyState = baseState[eventKey] || {};
const keyProps = baseProps[eventKey] || {};
Expand All @@ -397,7 +399,7 @@ export function getExternalMutations(
} else {
// use keys from both state and props so that elements not intially included in baseProps
// will be used. (i.e. labels)
const targets = uniq(keys(keyProps).concat(keys(keyState)));
const targets = uniq(Object.keys(keyProps).concat(Object.keys(keyState)));
memo[eventKey] = targets.reduce((m, target) => {
const identifier = { eventKey, target, childName };
const mutation = getExternalMutation(
Expand Down
6 changes: 3 additions & 3 deletions 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, property, pick, keys } from "lodash";
import { defaults, property, pick } from "lodash";
import { CallbackArgs } from "../types/callbacks";
import { ValueOrAccessor } from "../types/prop-types";

Expand Down Expand Up @@ -139,10 +139,10 @@ export function evaluateStyle(style, props) {
if (props.disableInlineStyles) {
return {};
}
if (!style || !keys(style).some((value) => isFunction(style[value]))) {
if (!style || !Object.keys(style).some((value) => isFunction(style[value]))) {
return style;
}
return keys(style).reduce((prev, curr) => {
return Object.keys(style).reduce((prev, curr) => {
prev[curr] = evaluateProp(style[curr], props);
return prev;
}, {});
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-core/src/victory-util/transitions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, identity, keys } from "lodash";
import { defaults, identity } from "lodash";
import React from "react";
import { AnimatePropTypeInterface } from "../types/prop-types";

Expand All @@ -16,7 +16,7 @@ function getKeyedData(data) {

function getKeyedDataDifference(a, b) {
let hasDifference = false;
const difference = keys(a).reduce((_difference, key) => {
const difference = Object.keys(a).reduce((_difference, key) => {
if (!(key in b)) {
hasDifference = true;
_difference[key] = true;
Expand Down
6 changes: 3 additions & 3 deletions packages/victory-legend/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, groupBy, keys, sum, range } from "lodash";
import { defaults, groupBy, sum, range } from "lodash";
import { Helpers, Style, TextSize } from "victory-core";
import { VictoryLegendProps } from "./victory-legend";

Expand Down Expand Up @@ -90,7 +90,7 @@ const getColumnWidths = (props, data) => {
? (gutter.left || 0) + (gutter.right || 0)
: gutter || 0;
const dataByColumn = groupBy(data, "column");
const columns = keys(dataByColumn);
const columns = Object.keys(dataByColumn);
return columns.reduce<number[]>((memo, curr, index) => {
const lengths = dataByColumn[curr].map((d) => {
return d.textSize.width + d.size + d.symbolSpacer + gutterWidth;
Expand All @@ -107,7 +107,7 @@ const getRowHeights = (props, data) => {
? (gutter.top || 0) + (gutter.bottom || 0)
: gutter || 0;
const dataByRow = groupBy(data, "row");
return keys(dataByRow).reduce<number[]>((memo, curr, index) => {
return Object.keys(dataByRow).reduce<number[]>((memo, curr, index) => {
const rows = dataByRow[curr];
const lengths = rows.map((d) => {
return d.textSize.height + d.symbolSpacer + gutterHeight;
Expand Down
10 changes: 5 additions & 5 deletions packages/victory-shared-events/src/victory-shared-events.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, isEmpty, fromPairs, keys, difference } from "lodash";
import { defaults, isEmpty, fromPairs, difference } from "lodash";
import React from "react";
import {
EventCallbackInterface,
Expand Down Expand Up @@ -74,13 +74,13 @@ export class VictorySharedEvents extends React.Component<VictorySharedEventsProp
}

componentDidMount() {
const globalEventKeys = keys(this.globalEvents);
const globalEventKeys = Object.keys(this.globalEvents);
globalEventKeys.forEach((key) => this.addGlobalListener(key));
this.prevGlobalEventKeys = globalEventKeys;
}

componentDidUpdate() {
const globalEventKeys = keys(this.globalEvents);
const globalEventKeys = Object.keys(this.globalEvents);
const removedGlobalEventKeys = difference(
this.prevGlobalEventKeys,
globalEventKeys,
Expand Down Expand Up @@ -152,7 +152,7 @@ export class VictorySharedEvents extends React.Component<VictorySharedEventsProp
props.externalEventMutations,
baseProps,
this.state,
keys(baseProps),
Object.keys(baseProps),
)
: undefined;
}
Expand Down Expand Up @@ -258,7 +258,7 @@ export class VictorySharedEvents extends React.Component<VictorySharedEventsProp
return memo.concat(child);
}, []);
};
const childNames = keys(baseProps);
const childNames = Object.keys(baseProps);
const childComponents = React.Children.toArray(props.children);
return alterChildren(childComponents, childNames);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-stack/src/helper-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define */
import { keys, orderBy } from "lodash";
import { orderBy } from "lodash";
import React from "react";
import { Helpers, Scale, Wrapper } from "victory-core";
import isEqual from "react-fast-compare";
Expand All @@ -19,7 +19,7 @@ function fillData(props, datasets) {
});
return prev;
}, {});
const xKeys = keys(xMap).map((k) => Number(k));
const xKeys = Object.keys(xMap).map((k) => Number(k));
const xArr = orderBy(xKeys);

return datasets.map((dataset) => {
Expand Down

0 comments on commit 7da7909

Please sign in to comment.