Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function archiveWeeks(tasks) {
return lodash.chain(tasks)
.reverse()
.groupBy({created_at} => new Date(created_at.getFullYear(), created_at.getMonth(), created_at.getDate()))
.map((t, d) => {date: new Date(d), tasks: t})
.groupBy(weekOf) // because I'm lazy
.map((d, w) => {week: w, days: d})
.value();
}
function weekOf(day) { // pretend it's inline
var sunday = new Date(day.date);
sunday.setDate(sunday.getDate() - sunday.getDay());
var saturday = new Date(sunday);
saturday.setDate(saturday.getDate() + 6);
return formatDate(sunday) + ' - ' + formatDate(saturday);
}
function archiveWeeks(tasks) {
var weeks = lodash.chain(tasks)
.reverse()
.groupBy(dayOf)
.map(toDayObj)
.groupBy(weekOf)
.map(toWeekObj)
.value();
return weeks;
}
function dayOf(task) {
var time = task.get('created_at');
return new Date(time.getFullYear(), time.getMonth(), time.getDate());
}
function weekOf(day) {
var sunday = new Date(day.date);
sunday.setDate(sunday.getDate() - sunday.getDay());
var saturday = new Date(sunday);
saturday.setDate(saturday.getDate() + 6);
return formatDate(sunday) + ' - ' + formatDate(saturday);
}
function toDayObj(tasks, day) {
return {
date: new Date(day),
tasks: tasks
};
}
function toWeekObj(days, week) {
return {
date: week,
days: days,
};
}
function formatDay(date) {
return dateformat(date, 'dddd, mmmm dS');
}
function formatDate(date) {
return dateformat(date, 'mmmm dS');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.