const endpoint = require('@runkit/runkit/json-endpoint/1.0.0');
const shuffleSeed = require('shuffle-seed');
const moment = require('moment');
const flow = require('lodash/fp/flow');
const chunk = require('lodash/fp/chunk');
const ceiling = require('lodash/fp/ceil');
const floor = require('lodash/fp/floor');
const shuffle = seed => array => shuffleSeed.shuffle(array, seed);
function getGroups(people, opts) {
const options = Object.assign({
maxGroupSize: 5
}, opts);
const values = Array.from(people).filter(v => v).sort();
const seed = values.join('').toLowerCase() + moment.utc().format('YYYYMMDD');
const strategy = target => {
const n = target.length;
const max = options.maxGroupSize;
const num_groups = ceiling(n/max);
return target.reduce((result, person, i)=>{
const group_number = i % num_groups;
result[group_number] = result[group_number] || [];
result[group_number].push(person);
return result;
},[]);
};
return flow(
shuffle(seed),
strategy,
)(values);
}
endpoint(exports, async function({ query }) {
if (!query || !query.people) {
throw new Error(`The 'people' parameter is required.`);
}
const peopleArray = query.people.split(',').map(v => v.trim());
if (!peopleArray) {
throw new Error(`The 'people' parameter must be a comma-separated list of names.`);
}
return {
version: '2',
groups: getGroups(peopleArray),
};
});