Skip to content

Commit 0474f3c

Browse files
committed
Merge pull request #147 from sbuhr/master
Prevent chunkBy to produce infinite digest loop.
2 parents 070a2ee + b275c96 commit 0474f3c

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/_filter/collection/chunk-by.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@
77
* Collect data into fixed-length chunks or blocks
88
*/
99

10-
angular.module('a8m.chunk-by', [])
11-
.filter('chunkBy', [function () {
12-
/**
13-
* @description
14-
* Get array with size `n` in `val` inside it.
15-
* @param n
16-
* @param val
17-
* @returns {Array}
18-
*/
19-
function fill(n, val) {
20-
var ret = [];
21-
while(n--) ret[n] = val;
22-
return ret;
23-
}
10+
angular.module('a8m.chunk-by', ['a8m.filter-watcher'])
11+
.filter('chunkBy', ['filterWatcher', function (filterWatcher) {
12+
return function (array, n, fillVal) {
2413

25-
return function (array, n, fillVal) {
26-
if (!isArray(array)) return array;
27-
return array.map(function(el, i, self) {
28-
i = i * n;
29-
el = self.slice(i, i + n);
30-
return !isUndefined(fillVal) && el.length < n
31-
? el.concat(fill(n - el.length, fillVal))
32-
: el;
33-
}).slice(0, Math.ceil(array.length / n));
34-
}
35-
}]);
14+
return filterWatcher.isMemoized('chunkBy', arguments) ||
15+
filterWatcher.memoize('chunkBy', arguments, this,
16+
_chunkBy(array, n, fillVal));
17+
/**
18+
* @description
19+
* Get array with size `n` in `val` inside it.
20+
* @param n
21+
* @param val
22+
* @returns {Array}
23+
*/
24+
function fill(n, val) {
25+
var ret = [];
26+
while (n--) ret[n] = val;
27+
return ret;
28+
}
29+
30+
function _chunkBy(array, n, fillVal) {
31+
if (!isArray(array)) return array;
32+
return array.map(function (el, i, self) {
33+
i = i * n;
34+
el = self.slice(i, i + n);
35+
return !isUndefined(fillVal) && el.length < n
36+
? el.concat(fill(n - el.length, fillVal))
37+
: el;
38+
}).slice(0, Math.ceil(array.length / n));
39+
}
40+
}
41+
}]);

test/spec/filter/collection/chunk-by.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,30 @@ describe('chunkByFilter', function() {
99
}));
1010

1111
it('should collect data into fixed-length chunks or blocks', function() {
12-
expect(filter([1, 2, 3, 4], 2)).toEqual([[1,2], [3,4]]);
13-
expect(filter([1, 2, 3, 4], 3)).toEqual([[1,2, 3], [4]]);
12+
expect(filter([1, 2, 3, 4], 2)).toEqual([[1, 2], [3, 4]]);
13+
});
14+
it('should collect data into fixed-length chunks or blocks', function() {
15+
expect(filter([1, 2, 3, 4], 3)).toEqual([[1, 2, 3], [4]]);
16+
});
17+
it('should collect data into fixed-length chunks or blocks', function() {
1418
expect(filter(['a', 'b', 'c', 'd'], 4)).toEqual([['a', 'b', 'c', 'd']]);
1519
});
1620

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

2228
it('should get a !collection and return it as-is', function() {
2329
expect(filter(!1)).toBeFalsy();
2430
expect(filter(1)).toEqual(1);
31+
});
32+
it('should get a !collection and return it as-is', function() {
2533
expect(filter('string')).toEqual('string');
34+
});
35+
it('should get a !collection and return it as-is', function() {
2636
expect(filter(undefined)).toEqual(undefined);
2737
});
2838
});

0 commit comments

Comments
 (0)