File tree 2 files changed +33
-9
lines changed
2 files changed +33
-9
lines changed Original file line number Diff line number Diff line change @@ -190,11 +190,11 @@ function orderByFilter($parse) {
190
190
if ( reverseOrder ) { array . reverse ( ) ; }
191
191
return array ;
192
192
193
- function getComparisonObject ( value ) {
193
+ function getComparisonObject ( value , index ) {
194
194
return {
195
195
value : value ,
196
196
predicateValues : predicates . map ( function ( predicate ) {
197
- return getPredicateValue ( predicate . get ( value ) ) ;
197
+ return getPredicateValue ( predicate . get ( value ) , index ) ;
198
198
} )
199
199
} ;
200
200
}
@@ -253,12 +253,19 @@ function orderByFilter($parse) {
253
253
return '' ;
254
254
}
255
255
256
- function getPredicateValue ( value ) {
256
+ function getPredicateValue ( value , index ) {
257
257
var type = typeof value ;
258
258
if ( type === 'string' ) {
259
259
value = value . toLowerCase ( ) ;
260
260
} else if ( type === 'object' ) {
261
261
value = objectToString ( value ) ;
262
+
263
+ // If the object is a POJO then it doesn't really have a well defined ordering
264
+ // To allow us to reverse the order of these objects, we artificially use its current
265
+ // index in the collection as its value
266
+ if ( value === '[object Object]' ) {
267
+ value = index ;
268
+ }
262
269
}
263
270
return { value : value , type : type } ;
264
271
}
Original file line number Diff line number Diff line change @@ -143,6 +143,23 @@ describe('Filter: orderBy', function() {
143
143
} ) ;
144
144
145
145
146
+ it ( 'should reverse array of objects with predicate of "-"' , function ( ) {
147
+ var array = [
148
+ { id : 2 } ,
149
+ { id : 1 } ,
150
+ { id : 4 } ,
151
+ { id : 3 }
152
+ ] ;
153
+ var reversedArray = [
154
+ { id : 3 } ,
155
+ { id : 4 } ,
156
+ { id : 1 } ,
157
+ { id : 2 }
158
+ ] ;
159
+ expect ( orderBy ( array , '-' ) ) . toEqualData ( reversedArray ) ;
160
+ } ) ;
161
+
162
+
146
163
it ( 'should not reverse array of objects with null prototype and no predicate' , function ( ) {
147
164
var array = [ 2 , 1 , 4 , 3 ] . map ( function ( id ) {
148
165
var obj = Object . create ( null ) ;
@@ -161,10 +178,10 @@ describe('Filter: orderBy', function() {
161
178
null
162
179
] ;
163
180
expect ( orderBy ( array ) ) . toEqualData ( [
164
- { id : 2 } ,
165
- { id : 3 } ,
166
181
null ,
167
- null
182
+ null ,
183
+ { id : 2 } ,
184
+ { id : 3 }
168
185
] ) ;
169
186
} ) ;
170
187
} ) ;
@@ -304,10 +321,10 @@ describe('Filter: orderBy', function() {
304
321
null
305
322
] ;
306
323
expect ( orderBy ( array ) ) . toEqualData ( [
307
- { id : 2 } ,
308
- { id : 3 } ,
309
324
null ,
310
- null
325
+ null ,
326
+ { id : 2 } ,
327
+ { id : 3 }
311
328
] ) ;
312
329
} ) ;
313
330
} ) ;
You can’t perform that action at this time.
0 commit comments