Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Lint api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed Aug 2, 2018
1 parent ed2dc3b commit efc9941
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 85 deletions.
162 changes: 80 additions & 82 deletions census/controllers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,152 +292,151 @@ let _outputEntryResults = function(results, questions, format, res) {
};

let _outputFlatEntryResults = function(results, questions, format, res) {

// Count
let maxFormats = 0
let maxCharacts = 0
let maxLocations = 0
let maxFormats = 0;
let maxCharacts = 0;
let maxLocations = 0;
for (const item of results) {
let answers = item.getSimpleAnswersForQuestions(questions);
let getAnswerById = id => answers.find(answer => answer.id === id) || {}
maxFormats = Math.max(maxFormats, (getAnswerById('format').value || []).length)
maxCharacts = Math.max(maxCharacts, (getAnswerById('characteristics').value || []).length)
maxLocations = Math.max(maxLocations, (getAnswerById('location').value || []).length)
let getAnswerById = id => answers.find(answer => answer.id === id) || {};
maxFormats = Math.max(maxFormats, (getAnswerById('format').value || []).length);
maxCharacts = Math.max(maxCharacts, (getAnswerById('characteristics').value || []).length);
maxLocations = Math.max(maxLocations, (getAnswerById('location').value || []).length);
}

// Mapper
let mapper = function(item) {
let answers = item.getSimpleAnswersForQuestions(questions);
let getAnswerById = id => answers.find(answer => answer.id === id) || {}
let result = {}
let getAnswerById = id => answers.find(answer => answer.id === id) || {};
let result = {};

// General
Object.assign(result, {
'id': item.id,
'site': item.site,
'timestamp': moment(item.createdAt).format('YYYY-MM-DDTHH:mm:ss'),
'year': item.year,
'place': item.place,
'dataset': item.dataset,
})
id: item.id,
site: item.site,
timestamp: moment(item.createdAt).format('YYYY-MM-DDTHH:mm:ss'),
year: item.year,
place: item.place,
dataset: item.dataset
});

// License
let openLicence = getAnswerById('open_licence')
let licenceURL = getAnswerById('licence_url')
let openLicence = getAnswerById('open_licence');
let licenceURL = getAnswerById('licence_url');
Object.assign(result, {
'Openly licenced?': openLicence.value,
'Comments licence': openLicence.commentValue,
'Licence URL': licenceURL.value,
'Comments Licence URL': licenceURL.commentValue,
})
'Comments Licence URL': licenceURL.commentValue
});

// Publicly available
let onlineFree = getAnswerById('online_free')
let onlineFree = getAnswerById('online_free');
Object.assign(result, {
'Publicly available online (no access controls)': onlineFree.value,
'Comments publicly available': onlineFree.commentValue,
})
'Comments publicly available': onlineFree.commentValue
});

// Online otherwise
let onlineOtherwise = getAnswerById('online_otherwise')
let onlineOtherwise = getAnswerById('online_otherwise');
Object.assign(result, {
'Online_otherwise?': onlineOtherwise.value,
'Comments online_otherwise': onlineOtherwise.commentValue,
})
'Comments online_otherwise': onlineOtherwise.commentValue
});

// Collector gov
let collectorGov = getAnswerById('collector_gov')
let collectorName = getAnswerById('collector_name')
let collectorGov = getAnswerById('collector_gov');
let collectorName = getAnswerById('collector_name');
Object.assign(result, {
'Collector_Gov': collectorGov.value,
'Comment_gov collector': collectorGov.commentValue,
'Collector name': collectorName.value,
'Comments collector name': collectorName.commentValue,
})
'Comments collector name': collectorName.commentValue
});

// Collector non-gov
let collectorNonGov = getAnswerById('collector_non_gov')
let collectorNonGov = getAnswerById('collector_non_gov');
Object.assign(result, {
'Collector_non-gov': collectorNonGov.value,
'Comment_non-gov': collectorNonGov.commentValue,
})
'Comment_non-gov': collectorNonGov.commentValue
});

// Findable
let findable = getAnswerById('findable')
let findableSteps = getAnswerById('findable_steps')
let findable = getAnswerById('findable');
let findableSteps = getAnswerById('findable_steps');
Object.assign(result, {
'Findable': findable.value,
'Findable_comment': findable.commentValue,
'Findable_steps taken': findableSteps.value,
'Comments_steps taken': findableSteps.commentValue,
})
'Comments_steps taken': findableSteps.commentValue
});

// Timely
let timely = getAnswerById('timely')
let timely = getAnswerById('timely');
Object.assign(result, {
'Timely': timely.value,
'Timely_comment': timely.commentValue,
})
Timely: timely.value,
Timely_comment: timely.commentValue
});

