-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(filter): throw error if not used with an array #10352
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@ngdoc error | ||
@name filter:notarray | ||
@fullName Not an array | ||
@description | ||
|
||
This error occurs when {@link ng.filter filter} is not used with an array. | ||
Filter must be used with an array so a subset of items can be returned. | ||
The array can be initialized asynchronously so null or undefined won't throw this error. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,7 +195,7 @@ describe('Filter: filter', function() { | |
expect(filter(items, expr, true).length).toBe(1); | ||
expect(filter(items, expr, true)[0]).toBe(items[0]); | ||
|
||
// Inherited function proprties | ||
// Inherited function properties | ||
function Item(text) { | ||
this.text = text; | ||
} | ||
|
@@ -323,6 +323,35 @@ describe('Filter: filter', function() { | |
}); | ||
|
||
|
||
it('should throw an error when is not used with an array', function() { | ||
var item = {'not': 'array'}; | ||
expect(function() { filter(item, {}); }). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like some extra test cases here --- for instance, one with Otherwise it looks pretty good, I'd land this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @caitp! I've updated with three new test cases, but I'm not sure if they are what you meant. So please check it and let me know. Cheers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sort of --- they can be in the same |
||
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"not":"array"}'); | ||
|
||
item = Object.create(null); | ||
expect(function() { filter(item, {}); }). | ||
toThrowMinErr('filter', 'notarray', 'Expected array but received: {}'); | ||
|
||
item = { | ||
toString: null, | ||
valueOf: null | ||
}; | ||
expect(function() { filter(item, {}); }). | ||
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"toString":null,"valueOf":null}'); | ||
}); | ||
|
||
|
||
it('should return undefined when the array is undefined', function() { | ||
expect(filter(undefined, {})).toBeUndefined(); | ||
}); | ||
|
||
|
||
it('should return null when the value of the array is null', function() { | ||
var item = null; | ||
expect(filter(item, {})).toBe(null); | ||
}); | ||
|
||
|
||
describe('should support comparator', function() { | ||
|
||
it('not consider `object === "[object Object]"` in non-strict comparison', function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally I think it's good to show causes of the error in code snippets, as well as suggested fixes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'll try to add it when I find time this week. Thanks!