Skip to content

Instantly share code, notes, and snippets.

@abele
Last active September 19, 2015 08:38
Show Gist options
  • Save abele/df0d16eb3464c9c9b219 to your computer and use it in GitHub Desktop.
Save abele/df0d16eb3464c9c9b219 to your computer and use it in GitHub Desktop.
JavaScript code snippet code review and refactoring. Snippet DID NOT have tests. NO guarantee that changes doesn't break it.
set_viewable_dates: function (task, month_to_check, now) {
/*
it will set:
start_to_show, end_to_show (and postponed_to_show) | <-- moment() dates |
variables to be used in list template,
so that original start, end (and postponed) values of a task wouldn't be affected
if a tasks start, end, postponed values span over month_to_check,
start, end, postponed will be reduced to only contain values which cover month_to_check
so that start_to_show <= month_to_check >= end_to_show
*/
var tmp = {};
tmp.task_start = moment(_.cloneDeep(task.start));
tmp.task_end = moment(_.cloneDeep(task.end));
// start date which will be used in list view only
// set start to be the first day of current month,
// if tasks begins before the start of the month in question
if (tmp.task_start.month() < month_to_check) {
task.start_to_show =
moment()
.year(tmp.task_start.year())
.month(month_to_check)
.date(1)
.hour(0)
.minute(0)
.second(0);
}
else {
task.start_to_show = moment(_.cloneDeep(tmp.task_start));
}
// change start to today
// if task has begun before today
//if (task.start_to_show.diff(now, 'days') < 0) {
// task.start_to_show = now;
// task.start_to_show.hour(0).minute(0).second(0);
// task.set_to_add_all_day = true; // set task to all day, as it has begun earlier than today
//}
/*
TODO: check if postponed exists -
TODO: probably could set it to end_to_show without going through the two following ifs
as postponed > end
*/
// end date which will be used in list view only
// set end to be num of days of current month,
// if end spans over current month
if (tmp.task_end.month() > month_to_check) {
task.end_to_show =
moment()
.year(tmp.task_end.year())
.month(month_to_check)
.date(service.get_days_in_month(tmp.task_end.year(), month_to_check))
.hour(23)
.minute(59)
.second(59);
}
else {
task.end_to_show = moment(_.cloneDeep(tmp.task_end));
}
// postponed date which will be used in list view only
// set postponed to be num of days of current month,
// if postponed spans over current month
if (task.postponed) {
tmp.task_postponed = moment(_.cloneDeep(task.postponed));
if (tmp.task_postponed.month() > month_to_check) {
task.postponed_to_show =
moment()
.year(tmp.task_postponed.year())
.month(month_to_check)
.date(service.get_days_in_month(tmp.task_postponed.year(), month_to_check))
.hour(23)
.minute(59)
.second(59);
}
else {
task.postponed_to_show = moment(_.cloneDeep(tmp.task_postponed));
}
task.end_to_show = service.get_task_end(task);
}
return task;
},
list_contains_date: function (output_task_list, end_d) {
if (output_task_list.length) {
for (var day = 0; day < output_task_list.length; day++) {
if (output_task_list[day].date.year() === end_d.year() &&
output_task_list[day].date.month() === end_d.month() &&
output_task_list[day].date.date() === end_d.date()) {
return true;
}
}
}
return false;
},
check_and_create_date: function (output_task_list, date_to_add) {
if (!service.list_contains_date(output_task_list, date_to_add)) {
output_task_list.push(
{
'date': moment(date_to_add),
'tasks': [],
'tasks_all_day': []
}
);
}
},
add_task_to_single_day_list: function (output_task_list, task, end) {
for (var j = 0; j < output_task_list.length; j++) {
// compare only y/m/d
if (output_task_list[j].date.year() === end.year() &&
output_task_list[j].date.month() === end.month() &&
output_task_list[j].date.date() === end.date()) {
output_task_list[j].tasks.push(task);
break;
}
}
},
add_task_to_all_day_list: function (output_task_list, task, end) {
for (var j = 0; j < output_task_list.length; j++) {
// compare only y/m/d
if (moment(output_task_list[j].date).get('year') === moment(end).get('year') &&
moment(output_task_list[j].date).get('month') === moment(end).get('month') &&
moment(output_task_list[j].date).get('date') === moment(end).get('date')) {
output_task_list[j].tasks_all_day.push(task);
break;
}
}
},
get_days_in_month: function (year, month) {
// month 0 by default returns January
// if day is set to 0, month 1 will return January as well
return new Date(year, month + 1, 0).getDate();
},
get_task_end: function (task) {
var p = task.postponed_to_show ? task.postponed_to_show : task.postponed;
var e = task.end_to_show ? task.end_to_show : task.end;
if (p) {
var max = moment.max(p, e);
return moment(_.cloneDeep(max));
}
else {
return moment(_.cloneDeep(e));
}
},
get_task_start: function (task) {
return task.start_to_show ? task.start_to_show : task.start;
},
is_task_multi_day: function (task) {
if (task.is_multi_day) {
return true;
}
else {
// should be checking original start date to correctly determine duration of task
task.is_multi_day = moment(task.start).diff(moment(service.get_task_end(task)), 'days') < 0;
return task.is_multi_day;
}
},
is_task_in_current_month: function (task, month_to_check) {
return moment(task.start).month() <= month_to_check && month_to_check <= moment(task.end).month();
},
add_all_day_task: function (output_task_list, task, date_to_add) {
service.check_and_create_date(output_task_list, date_to_add);
service.add_task_to_all_day_list(output_task_list, task, date_to_add);
},
add_task_end: function (output_task_list, task, date_to_add) {
//console.log('TASK IN ADD TASK END: ', JSON.stringify(task, null, 2));
service.check_and_create_date(output_task_list, date_to_add);
if (task.allday) {
service.add_all_day_task(output_task_list, task, date_to_add);
}
else {
service.add_task_to_single_day_list(output_task_list, task, date_to_add);
}
},
add_task_start: function (output_task_list, task) {
// TODO: if first day of task - set flag, which indicates just to show the START time
var new_end = _.cloneDeep(task.start_to_show);
//console.log('start to show before:', JSON.stringify(task.start_to_show, null, 2));
//console.log('new_end before:', JSON.stringify(new_end, null, 2));
var tmp_task = _.cloneDeep(task);
tmp_task.end_to_show = moment(new_end).hour(23).minute(59).second(59);
tmp_task.postponed_to_show = moment(_.cloneDeep(task.end_to_show));
service.check_and_create_date(output_task_list, tmp_task.end_to_show);
var tmp_diff = moment(tmp_task.start_to_show).diff(moment(tmp_task.start), 'days');
// if dates are on, e.g., 01.03 and 02.03, diff will give result of 1
// if dates are on, e.g., 31.03 and 01.04, diff will output 0
if (moment(tmp_task.start).month === moment(tmp_task.start_to_show).month()) {
tmp_diff += 1;
}
if (tmp_diff > 0) {
//if start_to_show is after start, it means
// that the start of a task should be put as an all day task
service.add_task_to_all_day_list(output_task_list, tmp_task, tmp_task.end_to_show);
}
else {
service.add_task_to_single_day_list(output_task_list, tmp_task, tmp_task.end_to_show);
}
}
};
// XXX: function toooo long
set_viewable_dates: function (task, month_to_check, now) {
/*
it will set:
start_to_show, end_to_show (and postponed_to_show) | <-- moment() dates |
variables to be used in list template,
so that original start, end (and postponed) values of a task wouldn't be affected
if a tasks start, end, postponed values span over month_to_check,
start, end, postponed will be reduced to only contain values which cover month_to_check
so that start_to_show <= month_to_check >= end_to_show
*/
var tmp = {};
tmp.task_start = moment(_.cloneDeep(task.start));
tmp.task_end = moment(_.cloneDeep(task.end));
// start date which will be used in list view only
// set start to be the first day of current month,
// if tasks begins before the start of the month in question
if (tmp.task_start.month() < month_to_check) {
// XXX: pull out as separate function
task.start_to_show =
moment()
.year(tmp.task_start.year())
.month(month_to_check)
.date(1)
.hour(0)
.minute(0)
.second(0);
}
else {
task.start_to_show = moment(_.cloneDeep(tmp.task_start));
}
// XXX: Use VCS to keep history
// change start to today
// if task has begun before today
//if (task.start_to_show.diff(now, 'days') < 0) {
// task.start_to_show = now;
// task.start_to_show.hour(0).minute(0).second(0);
// task.set_to_add_all_day = true; // set task to all day, as it has begun earlier than today
//}
/*
TODO: check if postponed exists -
TODO: probably could set it to end_to_show without going through the two following ifs
as postponed > end
*/
// end date which will be used in list view only
// set end to be num of days of current month,
// if end spans over current month
if (tmp.task_end.month() > month_to_check) {
// XXX: pull out as separate function
task.end_to_show =
moment()
.year(tmp.task_end.year())
.month(month_to_check)
.date(service.get_days_in_month(tmp.task_end.year(), month_to_check))
.hour(23)
.minute(59)
.second(59);
}
else {
task.end_to_show = moment(_.cloneDeep(tmp.task_end));
}
// postponed date which will be used in list view only
// set postponed to be num of days of current month,
// if postponed spans over current month
if (task.postponed) {
tmp.task_postponed = moment(_.cloneDeep(task.postponed));
if (tmp.task_postponed.month() > month_to_check) {
// XXX: pull out as separate function
task.postponed_to_show =
moment()
.year(tmp.task_postponed.year())
.month(month_to_check)
.date(service.get_days_in_month(tmp.task_postponed.year(), month_to_check))
.hour(23)
.minute(59)
.second(59);
}
else {
task.postponed_to_show = moment(_.cloneDeep(tmp.task_postponed));
}
task.end_to_show = service.get_task_end(task);
}
return task;
},
list_contains_date: function (output_task_list, end_d) {
// XXX: Useles check. arrays of zero length don't get iterated.
// XXX: Use forEach
if (output_task_list.length) {
for (var day = 0; day < output_task_list.length; day++) {
// XXX: Pull out separate function
if (output_task_list[day].date.year() === end_d.year() &&
output_task_list[day].date.month() === end_d.month() &&
output_task_list[day].date.date() === end_d.date()) {
return true;
}
}
}
return false;
},
// XXX: check what? Maybe `add_new` would be more appropriate.
check_and_create_date: function (output_task_list, date_to_add) {
if (!service.list_contains_date(output_task_list, date_to_add)) {
output_task_list.push(
{
'date': moment(date_to_add),
'tasks': [],
'tasks_all_day': []
}
);
}
},
add_task_to_single_day_list: function (output_task_list, task, end) {
for (var j = 0; j < output_task_list.length; j++) {
// compare only y/m/d
// XXX: Pull out separate function
if (output_task_list[j].date.year() === end.year() &&
output_task_list[j].date.month() === end.month() &&
output_task_list[j].date.date() === end.date()) {
output_task_list[j].tasks.push(task);
break;
}
}
},
add_task_to_all_day_list: function (output_task_list, task, end) {
for (var j = 0; j < output_task_list.length; j++) {
// compare only y/m/d
// XXX: Pull out separate function
if (moment(output_task_list[j].date).get('year') === moment(end).get('year') &&
moment(output_task_list[j].date).get('month') === moment(end).get('month') &&
moment(output_task_list[j].date).get('date') === moment(end).get('date')) {
output_task_list[j].tasks_all_day.push(task);
break;
}
}
},
// XXX: skip `get` prefix. It's function, it's supposed to return something.
get_days_in_month: function (year, month) {
// month 0 by default returns January
// if day is set to 0, month 1 will return January as well
return new Date(year, month + 1, 0).getDate();
},
// XXX: skp `get` prefix.
get_task_end: function (task) {
// XXX: use descriptive naming
var p = task.postponed_to_show ? task.postponed_to_show : task.postponed;
var e = task.end_to_show ? task.end_to_show : task.end;
if (p) {
var max = moment.max(p, e);
return moment(_.cloneDeep(max));
}
else {
return moment(_.cloneDeep(e));
}
},
// XXX: skip `get` prefix
get_task_start: function (task) {
return task.start_to_show ? task.start_to_show : task.start;
},
// XXX: use modules as namespace. Then you can skip `task` prefix.
is_task_multi_day: function (task) {
if (task.is_multi_day) {
return true;
}
else {
// should be checking original start date to correctly determine duration of task
task.is_multi_day = moment(task.start).diff(moment(service.get_task_end(task)), 'days') < 0;
return task.is_multi_day;
}
},
// XXX: correct naming. This funtion tests if task is in given month
is_task_in_current_month: function (task, month_to_check) {
return moment(task.start).month() <= month_to_check && month_to_check <= moment(task.end).month();
},
add_all_day_task: function (output_task_list, task, date_to_add) {
service.check_and_create_date(output_task_list, date_to_add);
service.add_task_to_all_day_list(output_task_list, task, date_to_add);
},
// XXX: Separate service for all day tasks and single day tasks
// XXX: Maybe we can move some logic to task class it self!?
add_task_end: function (output_task_list, task, date_to_add) {
//console.log('TASK IN ADD TASK END: ', JSON.stringify(task, null, 2));
service.check_and_create_date(output_task_list, date_to_add);
if (task.allday) {
service.add_all_day_task(output_task_list, task, date_to_add);
}
else {
service.add_task_to_single_day_list(output_task_list, task, date_to_add);
}
},
add_task_start: function (output_task_list, task) {
// TODO: if first day of task - set flag, which indicates just to show the START time
var new_end = _.cloneDeep(task.start_to_show);
//console.log('start to show before:', JSON.stringify(task.start_to_show, null, 2));
//console.log('new_end before:', JSON.stringify(new_end, null, 2));
var tmp_task = _.cloneDeep(task);
tmp_task.end_to_show = moment(new_end).hour(23).minute(59).second(59);
tmp_task.postponed_to_show = moment(_.cloneDeep(task.end_to_show));
service.check_and_create_date(output_task_list, tmp_task.end_to_show);
var tmp_diff = moment(tmp_task.start_to_show).diff(moment(tmp_task.start), 'days');
// if dates are on, e.g., 01.03 and 02.03, diff will give result of 1
// if dates are on, e.g., 31.03 and 01.04, diff will output 0
if (moment(tmp_task.start).month === moment(tmp_task.start_to_show).month()) {
tmp_diff += 1;
}
// XXX: Cool explicit else
if (tmp_diff > 0) {
// XXX: This logic should go somewhere else. Task class?
// add_task_to_all_day? These conditinals are spread out through all
// codebase.
//if start_to_show is after start, it means
// that the start of a task should be put as an all day task
service.add_task_to_all_day_list(output_task_list, tmp_task, tmp_task.end_to_show);
}
else {
service.add_task_to_single_day_list(output_task_list, tmp_task, tmp_task.end_to_show);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment