eventassigner-js
Version:
A NPM package to assign groups / persons to events based on their preference
60 lines (52 loc) • 1.75 kB
JavaScript
var checkInput = function checkInput(input) {
if (typeof input.events === 'undefined') {
return 0;
}
// Check that events have min, max
for (var eventId = 0; eventId < input.events.length; eventId += 1) {
if (typeof input.events[eventId].min === 'undefined' || typeof input.events[eventId].max === 'undefined' || typeof input.events[eventId].id === 'undefined') {
return 0;
}
}
// Check that events have unique ids
var eventIds = [];
for (var _eventId = 0; _eventId < input.events.length; _eventId += 1) {
if (eventIds.includes(input.events[_eventId].id)) {
return 0;
}
eventIds.push(input.events[_eventId].id);
}
if (typeof input.groups === 'undefined') {
return 0;
}
// Check that groups have unique ids
var groupIds = [];
for (var groupId = 0; groupId < input.groups.length; groupId += 1) {
if (groupIds.includes(input.groups[groupId].id)) {
return 0;
}
groupIds.push(input.groups[groupId].id);
}
// Check that every group has a preferences and size
for (var _groupId = 0; _groupId < input.groups.length; _groupId += 1) {
if (typeof input.groups[_groupId].size !== 'number' || typeof input.groups[_groupId].pref === 'undefined') {
return 0;
}
if (input.groups[_groupId].pref.constructor !== Array) {
return 0;
}
// Check that every preference maps to event id
for (var prefId = 0; prefId < input.groups[_groupId].pref.length; prefId += 1) {
if (!eventIds.includes(input.groups[_groupId].pref[prefId])) {
return 0;
}
}
}
// Check that updateL exists
if (typeof input.updateL !== 'function') {
return 0;
}
return 1;
};
module.exports = { checkInput: checkInput };
;