mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
fix(orderBy): maintain order in array of objects when predicate is not provided
In ES262, there are two properties which are used to get a primitive value from an Object: - valueOf() -- a method which returns a primitive value represented by the Object - toString() -- a method which returns a string value representing the Object. When comparing objects using relational operators, the abstract operation ToPrimitive(O, TypeHint) is used, which will use these methods to retrieve a value. This CL emulates the behaviour of ToPrimitive(), and ensures that no ordering occurs if the retrieved value is identical. This behaviour was previously used for Date objects, however it can be safely made generic as it applies to all objects. Closes #9566 Closes #9747 Closes #10311
This commit is contained in:
@@ -163,12 +163,29 @@ function orderByFilter($parse) {
|
||||
function compare(v1, v2) {
|
||||
var t1 = typeof v1;
|
||||
var t2 = typeof v2;
|
||||
if (t1 == t2) {
|
||||
if (isDate(v1) && isDate(v2)) {
|
||||
v1 = v1.valueOf();
|
||||
v2 = v2.valueOf();
|
||||
// Prepare values for Abstract Relational Comparison
|
||||
// (http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5):
|
||||
// If the resulting values are identical, return 0 to prevent
|
||||
// incorrect re-ordering.
|
||||
if (t1 === t2 && t1 === "object") {
|
||||
// If types are both numbers, emulate abstract ToPrimitive() operation
|
||||
// in order to get primitive values suitable for comparison
|
||||
t1 = typeof (v1 = v1.valueOf());
|
||||
t2 = typeof (v2 = v2.valueOf());
|
||||
if (t1 === t2 && t1 === "object") {
|
||||
// Object.prototype.valueOf will return the original object, by
|
||||
// default. If we do not receive a primitive value, use ToString()
|
||||
// instead.
|
||||
t1 = typeof (v1 = v1.toString());
|
||||
t2 = typeof (v2 = v2.toString());
|
||||
|
||||
// If the end result of toString() for each item is the same, do not
|
||||
// perform relational comparison, and do not re-order objects.
|
||||
if (t1 === t2 && v1 === v2) return 0;
|
||||
}
|
||||
if (t1 == "string") {
|
||||
}
|
||||
if (t1 === t2) {
|
||||
if (t1 === "string") {
|
||||
v1 = v1.toLowerCase();
|
||||
v2 = v2.toLowerCase();
|
||||
}
|
||||
|
||||
@@ -104,6 +104,17 @@ describe('Filter: orderBy', function() {
|
||||
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
|
||||
it('should not reverse array of objects with no predicate', function() {
|
||||
var array = [
|
||||
{ id: 2 },
|
||||
{ id: 1 },
|
||||
{ id: 4 },
|
||||
{ id: 3 }
|
||||
];
|
||||
expect(orderBy(array)).toEqualData(array);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -210,5 +221,16 @@ describe('Filter: orderBy', function() {
|
||||
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
|
||||
it('should not reverse array of objects with no predicate', function() {
|
||||
var array = [
|
||||
{ id: 2 },
|
||||
{ id: 1 },
|
||||
{ id: 4 },
|
||||
{ id: 3 }
|
||||
];
|
||||
expect(orderBy(array)).toEqualData(array);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user