Permalink
Comparing changes
Open a pull request
- 1 commit
- 2 files changed
- 1 commit comment
- 1 contributor
Commits on Dec 03, 2014
…t 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
Unified
Split
Showing
with
44 additions
and 5 deletions.
- +22 −5 src/ng/filter/orderBy.js
- +22 −0 test/ng/filter/orderBySpec.js
| @@ -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); | ||
| }); | ||
| }); | ||
| }); | ||
Showing you all comments on commits in this comparison.
This comment has been minimized.
This comment has been minimized.
michael-zhang-86
commented on 8bfeddb
Apr 28, 2015
|
when object array length is long than 10,orderBy in chrome will not correct. |