From 70b24332783d63c9db2ed21d512d9f7b526c5222 Mon Sep 17 00:00:00 2001 From: Bill Wallace Date: Mon, 25 Oct 2021 16:06:09 -0400 Subject: [PATCH] fix(dcmjs): Add a set of accessors to the sequence list so the API is more consistent (#224) * fix(dcmjs): Add a set of accessors to the sequence list so the API is more consistent. * fix(dcmjs): Added documentation on the addAccessors as requested --- src/DicomMetaDictionary.js | 29 ++++++++++++++++++++--------- src/utilities/addAccessors.js | 26 ++++++++++++++++++++++++++ src/utilities/index.js | 4 +++- test/test_data.js | 3 ++- test/test_utilities.js | 27 ++++++++++++++++++++++++++- 5 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 src/utilities/addAccessors.js diff --git a/src/DicomMetaDictionary.js b/src/DicomMetaDictionary.js index 8158f82a..c6a9dd1e 100644 --- a/src/DicomMetaDictionary.js +++ b/src/DicomMetaDictionary.js @@ -1,6 +1,7 @@ import log from "./log.js"; -import { ValueRepresentation } from "./ValueRepresentation.js"; -import dictionary from "./dictionary.js"; +import { ValueRepresentation } from "./ValueRepresentation"; +import dictionary from "./dictionary"; +import addAccessors from "./utilities/addAccessors"; class DicomMetaDictionary { // intakes a custom dictionary that will be used to parse/denaturalize the dataset @@ -85,11 +86,13 @@ class DicomMetaDictionary { return namedDataset; } - // converts from DICOM JSON Model dataset - // to a natural dataset - // - sequences become lists - // - single element lists are replaced by their first element - // - object member names are dictionary, not group/element tag + /** converts from DICOM JSON Model dataset to a natural dataset + * - sequences become lists + * - single element lists are replaced by their first element, + * with single element lists remaining lists, but being a + * proxy for the child values, see addAccessors for examples + * - object member names are dictionary, not group/element tag + */ static naturalizeDataset(dataset) { const naturalDataset = { _vrMap: {} @@ -142,8 +145,16 @@ class DicomMetaDictionary { } if (naturalDataset[naturalName].length === 1) { - naturalDataset[naturalName] = - naturalDataset[naturalName][0]; + const sqZero = naturalDataset[naturalName][0]; + if ( + sqZero && + typeof sqZero === "object" && + !sqZero.length + ) { + addAccessors(naturalDataset[naturalName], sqZero); + } else { + naturalDataset[naturalName] = sqZero; + } } } }); diff --git a/src/utilities/addAccessors.js b/src/utilities/addAccessors.js new file mode 100644 index 00000000..98da3590 --- /dev/null +++ b/src/utilities/addAccessors.js @@ -0,0 +1,26 @@ +/** + * Adds accessors (set/get) to dest for every property in src. + * @example + * src = [{a:5,b:'string', c:null}] + * addAccessors(src) + * src.c = 'outerChange' + * src[0].b='innerChange' + * + * assert src.a===5 + * assert src[0].c === 'outerChange' + * assert src.b === 'innerChange' + */ +const addAccessors = (dest, src) => { + Object.keys(src).forEach(key => { + Object.defineProperty(dest, key, { + get: () => { + return src[key]; + }, + set: v => { + src[key] = v; + } + }); + }); +}; + +export default addAccessors; diff --git a/src/utilities/index.js b/src/utilities/index.js index 75e29a64..8730f7ff 100644 --- a/src/utilities/index.js +++ b/src/utilities/index.js @@ -1,11 +1,13 @@ import TID1500 from "./TID1500/index.js"; import TID300 from "./TID300/index.js"; import message from "./Message.js"; +import addAccessors from "./addAccessors.js"; const utilities = { TID1500, TID300, - message + message, + addAccessors }; export default utilities; diff --git a/test/test_data.js b/test/test_data.js index 9a12c098..29000352 100644 --- a/test/test_data.js +++ b/test/test_data.js @@ -165,7 +165,8 @@ const tests = { const spacing = naturalSequence.SharedFunctionalGroupsSequence .PixelMeasuresSequence.SpacingBetweenSlices; expect(spacing).to.equal(0.12); - + expect(Array.isArray(naturalSequence.SharedFunctionalGroupsSequence)).to.equal(true); + expect(naturalSequence.ProcedureCodeSequence).to.have.property( "CodingSchemeDesignator", "L" diff --git a/test/test_utilities.js b/test/test_utilities.js index 3c715208..f00597fc 100644 --- a/test/test_utilities.js +++ b/test/test_utilities.js @@ -1,5 +1,30 @@ +const expect = require("chai").expect; +const dcmjs = require("../build/dcmjs"); +const { utilities } = dcmjs; +const { addAccessors } = utilities; exports.test = () => { - console.log('no tests yet'); + const tests = { + testAddAccessor: () => { + const baseValue = { a: 1, b: 2 }; + const arrValue = [baseValue]; + addAccessors(arrValue, baseValue); + expect(arrValue.a).to.equal(1); + baseValue.a = 3; + expect(arrValue.a).to.equal(3); + arrValue.b = 4; + expect(baseValue.b).to.equal(4); + const forArr = []; + arrValue.forEach(item => forArr.push(item)); + expect(forArr.length).to.equal(1); + }, + }; + + Object.keys(tests).forEach(testKey => { + console.log('Running', testKey); + const test = tests[testKey]; + test(); + console.log('Done running', testKey); + }); }