Skip to content

Instantly share code, notes, and snippets.

@L8D
Created June 18, 2015 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save L8D/0be9a20e767513971b76 to your computer and use it in GitHub Desktop.
Save L8D/0be9a20e767513971b76 to your computer and use it in GitHub Desktop.
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