This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
100 lines (82 loc) · 2.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*!
* natural-sort.js
* ===============
* Sorting with support for numbers, dates, unicode and more.
*
* http://github.com/studio-b12/natural-sort
* MIT License, © Studio B12 GmbH 2014
*
*//*
*
* Idea by Dave Koelle
* Original implementation by Jim Palmer
* Modified by Tomek Wiszniewski
*
*/
var naturalSort = function naturalSort (options) { 'use strict';
if (!options) options = {};
return function(a, b) {
var EQUAL = 0;
var GREATER = (options.direction == 'desc' ?
-1 :
1
);
var SMALLER = -GREATER;
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi;
var sre = /(^[ ]*|[ ]*$)/g;
var dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/;
var hre = /^0x[0-9a-f]+$/i;
var ore = /^0/;
var normalize = function normalize (value) {
var string = '' + value;
return (options.caseSensitive ?
string :
string.toLowerCase()
);
};
// Normalize values to strings
var x = normalize(a).replace(sre, '') || '';
var y = normalize(b).replace(sre, '') || '';
// chunk/tokenize
var xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0');
var yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0');
// Return immediately if at least one of the values is empty.
if (!x && !y) return EQUAL;
if (!x && y) return GREATER;
if ( x && !y) return SMALLER;
// numeric, hex or date detection
var xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x));
var yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
var oFxNcL, oFyNcL;
// first try and sort Hex codes or Dates
if (yD) {
if ( xD < yD ) return SMALLER;
else if ( xD > yD ) return GREATER;
}
// natural sorting through split numeric strings and default strings
for (var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
// find floats not starting with '0', string or 0 if not defined (Clint Priest)
oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
// handle numeric vs string comparison - number < string - (Kyle Adams)
if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? GREATER : SMALLER;
// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
else if (typeof oFxNcL !== typeof oFyNcL) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL < oFyNcL) return SMALLER;
if (oFxNcL > oFyNcL) return GREATER;
}
return EQUAL;
};
};
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else {
root.naturalSort = factory();
}
}(this, function () {
return naturalSort;
}));