Skip to content

Commit

Permalink
Replace lodash values and mapValues with native code (#2808)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot authored Feb 15, 2024
1 parent 34071c5 commit 9cdaf2c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .changeset/sharp-starfishes-peel.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions packages/victory-brush-container/src/brush-helpers.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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) &&
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down
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 @@ -313,6 +313,7 @@ describe("victory-core", () => {
"isHorizontal": [Function],
"isNil": [Function],
"isTooltip": [Function],
"mapValues": [Function],
"modifyProps": [Function],
"omit": [Function],
"radiansToDegrees": [Function],
Expand Down
7 changes: 3 additions & 4 deletions packages/victory-core/src/victory-util/axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
invert,
uniq,
orderBy,
values,
includes,
without,
} from "lodash";
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions packages/victory-core/src/victory-util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
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.
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-core/src/victory-util/wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
uniq,
groupBy,
uniqBy,
values,
isPlainObject,
} from "lodash";
import React from "react";
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/victory-cursor-container/src/cursor-helpers.tsx
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) &&
Expand Down
3 changes: 1 addition & 2 deletions packages/victory-scatter/src/helper-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { values } from "lodash";
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";

export const getSymbol = (data, props) => {
Expand All @@ -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();
Expand Down

0 comments on commit 9cdaf2c

Please sign in to comment.