-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(orderBy): Throw exception when orderBy is given a non array-like object. #11719
Conversation
@@ -155,7 +155,7 @@ | |||
orderByFilter.$inject = ['$parse']; | |||
function orderByFilter($parse) { | |||
return function(array, sortPredicate, reverseOrder) { | |||
if (!(isArrayLike(array))) return array; | |||
if (!(isArrayLike(array))) throw minErr('filter')('notarray', 'Expected array but received: {0}', array); |
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.
Sadly this error code is specific to the filterFilter
so we have to provide a new one and associated documentation in docs/content/error
or alternatively update the documentation to make this error code more generic.
@nhodges @petebacondarwin Hi guys, I've been working in the documentation needed for this pull request. Please look at this master...frankweb:orderby-notarray-documentation I'd like to contribute to this pull request by providing documentation. |
👍 @Frankweb for providing documentation, I updated my PR to be fast forwardable. |
@nhodges 👍 do you want to include my documentation in your PR? |
We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm. |
@Frankweb You got it :) |
@Frankweb looks like the task to build the docs is failing after merging, can you take a look? |
@nhodges Of course, I'll take a look in a while :) |
@@ -177,7 +177,7 @@ orderByFilter.$inject = ['$parse']; | |||
function orderByFilter($parse) { | |||
return function(array, sortPredicate, reverseOrder) { | |||
|
|||
if (!(isArrayLike(array))) return array; | |||
if (!(isArrayLike(array))) throw minErr('filter')('notarray', 'Expected array but received: {0}', array); |
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.
grunt test
runs without errors if I change minErr('filter')
to minErr('orderBy')
👍 @Frankweb thanks for the heads up. Fixed and we have a green build again. |
@nhodges, could you take care of the unresolved comments ? |
}, | ||
three: { | ||
name: 'something 3' | ||
}, |
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.
Also, the indentation is funny for this object. TBH, I would change the example to something like:
<!-- HTML -->
<label>
Order by:
<select ng-model="orderProp" ng-options="prop for prop in ['id', 'name']"></select>
</label>
<div ng-repeat="(key, value) in myObj | orderBy:orderProp">
{{ key }} : {{ value }}
</div>
/* JS */
angular.module('aModule', [])
.controller('aController', function($scope) {
var myObj = {
one: {id: 1, name: 'Some thing'},
two: {id: 2, name: 'Another thing'},
three: {id: 3, name: 'A third thing'}
};
$scope.arrFromMyObj = Object.keys(myObj).map(function(key) {
return myObj[key];
});
});
@gkalpak You got it. Pushed. |
@gkalpak @petebacondarwin @Frankweb Anything else I can take care of? |
@nhodges Sorry for the last comment, I already deleted it, my comment was about other PR. |
There is only one problem with the PR, I think all the commits should be made from one account, since you merged my branch this PR has commits with two accounts. Because of this, cla label says |
Hey Francisco, I think if you just sign the CLA should be good? Made with thumbs.
|
@nhodges I guess we should comment something to the googlebot, but I don't know exactly what. |
@googlebot I'm okay with these commits. |
@googlebot I authored these commits |
Hey Francisco, can you try clicking the link below and filling out the form? http://code.google.com/legal/individual-cla-v1.0.html Thanks! |
@nhodges Of course, but I'm already signed in. I'll try again. |
@petebacondarwin Thank you it's good to know that. Do you need another change to this PR? |
</div> | ||
``` | ||
|
||
orderBy must be used with an array so a subset of items can be returned. |
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.
orderBy
must be used with an array-like value so a subset of items can be returned.
I have added a few minor comments. |
I went ahead and took care of the requested changes to docs and added a test that takes in a strong and returns a sorted array. |
@nhodges Good! Please squash the commits, this is your pull request. |
Order by: | ||
<select ng-model="orderProp" ng-options="prop for prop in ['id', 'name']"></select> | ||
</label> | ||
<div ng-repeat="(key, value) in myObj | orderBy:orderProp"> |
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.
myObj --> arrFromMyObj
With #11719 (comment), I think we are good to go 😄 Thx @nhodges and @Frankweb (and @petebacondarwin) for working on this one ! |
@nhodges, actually could you rebase your changes on master. It's been a long time since the first commit... |
BREAKING CHANGES: These changes alter the behavior of AngularJS such that it will no longer throw an error for non array values, as it will accept array-like values as well. It will throw an error if no array-like value is passed.
@gkalpak rebased. LMK if I can do anything else or if you want me to take care of the changes above. |
@@ -177,7 +177,7 @@ orderByFilter.$inject = ['$parse']; | |||
function orderByFilter($parse) { | |||
return function(array, sortPredicate, reverseOrder) { | |||
|
|||
if (!(isArrayLike(array))) return array; | |||
if (!(isArrayLike(array))) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array); |
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.
I was in the middle of merging when I realized there is an issue here: We need to allow null
/undefined
(in order to support asnc loading).
This block needs to be modified (see filter#L132-L138 for an example).
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.
@gkalpak I guess something like
if(!array) return array;
if (!(isArrayLike(array))) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array);
would be easier to read than the example.
This is just an idea,of course :) what do you think about that?
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.
I think that is about right and similar to what we do in other filters.
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.
In filterFilter
we only allow null, undefined and array-like (and I think that should we do here as well). So it would be more like:
if (array == null) return array;
@nhodges, there seems to be one last little thing to do 😄 |
@gkalpak Sure thing, please take a look at the latest changeset. Cheers! |
I updated the documentation (per #11719 (comment)), made the change described in #11719 (comment), fixed some typos in the error message and merged ! Well done everyone 😃 |
BREAKING CHANGE: Previously, an non array-like input would pass through the orderBy filter unchanged. Now, an error is thrown. This can be worked around by converting an object to an array, either manually or using a filter such as https://github.com/petebacondarwin/angular-toArrayFilter. (`null` and `undefined` still pass through without an error, in order to support asynchronous loading of resources.) Closes angular#11255 Closes angular#11719
Throws exception when orderBy is given a non array-like object to sort.
Closes #11255.