Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"first" filter returns an object instead of an array when only one object is present #19

Closed
ostruk opened this issue Sep 8, 2014 · 3 comments
Labels

Comments

@ostruk
Copy link

ostruk commented Sep 8, 2014

I think it is better to keep return types consistent to avoid unexpected behavior, such as use of a filter inside ng-repeat, which causes ng-repeat to iterate over object properties instead of the object itself.

Replace "return (count < 2) ? result[0] : result;" with just "return result;" inside "getFirstMatches" function.

@a8m
Copy link
Owner

a8m commented Sep 8, 2014

We could do something like that:
If filter get an arguments (e.g: collection | first: 1: expression ), it's return result as array.
But, if it doesn't (e.g: collection | first ), it's return an object(the first one).
so, we could do something like that:

<h1> {{ collection | first }}</h1>
<!-- or -->
<th ng-repeat="col in collection | first: 1: 'expression'">
  {{ col }}
</th>

@ostruk what do you think ?

@ostruk
Copy link
Author

ostruk commented Sep 9, 2014

I like this, and then collection | first | expression would also return an object? In my case n=3, but sometimes collection would have only one item, resulting in unexpected behavior.

Thanks!

@a8m
Copy link
Owner

a8m commented Sep 9, 2014

@ostruk only filter without an arguments return an object
e.g:
collection | first object
collection | first: n array
collection | first: expression array
collection | first: n: expression array

same with last filter.
want to PR?

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

No branches or pull requests

2 participants