Skip to content

Instantly share code, notes, and snippets.

@brigand
Forked from L8D/butchered.js
Last active August 29, 2015 14:23
Show Gist options
  • Save brigand/19c24bec0322bec48582 to your computer and use it in GitHub Desktop.
Save brigand/19c24bec0322bec48582 to your computer and use it in GitHub Desktop.
function archiveWeeks(tasks) {
var startOfDay = (date) => new Date(date).setHours(0, 0, 0);
return lodash.chain(tasks)
.reverse()
.groupBy((task) => startOfDay(task.get('created_at'))
.map((tasks, date) => ({tasks: tasks, date: new Date(date)}))
.groupBy(weekOf) // because I'm lazy
.map((days, week) => ({days: days, week: week}))
.value();
}
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 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