From 8bfeddb5d671017f4a21b8b46334ac816710b143 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Wed, 3 Dec 2014 12:59:41 -0500 Subject: [PATCH] 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 --- src/ng/filter/orderBy.js | 27 ++++++++++++++++++++++----- test/ng/filter/orderBySpec.js | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 3659c55a..cfa8f151 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.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(); } diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index 788f145f..11eec432 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -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); + }); }); });