Skip to content

Commit

Permalink
fix: length tool throws error when measured value "length" is undefin…
Browse files Browse the repository at this point in the history
…ed (#1189)

* fix: length tool throws error when measured value "length" is undefined

* move internal function up scope

* appease linter
  • Loading branch information
dannyrb authored Mar 13, 2020
1 parent e70b894 commit 9a71b62
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/tools/annotation/LengthTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,25 @@ export default class LengthTool extends BaseAnnotationTool {
});
}

function textBoxText(data, rowPixelSpacing, colPixelSpacing) {
// - SideEffect: Updates annotation 'suffix'
function textBoxText(annotation, rowPixelSpacing, colPixelSpacing) {
const measuredValue = _sanitizeMeasuredValue(annotation.length);

// measured value is not defined, return empty string
if (!measuredValue) {
return '';
}

// Set the length text suffix depending on whether or not pixelSpacing is available
let suffix = 'mm';

if (!rowPixelSpacing || !colPixelSpacing) {
suffix = 'pixels';
}

data.unit = suffix;
annotation.unit = suffix;

return `${data.length.toFixed(2)} ${suffix}`;
return `${measuredValue.toFixed(2)} ${suffix}`;
}

function textBoxAnchorPoints(handles) {
Expand All @@ -250,3 +258,17 @@ export default class LengthTool extends BaseAnnotationTool {
}
}
}

/**
* Attempts to sanitize a value by casting as a number; if unable to cast,
* we return `undefined`
*
* @param {*} value
* @returns a number or undefined
*/
function _sanitizeMeasuredValue(value) {
const parsedValue = Number(value);
const isNumber = !isNaN(parsedValue);

return isNumber ? parsedValue : undefined;
}

0 comments on commit 9a71b62

Please sign in to comment.