From a14e76c45f60245a7431cc306d48f07f3e4b32c4 Mon Sep 17 00:00:00 2001 From: Charlie Brown Date: Tue, 12 Mar 2024 12:39:46 -0500 Subject: [PATCH] Replace lodash.invert with native code (#2830) --- .changeset/flat-phones-reply.md | 5 +++++ packages/victory-core/src/exports.test.ts | 1 + packages/victory-core/src/victory-util/axis.tsx | 6 +++--- .../victory-core/src/victory-util/helpers.test.ts | 14 ++++++++++++++ packages/victory-core/src/victory-util/helpers.ts | 11 +++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 .changeset/flat-phones-reply.md diff --git a/.changeset/flat-phones-reply.md b/.changeset/flat-phones-reply.md new file mode 100644 index 000000000..db3625f0e --- /dev/null +++ b/.changeset/flat-phones-reply.md @@ -0,0 +1,5 @@ +--- +"victory-core": patch +--- + +Replace lodash.invert with native code diff --git a/packages/victory-core/src/exports.test.ts b/packages/victory-core/src/exports.test.ts index 63a5f8212..e02a329f9 100644 --- a/packages/victory-core/src/exports.test.ts +++ b/packages/victory-core/src/exports.test.ts @@ -309,6 +309,7 @@ describe("victory-core", () => { "getRadius": [Function], "getRange": [Function], "getStyles": [Function], + "invert": [Function], "isFunction": [Function], "isHorizontal": [Function], "isNil": [Function], diff --git a/packages/victory-core/src/victory-util/axis.tsx b/packages/victory-core/src/victory-util/axis.tsx index c3d5d29f4..bc63395ea 100644 --- a/packages/victory-core/src/victory-util/axis.tsx +++ b/packages/victory-core/src/victory-util/axis.tsx @@ -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"; @@ -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 @@ -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); }; diff --git a/packages/victory-core/src/victory-util/helpers.test.ts b/packages/victory-core/src/victory-util/helpers.test.ts index 80ba01061..a2f0937af 100644 --- a/packages/victory-core/src/victory-util/helpers.test.ts +++ b/packages/victory-core/src/victory-util/helpers.test.ts @@ -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 }; diff --git a/packages/victory-core/src/victory-util/helpers.ts b/packages/victory-core/src/victory-util/helpers.ts index 7a43e70bf..24d7a8482 100644 --- a/packages/victory-core/src/victory-util/helpers.ts +++ b/packages/victory-core/src/victory-util/helpers.ts @@ -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) { + 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