|
1 | 1 | import { GraphicsObject } from "lib/types" |
2 | 2 |
|
3 | 3 | 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 | + |
25 | 20 | return Math.max(maxPointStep, maxLineStep, maxRectStep, maxCircleStep) |
26 | 21 | } |
0 commit comments