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

fix(filter): throw error if not used with an array #10352

Closed
wants to merge 1 commit into from
Closed

fix(filter): throw error if not used with an array #10352

wants to merge 1 commit into from

Conversation

Puigcerber
Copy link
Contributor

Throw error if filter is not used with an array.

Closes #9992

BREAKING CHANGE: Previously, the filter was not applied if used with a non array.
Now, it throws an error.

@googlebot
Copy link

CLAs look good, thanks!

@jbedard
Copy link
Contributor

jbedard commented Dec 6, 2014

I think null/undefined should still return without error?

@Puigcerber
Copy link
Contributor Author

Hi @jbedard! I'm not sure about it. Let's wait for other opinions. Cheers.

@gkalpak
Copy link
Member

gkalpak commented Dec 8, 2014

Definitely not throwing on undefined/null. It is a common pattern to initialize a resource asynchronously (in which case you shouldn't have to pre-initialize it ot an empty array or object first).

@Puigcerber
Copy link
Contributor Author

Yeah, actually that makes sense.

@caitp
Copy link
Contributor

caitp commented Dec 8, 2014

since this is a breaking change (albeit a tiny one), i'm not sure we want to ship this before 1.4 --- @petebacondarwin confirm

'| filter:isNotFoo',
'ng-model': 'selected'
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this and add the change that was suggested

@caitp caitp added this to the Backlog milestone Dec 8, 2014
@caitp
Copy link
Contributor

caitp commented Dec 11, 2014

So we decided we're gonna do this for 1.4 --- I think we can merge this as soon as we have moved 1.3 to its own branch, but I'm just going to take a quick look to see if there are any adjustments to be made here...

@@ -323,6 +323,25 @@ 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, {}); }).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like some extra test cases here --- for instance, one with Object.create(null), one with a non-function toString and valueOf method --- These will work just fine currently, but if the implementation of minErr changes they might need some refactoring later on.

Otherwise it looks pretty good, I'd land this

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort of --- they can be in the same it() block as the first test you wrote --- testing that it throws a specific $minErr error, rather than the TypeError that will be thrown if we change the implementation of minerr

@caitp caitp self-assigned this Dec 11, 2014
@caitp caitp added 1.4 and removed 1.4-candidate labels Dec 11, 2014


it('should throw an error when used with the toString() method', function() {
var item = Object.create(Object.prototype);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite :>

var item = {
  toString: null,
  valueOf: null,
};
expect(function() { filter(item, {}); }).
      toThrowMinErr('filter', 'notarray', 'Expected array but received: {"toString": null, "valueOf": null}');

Throw error if filter is not used with an array.

Closes #9992

BREAKING CHANGE: Previously, the filter was not applied if used with a non array.
Now, it throws an error.
@caitp caitp modified the milestones: 1.4.0-beta.2 / 1.3.11, Backlog Jan 23, 2015

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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!

@caitp
Copy link
Contributor

caitp commented Jan 23, 2015

lgtm, will land today

@caitp
Copy link
Contributor

caitp commented Jan 23, 2015

a good followup bug would be to improve the error document a bit

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate error when attempting to use filter on objects
6 participants