Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3aa5752

Browse files
committed
fix(orderBy): make object-to-primtiive behaviour work for objects with null prototype
1 parent 8bfeddb commit 3aa5752

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/ng/filter/orderBy.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,18 @@ function orderByFilter($parse) {
170170
if (t1 === t2 && t1 === "object") {
171171
// If types are both numbers, emulate abstract ToPrimitive() operation
172172
// in order to get primitive values suitable for comparison
173-
t1 = typeof (v1 = v1.valueOf());
174-
t2 = typeof (v2 = v2.valueOf());
173+
t1 = typeof (v1.valueOf ? v1 = v1.valueOf() : v1);
174+
t2 = typeof (v2.valueOf ? v2 = v2.valueOf() : v2);
175175
if (t1 === t2 && t1 === "object") {
176176
// Object.prototype.valueOf will return the original object, by
177177
// default. If we do not receive a primitive value, use ToString()
178178
// instead.
179-
t1 = typeof (v1 = v1.toString());
180-
t2 = typeof (v2 = v2.toString());
179+
t1 = typeof (v1.toString ? v1 = v1.toString() : v1);
180+
t2 = typeof (v2.toString ? v2 = v2.toString() : v2);
181181

182182
// If the end result of toString() for each item is the same, do not
183183
// perform relational comparison, and do not re-order objects.
184-
if (t1 === t2 && v1 === v2) return 0;
184+
if (t1 === t2 && v1 === v2 || t1 === "object") return 0;
185185
}
186186
}
187187
if (t1 === t2) {

test/ng/filter/orderBySpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ describe('Filter: orderBy', function() {
115115
];
116116
expect(orderBy(array)).toEqualData(array);
117117
});
118+
119+
120+
it('should not reverse array of objects with null prototype and no predicate', function() {
121+
var array = [2,1,4,3].map(function(id) {
122+
var obj = Object.create(null);
123+
obj.id = id;
124+
return obj;
125+
});
126+
expect(orderBy(array)).toEqualData(array);
127+
});
118128
});
119129

120130

@@ -232,5 +242,15 @@ describe('Filter: orderBy', function() {
232242
];
233243
expect(orderBy(array)).toEqualData(array);
234244
});
245+
246+
247+
it('should not reverse array of objects with null prototype and no predicate', function() {
248+
var array = [2,1,4,3].map(function(id) {
249+
var obj = Object.create(null);
250+
obj.id = id;
251+
return obj;
252+
});
253+
expect(orderBy(array)).toEqualData(array);
254+
});
235255
});
236256
});

0 commit comments

Comments
 (0)