// Free
let free = getAnswerById('free')
let free = getAnswerById('free');
Object.assign(result, {
'Free': free.value,
'Free_comment': free.commentValue,
})
Free: free.value,
Free_comment: free.commentValue
});

// Usability
let usability = getAnswerById('usability')
let usability = getAnswerById('usability');
Object.assign(result, {
'Usability': usability.value,
'Usability_comment': usability.commentValue,
})
Usability: usability.value,
Usability_comment: usability.commentValue
});

// Bulk
let bulk = getAnswerById('bulk')
let bulk = getAnswerById('bulk');
Object.assign(result, {
'Downloadable at once': bulk.value,
'Downloadable at once comment': bulk.commentValue,
})
'Downloadable at once comment': bulk.commentValue
});

// Format
let format = getAnswerById('format')
let format = getAnswerById('format');
for (const index of _.range(maxFormats)) {
result[`Format ${index + 1}`] = format.value[index]
result[`Format ${index + 1}`] = format.value[index];
}
result['Format comment'] = format.commentValue
result['Format comment'] = format.commentValue;

// Characteristics
let characts = getAnswerById('characteristics')
let characts = getAnswerById('characteristics');
for (const index of _.range(maxCharacts)) {
result[`Data element ${index + 1}`] = characts.value[index]
result[`Data element ${index + 1}`] = characts.value[index];
}
result['Data element comment'] = characts.commentValue
result['Data element comment'] = characts.commentValue;

// Location
let location = getAnswerById('location')
let location = getAnswerById('location');
for (const index of _.range(maxLocations)) {
const data = location.value[index]
result[`URL ${index + 1}`] = data ? data.urlValue : ''
result[`Description URL ${index + 1}`] = data ? data.descValue : ''
const data = location.value[index];
result[`URL ${index + 1}`] = data ? data.urlValue : '';
result[`Description URL ${index + 1}`] = data ? data.descValue : '';
}
result['URL comment'] = location.commentValue
result['URL comment'] = location.commentValue;

// Metadata
Object.assign(result, {
'reviewed': item.reviewed ? 'Yes' : 'No',
'reviewResult': item.reviewResult ? 'Yes' : 'No',
'reviewComments': item.reviewComments,
'details': item.details,
'isCurrent': item.isCurrent ? 'Yes' : 'No',
'isOpen': item.isOpenForQuestions(questions) ? 'Yes' : 'No',
'submitter': item.Submitter ? item.Submitter.fullName() : '',
'reviewer': item.Reviewer ? item.Reviewer.fullName() : '',
'score': item.computedScore,
'relativeScore': item.computedRelativeScore
})

return result
reviewed: item.reviewed ? 'Yes' : 'No',
reviewResult: item.reviewResult ? 'Yes' : 'No',
reviewComments: item.reviewComments,
details: item.details,
isCurrent: item.isCurrent ? 'Yes' : 'No',
isOpen: item.isOpenForQuestions(questions) ? 'Yes' : 'No',
submitter: item.Submitter ? item.Submitter.fullName() : '',
reviewer: item.Reviewer ? item.Reviewer.fullName() : '',
score: item.computedScore,
relativeScore: item.computedRelativeScore
});

return result;
};

// Response
Expand All @@ -447,7 +446,7 @@ let _outputFlatEntryResults = function(results, questions, format, res) {
break;
}
case 'csv': {
const columns = (results.length) ? Object.keys(mapper(results[0])) : []
const columns = (results.length) ? Object.keys(mapper(results[0])) : [];
outputItemsAsCsv(res, results, mapper, columns);
break;
}
Expand All @@ -456,7 +455,6 @@ let _outputFlatEntryResults = function(results, questions, format, res) {
break;
}
}

};

let pendingEntries = function(req, res, next) {
Expand Down Expand Up @@ -509,11 +507,11 @@ let entries = function(req, res, next) {
}

// Modifier can be only `flat` or `undefined`
let outputFunction
let outputFunction;
if (modifier === 'flat') {
outputFunction = _outputFlatEntryResults
outputFunction = _outputFlatEntryResults;
} else if (modifier === undefined) {
outputFunction = _outputEntryResults
outputFunction = _outputEntryResults;
} else {
return res.sendStatus(404);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ describe('API', function() {
browser.assert.success();
const data = JSON.parse(browser.text());
const item = data.results[0];
assert.deepEqual(item['Format 1'], 'AsciiDoc')
assert.deepEqual(item['Format 2'], 'CSV')
assert.deepEqual(item['Format 3'], 'HTML')
assert.deepEqual(item['Format 1'], 'AsciiDoc');
assert.deepEqual(item['Format 2'], 'CSV');
assert.deepEqual(item['Format 3'], 'HTML');
done();
});
});
Expand Down

0 comments on commit efc9941

Please sign in to comment.