diff --git a/src/_filter/collection/chunk-by.js b/src/_filter/collection/chunk-by.js index 9c9f1f9..eebfccc 100644 --- a/src/_filter/collection/chunk-by.js +++ b/src/_filter/collection/chunk-by.js @@ -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)); + } + } + }]); diff --git a/test/spec/filter/collection/chunk-by.js b/test/spec/filter/collection/chunk-by.js index f069f2b..4580391 100644 --- a/test/spec/filter/collection/chunk-by.js +++ b/test/spec/filter/collection/chunk-by.js @@ -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); }); });