Skip to content

Instantly share code, notes, and snippets.

@kangax
Created August 17, 2014 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kangax/e8d90012953a1cae2111 to your computer and use it in GitHub Desktop.
Save kangax/e8d90012953a1cae2111 to your computer and use it in GitHub Desktop.
function formatSchedule(schedules) {
var dayAbbr = [ 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс' ];
function dayIndicesToWord(indices) {
if (areDayIndicesSequential(indices)) {
return dayAbbr[indices[0]] + '-' + dayAbbr[_.last(indices)];
}
else {
var daysAsWords = _.map(indices, function(index) {
return dayAbbr[index];
});
return daysAsWords.join(',');
}
}
var hoursPairs = _.pluck(schedules, '0');
var hours = _.map(hoursPairs, function(o) {
return o.from + '-' + o.to;
});
var daysGroupedByHours = _.reduce(hours, function(memo, hour, dayIndex) {
if (!memo[hour]) {
memo[hour] = [ ]
}
memo[hour].push(dayIndex);
return memo;
}, { });
var resultStr = _.reduce(daysGroupedByHours, function(str, dayIndices, hour) {
str += dayIndicesToWord(dayIndices) + ': ' + hour + ' ';
return str;
}, '');
return resultStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment