Skip to content

Commit

Permalink
Replace lodash difference with native code (#2828)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot committed Mar 12, 2024
1 parent 61155cd commit 45c1080
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/honest-camels-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"victory-core": patch
"victory-shared-events": patch
---

Replace lodash difference with native code
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 @@ -245,6 +245,7 @@ describe("victory-core", () => {
"containsNumbers": [Function],
"containsOnlyStrings": [Function],
"containsStrings": [Function],
"difference": [Function],
"getMaxValue": [Function],
"getMinValue": [Function],
"isArrayOfArrays": [Function],
Expand Down
10 changes: 6 additions & 4 deletions packages/victory-core/src/victory-util/add-events.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from "react";
import { defaults, difference, isEmpty, pick } from "lodash";
import type { ComponentEvent } from "./events";
import * as Events from "./events";
import { defaults, isEmpty, pick } from "lodash";
import isEqual from "react-fast-compare";

import { VictoryLabelableProps } from "../types/prop-types";
import { VictoryTransition } from "../victory-transition/victory-transition";
import { VictoryCommonProps, VictoryDatableProps } from "./common-props";
import { VictoryLabelableProps } from "../types/prop-types";
import { difference } from "./collection";
import type { ComponentEvent } from "./events";
import * as Events from "./events";
import { isFunction, isNil } from "./helpers";

// DISCLAIMER:
Expand Down
25 changes: 25 additions & 0 deletions packages/victory-core/src/victory-util/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ describe("victory-util/collection", () => {
});
});

describe("difference", () => {
it("handles empty arguments", () => {
// @ts-expect-error "Method expects 2 arguments"
expect(Collection.difference()).toEqual([]);
});

it("handles empty arrays", () => {
expect(Collection.difference([], [])).toEqual([]);
});

it("returns an empty array if there are no differences", () => {
expect(Collection.difference([1, 2], [1, 2])).toEqual([]);
});

it("returns the difference between two arrays", () => {
expect(Collection.difference([1, 2], [2, 3])).toEqual([1]);
});

it("returns the difference between two unequal arrays", () => {
expect(Collection.difference([1, 2, 3, 4, 5], [5, 2, 10])).toEqual([
1, 3, 4,
]);
});
});

describe("isArrayOfArrays", () => {
it("handles empty argument", () => {
// @ts-expect-error "Method expects 1 argument"
Expand Down
13 changes: 13 additions & 0 deletions packages/victory-core/src/victory-util/collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ export function containsOnlyStrings(
);
}

/**
* Creates an array of array values not included in the other given arrays
* @param a The array to inspect
* @param b The values to exclude
* @returns The new array of filtered values
*/
export function difference<T>(a: Array<T>, b: Array<T>): Array<T> {
if (a && b) {
return a.filter((value) => !b.includes(value));
}
return [];
}

export function isArrayOfArrays<T>(
collection: Array<T> | Array<Array<T>> | unknown,
): collection is Array<Array<T>> {
Expand Down
7 changes: 4 additions & 3 deletions packages/victory-shared-events/src/victory-shared-events.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defaults, isEmpty, fromPairs, difference } from "lodash";
import { defaults, isEmpty, fromPairs } from "lodash";
import React from "react";
import {
Collection,
EventCallbackInterface,
EventMixinCalculatedValues,
EventPropTypeInterface,
Expand Down Expand Up @@ -81,12 +82,12 @@ export class VictorySharedEvents extends React.Component<VictorySharedEventsProp

componentDidUpdate() {
const globalEventKeys = Object.keys(this.globalEvents);
const removedGlobalEventKeys = difference(
const removedGlobalEventKeys = Collection.difference(
this.prevGlobalEventKeys,
globalEventKeys,
);
removedGlobalEventKeys.forEach((key) => this.removeGlobalListener(key));
const addedGlobalEventKeys = difference(
const addedGlobalEventKeys = Collection.difference(
globalEventKeys,
this.prevGlobalEventKeys,
);
Expand Down

0 comments on commit 45c1080

Please sign in to comment.