Skip to content

Instantly share code, notes, and snippets.

@intellix
Created December 5, 2016 21:24
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 intellix/61befe98cbc69af5b499bc8fbf29af49 to your computer and use it in GitHub Desktop.
Save intellix/61befe98cbc69af5b499bc8fbf29af49 to your computer and use it in GitHub Desktop.
(function() {
'use strict';
angular
.module('chunkFilter')
.filter('chunk', chunk);
function chunk() {
return function(value, size, vertical, preserveKeys) {
var chunks = [];
var chunkCount = size;
var chunkIndex;
if (!value || value.length === 0) {
return value;
}
vertical = vertical || false;
preserveKeys = preserveKeys || false;
if (!vertical) {
chunkCount = Math.ceil(value.length / size);
}
for (chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++) {
chunks.push([]);
}
chunkIndex = 0;
value.forEach(function(value, key) {
if (preserveKeys) {
chunks[chunkIndex][key] = value;
} else {
chunks[chunkIndex].push(value);
}
if (++chunkIndex === chunkCount) {
chunkIndex = 0;
}
});
// Hack against infdig when used in view - http://stackoverflow.com/a/29117743
if (!value.$$splitListFilter || !angular.equals(value.$$splitListFilter, chunks)) {
value.$$splitListFilter = chunks;
}
return value.$$splitListFilter;
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment