Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions src/_filter/collection/chunk-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@
* Collect data into fixed-length chunks or blocks
*/

angular.module('a8m.chunk-by', [])
.filter('chunkBy', [function () {
/**
* @description
* Get array with size `n` in `val` inside it.
* @param n
* @param val
* @returns {Array}
*/
function fill(n, val) {
var ret = [];
while(n--) ret[n] = val;
return ret;
}
angular.module('a8m.chunk-by', ['a8m.filter-watcher'])
.filter('chunkBy', ['filterWatcher', function (filterWatcher) {
return function (array, n, fillVal) {

return function (array, n, fillVal) {
if (!isArray(array)) return array;
return array.map(function(el, i, self) {
i = i * n;
el = self.slice(i, i + n);
return !isUndefined(fillVal) && el.length < n
? el.concat(fill(n - el.length, fillVal))
: el;
}).slice(0, Math.ceil(array.length / n));
}
}]);
return filterWatcher.isMemoized('chunkBy', arguments) ||
filterWatcher.memoize('chunkBy', arguments, this,
_chunkBy(array, n, fillVal));
/**
* @description
* Get array with size `n` in `val` inside it.
* @param n
* @param val
* @returns {Array}
*/
function fill(n, val) {
var ret = [];
while (n--) ret[n] = val;
return ret;
}

function _chunkBy(array, n, fillVal) {
if (!isArray(array)) return array;
return array.map(function (el, i, self) {
i = i * n;
el = self.slice(i, i + n);
return !isUndefined(fillVal) && el.length < n
? el.concat(fill(n - el.length, fillVal))
: el;
}).slice(0, Math.ceil(array.length / n));
}
}
}]);
14 changes: 12 additions & 2 deletions test/spec/filter/collection/chunk-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ describe('chunkByFilter', function() {
}));

it('should collect data into fixed-length chunks or blocks', function() {
expect(filter([1, 2, 3, 4], 2)).toEqual([[1,2], [3,4]]);
expect(filter([1, 2, 3, 4], 3)).toEqual([[1,2, 3], [4]]);
expect(filter([1, 2, 3, 4], 2)).toEqual([[1, 2], [3, 4]]);
});
it('should collect data into fixed-length chunks or blocks', function() {
expect(filter([1, 2, 3, 4], 3)).toEqual([[1, 2, 3], [4]]);
});
it('should collect data into fixed-length chunks or blocks', function() {
expect(filter(['a', 'b', 'c', 'd'], 4)).toEqual([['a', 'b', 'c', 'd']]);
});

it('should get an fill-value and complete blocks that less than `n`', function() {
expect(filter([1, 2, 3, 4, 5], 2, 0)).toEqual([[1, 2], [3, 4], [5, 0]]);
});
it('should get an fill-value and complete blocks that less than `n`', function() {
expect(filter([1, 2, 3, 4], 3, 1)).toEqual([[1, 2, 3], [4, 1, 1]]);
});

it('should get a !collection and return it as-is', function() {
expect(filter(!1)).toBeFalsy();
expect(filter(1)).toEqual(1);
});
it('should get a !collection and return it as-is', function() {
expect(filter('string')).toEqual('string');
});
it('should get a !collection and return it as-is', function() {
expect(filter(undefined)).toEqual(undefined);
});
});