From d16f49db298514f27633cfd208efe007c24b25a8 Mon Sep 17 00:00:00 2001 From: yagni Date: Thu, 15 Sep 2022 13:29:28 -0500 Subject: [PATCH] fix: isPrivateTag no longer silently returns false when given a tag whose last digit in the group is a hex digit. Note: Direct calls to isPrivateTag can now throw a string as an exception if the tag is corrupt. --- src/util/util.js | 7 +++++-- test/util_test.js | 11 ++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/util/util.js b/src/util/util.js index 5dd140f..4428a63 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -41,12 +41,15 @@ const isStringVr = (vr) => stringVrs[vr]; * Tests to see if a given tag in the format xggggeeee is a private tag or not * @param tag * @returns {boolean} + * @throws error if fourth character cannot be parsed */ const isPrivateTag = (tag) => { - const lastGroupDigit = parseInt(tag[4], 10); + const lastGroupDigit = parseInt(tag[4], 16); + if (isNaN(lastGroupDigit)) { + throw 'dicomParser.isPrivateTag: cannot parse last character of group'; + } const groupIsOdd = (lastGroupDigit % 2) === 1; - return groupIsOdd; }; diff --git a/test/util_test.js b/test/util_test.js index 6d6e478..8de0414 100644 --- a/test/util_test.js +++ b/test/util_test.js @@ -6,7 +6,7 @@ describe('util', () => { describe('#isPrivateTag', () => { it('should return `true` for a private tag', () => { - const isPrivateTag = util.isPrivateTag('x00190010'); + const isPrivateTag = util.isPrivateTag('x001d0010'); expect(isPrivateTag).to.equal(true); }) @@ -15,6 +15,15 @@ describe('util', () => { expect(isPrivateTag).to.equal(false); }) + it('should throw an exception', () => { + // Arrange + const tag = 'x100z0010'; + const invoker = () => util.isPrivateTag(tag); + + // Act / Assert + expect(invoker).to.throw(); + }); + }); describe('#parsePN', () => {