maker.js icon indicating copy to clipboard operation
maker.js copied to clipboard

Don't return undefined when an empty model is passed to makerjs.measure.modelExtents

Open xavi- opened this issue 8 months ago • 2 comments

Currently makerjs.measure.modelExtents({ models: {} }) returns undefined which makes code prone to NPE's

This PR updates modelExtents to instead return an empty IMeasureWithCenter object: { height: 0, width: 0, center: null, low: null, high: null }

xavi- avatar May 31 '25 08:05 xavi-

It might be better to return { width: 0, height: 0, center: [0, 0], low: [0, 0], high: [0, 0] } instead since according to the types, center, low, and high aren't nullable

xavi- avatar May 31 '25 09:05 xavi-

Thanks for the PR! But, since the whole library doesn't have 100% test coverage (my fault not yours), I'm a little hesitant to take this. Perhaps you could add your own defensive function in your own code:

function alwaysMeasureModel(model) {
   return makerjs.measure.modelExtents(model) || { width: 0, height: 0, center: [0, 0], low: [0, 0], high: [0, 0] };
}

danmarshall avatar Jun 10 '25 22:06 danmarshall

Or perhaps the return type of modelExtents should be changed to IMeasureWithCenter | null? I created this PR mostly because I got bitten by this in production

xavi- avatar Jun 19 '25 14:06 xavi-

Sorry it bit you in production. Yes the | null is a good approach.

danmarshall avatar Jun 19 '25 19:06 danmarshall

Updated PR. Also updated method to explicitly return null

xavi- avatar Jul 03 '25 00:07 xavi-