diff --git a/src/tools/annotation/LengthTool.js b/src/tools/annotation/LengthTool.js index 3713fe6dc..041b10ac9 100644 --- a/src/tools/annotation/LengthTool.js +++ b/src/tools/annotation/LengthTool.js @@ -227,7 +227,15 @@ 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'; @@ -235,9 +243,9 @@ export default class LengthTool extends BaseAnnotationTool { suffix = 'pixels'; } - data.unit = suffix; + annotation.unit = suffix; - return `${data.length.toFixed(2)} ${suffix}`; + return `${measuredValue.toFixed(2)} ${suffix}`; } function textBoxAnchorPoints(handles) { @@ -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; +}