diff --git a/validators/__tests__/headerFields.spec.js b/validators/__tests__/headerFields.spec.js new file mode 100644 index 000000000..23a18b686 --- /dev/null +++ b/validators/__tests__/headerFields.spec.js @@ -0,0 +1,65 @@ +const assert = require('chai').assert +const { collect39Issues } = require('../headerFields') + +describe('headerFields', () => { + describe('collect39Issues()', () => { + it('should return an empty array if there are no files in allIssues39Dict', () => { + const allIssues39Dict = {} + const issues = collect39Issues(allIssues39Dict) + assert.isArray(issues) + assert.lengthOf(issues, 0) + }) + it('should return one issue per file in the allIssues39Dict', () => { + const allIssues39Dict = { + file_1: [ + { + code: 39, + reason: 'for some reason', + }, + { + code: 39, + reason: 'for some other reason', + }, + ], + } + const issues = collect39Issues(allIssues39Dict) + assert.isArray(issues) + assert.lengthOf(issues, 1) + }) + it('should return one issue for each file with code 39 issues', () => { + const allIssues39Dict = { + file_1: [ + { + code: 39, + reason: 'reason1', + }, + ], + file_2: [ + { + code: 39, + reason: 'reason2', + }, + ], + } + const issues = collect39Issues(allIssues39Dict) + assert.lengthOf(issues, 2) + }) + it('constructs a combined reason string from each issue.reason of a file', () => { + const allIssues39Dict = { + file_1: [ + { + code: 39, + reason: 'reason1', + }, + { + code: 39, + reason: 'reason2', + }, + ], + } + const issues = collect39Issues(allIssues39Dict) + assert.lengthOf(issues, 1) + assert(issues[0].reason == ' reason1 reason2') + }) + }) +}) diff --git a/validators/headerFields.js b/validators/headerFields.js index e1a344dd0..9dd532996 100644 --- a/validators/headerFields.js +++ b/validators/headerFields.js @@ -12,7 +12,7 @@ var Issue = utils.issues.Issue * arrays more arguments will need to be added to headerField. */ -var headerFields = function headerFields(headers) { +const headerFields = headers => { var finalIssues = [] var allIssues39Dict = {} var fields = ['dim', 'pixdim'] @@ -34,18 +34,22 @@ var headerFields = function headerFields(headers) { } } - for (let file in allIssues39Dict) { - if (allIssues39Dict.hasOwnProperty(file)) { - const firstIssue = allIssues39Dict[file][0] - let evidence = '' - for (var issue of allIssues39Dict[file]) { - evidence = evidence + ' ' + allIssues39Dict[file][issue].reason - } - firstIssue.reason = evidence - finalIssues.push(firstIssue) + finalIssues = finalIssues.concat(collect39Issues(allIssues39Dict)) + + return finalIssues +} + +const collect39Issues = allIssues39Dict => { + const finalIssues = [] + for (let file of Object.keys(allIssues39Dict)) { + const firstIssue = allIssues39Dict[file][0] + let evidence = '' + for (var issue of allIssues39Dict[file]) { + evidence = evidence + ' ' + issue.reason } + firstIssue.reason = evidence + finalIssues.push(firstIssue) } - return finalIssues } @@ -58,7 +62,7 @@ var headerFields = function headerFields(headers) { * dimensionality of similar anatomy/functional/dwi headers are being compared. */ -var headerField = function headerField(headers, field) { +const headerField = (headers, field) => { var nifti_types = {} var issues = {} for (var header_index = 0; header_index < headers.length; header_index++) { @@ -249,7 +253,7 @@ var headerField = function headerField(headers, field) { * errors that cause resolutions to be slightly different. Returns true if * the two headers are signifigantly different */ -function headerFieldCompare(header1, header2) { +const headerFieldCompare = (header1, header2) => { var hdr1 = header1.split(',') var hdr2 = header2.split(',') if (hdr1.length !== hdr2.length) { @@ -272,3 +276,4 @@ function headerFieldCompare(header1, header2) { } module.exports = headerFields +module.exports.collect39Issues = collect39Issues