From 9a71b6281a08c4ac3f6ba463d45dc78a1bf1887d Mon Sep 17 00:00:00 2001 From: Danny Brown Date: Fri, 13 Mar 2020 09:47:19 -0400 Subject: [PATCH] fix: length tool throws error when measured value "length" is undefined (#1189) * fix: length tool throws error when measured value "length" is undefined * move internal function up scope * appease linter --- src/tools/annotation/LengthTool.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) 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; +}