Skip to content

Commit

Permalink
fix(dcmjs): Add a set of accessors to the sequence list so the API is…
Browse files Browse the repository at this point in the history
… more consistent (dcmjs-org#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
  • Loading branch information
wayfarer3130 authored Oct 25, 2021
1 parent 7a7df33 commit 70b2433
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
29 changes: 20 additions & 9 deletions src/DicomMetaDictionary.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: {}
Expand Down Expand Up @@ -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;
}
}
}
});
Expand Down
26 changes: 26 additions & 0 deletions src/utilities/addAccessors.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion src/utilities/index.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion test/test_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 26 additions & 1 deletion test/test_utilities.js
Original file line number Diff line number Diff line change
@@ -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);
});
}

0 comments on commit 70b2433

Please sign in to comment.