Guest User

Untitled

a guest
Apr 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. /** You'll need lodash since I'm using hints of FP */
  2. const _ = require('lodash');
  3.  
  4. /**
  5. * Generates pairings bases on the base array provided
  6. * @param {Array} base Array of strings for generating pairings
  7. * @return {Array} Array containing nested groups of pairings, each representing
  8. * every possible grouping.
  9. */
  10. function generateArrayOfPairs(base) {
  11. const modBase = _.clone(base);
  12.  
  13. // push extra (nobody) to array if uneven number
  14. if (modBase.length % 2 !== 0) modBase.push('nobody');
  15.  
  16. const [group1, group2] = _.chunk(modBase, modBase.length / 2);
  17. const output = [];
  18.  
  19. let i = modBase.length;
  20. while (i--) {
  21. // some shifting because I'm too lazy for linked lists
  22. group1.unshift(group2.shift());
  23. group2.push(group1.pop());
  24. // combine each array into 2-indexed pairs
  25. output.push(_.zip(group1, group2));
  26. }
  27.  
  28. return output;
  29. }
  30.  
  31. /**
  32. * Converts raw name string to readable form
  33. */
  34. const wrapName = name => {
  35. const names = _.startCase(name).split(' ');
  36. return `**${_.head(names)} ${_.last(names)[0]}.**`;
  37. }
  38.  
  39. /**
  40. * Mapping function for converting
  41. * @param {Array} group The group of pairings to use (mapped value)
  42. * @param {Number} index Group index
  43. * @return {String} The presentable string for a specific group
  44. */
  45. const mapToReadable = (group, index) => `## Week ${index + 1}
  46. ${group
  47. .map(name => name
  48. .map(wrapName)
  49. .join(' meets with ')
  50. ).join('\n')
  51. }`
  52.  
  53. // start output
  54. console.log(_.flow(
  55. // split string literal to array without null values
  56. names => names
  57. .split('\n')
  58. // remove any empty values
  59. .filter(name => name !== ''),
  60. // generate raw pairings
  61. generateArrayOfPairs,
  62. // format as presentable string
  63. pairs => pairs
  64. .map(mapToReadable)
  65. .join('\n\n')
  66. // the below is case in-sensitve
  67. )(`Name 1
  68. Name 2
  69. Name 3`));
Add Comment
Please, Sign In to add comment