JSFiddle - React, Tailwind, and code Playground
by Artem
Babel + JSX
'use strict';
let tasks = [
{ name: 'Prepare presentation', duration: 120, type: 'work' },
{ name: 'Work out', duration: 60, type: 'private' },
{ name: 'Fill time journal', duration: 10, type: 'work' },
{ name: 'Plan year 2018', duration: 180, type: 'common' },
{ name: 'Work out', duration: 60, type: 'private' },
{ name: 'Watch a movie', duration: 120, type: 'private' },
{ name: 'Work out', duration: 60, type: 'private' },
{ name: 'Fill time journal', duration: 10, type: 'work' }
];
const TYPE_COMMON = 'common';
tasks.reduce((acc, [name, duration, type], t) => {
if (type !== TYPE_COMMON) {
const idx = acc.findIndex(task => task.name === name);
if (!idx) {
acc.push(t[idx]);
} else {
const task = t.splice(idx, 1)[0];
acc.push({...task, duration: task.duration + duration});
}
}
return acc;
}, []);
let filter = (task) => task.type !=='common';
let watchMoviesDuration = (total, current) => {
if (current.name === 'Watch a movie') {
return total + current.duration;
}
return total;
};
let workOutDuration = (total, current) => {
if (current.name === 'Work out') {
return total + current.duration;
}
return total;
};
let preparePresentationDuration = (total, current) => {
if (current.name === 'Prepare presentation') {
return total + current.duration;
}
return total;
};
let fullTimeJournalDuration = (total, current) => {
if (current.name === 'Fill time journal') {
return total + current.duration;
}
return total;
};
let convertToHours = (task) => {
task.duration /= 60;
return task;
};
tasks
.filter(filter)
.map(convertToHours);
let watchedMoviesDurationInHours = tasks
.reduce(watchMoviesDuration, 0);
let workOutDurationInHours = tasks
.reduce(workOutDuration, 0);
let preparePresentationDurationInHours = tasks
.reduce(preparePresentationDuration, 0);
let fullTimeJournalDurationInHours = Math.round(tasks
...