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): sort by identity if no predicate is given #9403
Closed
Closed
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
* correctly, make sure they are actually being saved as numbers and not strings. | ||
* | ||
* @param {Array} array The array to sort. | ||
* @param {function(*)|string|Array.<(function(*)|string)>} expression A predicate to be | ||
* @param {function(*)|string|Array.<(function(*)|string)>=} expression A predicate to be | ||
* used by the comparator to determine the order of elements. | ||
* | ||
* Can be one of: | ||
|
@@ -24,10 +24,13 @@ | |
* is interpreted as a property name to be used in comparisons (for example `"special name"` | ||
* to sort object by the value of their `special name` property). An expression can be | ||
* optionally prefixed with `+` or `-` to control ascending or descending sort order | ||
* (for example, `+name` or `-name`). | ||
* (for example, `+name` or `-name`). If no property is provided, (e.g. `'+'`) then the array | ||
* element itself is used to compare where sorting. | ||
* - `Array`: An array of function or string predicates. The first predicate in the array | ||
* is used for sorting, but when two items are equivalent, the next predicate is used. | ||
* | ||
* If the predicate is missing or empty then it defaults to `'+'`. | ||
* | ||
* @param {boolean=} reverse Reverse the order of the array. | ||
* @returns {Array} Sorted copy of the source array. | ||
* | ||
|
@@ -116,15 +119,21 @@ orderByFilter.$inject = ['$parse']; | |
function orderByFilter($parse){ | ||
return function(array, sortPredicate, reverseOrder) { | ||
if (!(isArrayLike(array))) return array; | ||
if (!sortPredicate) return array; | ||
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate]; | ||
if (sortPredicate.length === 0) { sortPredicate = ['+']; } | ||
sortPredicate = sortPredicate.map(function(predicate){ | ||
var descending = false, get = predicate || identity; | ||
if (isString(predicate)) { | ||
if ((predicate.charAt(0) == '+' || predicate.charAt(0) == '-')) { | ||
descending = predicate.charAt(0) == '-'; | ||
predicate = predicate.substring(1); | ||
} | ||
if ( predicate === '' ) { | ||
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. no spaces |
||
// Effectively no predicate was passed so we compare identity | ||
return reverseComparator(function(a,b) { | ||
return compare(a, b); | ||
}, descending); | ||
} | ||
get = $parse(predicate); | ||
if (get.constant) { | ||
var key = get(); | ||
|
This file contains 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
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.
Any particular reason why
==
instead of===
?