Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** You'll need lodash since I'm using hints of FP */
- const _ = require('lodash');
- /**
- * Generates pairings bases on the base array provided
- * @param {Array} base Array of strings for generating pairings
- * @return {Array} Array containing nested groups of pairings, each representing
- * every possible grouping.
- */
- function generateArrayOfPairs(base) {
- const modBase = _.clone(base);
- // push extra (nobody) to array if uneven number
- if (modBase.length % 2 !== 0) modBase.push('nobody');
- const [group1, group2] = _.chunk(modBase, modBase.length / 2);
- const output = [];
- let i = modBase.length;
- while (i--) {
- // some shifting because I'm too lazy for linked lists
- group1.unshift(group2.shift());
- group2.push(group1.pop());
- // combine each array into 2-indexed pairs
- output.push(_.zip(group1, group2));
- }
- return output;
- }
- /**
- * Converts raw name string to readable form
- */
- const wrapName = name => {
- const names = _.startCase(name).split(' ');
- return `**${_.head(names)} ${_.last(names)[0]}.**`;
- }
- /**
- * Mapping function for converting
- * @param {Array} group The group of pairings to use (mapped value)
- * @param {Number} index Group index
- * @return {String} The presentable string for a specific group
- */
- const mapToReadable = (group, index) => `## Week ${index + 1}
- ${group
- .map(name => name
- .map(wrapName)
- .join(' meets with ')
- ).join('\n')
- }`
- // start output
- console.log(_.flow(
- // split string literal to array without null values
- names => names
- .split('\n')
- // remove any empty values
- .filter(name => name !== ''),
- // generate raw pairings
- generateArrayOfPairs,
- // format as presentable string
- pairs => pairs
- .map(mapToReadable)
- .join('\n\n')
- // the below is case in-sensitve
- )(`Name 1
- Name 2
- Name 3`));
Add Comment
Please, Sign In to add comment