Skip to content

Commit 5a5f208

Browse files
committed
Replace lodash isNil and isNan with native code
1 parent cf42d6e commit 5a5f208

File tree

18 files changed

+49
-45
lines changed

18 files changed

+49
-45
lines changed

packages/victory-area/src/helper-methods.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isNil } from "lodash";
21
import {
32
Helpers,
43
LabelHelpers,
@@ -132,7 +131,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
132131
(text !== undefined && text !== null) ||
133132
(labels && (events || sharedEvents))
134133
) {
135-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
134+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
136135
childProps[eventKey] = { labels: LabelHelpers.getProps(props, index) };
137136
}
138137
return childProps;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNil, isPlainObject } from "lodash";
1+
import { isPlainObject } from "lodash";
22
import { Helpers, VictoryStyleObject } from "victory-core";
33
import { BarProps } from "./bar";
44
import {
@@ -41,12 +41,12 @@ const getCornerRadiusFromObject = (
4141
corner: VictoryBarCornerRadiusKey,
4242
fallback: "top" | "bottom",
4343
) => {
44-
if (!isNil(cornerRadius[corner])) {
44+
if (!Helpers.isNil(cornerRadius[corner])) {
4545
realCornerRadius[corner] = Helpers.evaluateProp(
4646
cornerRadius[corner],
4747
props,
4848
);
49-
} else if (!isNil(cornerRadius[fallback])) {
49+
} else if (!Helpers.isNil(cornerRadius[fallback])) {
5050
realCornerRadius[corner] = Helpers.evaluateProp(
5151
cornerRadius[fallback],
5252
props,

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isNil } from "lodash";
21
import {
32
Collection,
43
Data,
@@ -115,7 +114,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
115114
};
116115

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

121120
const dataProps = {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { orderBy, defaults, uniq, groupBy, keys, isNaN, isNil } from "lodash";
1+
import { orderBy, defaults, uniq, groupBy, keys } from "lodash";
22
import { Helpers, Scale, Domain, Data, Collection } from "victory-core";
33
import {
44
min as d3Min,
@@ -30,7 +30,7 @@ const checkProcessedData = (data) => {
3030
return false;
3131
};
3232

33-
const nanToNull = (val) => (isNaN(val) ? null : val);
33+
const nanToNull = (val) => (Number.isNaN(val) ? null : val);
3434

3535
const getSummaryStatistics = (data) => {
3636
const dependentVars = data.map((datum) => datum._y);
@@ -493,7 +493,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
493493
};
494494
const boxScale = scale.y;
495495
return data.reduce((acc, datum, index) => {
496-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
496+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
497497

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

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { flatten, isNil } from "lodash";
2+
import { flatten } from "lodash";
33
import {
44
Helpers,
55
VictoryLabel,
@@ -234,14 +234,14 @@ class VictoryBoxPlotBase extends React.Component<VictoryBoxPlotProps> {
234234
}
235235

236236
shouldRenderDatum(datum) {
237-
const hasX = !isNil(datum._x);
238-
const hasY = !isNil(datum._y);
237+
const hasX = !Helpers.isNil(datum._x);
238+
const hasY = !Helpers.isNil(datum._y);
239239
const hasSummaryStatistics =
240-
!isNil(datum._min) &&
241-
!isNil(datum._max) &&
242-
!isNil(datum._median) &&
243-
!isNil(datum._q1) &&
244-
!isNil(datum._q3);
240+
!Helpers.isNil(datum._min) &&
241+
!Helpers.isNil(datum._max) &&
242+
!Helpers.isNil(datum._median) &&
243+
!Helpers.isNil(datum._q1) &&
244+
!Helpers.isNil(datum._q3);
245245

246246
return hasSummaryStatistics && (this.props.horizontal ? hasY : hasX);
247247
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, isNil, isFunction, isPlainObject } from "lodash";
1+
import { defaults, isFunction, isPlainObject } from "lodash";
22
import {
33
Helpers,
44
Scale,
@@ -417,7 +417,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
417417

418418
// eslint-disable-next-line complexity
419419
return data.reduce((childProps, datum, index) => {
420-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
420+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
421421
const x = scale.x(datum._x1 !== undefined ? datum._x1 : datum._x);
422422
const formattedDatum = formatDataFromDomain(datum, domain);
423423
const { _low, _open, _close, _high } = formattedDatum;

packages/victory-candlestick/src/victory-candlestick.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
NumberOrCallback,
2020
EventsMixinClass,
2121
} from "victory-core";
22-
import { isNil } from "lodash";
2322
import { Candle } from "./candle";
2423
import { getDomain, getData, getBaseProps } from "./helper-methods";
2524

@@ -127,7 +126,7 @@ const defaultData = [
127126
];
128127
/* eslint-enable no-magic-numbers */
129128
const datumHasXandY = (datum) => {
130-
return !isNil(datum._x) && !isNil(datum._y);
129+
return !Helpers.isNil(datum._x) && !Helpers.isNil(datum._y);
131130
};
132131

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

193192
shouldRenderDatum = (datum) => {
194193
return (
195-
!isNil(datum._x) &&
196-
!isNil(datum._high) &&
197-
!isNil(datum._low) &&
198-
!isNil(datum._close) &&
199-
!isNil(datum._open)
194+
!Helpers.isNil(datum._x) &&
195+
!Helpers.isNil(datum._high) &&
196+
!Helpers.isNil(datum._low) &&
197+
!Helpers.isNil(datum._close) &&
198+
!Helpers.isNil(datum._open)
200199
);
201200
};
202201

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

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ describe("victory-core", () => {
310310
"getRange": [Function],
311311
"getStyles": [Function],
312312
"isHorizontal": [Function],
313+
"isNil": [Function],
313314
"isTooltip": [Function],
314315
"modifyProps": [Function],
315316
"omit": [Function],

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
difference,
55
isEmpty,
66
isFunction,
7-
isNil,
87
keys,
98
pick,
109
without,
@@ -15,6 +14,7 @@ import isEqual from "react-fast-compare";
1514
import { VictoryTransition } from "../victory-transition/victory-transition";
1615
import { VictoryCommonProps, VictoryDatableProps } from "./common-props";
1716
import { VictoryLabelableProps } from "../types/prop-types";
17+
import { isNil } from "./helpers";
1818

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

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

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ export function getRange(props, axis) {
185185
: getCartesianRange(props, axis);
186186
}
187187

188+
/**
189+
* Checks if `value` is `null` or `undefined`.
190+
* @returns {boolean} Returns `true` if `value` is nullish, else `false`.
191+
*/
192+
export function isNil(value: any): boolean {
193+
// eslint-disable-next-line eqeqeq
194+
return value == null;
195+
}
196+
188197
export function createAccessor(key) {
189198
// creates a data accessor function
190199
// given a property key, path, array index, or null for identity.

packages/victory-errorbar/src/helper-methods.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, assign, isNil } from "lodash";
1+
import { defaults, assign } from "lodash";
22
import {
33
Helpers,
44
LabelHelpers,
@@ -216,7 +216,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
216216
};
217217

218218
return data.reduce((childProps, datum, index) => {
219-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
219+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
220220
const { x, y } = Helpers.scalePoint(assign({}, props, { scale }), datum);
221221
const formattedDatum = formatDataFromDomain(datum, domain);
222222
const errorX = getErrors(props, formattedDatum, "x");

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isNil } from "lodash";
21
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";
32
import { getBarPosition } from "victory-bar";
43
import isEqual from "react-fast-compare";
@@ -122,7 +121,7 @@ export const getFormattedData = cacheLastValue(
122121

123122
export const getData = (props: VictoryHistogramProps) => {
124123
const { bins, data, x } = props;
125-
const dataIsPreformatted = data?.some(({ _y }) => !isNil(_y));
124+
const dataIsPreformatted = data?.some(({ _y }) => !Helpers.isNil(_y));
126125

127126
const formattedData = dataIsPreformatted
128127
? data
@@ -244,7 +243,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
244243
};
245244

246245
return data.reduce((childProps, datum, index) => {
247-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
246+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
248247

249248
const { x, y, y0, x0 } = getBarPosition(props, datum);
250249
const barWidth = getBarWidth(datum);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, groupBy, keys, sum, range, isNil } from "lodash";
1+
import { defaults, groupBy, keys, sum, range } from "lodash";
22
import { Helpers, Style, TextSize } from "victory-core";
33
import { VictoryLegendProps } from "./victory-legend";
44

@@ -302,7 +302,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
302302
return groupedData.reduce((childProps, datum, i) => {
303303
const color = colorScale[i % colorScale.length];
304304
const dataStyle = defaults({}, datum.symbol, style.data, { fill: color });
305-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : i;
305+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : i;
306306
const offset = getOffset(datum, rowHeights, columnWidths);
307307
const originY = y + borderPadding.top + datum.symbolSpacer;
308308
const originX = x + borderPadding.left + datum.symbolSpacer;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isNil } from "lodash";
21
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";
32

43
const getCalculatedValues = (props) => {
@@ -101,7 +100,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
101100
(text !== undefined && text !== null) ||
102101
(labels && (events || sharedEvents))
103102
) {
104-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
103+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
105104
childProps[eventKey] = { labels: LabelHelpers.getProps(props, index) };
106105
}
107106
return childProps;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 90, 135, 180, 225, 270, 315, 360] }]*/
2-
import { defaults, isFunction, isPlainObject, isNil } from "lodash";
2+
import { defaults, isFunction, isPlainObject } from "lodash";
33
import * as d3Shape from "victory-vendor/d3-shape";
44

55
import { Helpers, Data, Style } from "victory-core";
@@ -321,7 +321,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
321321
endAngle: Helpers.radiansToDegrees(slice.endAngle),
322322
padAngle: Helpers.radiansToDegrees(slice.padAngle),
323323
});
324-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
324+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
325325
const dataProps = {
326326
index,
327327
slice,

packages/victory-pie/src/victory-pie.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
EventsMixinClass,
2222
VictoryDatableProps,
2323
} from "victory-core";
24-
import { isNil } from "lodash";
2524
import { getBaseProps } from "./helper-methods";
2625
import {
2726
Slice,
@@ -90,7 +89,7 @@ const fallbackProps = {
9089
};
9190

9291
const datumHasXandY = (datum) => {
93-
return !isNil(datum._x) && !isNil(datum._y);
92+
return !Helpers.isNil(datum._x) && !Helpers.isNil(datum._y);
9493
};
9594

9695
// eslint-disable-next-line @typescript-eslint/no-empty-interface

packages/victory-scatter/src/helper-methods.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { values, isNil } from "lodash";
1+
import { values } from "lodash";
22
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";
33

44
export const getSymbol = (data, props) => {
@@ -123,7 +123,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
123123
};
124124

125125
return data.reduce((childProps, datum, index) => {
126-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
126+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
127127
const { x, y } = Helpers.scalePoint(props, datum);
128128
const dataProps = {
129129
x,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { without, isNil } from "lodash";
1+
import { without } from "lodash";
22
// victory-vendor note: This module is still CommonJS, so not part of victory-vendor.
33
import { voronoi as d3Voronoi } from "d3-voronoi";
44
import { Helpers, LabelHelpers, Scale, Domain, Data } from "victory-core";
@@ -123,7 +123,7 @@ export const getBaseProps = (initialProps, fallbackProps) => {
123123

124124
return data.reduce((childProps, datum, index) => {
125125
const polygon = without(polygons[index], "data");
126-
const eventKey = !isNil(datum.eventKey) ? datum.eventKey : index;
126+
const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
127127
const { x, y } = Helpers.scalePoint(props, datum);
128128
const dataProps = {
129129
x,

0 commit comments

Comments
 (0)