Source: functions/groupAdvents.js

  1. /**
  2. * @module groupAdvents
  3. */
  4. const compareDates = require('../util/compareDates');
  5. /**
  6. * @function
  7. * @name groupAdvents
  8. * @description Group Advents by Start Date into Arrays
  9. * @param {Object[]} advents Array of Advents
  10. * @returns {Object[][]} Array of Arrays of Advents with the Same Start Date
  11. */
  12. function groupAdvents(advents) {
  13. if (advents.length === 0) {
  14. return advents;
  15. }
  16. const groups = [[advents[0]]];
  17. for (let i = 1, group = 0; i < advents.length; i++) {
  18. if (compareDates(groups[group][0].start, advents[i].start) === 0) {
  19. groups[group].push(advents[i]);
  20. } else {
  21. groups.push([advents[i]]);
  22. group++;
  23. }
  24. }
  25. return groups;
  26. }
  27. module.exports = groupAdvents;