Created
August 17, 2014 19:25
-
-
Save kangax/e8d90012953a1cae2111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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