Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor, reduce complexity and adding a "getImageSize_test" unit test
- Loading branch information
1 parent
70206bb
commit 533a6ac
Showing
6 changed files
with
144 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
import { validateParameterUndefinedOrNull } from './validator.js'; | ||
|
||
/** | ||
* Check if the angle is rotated | ||
* @param {Number} rotation the rotation angle | ||
* @returns {Boolean} true if the angle is rotated; Otherwise, false. | ||
* @memberof Internal | ||
*/ | ||
function isRotated (rotation) { | ||
return !(rotation === null || rotation === undefined || rotation === 0 || rotation === 180); | ||
} | ||
|
||
/** | ||
* Retrieves the current image dimensions given an enabled element | ||
* | ||
* @param {any} image The Cornerstone image. | ||
* @param {Number} rotation Optional. The rotation angle of the image. | ||
* @return {{width, height}} The Image dimensions | ||
* @return {{width:Number, height:Number}} The Image dimensions | ||
* @memberof Internal | ||
*/ | ||
export default function (image, rotation = null) { | ||
|
||
if (image.width === undefined || image.height === undefined) { | ||
throw new Error('getImageSize: parameter image must have width/height'); | ||
} | ||
validateParameterUndefinedOrNull(image, 'getImageSize: parameter image must not be undefined'); | ||
validateParameterUndefinedOrNull(image.width, 'getImageSize: parameter image must have width'); | ||
validateParameterUndefinedOrNull(image.height, 'getImageSize: parameter image must have height'); | ||
|
||
|
||
if (rotation === undefined || rotation === 0 || rotation === 180) { | ||
if (isRotated(rotation)) { | ||
return { | ||
width: image.width, | ||
height: image.height | ||
height: image.width, | ||
width: image.height | ||
}; | ||
} | ||
|
||
return { | ||
width: image.height, | ||
height: image.width | ||
width: image.width, | ||
height: image.height | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
/** | ||
* Check if the supplied parameter is undefined and throws and error | ||
* @param {any} checkParam the parameter to validate for undefined | ||
* @param {any} errorMsg the error message to be thrown | ||
* @returns {void} | ||
* @memberof internal | ||
*/ | ||
export function validateParameterUndefined (checkParam, errorMsg) { | ||
if (checkParam === undefined) { | ||
throw new Error(errorMsg); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Check if the supplied parameter is undefined or null and throws and error | ||
* @param {any} checkParam the parameter to validate for undefined | ||
* @param {any} errorMsg the error message to be thrown | ||
* @returns {void} | ||
* @memberof internal | ||
*/ | ||
export function validateParameterUndefinedOrNull (checkParam, errorMsg) { | ||
if (checkParam === undefined || checkParam === null) { | ||
throw new Error(errorMsg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { should, expect } from 'chai'; | ||
|
||
import getImageSize from '../../src/internal/getImageSize.js'; | ||
|
||
should(); | ||
|
||
describe('getImageSize', function () { | ||
|
||
describe('when image parameters is not passed', function () { | ||
it('should throw an error', function () { | ||
|
||
expect(function () { | ||
getImageSize(); | ||
}).to.throw('getImageSize: parameter image must not be undefined'); | ||
|
||
expect(function () { | ||
getImageSize({ width: 50 }); | ||
}).to.throw('getImageSize: parameter image must have height'); | ||
|
||
expect(function () { | ||
getImageSize({ height: 100 }); | ||
}).to.throw('getImageSize: parameter image must have width'); | ||
}); | ||
}); | ||
|
||
describe('when an image is passed with no rotation', function () { | ||
it('should return the image width/height', function () { | ||
const image = { | ||
width: 50, | ||
height:100 | ||
}; | ||
|
||
const imageSizeNoRotationParameter = getImageSize(image); | ||
const imageSize0RotationParameter = getImageSize(image, 0); | ||
const imageSize180RotationParameter = getImageSize(image, 180); | ||
|
||
imageSizeNoRotationParameter.should.be.deep.equal(image); | ||
imageSize0RotationParameter.should.be.deep.equal(image); | ||
imageSize180RotationParameter.should.be.deep.equal(image); | ||
}); | ||
}); | ||
|
||
describe('when an image is passed rotated', function () { | ||
it('should return the image width/height rotated', function () { | ||
let image = { | ||
width: 50, | ||
height:100 | ||
}; | ||
|
||
const returnedImageSize = getImageSize(image, 90); | ||
|
||
// rotate | ||
image.width = 100; | ||
image.height = 50; | ||
|
||
returnedImageSize.should.be.deep.equal(image); | ||
}); | ||
}); | ||
}); |