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

fix(orderBy): Throw exception when orderBy is given a non array-like object. #11719

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/content/error/orderBy/notarray.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@ngdoc error
@name orderBy:notarray
@fullName Value is not array-like
@description

This error occurs when {@link ng.orderBy orderBy} is not passed an array-like value:
```html
<input ng-model="search">
<div ng-repeat="(key, value) in myObj | orderBy:search">
{{ key }} : {{ value }}
</div>
```

orderBy must be used with an array-like value so a subset of items can be returned.
The array can be initialized asynchronously and therefore `null` or `undefined` won't throw this error.

To use orderBy to order the properties of an object, you can create your own array based on that object:
```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];
});
});
```
That can be used as:
```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">
Copy link
Member

Choose a reason for hiding this comment

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

myObj --> arrFromMyObj

{{ key }} : {{ value }}
</div>
```

You could as well convert the object to an array using a filter such as
[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter):
```html
<input ng-model="search">
<div ng-repeat="item in myObj | toArray:false | filter:search">
Copy link
Member

Choose a reason for hiding this comment

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

filter:search --> orderBy:search

{{ item }}
</div>
```
4 changes: 3 additions & 1 deletion src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ orderByFilter.$inject = ['$parse'];
function orderByFilter($parse) {
return function(array, sortPredicate, reverseOrder) {

if (!(isArrayLike(array))) return array;
if (!array) { return array; }

if (!isArrayLike(array)) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array);

if (!isArray(sortPredicate)) { sortPredicate = [sortPredicate]; }
if (sortPredicate.length === 0) { sortPredicate = ['+']; }
Expand Down
2 changes: 1 addition & 1 deletion test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('ngOptions', function() {

.directive('oCompileContents', function() {
return {
link: function(scope, element) {
link: function(scope, element) {
linkLog.push('linkCompileContents');
$compile(element.contents())(scope);
}
Expand Down
14 changes: 14 additions & 0 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ describe('Filter: orderBy', function() {


describe('(Arrays)', function() {
it('should throw an exception if no array-like object is provided', function() {
expect(function() { orderBy({}); }).
toThrowMinErr('orderBy', 'notarray', 'Expected array but received: {}');
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a test that shows that an array-like (i.e. not a pure array) does not fail, unless there is already such a test in the spec.

it('should not throw an exception if a null or undefined value is provided', function() {
expect(orderBy(null)).toEqual(null);
expect(orderBy(undefined)).toEqual(undefined);
});

it('should not throw an exception if an array-like object is provided', function() {
expect(orderBy('cba')).toEqual(['a', 'b', 'c']);
});

it('should return sorted array if predicate is not provided', function() {
expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);

Expand Down