Skip to content

Commit

Permalink
Replace lodash.invert with native code (#2830)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot committed Mar 12, 2024
1 parent 3d6f8ad commit a14e76c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-phones-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-core": patch
---

Replace lodash.invert 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 @@ -309,6 +309,7 @@ describe("victory-core", () => {
"getRadius": [Function],
"getRange": [Function],
"getStyles": [Function],
"invert": [Function],
"isFunction": [Function],
"isHorizontal": [Function],
"isNil": [Function],
Expand Down
6 changes: 3 additions & 3 deletions packages/victory-core/src/victory-util/axis.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { defaults, identity, isObject, invert, uniq, orderBy } from "lodash";
import { defaults, identity, isObject, uniq, orderBy } from "lodash";
import * as Collection from "./collection";
import * as Domain from "./domain";
import * as Helpers from "./helpers";
Expand Down Expand Up @@ -146,7 +146,7 @@ function getDefaultTickFormat(props) {
? (x, index) => tickValues[index]
: fallbackFormat;
}
const invertedStringMap = stringMap && invert(stringMap);
const invertedStringMap = stringMap && Helpers.invert(stringMap);
const tickValueArray = orderBy(Object.values(stringMap), (n) => n);
const dataNames = tickValueArray.map((tick: any) => invertedStringMap[tick]);
// string ticks should have one tick of padding at the beginning
Expand Down Expand Up @@ -252,7 +252,7 @@ export function getTickFormat(props, scale) {
return (x, index) => filteredTickFormat[index];
} else if (tickFormat && Helpers.isFunction(tickFormat)) {
const applyStringTicks = (tick, index, ticks) => {
const invertedStringMap = invert(stringMap);
const invertedStringMap = Helpers.invert(stringMap);
const stringTickArray = ticks.map((t) => invertedStringMap[t]);
return props.tickFormat(invertedStringMap[tick], index, stringTickArray);
};
Expand Down
14 changes: 14 additions & 0 deletions packages/victory-core/src/victory-util/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import * as Helpers from "./helpers";

describe("victory-util/helpers", () => {
describe("invert", () => {
it("inverts a given object", () => {
const data = { x: 3, y: "2", z: 1 };
const invertedData = Helpers.invert(data);
expect(invertedData).toEqual({ 3: "x", 2: "y", 1: "z" });
});

it("handles duplicate values by taking the last key", () => {
const data = { x: 3, y: 2, z: 1, a: 2 };
const invertedData = Helpers.invert(data);
expect(invertedData).toEqual({ 3: "x", 2: "a", 1: "z" });
});
});

describe("omit", () => {
const data = { x: 3, y: 2, z: 1 };

Expand Down
11 changes: 11 additions & 0 deletions packages/victory-core/src/victory-util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ function getPolarRange(props, axis) {

// Exported Functions

/**
* Creates an object composed of the inverted keys and values of object.
* If object contains duplicate values, subsequent values overwrite property assignments of previous values.
*/
export function invert(original: Record<string, string | number>) {
return Object.entries(original).reduce((acc, current) => {
acc[current[1]] = current[0];
return acc;
}, {});
}

/**
* creates an object with some keys excluded
* replacement for lodash.omit for performance. does not mimic the entire lodash.omit api
Expand Down

0 comments on commit a14e76c

Please sign in to comment.