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

filterFilter doesn't work with Array like objects anymore #11782

Closed
superasn opened this issue May 1, 2015 · 8 comments
Closed

filterFilter doesn't work with Array like objects anymore #11782

superasn opened this issue May 1, 2015 · 8 comments

Comments

@superasn
Copy link

superasn commented May 1, 2015

filterFilter also doesn't work with Array like objects anymore, like it used to before.

Please take a look at this fiddle:

Works like it should with previous version of Angular (v1.2):
https://jsfiddle.net/superasn/gznzjgmb/

Breaks with newer versions of Angular (v1.3 and above):
https://jsfiddle.net/superasn/c15yvq6g/

@petebacondarwin
Copy link
Member

@gkalpak can you take a look at this one?

gonzaloruizdevilla pushed a commit to gonzaloruizdevilla/angular.js that referenced this issue May 1, 2015
Throw error if filter is not used with an array like object.

Closes angular#11782
@gkalpak
Copy link
Member

gkalpak commented May 1, 2015

Will do.

gonzaloruizdevilla pushed a commit to gonzaloruizdevilla/angular.js that referenced this issue May 1, 2015
Throw error if filter is not used with an array like object.

Closes angular#11782
@gkalpak
Copy link
Member

gkalpak commented May 2, 2015

A little background:

  • This was/is not working on 1.2.x. In fact, "array-like" objects have not been supported since v1.1.3.
  • In this context, "array-like" objects are objects prototypally inheriting from Array (so not what Angular internally refers to as "array-like" (i.e. objects for which isArrayLike() returns true)).
  • Pre v1.1.3, filterFilter was checking whether array instanceof Array (which included "subclasses" of Array). Then is switched to Object.prototype.toString.call(array) === '[object Array]' and later to Array.isArray(array).

That said, I don't see why we couldn't support array-like objects (in the internal Angular sense). It seems to be a trivial change.

@gonzaloruizdevilla has already submitted a PR (#11787) we can work on, so let's move the discussion there.

@gkalpak gkalpak self-assigned this May 2, 2015
@petebacondarwin
Copy link
Member

One can ducktype an array for one's own purposes without it being a proper subclass and still like Angular to treat it like a collection in some cases.

@caitp
Copy link
Contributor

caitp commented May 2, 2015

https://jsfiddle.net/0L11e3xf/ here's a proper version of the 1.3 repro posted above which works correctly, by subclassing the array "properly" --- but yes, filterFilter should replace isArray() with isArrayLike(), I guess. It would be a bonus if the isArrayLike() test didn't use the hasProperty() check :|

@gkalpak
Copy link
Member

gkalpak commented May 2, 2015

@caitp, still not sure what you suggest :)

This doesn't work as is (because of the isArray(array) check and array.filter).
I suggested replacing isArray with isArrayLike and array.filter(...) with isArray(array) ? array.filter(...) : Array.prototype.filter.call(array, ...).

Are you saying something different ?

Regarding the length > 0 && (length - 1) in obj check in isArrayLike, I agree it is useless, but doesn't hurt either (other than slowing it down). I would be fine removing it.

@caitp
Copy link
Contributor

caitp commented May 2, 2015

but yes, filterFilter should replace isArray() with isArrayLike(), I guess.

I had to re-word that whole slew of things to sound less negative, that's all

@gonzaloruizdevilla
Copy link
Contributor

@gkalpak I don't think the typeof length === 'number' && length > 0 && (length - 1) in obj part is useless.
Actually, my PR #1938 with the first isArrayLike implementation was meant to distinguish between arrayLike objects and other objects with a numeric length property.

It's funny to see how today's implementation is so similar to the one I PR those days. @IgorMinar had to make a difficult implementation due to IE8 support in those versions (ec54712)

gonzaloruizdevilla pushed a commit to gonzaloruizdevilla/angular.js that referenced this issue May 16, 2015
Throw error if filter is not used with an array like object.

Closes angular#11782
gonzaloruizdevilla pushed a commit to gonzaloruizdevilla/angular.js that referenced this issue May 16, 2015
Throw error if filter is not used with an array like object.

Closes angular#11782
gonzaloruizdevilla pushed a commit to gonzaloruizdevilla/angular.js that referenced this issue May 19, 2015
Throw error if filter is not used with an array like object.

Closes angular#11782
netman92 pushed a commit to netman92/angular.js that referenced this issue Aug 8, 2015
Throw error if filter is not used with an array like object.

Closes angular#11782
Closes angular#11787
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.