From 9cdaf2c080a26d1e228c75a8bd1e93b8a43c3b59 Mon Sep 17 00:00:00 2001 From: Charlie Brown Date: Thu, 15 Feb 2024 12:05:25 -0600 Subject: [PATCH] Replace lodash values and mapValues with native code (#2808) --- .changeset/sharp-starfishes-peel.md | 8 ++++++++ .../victory-brush-container/src/brush-helpers.ts | 10 +++++----- packages/victory-core/src/exports.test.ts | 1 + packages/victory-core/src/victory-util/axis.tsx | 7 +++---- .../victory-core/src/victory-util/helpers.ts | 16 ++++++++++++++++ .../victory-core/src/victory-util/wrapper.tsx | 3 +-- .../src/cursor-helpers.tsx | 6 +++--- packages/victory-scatter/src/helper-methods.tsx | 3 +-- 8 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 .changeset/sharp-starfishes-peel.md diff --git a/.changeset/sharp-starfishes-peel.md b/.changeset/sharp-starfishes-peel.md new file mode 100644 index 000000000..1ccebb46f --- /dev/null +++ b/.changeset/sharp-starfishes-peel.md @@ -0,0 +1,8 @@ +--- +"victory-brush-container": patch +"victory-core": patch +"victory-cursor-container": patch +"victory-scatter": patch +--- + +Replace lodash values and mapValues with native code diff --git a/packages/victory-brush-container/src/brush-helpers.ts b/packages/victory-brush-container/src/brush-helpers.ts index 97be8fe23..96cc8a30c 100644 --- a/packages/victory-brush-container/src/brush-helpers.ts +++ b/packages/victory-brush-container/src/brush-helpers.ts @@ -1,5 +1,5 @@ import { Helpers as CoreHelpers, Selection } from "victory-core"; -import { throttle, defaults, mapValues } from "lodash"; +import { throttle, defaults } from "lodash"; import isEqual from "react-fast-compare"; const Helpers = { @@ -12,8 +12,8 @@ const Helpers = { }, withinBounds(point, bounds, padding?) { - const { x1, x2, y1, y2 } = mapValues(bounds, Number); - const { x, y } = mapValues(point, Number); + const { x1, x2, y1, y2 } = CoreHelpers.mapValues(bounds, Number); + const { x, y } = CoreHelpers.mapValues(point, Number); const paddingValue = padding ? padding / 2 : 0; return ( x + paddingValue >= Math.min(x1, x2) && @@ -190,7 +190,7 @@ const Helpers = { }, constrainBox(box, fullDomainBox) { - const { x1, y1, x2, y2 } = mapValues(fullDomainBox, Number); + const { x1, y1, x2, y2 } = CoreHelpers.mapValues(fullDomainBox, Number); return { x1: box.x2 > x2 ? x2 - Math.abs(box.x2 - box.x1) : Math.max(box.x1, x1), y1: box.y2 > y2 ? y2 - Math.abs(box.y2 - box.y1) : Math.max(box.y1, y1), @@ -200,7 +200,7 @@ const Helpers = { }, constrainPoint(point, fullDomainBox) { - const { x1, y1, x2, y2 } = mapValues(fullDomainBox, Number); + const { x1, y1, x2, y2 } = CoreHelpers.mapValues(fullDomainBox, Number); return { x: Math.min(Math.max(point.x, x1), x2), y: Math.min(Math.max(point.y, y1), y2), diff --git a/packages/victory-core/src/exports.test.ts b/packages/victory-core/src/exports.test.ts index ce4c81030..63a5f8212 100644 --- a/packages/victory-core/src/exports.test.ts +++ b/packages/victory-core/src/exports.test.ts @@ -313,6 +313,7 @@ describe("victory-core", () => { "isHorizontal": [Function], "isNil": [Function], "isTooltip": [Function], + "mapValues": [Function], "modifyProps": [Function], "omit": [Function], "radiansToDegrees": [Function], diff --git a/packages/victory-core/src/victory-util/axis.tsx b/packages/victory-core/src/victory-util/axis.tsx index 451ca815e..d34ae19c5 100644 --- a/packages/victory-core/src/victory-util/axis.tsx +++ b/packages/victory-core/src/victory-util/axis.tsx @@ -6,7 +6,6 @@ import { invert, uniq, orderBy, - values, includes, without, } from "lodash"; @@ -157,8 +156,8 @@ function getDefaultTickFormat(props) { : fallbackFormat; } const invertedStringMap = stringMap && invert(stringMap); - const tickValueArray = orderBy(values(stringMap), (n) => n); - const dataNames = tickValueArray.map((tick) => invertedStringMap[tick]); + 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 const dataTicks = ["", ...dataNames, ""]; return (x) => dataTicks[x]; @@ -174,7 +173,7 @@ function getStringTicks(props) { categories && Collection.containsOnlyStrings(categories) ? categories.map((tick) => stringMap[tick]) : undefined; - const ticksFromStringMap = stringMap && values(stringMap); + const ticksFromStringMap = stringMap && Object.values(stringMap); return ticksFromCategories && ticksFromCategories.length !== 0 ? ticksFromCategories : ticksFromStringMap; diff --git a/packages/victory-core/src/victory-util/helpers.ts b/packages/victory-core/src/victory-util/helpers.ts index 196bc6196..0e42df739 100644 --- a/packages/victory-core/src/victory-util/helpers.ts +++ b/packages/victory-core/src/victory-util/helpers.ts @@ -237,6 +237,22 @@ export function getCurrentAxis(axis, horizontal) { return horizontal ? otherAxis : axis; } +/** + * Creates an object with the same keys as object and values generated by running + * each own enumerable string keyed property of object through the function fn + */ +export function mapValues( + values: T, + fn: (value?: any) => any, +): T | undefined { + if (values) { + return Object.keys(values).reduce((acc, key) => { + acc[key] = fn(values[key]); + return acc; + }, {} as T); + } +} + /** * Creates an array of numbers (positive and/or negative) progressing * from start up to, but not including, end. diff --git a/packages/victory-core/src/victory-util/wrapper.tsx b/packages/victory-core/src/victory-util/wrapper.tsx index 13ab3a2c0..ef18726f0 100644 --- a/packages/victory-core/src/victory-util/wrapper.tsx +++ b/packages/victory-core/src/victory-util/wrapper.tsx @@ -4,7 +4,6 @@ import { uniq, groupBy, uniqBy, - values, isPlainObject, } from "lodash"; import React from "react"; @@ -127,7 +126,7 @@ export function getDataFromChildren(props, childComponents) { combine, ); const group = stacked ? "_group" : "_stack"; - return values(groupBy(datasets, group)); + return Object.values(groupBy(datasets, group)); } export function getData(props, childComponents) { diff --git a/packages/victory-cursor-container/src/cursor-helpers.tsx b/packages/victory-cursor-container/src/cursor-helpers.tsx index 0e2276189..06fae0aa3 100644 --- a/packages/victory-cursor-container/src/cursor-helpers.tsx +++ b/packages/victory-cursor-container/src/cursor-helpers.tsx @@ -1,5 +1,5 @@ import { Helpers, Selection, SVGCoordinateType } from "victory-core"; -import { throttle, mapValues } from "lodash"; +import { throttle } from "lodash"; const ON_MOUSE_MOVE_THROTTLE_MS = 16; @@ -13,8 +13,8 @@ class CursorHelpersClass { } withinBounds(point, bounds) { - const { x1, x2, y1, y2 } = mapValues(bounds, Number); - const { x, y } = mapValues(point, Number); + const { x1, x2, y1, y2 } = Helpers.mapValues(bounds, Number); + const { x, y } = Helpers.mapValues(point, Number); return ( x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && diff --git a/packages/victory-scatter/src/helper-methods.tsx b/packages/victory-scatter/src/helper-methods.tsx index 43e083d94..6518f7ad7 100644 --- a/packages/victory-scatter/src/helper-methods.tsx +++ b/packages/victory-scatter/src/helper-methods.tsx @@ -1,4 +1,3 @@ -import { values } from "lodash"; import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core"; export const getSymbol = (data, props) => { @@ -14,7 +13,7 @@ export const getBubbleSize = (datum, props) => { const zMin = Math.min(...zData); const zMax = Math.max(...zData); const getMaxRadius = () => { - const minPadding = Math.min(...values(Helpers.getPadding(props))); + const minPadding = Math.min(...Object.values(Helpers.getPadding(props))); return Math.max(minPadding, 5); // eslint-disable-line no-magic-numbers }; const maxRadius = maxBubbleSize || getMaxRadius();