Skip to content

Commit

Permalink
Additional test cases, account for maxCharLength
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Berry committed Aug 27, 2024
1 parent f760f78 commit b803667
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/DicomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class DicomMessage {
if (typeof value === "string") {
const delimiterChar = String.fromCharCode(VM_DELIMITER);
rawValues = vr.dropPadByte(rawValue.split(delimiterChar))
values = value.split(delimiterChar);
values = vr.dropPadByte(value.split(delimiterChar));
}
} else if (vr.type == "SQ") {
rawValues = rawValue;
Expand Down
11 changes: 4 additions & 7 deletions src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ class ValueRepresentation {
* @returns {string[]} The modified array, with the padding byte potentially removed from the last value.
*/
dropPadByte(values) {
if (!Array.isArray(values) || !this.maxLength || !this.padByte) {
const maxLength = this.maxLength ?? this.maxCharLength;
if (!Array.isArray(values) || !maxLength || !this.padByte) {
return values;
}

Expand All @@ -162,8 +163,8 @@ class ValueRepresentation {

// If the last element exceeds the max length by exactly one character and ends with the padding byte,
// trim the padding byte to avoid a max length violation during write.
if (lastValue.length === this.maxLength + 1 && lastValue.endsWith(padChar)) {
values[lastIdx] = lastValue.substring(0, this.maxLength); // Trim the padding byte
if (lastValue.length === maxLength + 1 && lastValue.endsWith(padChar)) {
values[lastIdx] = lastValue.substring(0, maxLength); // Trim the padding byte
}
}

Expand Down Expand Up @@ -355,10 +356,6 @@ class AsciiStringRepresentation extends ValueRepresentation {
return stream.readAsciiString(length);
}

applyFormatting(value) {
return value.trim();
}

writeBytes(stream, value, writeOptions) {
const written = super.write(stream, "AsciiString", value);

Expand Down
44 changes: 25 additions & 19 deletions test/lossless-read-write.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import "regenerator-runtime/runtime.js";

import fs from "fs";
import dcmjs from "../src/index.js";
import {deepEqual} from "../src/utilities/deepEqual";
import { deepEqual } from "../src/utilities/deepEqual";

import {getTestDataset} from "./testUtils";
import { getTestDataset } from "./testUtils";

const {DicomDict, DicomMessage} = dcmjs.data;
const { DicomDict, DicomMessage } = dcmjs.data;


describe('lossless-read-write', () => {
Expand Down Expand Up @@ -192,21 +192,6 @@ describe('lossless-read-write', () => {
expect(deepEqual(expectedDataset, outputDicomDictPass2.dict)).toBeTruthy();
});

test('test DA with multiplicity > 1 and added space for even padding is read and written correctly', () => {
const dataset = {
'00081160': {
vr: 'DA',
Value: ["20140601", "20140601 "],
}
};

const dicomDict = new DicomDict({});
dicomDict.dict = dataset;

// write and re-read
const outputDicomDict = DicomMessage.readFile(dicomDict.write());
});

describe('Multiplicity for non-binary String VRs', () => {
const maxLengthCases = [
{
Expand Down Expand Up @@ -249,6 +234,27 @@ describe('lossless-read-write', () => {
Value: [123456789012, 123456789012],
_rawValue: ["123456789012", "123456789012"]
},
{
vr: 'LO',
Value: ["ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP"],
_rawValue: ["ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP"]
},
{
vr: 'SH',
Value: ["ABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOP"],
_rawValue: ["ABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOP"]
},
{
vr: 'UI',
Value: ["1.2.840.12345678901234567890123456789012345678901234567890123456", "1.2.840.12345678901234567890123456789012345678901234567890123456"],
_rawValue: ["1.2.840.12345678901234567890123456789012345678901234567890123456", "1.2.840.12345678901234567890123456789012345678901234567890123456"]
},
{
vr: 'TM',
Value: ["142530.1234567", "142530.1234567"],
_rawValue: ["142530.1234567", "142530.1234567"],
},

];

test.each(maxLengthCases)(
Expand All @@ -274,7 +280,7 @@ describe('lossless-read-write', () => {
}
};

expect(expectedDataset).toEqual(outputDicomDict.dict);
expect(outputDicomDict.dict).toEqual(expectedDataset);

// re-write should succeed without max length issues
const outputDicomDictPass2 = DicomMessage.readFile(outputDicomDict.write());
Expand Down

0 comments on commit b803667

Please sign in to comment.