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

Replace lodash isNil and isNan with native code #2800

Merged
merged 2 commits into from
Feb 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/warm-pugs-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"victory-area": patch
"victory-bar": patch
"victory-box-plot": patch
"victory-candlestick": patch
"victory-core": patch
"victory-errorbar": patch
"victory-histogram": patch
"victory-legend": patch
"victory-line": patch
"victory-pie": patch
"victory-scatter": patch
"victory-voronoi": patch
---

Replace lodash isNil and isNan with native code
3 changes: 1 addition & 2 deletions packages/victory-area/src/helper-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from "lodash";
import {
Helpers,
LabelHelpers,
Expand Down Expand Up @@ -132,7 +131,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
(text !== undefined && text !== null) ||
(labels && (events || sharedEvents))
) {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
childProps[eventKey] = { labels: LabelHelpers.getProps(props, index) };
}
return childProps;
Expand Down
6 changes: 3 additions & 3 deletions packages/victory-bar/src/bar-helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil, isPlainObject } from "lodash";
import { isPlainObject } from "lodash";
import { Helpers, VictoryStyleObject } from "victory-core";
import { BarProps } from "./bar";
import {
Expand Down Expand Up @@ -41,12 +41,12 @@ const getCornerRadiusFromObject = (
corner: VictoryBarCornerRadiusKey,
fallback: "top" | "bottom",
) => {
if (!isNil(cornerRadius[corner])) {
if (!Helpers.isNil(cornerRadius[corner])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[corner],
props,
);
} else if (!isNil(cornerRadius[fallback])) {
} else if (!Helpers.isNil(cornerRadius[fallback])) {
realCornerRadius[corner] = Helpers.evaluateProp(
cornerRadius[fallback],
props,
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-bar/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from "lodash";
import {
Collection,
Data,
Expand Down Expand Up @@ -115,7 +114,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
};

return data.reduce((childProps, datum, index) => {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const { x, y, y0, x0 } = getBarPosition(props, datum);

const dataProps = {
Expand Down
6 changes: 3 additions & 3 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, isNaN, isNil } from "lodash";
import { orderBy, defaults, uniq, groupBy, keys } from "lodash";
import { Helpers, Scale, Domain, Data, Collection } from "victory-core";
import {
min as d3Min,
Expand Down Expand Up @@ -30,7 +30,7 @@ const checkProcessedData = (data) => {
return false;
};

const nanToNull = (val) => (isNaN(val) ? null : val);
const nanToNull = (val) => (Number.isNaN(val) ? null : val);

const getSummaryStatistics = (data) => {
const dependentVars = data.map((datum) => datum._y);
Expand Down Expand Up @@ -493,7 +493,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
};
const boxScale = scale.y;
return data.reduce((acc, datum, index) => {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;

if (isDatumOutOfBounds(datum, domain)) return acc;

Expand Down
16 changes: 8 additions & 8 deletions packages/victory-box-plot/src/victory-box-plot.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { flatten, isNil } from "lodash";
import { flatten } from "lodash";
import {
Helpers,
VictoryLabel,
Expand Down Expand Up @@ -234,14 +234,14 @@ class VictoryBoxPlotBase extends React.Component<VictoryBoxPlotProps> {
}

shouldRenderDatum(datum) {
const hasX = !isNil(datum._x);
const hasY = !isNil(datum._y);
const hasX = !Helpers.isNil(datum._x);
const hasY = !Helpers.isNil(datum._y);
const hasSummaryStatistics =
!isNil(datum._min) &&
!isNil(datum._max) &&
!isNil(datum._median) &&
!isNil(datum._q1) &&
!isNil(datum._q3);
!Helpers.isNil(datum._min) &&
!Helpers.isNil(datum._max) &&
!Helpers.isNil(datum._median) &&
!Helpers.isNil(datum._q1) &&
!Helpers.isNil(datum._q3);

return hasSummaryStatistics && (this.props.horizontal ? hasY : hasX);
}
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, isNil, isFunction, isPlainObject } from "lodash";
import { defaults, isFunction, isPlainObject } from "lodash";
import {
Helpers,
Scale,
Expand Down Expand Up @@ -417,7 +417,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {

// eslint-disable-next-line complexity
return data.reduce((childProps, datum, index) => {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const x = scale.x(datum._x1 !== undefined ? datum._x1 : datum._x);
const formattedDatum = formatDataFromDomain(datum, domain);
const { _low, _open, _close, _high } = formattedDatum;
Expand Down
13 changes: 6 additions & 7 deletions packages/victory-candlestick/src/victory-candlestick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
NumberOrCallback,
EventsMixinClass,
} from "victory-core";
import { isNil } from "lodash";
import { Candle } from "./candle";
import { getDomain, getData, getBaseProps } from "./helper-methods";

Expand Down Expand Up @@ -127,7 +126,7 @@ const defaultData = [
];
/* eslint-enable no-magic-numbers */
const datumHasXandY = (datum) => {
return !isNil(datum._x) && !isNil(datum._y);
return !Helpers.isNil(datum._x) && !Helpers.isNil(datum._y);
};

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down Expand Up @@ -192,11 +191,11 @@ class VictoryCandlestickBase extends React.Component<VictoryCandlestickProps> {

shouldRenderDatum = (datum) => {
return (
!isNil(datum._x) &&
!isNil(datum._high) &&
!isNil(datum._low) &&
!isNil(datum._close) &&
!isNil(datum._open)
!Helpers.isNil(datum._x) &&
!Helpers.isNil(datum._high) &&
!Helpers.isNil(datum._low) &&
!Helpers.isNil(datum._close) &&
!Helpers.isNil(datum._open)
);
};

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 @@ -310,6 +310,7 @@ describe("victory-core", () => {
"getRange": [Function],
"getStyles": [Function],
"isHorizontal": [Function],
"isNil": [Function],
"isTooltip": [Function],
"modifyProps": [Function],
"omit": [Function],
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-core/src/victory-util/add-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
difference,
isEmpty,
isFunction,
isNil,
keys,
pick,
without,
Expand All @@ -15,6 +14,7 @@ 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";

// DISCLAIMER:
// This file is not currently tested, and it is first on the list of files
Expand Down
9 changes: 9 additions & 0 deletions packages/victory-core/src/victory-util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ export function getRange(props, axis) {
: getCartesianRange(props, axis);
}

/**
* Checks if `value` is `null` or `undefined`.
* @returns {boolean} Returns `true` if `value` is nullish, else `false`.
*/
export function isNil(value: any): boolean {
// eslint-disable-next-line eqeqeq
return value == null;
}

export function createAccessor(key) {
// creates a data accessor function
// given a property key, path, array index, or null for identity.
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-errorbar/src/helper-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, assign, isNil } from "lodash";
import { defaults, assign } from "lodash";
import {
Helpers,
LabelHelpers,
Expand Down Expand Up @@ -216,7 +216,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
};

return data.reduce((childProps, datum, index) => {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const { x, y } = Helpers.scalePoint(assign({}, props, { scale }), datum);
const formattedDatum = formatDataFromDomain(datum, domain);
const errorX = getErrors(props, formattedDatum, "x");
Expand Down
5 changes: 2 additions & 3 deletions packages/victory-histogram/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from "lodash";
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";
import { getBarPosition } from "victory-bar";
import isEqual from "react-fast-compare";
Expand Down Expand Up @@ -122,7 +121,7 @@ export const getFormattedData = cacheLastValue(

export const getData = (props: VictoryHistogramProps) => {
const { bins, data, x } = props;
const dataIsPreformatted = data?.some(({ _y }) => !isNil(_y));
const dataIsPreformatted = data?.some(({ _y }) => !Helpers.isNil(_y));

const formattedData = dataIsPreformatted
? data
Expand Down Expand Up @@ -244,7 +243,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
};

return data.reduce((childProps, datum, index) => {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;

const { x, y, y0, x0 } = getBarPosition(props, datum);
const barWidth = getBarWidth(datum);
Expand Down
4 changes: 2 additions & 2 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, isNil } from "lodash";
import { defaults, groupBy, keys, sum, range } from "lodash";
import { Helpers, Style, TextSize } from "victory-core";
import { VictoryLegendProps } from "./victory-legend";

Expand Down Expand Up @@ -302,7 +302,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
return groupedData.reduce((childProps, datum, i) => {
const color = colorScale[i % colorScale.length];
const dataStyle = defaults({}, datum.symbol, style.data, { fill: color });
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : i;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : i;
const offset = getOffset(datum, rowHeights, columnWidths);
const originY = y + borderPadding.top + datum.symbolSpacer;
const originX = x + borderPadding.left + datum.symbolSpacer;
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-line/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from "lodash";
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";

const getCalculatedValues = (props) => {
Expand Down Expand Up @@ -101,7 +100,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
(text !== undefined && text !== null) ||
(labels && (events || sharedEvents))
) {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
childProps[eventKey] = { labels: LabelHelpers.getProps(props, index) };
}
return childProps;
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-pie/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 90, 135, 180, 225, 270, 315, 360] }]*/
import { defaults, isFunction, isPlainObject, isNil } from "lodash";
import { defaults, isFunction, isPlainObject } from "lodash";
import * as d3Shape from "victory-vendor/d3-shape";

import { Helpers, Data, Style } from "victory-core";
Expand Down Expand Up @@ -321,7 +321,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
endAngle: Helpers.radiansToDegrees(slice.endAngle),
padAngle: Helpers.radiansToDegrees(slice.padAngle),
});
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const dataProps = {
index,
slice,
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-pie/src/victory-pie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
EventsMixinClass,
VictoryDatableProps,
} from "victory-core";
import { isNil } from "lodash";
import { getBaseProps } from "./helper-methods";
import {
Slice,
Expand Down Expand Up @@ -90,7 +89,7 @@ const fallbackProps = {
};

const datumHasXandY = (datum) => {
return !isNil(datum._x) && !isNil(datum._y);
return !Helpers.isNil(datum._x) && !Helpers.isNil(datum._y);
};

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-scatter/src/helper-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { values, isNil } from "lodash";
import { values } from "lodash";
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";

export const getSymbol = (data, props) => {
Expand Down Expand Up @@ -123,7 +123,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
};

return data.reduce((childProps, datum, index) => {
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const { x, y } = Helpers.scalePoint(props, datum);
const dataProps = {
x,
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-voronoi/src/helper-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { without, isNil } from "lodash";
import { without } from "lodash";
// victory-vendor note: This module is still CommonJS, so not part of victory-vendor.
import { voronoi as d3Voronoi } from "d3-voronoi";
import { Helpers, LabelHelpers, Scale, Domain, Data } from "victory-core";
Expand Down Expand Up @@ -123,7 +123,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {

return data.reduce((childProps, datum, index) => {
const polygon = without(polygons[index], "data");
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const { x, y } = Helpers.scalePoint(props, datum);
const dataProps = {
x,
Expand Down
Loading