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 difference with native code #2828

Merged
merged 3 commits into from
Mar 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
carbonrobot marked this conversation as resolved.
Show resolved Hide resolved
}
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