Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit cea8e75

Browse files
Puigcerbercaitp
authored andcommitted
fix(filterFilter): throw error if input is not an array
Throw error if filter is not used with an array. BREAKING CHANGE: Previously, the filter was not applied if used with a non array. Now, it throws an error. This can be worked around by converting an object to an array, using a filter such as https://github.com/petebacondarwin/angular-toArrayFilter Closes #9992 Closes #10352
1 parent b3a9bd3 commit cea8e75

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@ngdoc error
2+
@name filter:notarray
3+
@fullName Not an array
4+
@description
5+
6+
This error occurs when {@link ng.filter filter} is not used with an array.
7+
Filter must be used with an array so a subset of items can be returned.
8+
The array can be initialized asynchronously so null or undefined won't throw this error.

src/ng/filter/filter.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@
124124
*/
125125
function filterFilter() {
126126
return function(array, expression, comparator) {
127-
if (!isArray(array)) return array;
127+
if (!isArray(array)) {
128+
if (array == null) {
129+
return array;
130+
} else {
131+
throw minErr('filter')('notarray', 'Expected array but received: {0}', array);
132+
}
133+
}
128134

129135
var predicateFn;
130136
var matchAgainstAnyProp;

test/ng/filter/filterSpec.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe('Filter: filter', function() {
271271
expect(filter(items, expr, true).length).toBe(1);
272272
expect(filter(items, expr, true)[0]).toBe(items[0]);
273273

274-
// Inherited function proprties
274+
// Inherited function properties
275275
function Item(text) {
276276
this.text = text;
277277
}
@@ -399,6 +399,35 @@ describe('Filter: filter', function() {
399399
});
400400

401401

402+
it('should throw an error when is not used with an array', function() {
403+
var item = {'not': 'array'};
404+
expect(function() { filter(item, {}); }).
405+
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"not":"array"}');
406+
407+
item = Object.create(null);
408+
expect(function() { filter(item, {}); }).
409+
toThrowMinErr('filter', 'notarray', 'Expected array but received: {}');
410+
411+
item = {
412+
toString: null,
413+
valueOf: null
414+
};
415+
expect(function() { filter(item, {}); }).
416+
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"toString":null,"valueOf":null}');
417+
});
418+
419+
420+
it('should return undefined when the array is undefined', function() {
421+
expect(filter(undefined, {})).toBeUndefined();
422+
});
423+
424+
425+
it('should return null when the value of the array is null', function() {
426+
var item = null;
427+
expect(filter(item, {})).toBe(null);
428+
});
429+
430+
402431
describe('should support comparator', function() {
403432

404433
it('not consider `object === "[object Object]"` in non-strict comparison', function() {

test/ngScenario/dslSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ describe("angular.scenario.dsl", function() {
624624
});
625625

626626
it('should match bindings by substring match', function() {
627-
compile('<pre ng-bind="foo.bar | filter"></pre>', 'binding value');
627+
compile('<pre ng-bind="foo.bar | lowercase"></pre>', 'binding value');
628628
$root.dsl.binding('foo . bar');
629629
expect($root.futureResult).toEqual('binding value');
630630
});

0 commit comments

Comments
 (0)