This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d93e526
fix(orderBy): Throw exception when orderBy is given a non array-like …
nhodges 6ddfc27
Documentation to orderby when the parameter is not an array.
fhernandezn 77cefef
fix(orderBy): Change filter to orderBy in error thrown.
nhodges 0c47849
chore(orderBy): Update documentation based on code review.
nhodges 1c997ce
fix(orderBy): Modify docs and add test for array-like object.
nhodges e1e87ec
fix(orderBy): Return null/undefined as-is, add specs for this edge case.
nhodges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
{{ 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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filter:search --> orderBy:search |
||
{{ item }} | ||
</div> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: {}'); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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