|
16 | 16 | : define |
17 | 17 | ) |
18 | 18 | /*define*/([], function factory() { |
19 | | - |
20 | | -function naturalSort (a, b) { |
21 | 19 | var re = /(^([+\-]?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?(?=\D|\s|$))|^0x[\da-fA-F]+$|\d+)/g, |
22 | 20 | sre = /^\s+|\s+$/g, // trim pre-post whitespace |
23 | 21 | snre = /\s+/g, // normalize all whitespace to single ' ' character |
24 | 22 | dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, |
25 | 23 | hre = /^0x[0-9a-f]+$/i, |
26 | 24 | ore = /^0/, |
| 25 | + b0re = /^\0/, |
| 26 | + e0re = /\0$/, |
27 | 27 | i = function(s) { |
28 | 28 | return (naturalSort.insensitive && ('' + s).toLowerCase() || '' + s).replace(sre, ''); |
29 | 29 | }, |
| 30 | + normChunk = function(s, l) { |
| 31 | + // normalize spaces; find floats not starting with '0', string or 0 if not defined (Clint Priest) |
| 32 | + return (!s.match(ore) || l == 1) && parseFloat(s) || s.replace(snre, ' ').replace(sre, '') || 0; |
| 33 | + }; |
| 34 | + |
| 35 | +function naturalSort (a, b) { |
30 | 36 | // convert all to strings strip whitespace |
31 | | - x = i(a), |
32 | | - y = i(b), |
| 37 | + var x = i(a) || '', |
| 38 | + y = i(b) || '', |
33 | 39 | // chunk/tokenize |
34 | | - xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), |
35 | | - yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), |
| 40 | + xN = x.replace(re, '\0$1\0').replace(e0re,'').replace(b0re,'').split('\0'), |
| 41 | + yN = y.replace(re, '\0$1\0').replace(e0re,'').replace(b0re,'').split('\0'), |
36 | 42 | // numeric, hex or date detection |
37 | 43 | xD = parseInt(x.match(hre), 16) || (xN.length !== 1 && Date.parse(x)), |
38 | 44 | yD = parseInt(y.match(hre), 16) || xD && y.match(dre) && Date.parse(y) || null, |
39 | | - normChunk = function(s, l) { |
40 | | - // normalize spaces; find floats not starting with '0', string or 0 if not defined (Clint Priest) |
41 | | - return (!s.match(ore) || l == 1) && parseFloat(s) || s.replace(snre, ' ').replace(sre, '') || 0; |
42 | | - }, |
43 | 45 | oFxNcL, oFyNcL; |
44 | 46 | // first try and sort Hex codes or Dates |
45 | 47 | if (yD) { |
|
0 commit comments