This will be a nice feature of TripTime:

As a user, I want to be able to generate and download a pdf for the plan of my trip, so that I can carry my plan around and share with my friends.

The best organisation of the pdf would be in the order of date. So, I have an array of activities and an array of travels. I want them to be grouped by dates, and these groups should be in order as well. Sounds like a couple of intimidating iterations to me on first thought.

Luckily there’s a js package, underscore that provides a range of functional programming helpers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function groupByDay(events) {
const occurrenceDay = function(event) {
return moment(event.start)
.startOf('day')
.format('dddd Do MMMM, YYYY');
};
const groupToDay = function(group, day) {
return {
day: day,
events: group,
};
};
return _.chain(events)
.groupBy(occurrenceDay)
.map(groupToDay)
.sortBy('day')
.value();
}

chain

Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.

groupBy

Splits a collection into sets, grouped by the result of running each value through iteratee. If iteratee is a string instead of a function, groups by the property named by iteratee on each of the values.

map

Produces a new array of values by mapping each value in list through a transformation function (iteratee).

sortBy

Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee. iteratee may also be the string name of the property to sort by (eg. length).