Skip to content

Commit 9cdaf2c

Browse files
authored
Replace lodash values and mapValues with native code (#2808)
1 parent 34071c5 commit 9cdaf2c

File tree

8 files changed

+38
-16
lines changed

8 files changed

+38
-16
lines changed

.changeset/sharp-starfishes-peel.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"victory-brush-container": patch
3+
"victory-core": patch
4+
"victory-cursor-container": patch
5+
"victory-scatter": patch
6+
---
7+
8+
Replace lodash values and mapValues with native code

packages/victory-brush-container/src/brush-helpers.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Helpers as CoreHelpers, Selection } from "victory-core";
2-
import { throttle, defaults, mapValues } from "lodash";
2+
import { throttle, defaults } from "lodash";
33
import isEqual from "react-fast-compare";
44

55
const Helpers = {
@@ -12,8 +12,8 @@ const Helpers = {
1212
},
1313

1414
withinBounds(point, bounds, padding?) {
15-
const { x1, x2, y1, y2 } = mapValues(bounds, Number);
16-
const { x, y } = mapValues(point, Number);
15+
const { x1, x2, y1, y2 } = CoreHelpers.mapValues(bounds, Number);
16+
const { x, y } = CoreHelpers.mapValues(point, Number);
1717
const paddingValue = padding ? padding / 2 : 0;
1818
return (
1919
x + paddingValue >= Math.min(x1, x2) &&
@@ -190,7 +190,7 @@ const Helpers = {
190190
},
191191

192192
constrainBox(box, fullDomainBox) {
193-
const { x1, y1, x2, y2 } = mapValues(fullDomainBox, Number);
193+
const { x1, y1, x2, y2 } = CoreHelpers.mapValues(fullDomainBox, Number);
194194
return {
195195
x1: box.x2 > x2 ? x2 - Math.abs(box.x2 - box.x1) : Math.max(box.x1, x1),
196196
y1: box.y2 > y2 ? y2 - Math.abs(box.y2 - box.y1) : Math.max(box.y1, y1),
@@ -200,7 +200,7 @@ const Helpers = {
200200
},
201201

202202
constrainPoint(point, fullDomainBox) {
203-
const { x1, y1, x2, y2 } = mapValues(fullDomainBox, Number);
203+
const { x1, y1, x2, y2 } = CoreHelpers.mapValues(fullDomainBox, Number);
204204
return {
205205
x: Math.min(Math.max(point.x, x1), x2),
206206
y: Math.min(Math.max(point.y, y1), y2),

packages/victory-core/src/exports.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ describe("victory-core", () => {
313313
"isHorizontal": [Function],
314314
"isNil": [Function],
315315
"isTooltip": [Function],
316+
"mapValues": [Function],
316317
"modifyProps": [Function],
317318
"omit": [Function],
318319
"radiansToDegrees": [Function],

packages/victory-core/src/victory-util/axis.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
invert,
77
uniq,
88
orderBy,
9-
values,
109
includes,
1110
without,
1211
} from "lodash";
@@ -157,8 +156,8 @@ function getDefaultTickFormat(props) {
157156
: fallbackFormat;
158157
}
159158
const invertedStringMap = stringMap && invert(stringMap);
160-
const tickValueArray = orderBy(values(stringMap), (n) => n);
161-
const dataNames = tickValueArray.map((tick) => invertedStringMap[tick]);
159+
const tickValueArray = orderBy(Object.values(stringMap), (n) => n);
160+
const dataNames = tickValueArray.map((tick: any) => invertedStringMap[tick]);
162161
// string ticks should have one tick of padding at the beginning
163162
const dataTicks = ["", ...dataNames, ""];
164163
return (x) => dataTicks[x];
@@ -174,7 +173,7 @@ function getStringTicks(props) {
174173
categories && Collection.containsOnlyStrings(categories)
175174
? categories.map((tick) => stringMap[tick])
176175
: undefined;
177-
const ticksFromStringMap = stringMap && values(stringMap);
176+
const ticksFromStringMap = stringMap && Object.values(stringMap);
178177
return ticksFromCategories && ticksFromCategories.length !== 0
179178
? ticksFromCategories
180179
: ticksFromStringMap;

packages/victory-core/src/victory-util/helpers.ts

+16
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ export function getCurrentAxis(axis, horizontal) {
237237
return horizontal ? otherAxis : axis;
238238
}
239239

240+
/**
241+
* Creates an object with the same keys as object and values generated by running
242+
* each own enumerable string keyed property of object through the function fn
243+
*/
244+
export function mapValues<T>(
245+
values: T,
246+
fn: (value?: any) => any,
247+
): T | undefined {
248+
if (values) {
249+
return Object.keys(values).reduce((acc, key) => {
250+
acc[key] = fn(values[key]);
251+
return acc;
252+
}, {} as T);
253+
}
254+
}
255+
240256
/**
241257
* Creates an array of numbers (positive and/or negative) progressing
242258
* from start up to, but not including, end.

packages/victory-core/src/victory-util/wrapper.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
uniq,
55
groupBy,
66
uniqBy,
7-
values,
87
isPlainObject,
98
} from "lodash";
109
import React from "react";
@@ -127,7 +126,7 @@ export function getDataFromChildren(props, childComponents) {
127126
combine,
128127
);
129128
const group = stacked ? "_group" : "_stack";
130-
return values(groupBy(datasets, group));
129+
return Object.values(groupBy(datasets, group));
131130
}
132131

133132
export function getData(props, childComponents) {

packages/victory-cursor-container/src/cursor-helpers.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Helpers, Selection, SVGCoordinateType } from "victory-core";
2-
import { throttle, mapValues } from "lodash";
2+
import { throttle } from "lodash";
33

44
const ON_MOUSE_MOVE_THROTTLE_MS = 16;
55

@@ -13,8 +13,8 @@ class CursorHelpersClass {
1313
}
1414

1515
withinBounds(point, bounds) {
16-
const { x1, x2, y1, y2 } = mapValues(bounds, Number);
17-
const { x, y } = mapValues(point, Number);
16+
const { x1, x2, y1, y2 } = Helpers.mapValues(bounds, Number);
17+
const { x, y } = Helpers.mapValues(point, Number);
1818
return (
1919
x >= Math.min(x1, x2) &&
2020
x <= Math.max(x1, x2) &&

packages/victory-scatter/src/helper-methods.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { values } from "lodash";
21
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";
32

43
export const getSymbol = (data, props) => {
@@ -14,7 +13,7 @@ export const getBubbleSize = (datum, props) => {
1413
const zMin = Math.min(...zData);
1514
const zMax = Math.max(...zData);
1615
const getMaxRadius = () => {
17-
const minPadding = Math.min(...values(Helpers.getPadding(props)));
16+
const minPadding = Math.min(...Object.values(Helpers.getPadding(props)));
1817
return Math.max(minPadding, 5); // eslint-disable-line no-magic-numbers
1918
};
2019
const maxRadius = maxBubbleSize || getMaxRadius();

0 commit comments

Comments
 (0)