@@ -11,17 +11,7 @@ var module = angular.module('ui.grid');
11
11
*
12
12
*/
13
13
14
- module . service ( 'rowSorter' , [ '$parse' , 'uiGridConstants' , function ( $parse , uiGridConstants ) {
15
- var currencyRegexStr =
16
- '(' +
17
- uiGridConstants . CURRENCY_SYMBOLS
18
- . map ( function ( a ) { return '\\' + a ; } ) // Escape all the currency symbols ($ at least will jack up this regex)
19
- . join ( '|' ) + // Join all the symbols together with |s
20
- ')?' ;
21
-
22
- // /^[-+]?[£$¤¥]?[\d,.]+%?$/
23
- var numberStrRegex = new RegExp ( '^[-+]?' + currencyRegexStr + '[\\d,.]+' + currencyRegexStr + '%?$' ) ;
24
-
14
+ module . service ( 'rowSorter' , [ 'uiGridConstants' , function ( uiGridConstants ) {
25
15
var rowSorter = {
26
16
// Cache of sorting functions. Once we create them, we don't want to keep re-doing it
27
17
// this takes a piece of data from the cell and tries to determine its type and what sorting
@@ -74,17 +64,16 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
74
64
*/
75
65
rowSorter . handleNulls = function handleNulls ( a , b ) {
76
66
// We want to allow zero values and false values to be evaluated in the sort function
77
- if ( ( ! a && a ! == 0 && a !== false ) || ( ! b && b ! == 0 && b !== false ) ) {
67
+ if ( ( a == void 0 ) || ( b == void 0 ) ) {
78
68
// We want to force nulls and such to the bottom when we sort... which effectively is "greater than"
79
- if ( ( ! a && a ! == 0 && a !== false ) && ( ! b && b ! == 0 && b !== false ) ) {
69
+ if ( ( a == void 0 ) && ( b == void 0 ) ) {
80
70
return 0 ;
81
71
}
82
- else if ( ! a && a !== 0 && a !== false ) {
72
+
73
+ if ( a == void 0 ) {
83
74
return 1 ;
84
75
}
85
- else if ( ! b && b !== 0 && b !== false ) {
86
- return - 1 ;
87
- }
76
+ return - 1 ; //b == void 0
88
77
}
89
78
return null ;
90
79
} ;
@@ -102,17 +91,18 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
102
91
*/
103
92
rowSorter . basicSort = function basicSort ( a , b ) {
104
93
var nulls = rowSorter . handleNulls ( a , b ) ;
105
- if ( nulls !== null ) {
94
+ if ( nulls !== null ) {
106
95
return nulls ;
107
- } else {
108
- if ( a === b ) {
109
- return 0 ;
110
- }
111
- if ( a < b ) {
112
- return - 1 ;
113
- }
114
- return 1 ;
115
96
}
97
+
98
+ if ( a === b ) {
99
+ return 0 ;
100
+ }
101
+
102
+ if ( a < b ) {
103
+ return - 1 ;
104
+ }
105
+ return 1 ;
116
106
} ;
117
107
118
108
@@ -127,14 +117,15 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
127
117
*/
128
118
rowSorter . sortNumber = function sortNumber ( a , b ) {
129
119
var nulls = rowSorter . handleNulls ( a , b ) ;
130
- if ( nulls !== null ) {
131
- return nulls ;
132
- } else {
133
- return a - b ;
134
- }
120
+ return ( nulls !== null ) ? nulls : a - b ;
135
121
} ;
136
122
137
123
124
+ function parseNumStr ( numStr ) {
125
+ return parseFloat ( numStr . replace ( / [ ^ 0 - 9 . - ] / g, '' ) ) ;
126
+ }
127
+
128
+
138
129
/**
139
130
* @ngdoc method
140
131
* @methodOf ui.grid.class:rowSorter
@@ -147,45 +138,23 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
147
138
*/
148
139
rowSorter . sortNumberStr = function sortNumberStr ( a , b ) {
149
140
var nulls = rowSorter . handleNulls ( a , b ) ;
150
- if ( nulls !== null ) {
141
+ if ( nulls !== null ) {
151
142
return nulls ;
152
- } else {
153
- var numA , // The parsed number form of 'a'
154
- numB , // The parsed number form of 'b'
155
- badA = false ,
156
- badB = false ;
157
-
158
- // Try to parse 'a' to a float
159
- numA = parseFloat ( a . replace ( / [ ^ 0 - 9 . - ] / g, '' ) ) ;
160
-
161
- // If 'a' couldn't be parsed to float, flag it as bad
162
- if ( isNaN ( numA ) ) {
163
- badA = true ;
164
- }
165
-
166
- // Try to parse 'b' to a float
167
- numB = parseFloat ( b . replace ( / [ ^ 0 - 9 . - ] / g, '' ) ) ;
168
-
169
- // If 'b' couldn't be parsed to float, flag it as bad
170
- if ( isNaN ( numB ) ) {
171
- badB = true ;
172
- }
173
-
174
- // We want bad ones to get pushed to the bottom... which effectively is "greater than"
175
- if ( badA && badB ) {
176
- return 0 ;
177
- }
143
+ }
178
144
179
- if ( badA ) {
180
- return 1 ;
181
- }
145
+ var numA = parseNumStr ( a ) , // The parsed number form of 'a'
146
+ numB = parseNumStr ( b ) ; // The parsed number form of 'b'
182
147
183
- if ( badB ) {
184
- return - 1 ;
185
- }
148
+ // If 'a' couldn't be parsed to float, flag it as bad
149
+ var badA = isNaN ( numA ) ,
150
+ badB = isNaN ( numB ) ;
186
151
187
- return numA - numB ;
152
+ // We want bad ones to get pushed to the bottom... which effectively is "greater than"
153
+ if ( badA || badB ) {
154
+ return ( badA && badB ) ? 0 : ( badA ? 1 : - 1 ) ;
188
155
}
156
+
157
+ return numA - numB ;
189
158
} ;
190
159
191
160
@@ -200,14 +169,13 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
200
169
*/
201
170
rowSorter . sortAlpha = function sortAlpha ( a , b ) {
202
171
var nulls = rowSorter . handleNulls ( a , b ) ;
203
- if ( nulls !== null ) {
172
+ if ( nulls !== null ) {
204
173
return nulls ;
205
- } else {
206
- var strA = a . toString ( ) . toLowerCase ( ) ,
207
- strB = b . toString ( ) . toLowerCase ( ) ;
208
-
209
- return strA === strB ? 0 : strA . localeCompare ( strB ) ;
210
174
}
175
+
176
+ var strA = a . toString ( ) . toLowerCase ( ) ,
177
+ strB = b . toString ( ) . toLowerCase ( ) ;
178
+ return strA === strB ? 0 : strA . localeCompare ( strB ) ;
211
179
} ;
212
180
213
181
@@ -223,20 +191,13 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
223
191
*/
224
192
rowSorter . sortDate = function sortDate ( a , b ) {
225
193
var nulls = rowSorter . handleNulls ( a , b ) ;
226
- if ( nulls !== null ) {
194
+ if ( nulls !== null ) {
227
195
return nulls ;
228
- } else {
229
- if ( ! ( a instanceof Date ) ) {
230
- a = new Date ( a ) ;
231
- }
232
- if ( ! ( b instanceof Date ) ) {
233
- b = new Date ( b ) ;
234
- }
235
- var timeA = a . getTime ( ) ,
236
- timeB = b . getTime ( ) ;
237
-
238
- return timeA === timeB ? 0 : ( timeA < timeB ? - 1 : 1 ) ;
239
196
}
197
+
198
+ var timeA = ( a instanceof Date ) ? a . getTime ( ) : new Date ( a ) . getTime ( ) ;
199
+ var timeB = ( b instanceof Date ) ? b . getTime ( ) : new Date ( b ) . getTime ( ) ;
200
+ return timeA === timeB ? 0 : ( timeA < timeB ? - 1 : 1 ) ;
240
201
} ;
241
202
242
203
@@ -252,20 +213,14 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
252
213
*/
253
214
rowSorter . sortBool = function sortBool ( a , b ) {
254
215
var nulls = rowSorter . handleNulls ( a , b ) ;
255
- if ( nulls !== null ) {
216
+ if ( nulls !== null ) {
256
217
return nulls ;
257
- } else {
258
- if ( a && b ) {
259
- return 0 ;
260
- }
218
+ }
261
219
262
- if ( ! a && ! b ) {
263
- return 0 ;
264
- }
265
- else {
266
- return a ? 1 : - 1 ;
267
- }
220
+ if ( ( a && b ) || ( ! a && ! b ) ) {
221
+ return 0 ;
268
222
}
223
+ return a ? 1 : - 1 ;
269
224
} ;
270
225
271
226
@@ -290,45 +245,38 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
290
245
* we might inspect the rows themselves to decide what sort of data might be there
291
246
* @returns {function } the sort function chosen for the column
292
247
*/
293
- rowSorter . getSortFn = function getSortFn ( grid , col , rows ) {
294
- var sortFn , item ;
295
-
248
+ rowSorter . getSortFn = function getSortFn ( col ) {
296
249
// See if we already figured out what to use to sort the column and have it in the cache
297
250
if ( rowSorter . colSortFnCache [ col . colDef . name ] ) {
298
- sortFn = rowSorter . colSortFnCache [ col . colDef . name ] ;
251
+ return rowSorter . colSortFnCache [ col . colDef . name ] ;
299
252
}
300
253
// If the column has its OWN sorting algorithm, use that
301
- else if ( col . sortingAlgorithm !== undefined ) {
302
- sortFn = col . sortingAlgorithm ;
254
+ if ( col . sortingAlgorithm != void 0 ) {
303
255
rowSorter . colSortFnCache [ col . colDef . name ] = col . sortingAlgorithm ;
256
+ return col . sortingAlgorithm ;
304
257
}
305
258
// Always default to sortAlpha when sorting after a cellFilter
306
- else if ( col . sortCellFiltered && col . cellFilter ) {
307
- sortFn = rowSorter . sortAlpha ;
308
- rowSorter . colSortFnCache [ col . colDef . name ] = sortFn ;
259
+ if ( col . sortCellFiltered && col . cellFilter ) {
260
+ rowSorter . colSortFnCache [ col . colDef . name ] = rowSorter . sortAlpha ;
261
+ return rowSorter . sortAlpha ;
309
262
}
263
+
310
264
// Try and guess what sort function to use
311
- else {
312
- // Guess the sort function
313
- sortFn = rowSorter . guessSortFn ( col . colDef . type ) ;
265
+ // Guess the sort function
266
+ var sortFn = rowSorter . guessSortFn ( col . colDef . type ) ;
314
267
315
- // If we found a sort function, cache it
316
- if ( sortFn ) {
317
- rowSorter . colSortFnCache [ col . colDef . name ] = sortFn ;
318
- }
319
- else {
320
- // We assign the alpha sort because anything that is null/undefined will never get passed to
321
- // the actual sorting function. It will get caught in our null check and returned to be sorted
322
- // down to the bottom
323
- sortFn = rowSorter . sortAlpha ;
324
- }
268
+ // If we found a sort function, cache it
269
+ if ( sortFn ) {
270
+ rowSorter . colSortFnCache [ col . colDef . name ] = sortFn ;
271
+ return sortFn ;
325
272
}
326
-
327
- return sortFn ;
273
+ // We assign the alpha sort because anything that is null/undefined will never get passed to
274
+ // the actual sorting function. It will get caught in our null check and returned to be sorted
275
+ // down to the bottom
276
+ return rowSorter . sortAlpha ;
328
277
} ;
329
278
330
279
331
-
332
280
/**
333
281
* @ngdoc method
334
282
* @methodOf ui.grid.class:rowSorter
@@ -349,26 +297,22 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
349
297
return - 1 ;
350
298
}
351
299
// Equal
352
- else if ( a . sort . priority === b . sort . priority ) {
300
+ if ( a . sort . priority === b . sort . priority ) {
353
301
return 0 ;
354
302
}
355
303
// B is higher
356
- else {
357
- return 1 ;
358
- }
304
+ return 1 ;
359
305
}
360
306
// Only A has a priority
361
- else if ( a . sort && a . sort . priority !== undefined ) {
307
+ if ( a . sort && a . sort . priority !== undefined ) {
362
308
return - 1 ;
363
309
}
364
310
// Only B has a priority
365
- else if ( b . sort && b . sort . priority !== undefined ) {
311
+ if ( b . sort && b . sort . priority !== undefined ) {
366
312
return 1 ;
367
313
}
368
314
// Neither has a priority
369
- else {
370
- return 0 ;
371
- }
315
+ return 0 ;
372
316
} ;
373
317
374
318
@@ -434,34 +378,31 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
434
378
var col , direction ;
435
379
436
380
// put a custom index field on each row, used to make a stable sort out of unstable sorts (e.g. Chrome)
437
- var setIndex = function ( row , idx ) {
381
+ rows . forEach ( function ( row , idx ) {
438
382
row . entity . $$uiGridIndex = idx ;
439
- } ;
440
- rows . forEach ( setIndex ) ;
383
+ } ) ;
441
384
442
385
// IE9-11 HACK.... the 'rows' variable would be empty where we call rowSorter.getSortFn(...) below. We have to use a separate reference
443
386
// var d = data.slice(0);
444
- var r = rows . slice ( 0 ) ;
387
+ // var r = rows.slice(0);
445
388
446
389
// Now actually sort the data
447
390
var rowSortFn = function ( rowA , rowB ) {
448
- var tem = 0 ,
449
- idx = 0 ,
450
- sortFn ;
391
+ var tem = 0 ,
392
+ idx = 0 ,
393
+ sortFn ;
451
394
452
395
while ( tem === 0 && idx < sortCols . length ) {
453
396
// grab the metadata for the rest of the logic
454
397
col = sortCols [ idx ] . col ;
455
398
direction = sortCols [ idx ] . sort . direction ;
456
399
457
- sortFn = rowSorter . getSortFn ( grid , col , r ) ;
400
+ sortFn = rowSorter . getSortFn ( col ) ;
458
401
459
402
// Webpack's compress will hoist and combine propA, propB into one var and break sorting functionality
460
403
// Wrapping in function prevents that unexpected behavior
461
404
var props = getCellValues ( grid , rowA , rowB , col ) ;
462
- var propA = props [ 0 ] ;
463
- var propB = props [ 1 ] ;
464
- tem = sortFn ( propA , propB , rowA , rowB , direction , col ) ;
405
+ tem = sortFn ( props [ 0 ] , props [ 1 ] , rowA , rowB , direction , col ) ;
465
406
466
407
idx ++ ;
467
408
}
@@ -475,20 +416,15 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
475
416
}
476
417
477
418
// Made it this far, we don't have to worry about null & undefined
478
- if ( direction === uiGridConstants . ASC ) {
479
- return tem ;
480
- } else {
481
- return 0 - tem ;
482
- }
419
+ return ( direction === uiGridConstants . ASC ) ? tem : 0 - tem ;
483
420
} ;
484
421
485
422
var newRows = rows . sort ( rowSortFn ) ;
486
423
487
424
// remove the custom index field on each row, used to make a stable sort out of unstable sorts (e.g. Chrome)
488
- var clearIndex = function ( row , idx ) {
489
- delete row . entity . $$uiGridIndex ;
490
- } ;
491
- rows . forEach ( clearIndex ) ;
425
+ rows . forEach ( function ( row , idx ) {
426
+ delete row . entity . $$uiGridIndex ;
427
+ } ) ;
492
428
493
429
return newRows ;
494
430
} ;
@@ -510,4 +446,4 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
510
446
return rowSorter ;
511
447
} ] ) ;
512
448
513
- } ) ( ) ;
449
+ } ) ( ) ;
0 commit comments