Skip to content

Commit 59bccab

Browse files
prx-lmomportuga
authored andcommitted
[hotfix/3360]
- replaced null check with void check - refactoring
1 parent af71e78 commit 59bccab

File tree

1 file changed

+85
-149
lines changed

1 file changed

+85
-149
lines changed

packages/core/src/js/services/rowSorter.js

Lines changed: 85 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@ var module = angular.module('ui.grid');
1111
*
1212
*/
1313

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) {
2515
var rowSorter = {
2616
// Cache of sorting functions. Once we create them, we don't want to keep re-doing it
2717
// 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
7464
*/
7565
rowSorter.handleNulls = function handleNulls(a, b) {
7666
// 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)) {
7868
// 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)) {
8070
return 0;
8171
}
82-
else if (!a && a !== 0 && a !== false) {
72+
73+
if (a == void 0) {
8374
return 1;
8475
}
85-
else if (!b && b !== 0 && b !== false) {
86-
return -1;
87-
}
76+
return -1;//b == void 0
8877
}
8978
return null;
9079
};
@@ -102,17 +91,18 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
10291
*/
10392
rowSorter.basicSort = function basicSort(a, b) {
10493
var nulls = rowSorter.handleNulls(a, b);
105-
if ( nulls !== null ) {
94+
if (nulls !== null) {
10695
return nulls;
107-
} else {
108-
if (a === b) {
109-
return 0;
110-
}
111-
if (a < b) {
112-
return -1;
113-
}
114-
return 1;
11596
}
97+
98+
if (a === b) {
99+
return 0;
100+
}
101+
102+
if (a < b) {
103+
return -1;
104+
}
105+
return 1;
116106
};
117107

118108

@@ -127,14 +117,15 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
127117
*/
128118
rowSorter.sortNumber = function sortNumber(a, b) {
129119
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;
135121
};
136122

137123

124+
function parseNumStr(numStr) {
125+
return parseFloat(numStr.replace(/[^0-9.-]/g, ''));
126+
}
127+
128+
138129
/**
139130
* @ngdoc method
140131
* @methodOf ui.grid.class:rowSorter
@@ -147,45 +138,23 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
147138
*/
148139
rowSorter.sortNumberStr = function sortNumberStr(a, b) {
149140
var nulls = rowSorter.handleNulls(a, b);
150-
if ( nulls !== null ) {
141+
if (nulls !== null) {
151142
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+
}
178144

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'
182147

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);
186151

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);
188155
}
156+
157+
return numA - numB;
189158
};
190159

191160

@@ -200,14 +169,13 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
200169
*/
201170
rowSorter.sortAlpha = function sortAlpha(a, b) {
202171
var nulls = rowSorter.handleNulls(a, b);
203-
if ( nulls !== null ) {
172+
if (nulls !== null) {
204173
return nulls;
205-
} else {
206-
var strA = a.toString().toLowerCase(),
207-
strB = b.toString().toLowerCase();
208-
209-
return strA === strB ? 0 : strA.localeCompare(strB);
210174
}
175+
176+
var strA = a.toString().toLowerCase(),
177+
strB = b.toString().toLowerCase();
178+
return strA === strB ? 0 : strA.localeCompare(strB);
211179
};
212180

213181

@@ -223,20 +191,13 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
223191
*/
224192
rowSorter.sortDate = function sortDate(a, b) {
225193
var nulls = rowSorter.handleNulls(a, b);
226-
if ( nulls !== null ) {
194+
if (nulls !== null) {
227195
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);
239196
}
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);
240201
};
241202

242203

@@ -252,20 +213,14 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
252213
*/
253214
rowSorter.sortBool = function sortBool(a, b) {
254215
var nulls = rowSorter.handleNulls(a, b);
255-
if ( nulls !== null ) {
216+
if (nulls !== null) {
256217
return nulls;
257-
} else {
258-
if (a && b) {
259-
return 0;
260-
}
218+
}
261219

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;
268222
}
223+
return a ? 1 : -1;
269224
};
270225

271226

@@ -290,45 +245,38 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
290245
* we might inspect the rows themselves to decide what sort of data might be there
291246
* @returns {function} the sort function chosen for the column
292247
*/
293-
rowSorter.getSortFn = function getSortFn(grid, col, rows) {
294-
var sortFn, item;
295-
248+
rowSorter.getSortFn = function getSortFn(col) {
296249
// See if we already figured out what to use to sort the column and have it in the cache
297250
if (rowSorter.colSortFnCache[col.colDef.name]) {
298-
sortFn = rowSorter.colSortFnCache[col.colDef.name];
251+
return rowSorter.colSortFnCache[col.colDef.name];
299252
}
300253
// 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) {
303255
rowSorter.colSortFnCache[col.colDef.name] = col.sortingAlgorithm;
256+
return col.sortingAlgorithm;
304257
}
305258
// 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;
309262
}
263+
310264
// 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);
314267

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;
325272
}
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;
328277
};
329278

330279

331-
332280
/**
333281
* @ngdoc method
334282
* @methodOf ui.grid.class:rowSorter
@@ -349,26 +297,22 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
349297
return -1;
350298
}
351299
// Equal
352-
else if (a.sort.priority === b.sort.priority) {
300+
if (a.sort.priority === b.sort.priority) {
353301
return 0;
354302
}
355303
// B is higher
356-
else {
357-
return 1;
358-
}
304+
return 1;
359305
}
360306
// Only A has a priority
361-
else if (a.sort && a.sort.priority !== undefined) {
307+
if (a.sort && a.sort.priority !== undefined) {
362308
return -1;
363309
}
364310
// Only B has a priority
365-
else if (b.sort && b.sort.priority !== undefined) {
311+
if (b.sort && b.sort.priority !== undefined) {
366312
return 1;
367313
}
368314
// Neither has a priority
369-
else {
370-
return 0;
371-
}
315+
return 0;
372316
};
373317

374318

@@ -434,34 +378,31 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
434378
var col, direction;
435379

436380
// 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) {
438382
row.entity.$$uiGridIndex = idx;
439-
};
440-
rows.forEach(setIndex);
383+
});
441384

442385
// IE9-11 HACK.... the 'rows' variable would be empty where we call rowSorter.getSortFn(...) below. We have to use a separate reference
443386
// var d = data.slice(0);
444-
var r = rows.slice(0);
387+
//var r = rows.slice(0);
445388

446389
// Now actually sort the data
447390
var rowSortFn = function (rowA, rowB) {
448-
var tem = 0,
449-
idx = 0,
450-
sortFn;
391+
var tem = 0,
392+
idx = 0,
393+
sortFn;
451394

452395
while (tem === 0 && idx < sortCols.length) {
453396
// grab the metadata for the rest of the logic
454397
col = sortCols[idx].col;
455398
direction = sortCols[idx].sort.direction;
456399

457-
sortFn = rowSorter.getSortFn(grid, col, r);
400+
sortFn = rowSorter.getSortFn(col);
458401

459402
// Webpack's compress will hoist and combine propA, propB into one var and break sorting functionality
460403
// Wrapping in function prevents that unexpected behavior
461404
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);
465406

466407
idx++;
467408
}
@@ -475,20 +416,15 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
475416
}
476417

477418
// 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;
483420
};
484421

485422
var newRows = rows.sort(rowSortFn);
486423

487424
// 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+
});
492428

493429
return newRows;
494430
};
@@ -510,4 +446,4 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
510446
return rowSorter;
511447
}]);
512448

513-
})();
449+
})();

0 commit comments

Comments
 (0)