Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(src/test): Address lint issues + setup lint/format CI action #406

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},

"rules": {
"no-loss-of-precision": "off"
}
}
27 changes: 27 additions & 0 deletions .github/workflows/lint-and-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run lint and format check

on:
- push
- pull_request

jobs:
lint-and-format:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install packages
run: yarn install --frozen-lockfile

- name: Run ESLint
run: yarn lint

- name: Check Prettier formatting
run: yarn format:check
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
node-version: 18

- name: Install packages
uses: bahmutov/npm-install@v1
run: yarn install --frozen-lockfile

- name: Run tests
run: npm run test
run: yarn test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"build:examples": "npm run build && npx cpx 'build/**/*.{js,map}' examples/js",
"start": "rollup -c -w",
"format": "prettier --write 'src/**/*.js' 'test/**/*.js'",
"lint": "eslint --fix ."
"format:check": "prettier --check 'src/**/*.js' 'test/**/*.js'",
"lint": "eslint --fix 'src/**/*.js' 'test/**/*.js'"
},
"repository": {
"type": "git",
Expand Down
4 changes: 3 additions & 1 deletion src/BufferStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ class ReadBufferStream extends BufferStream {
}

writeUint8Repeat(value, count) {
throw new Error(value, "writeUint8Repeat not implemented");
throw new Error(
`writeUint8Repeat not implemented (value: ${value}, count: ${count})`
);
}

writeInt8(value) {
Expand Down
26 changes: 20 additions & 6 deletions src/DicomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ class DicomMessage {

// apply VR specific formatting to the original _rawValue and compare to the Value
const vr = ValueRepresentation.createByTypeString(vrType);
const originalValue = tagObject._rawValue.map((val) => vr.applyFormatting(val))
const originalValue = tagObject._rawValue.map(val =>
vr.applyFormatting(val)
);

// if Value has not changed, write _rawValue unformatted back into the file
if (deepEqual(tagObject.Value, originalValue)) {
Expand Down Expand Up @@ -366,18 +368,28 @@ class DicomMessage {
var times = length / vr.maxLength,
i = 0;
while (i++ < times) {
const { rawValue, value } = vr.read(stream, vr.maxLength, syntax, options);
const { rawValue, value } = vr.read(
stream,
vr.maxLength,
syntax,
options
);
rawValues.push(rawValue);
values.push(value);
}
} else {
const { rawValue, value } = vr.read(stream, length, syntax, options);
const { rawValue, value } = vr.read(
stream,
length,
syntax,
options
);
if (!vr.isBinary() && singleVRs.indexOf(vr.type) == -1) {
rawValues = rawValue;
values = value
values = value;
if (typeof value === "string") {
const delimiterChar = String.fromCharCode(VM_DELIMITER);
rawValues = vr.dropPadByte(rawValue.split(delimiterChar))
rawValues = vr.dropPadByte(rawValue.split(delimiterChar));
values = vr.dropPadByte(value.split(delimiterChar));
}
} else if (vr.type == "SQ") {
Expand All @@ -388,7 +400,9 @@ class DicomMessage {
values = value;
} else {
Array.isArray(value) ? (values = value) : values.push(value);
Array.isArray(rawValue) ? (rawValues = rawValue) : rawValues.push(rawValue);
Array.isArray(rawValue)
? (rawValues = rawValue)
: rawValues.push(rawValue);
}
}
stream.setEndian(oldEndian);
Expand Down
46 changes: 27 additions & 19 deletions src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ class ValueRepresentation {
return values;
}


read(stream, length, syntax, readOptions = { forceStoreRaw: false }) {
if (this.fixed && this.maxLength) {
if (!length) return this.defaultValue;
Expand Down Expand Up @@ -639,14 +638,16 @@ class CodeString extends AsciiStringRepresentation {

readBytes(stream, length) {
const BACKSLASH = String.fromCharCode(VM_DELIMITER);
return this.dropPadByte(stream.readAsciiString(length).split(BACKSLASH));
return this.dropPadByte(
stream.readAsciiString(length).split(BACKSLASH)
);
}

applyFormatting(value) {
const trim = (str) => str.trim();
const trim = str => str.trim();

if (Array.isArray(value)) {
return value.map((str) => trim(str));
return value.map(str => trim(str));
}

return trim(value);
Expand Down Expand Up @@ -697,7 +698,6 @@ class DateValue extends AsciiStringRepresentation {
}

class NumericStringRepresentation extends AsciiStringRepresentation {

readBytes(stream, length) {
const BACKSLASH = String.fromCharCode(VM_DELIMITER);
const numStr = stream.readAsciiString(length);
Expand All @@ -714,10 +714,10 @@ class DecimalString extends NumericStringRepresentation {
}

applyFormatting(value) {
const formatNumber = (numberStr) => {
const formatNumber = numberStr => {
let returnVal = numberStr.trim().replace(/[^0-9.\\\-+e]/gi, "");
return returnVal === "" ? null : Number(returnVal);
}
};

if (Array.isArray(value)) {
return value.map(formatNumber);
Expand All @@ -728,7 +728,7 @@ class DecimalString extends NumericStringRepresentation {

convertToString(value) {
if (value === null) return "";
if (typeof value === 'string') return value;
if (typeof value === "string") return value;

let str = String(value);
if (str.length > this.maxLength) {
Expand Down Expand Up @@ -841,10 +841,10 @@ class IntegerString extends NumericStringRepresentation {
}

applyFormatting(value) {
const formatNumber = (numberStr) => {
const formatNumber = numberStr => {
let returnVal = numberStr.trim().replace(/[^0-9.\\\-+e]/gi, "");
return returnVal === "" ? null : Number(returnVal);
}
};

if (Array.isArray(value)) {
return value.map(formatNumber);
Expand All @@ -854,7 +854,7 @@ class IntegerString extends NumericStringRepresentation {
}

convertToString(value) {
if (typeof value === 'string') return value;
if (typeof value === "string") return value;
return value === null ? "" : String(value);
}

Expand Down Expand Up @@ -967,14 +967,17 @@ class PersonName extends EncodedStringRepresentation {
}

readBytes(stream, length) {
return this.readPaddedEncodedString(stream, length).split(String.fromCharCode(VM_DELIMITER));
return this.readPaddedEncodedString(stream, length).split(
String.fromCharCode(VM_DELIMITER)
);
}

applyFormatting(value) {
const parsePersonName = (valueStr) => dicomJson.pnConvertToJsonObject(valueStr, false);
const parsePersonName = valueStr =>
dicomJson.pnConvertToJsonObject(valueStr, false);

if (Array.isArray(value)) {
return value.map((valueStr) => parsePersonName(valueStr));
return value.map(valueStr => parsePersonName(valueStr));
}

return parsePersonName(value);
Expand Down Expand Up @@ -1321,16 +1324,16 @@ class UniqueIdentifier extends AsciiStringRepresentation {
// https://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.4.html

if (result.indexOf(BACKSLASH) === -1) {
return result
return result;
} else {
return this.dropPadByte(result.split(BACKSLASH));
}
}

applyFormatting(value) {
const removeInvalidUidChars = (uidStr) => {
const removeInvalidUidChars = uidStr => {
return uidStr.replace(/[^0-9.]/g, "");
}
};

if (Array.isArray(value)) {
return value.map(removeInvalidUidChars);
Expand Down Expand Up @@ -1385,7 +1388,12 @@ class ParsedUnknownValue extends BinaryRepresentation {
i = 0;

while (i++ < times) {
const { rawValue, value } = vr.read(streamFromBuffer, vr.maxLength, syntax, readOptions);
const { rawValue, value } = vr.read(
streamFromBuffer,
vr.maxLength,
syntax,
readOptions
);
rawValues.push(rawValue);
values.push(value);
}
Expand Down Expand Up @@ -1466,4 +1474,4 @@ let VRinstances = {
UT: new UnlimitedText()
};

export {ValueRepresentation};
export { ValueRepresentation };
1 change: 0 additions & 1 deletion src/adapters/Cornerstone/Bidirectional.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class Bidirectional {
isCreating: false,
longestDiameter,
shortestDiameter,
toolType: "Bidirectional",
toolName: "Bidirectional",
visible: true,
finding: findingGroup
Expand Down
3 changes: 0 additions & 3 deletions src/adapters/Cornerstone/EllipticalRoi.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import MeasurementReport from "./MeasurementReport";
import TID300Ellipse from "../../utilities/TID300/Ellipse";
import CORNERSTONE_4_TAG from "./cornerstone4Tag";
import { toArray } from "../helpers.js";

const ELLIPTICALROI = "EllipticalRoi";
const FINDING = "121071";
const FINDING_SITE = "G-C0E3";

class EllipticalRoi {
constructor() {}
Expand Down
3 changes: 0 additions & 3 deletions src/adapters/Cornerstone/Length.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import MeasurementReport from "./MeasurementReport.js";
import TID300Length from "../../utilities/TID300/Length.js";
import CORNERSTONE_4_TAG from "./cornerstone4Tag";
import { toArray } from "../helpers.js";

const LENGTH = "Length";
const FINDING = "121071";
const FINDING_SITE = "G-C0E3";

class Length {
constructor() {}
Expand Down
2 changes: 0 additions & 2 deletions src/adapters/Cornerstone/Segmentation_3X.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ function addImageIdSpecificBrushToolState(

const cToolsPixelData = brushDataI.pixelData;

const [rows, cols] = pixelData2D.shape;

for (let p = 0; p < cToolsPixelData.length; p++) {
if (pixelData2D.data[p]) {
cToolsPixelData[p] = 1;
Expand Down
Loading