(function(){ "use strict"; angular.module('shared') .factory('ChunkStudents', ['_', function ChunkStudentsFactory(_) { var thresholds = [30, 40]; var minCols = 3; return { chunk: chunk }; function chunk(students) { var studentsPerChunk = Math.ceil(students.length / numCols(students.length)); return _.chain(students) .groupBy(function (item, index) { return Math.floor(index / studentsPerChunk) }) .toArray() .value(); } function numCols(numStudents) { return _.reduce(thresholds, function (memo, threshold) { return numStudents > threshold ? memo + 1 : memo; }, minCols); } }]) })();