Skip to content

Commit 98f9113

Browse files
committed
avoid spreading into Math.max
1 parent c4a20bc commit 98f9113

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

site/utils/getMaxStep.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import { GraphicsObject } from "lib/types"
22

33
export function getMaxStep(graphics: GraphicsObject) {
4-
const maxPointStep = Math.max(
5-
0,
6-
...(graphics.points?.map((p) => (Number.isNaN(p.step) ? 0 : p.step || 0)) ??
7-
[]),
8-
)
9-
const maxLineStep = Math.max(
10-
0,
11-
...(graphics.lines?.map((l) => (Number.isNaN(l.step) ? 0 : l.step || 0)) ??
12-
[]),
13-
)
14-
const maxRectStep = Math.max(
15-
0,
16-
...(graphics.rects?.map((r) => (Number.isNaN(r.step) ? 0 : r.step || 0)) ??
17-
[]),
18-
)
19-
const maxCircleStep = Math.max(
20-
0,
21-
...(graphics.circles?.map((c) =>
22-
Number.isNaN(c.step) ? 0 : c.step || 0,
23-
) ?? []),
24-
)
4+
// Helper function to safely get max step from an array
5+
const getMaxStepFromArray = (items?: any[]) => {
6+
if (!items || items.length === 0) return 0
7+
8+
// Use reduce instead of spreading a potentially large array
9+
return items.reduce((max, item) => {
10+
const step = Number.isNaN(item.step) ? 0 : item.step || 0
11+
return Math.max(max, step)
12+
}, 0)
13+
}
14+
15+
const maxPointStep = getMaxStepFromArray(graphics.points)
16+
const maxLineStep = getMaxStepFromArray(graphics.lines)
17+
const maxRectStep = getMaxStepFromArray(graphics.rects)
18+
const maxCircleStep = getMaxStepFromArray(graphics.circles)
19+
2520
return Math.max(maxPointStep, maxLineStep, maxRectStep, maxCircleStep)
2621
}

0 commit comments

Comments
 (0)