Skip to content

Commit 45c1080

Browse files
authored
Replace lodash difference with native code (#2828)
1 parent 61155cd commit 45c1080

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

.changeset/honest-camels-compete.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"victory-core": patch
3+
"victory-shared-events": patch
4+
---
5+
6+
Replace lodash difference with native code

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

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ describe("victory-core", () => {
245245
"containsNumbers": [Function],
246246
"containsOnlyStrings": [Function],
247247
"containsStrings": [Function],
248+
"difference": [Function],
248249
"getMaxValue": [Function],
249250
"getMinValue": [Function],
250251
"isArrayOfArrays": [Function],

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React from "react";
2-
import { defaults, difference, isEmpty, pick } from "lodash";
3-
import type { ComponentEvent } from "./events";
4-
import * as Events from "./events";
2+
import { defaults, isEmpty, pick } from "lodash";
53
import isEqual from "react-fast-compare";
4+
5+
import { VictoryLabelableProps } from "../types/prop-types";
66
import { VictoryTransition } from "../victory-transition/victory-transition";
77
import { VictoryCommonProps, VictoryDatableProps } from "./common-props";
8-
import { VictoryLabelableProps } from "../types/prop-types";
8+
import { difference } from "./collection";
9+
import type { ComponentEvent } from "./events";
10+
import * as Events from "./events";
911
import { isFunction, isNil } from "./helpers";
1012

1113
// DISCLAIMER:

packages/victory-core/src/victory-util/collection.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ describe("victory-util/collection", () => {
5858
});
5959
});
6060

61+
describe("difference", () => {
62+
it("handles empty arguments", () => {
63+
// @ts-expect-error "Method expects 2 arguments"
64+
expect(Collection.difference()).toEqual([]);
65+
});
66+
67+
it("handles empty arrays", () => {
68+
expect(Collection.difference([], [])).toEqual([]);
69+
});
70+
71+
it("returns an empty array if there are no differences", () => {
72+
expect(Collection.difference([1, 2], [1, 2])).toEqual([]);
73+
});
74+
75+
it("returns the difference between two arrays", () => {
76+
expect(Collection.difference([1, 2], [2, 3])).toEqual([1]);
77+
});
78+
79+
it("returns the difference between two unequal arrays", () => {
80+
expect(Collection.difference([1, 2, 3, 4, 5], [5, 2, 10])).toEqual([
81+
1, 3, 4,
82+
]);
83+
});
84+
});
85+
6186
describe("isArrayOfArrays", () => {
6287
it("handles empty argument", () => {
6388
// @ts-expect-error "Method expects 1 argument"

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

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ export function containsOnlyStrings(
3434
);
3535
}
3636

37+
/**
38+
* Creates an array of array values not included in the other given arrays
39+
* @param a The array to inspect
40+
* @param b The values to exclude
41+
* @returns The new array of filtered values
42+
*/
43+
export function difference<T>(a: Array<T>, b: Array<T>): Array<T> {
44+
if (a && b) {
45+
return a.filter((value) => !b.includes(value));
46+
}
47+
return [];
48+
}
49+
3750
export function isArrayOfArrays<T>(
3851
collection: Array<T> | Array<Array<T>> | unknown,
3952
): collection is Array<Array<T>> {

packages/victory-shared-events/src/victory-shared-events.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { defaults, isEmpty, fromPairs, difference } from "lodash";
1+
import { defaults, isEmpty, fromPairs } from "lodash";
22
import React from "react";
33
import {
4+
Collection,
45
EventCallbackInterface,
56
EventMixinCalculatedValues,
67
EventPropTypeInterface,
@@ -81,12 +82,12 @@ export class VictorySharedEvents extends React.Component<VictorySharedEventsProp
8182

8283
componentDidUpdate() {
8384
const globalEventKeys = Object.keys(this.globalEvents);
84-
const removedGlobalEventKeys = difference(
85+
const removedGlobalEventKeys = Collection.difference(
8586
this.prevGlobalEventKeys,
8687
globalEventKeys,
8788
);
8889
removedGlobalEventKeys.forEach((key) => this.removeGlobalListener(key));
89-
const addedGlobalEventKeys = difference(
90+
const addedGlobalEventKeys = Collection.difference(
9091
globalEventKeys,
9192
this.prevGlobalEventKeys,
9293
);

0 commit comments

Comments
 (0)