fix(orderBy): make object-to-primtiive behaviour work for objects with null prototype

This commit is contained in:
Caitlin Potter
2014-12-03 13:34:49 -05:00
parent 8bfeddb5d6
commit 3aa5752894
2 changed files with 25 additions and 5 deletions

View File

@@ -170,18 +170,18 @@ function orderByFilter($parse) {
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());
t1 = typeof (v1.valueOf ? v1 = v1.valueOf() : v1);
t2 = typeof (v2.valueOf ? v2 = v2.valueOf() : v2);
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());
t1 = typeof (v1.toString ? v1 = v1.toString() : v1);
t2 = typeof (v2.toString ? v2 = v2.toString() : v2);
// 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 === t2 && v1 === v2 || t1 === "object") return 0;
}
}
if (t1 === t2) {

View File

@@ -115,6 +115,16 @@ describe('Filter: orderBy', function() {
];
expect(orderBy(array)).toEqualData(array);
});
it('should not reverse array of objects with null prototype and no predicate', function() {
var array = [2,1,4,3].map(function(id) {
var obj = Object.create(null);
obj.id = id;
return obj;
});
expect(orderBy(array)).toEqualData(array);
});
});
@@ -232,5 +242,15 @@ describe('Filter: orderBy', function() {
];
expect(orderBy(array)).toEqualData(array);
});
it('should not reverse array of objects with null prototype and no predicate', function() {
var array = [2,1,4,3].map(function(id) {
var obj = Object.create(null);
obj.id = id;
return obj;
});
expect(orderBy(array)).toEqualData(array);
});
});
});