(...rects: Rect[])
| 126 | * @returns Union of `rects`. If `rects` is empty, returns `zeroRect`. |
| 127 | */ |
| 128 | export function unionOfRects(...rects: Rect[]): Rect { |
| 129 | if (rects.length === 0) { |
| 130 | return zeroRect; |
| 131 | } |
| 132 | |
| 133 | const [firstRect, ...remainingRects] = rects; |
| 134 | const boxUnion = remainingRects |
| 135 | .map(rectToBox) |
| 136 | .reduce((intermediateUnion, nextBox): Box => { |
| 137 | const [unionTop, unionRight, unionBottom, unionLeft] = intermediateUnion; |
| 138 | const [nextTop, nextRight, nextBottom, nextLeft] = nextBox; |
| 139 | return [ |
| 140 | Math.min(unionTop, nextTop), |
| 141 | Math.max(unionRight, nextRight), |
| 142 | Math.max(unionBottom, nextBottom), |
| 143 | Math.min(unionLeft, nextLeft), |
| 144 | ]; |
| 145 | }, rectToBox(firstRect)); |
| 146 | return boxToRect(boxUnion); |
| 147 | } |
no test coverage detected