]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - js/datatables-natural-sort.js
Add a `Plugins` overview card
[irc/unrealircd/unrealircd-webpanel.git] / js / datatables-natural-sort.js
1 /*! © SpryMedia Ltd, Jim Palmer, Michael Buehler, Mike Grier, Clint Priest, Kyle Adams, guillermo - datatables.net/license */
2
3 (function( factory ){
4 if ( typeof define === 'function' && define.amd ) {
5 // AMD
6 define( ['jquery', 'datatables.net'], function ( $ ) {
7 return factory( $, window, document );
8 } );
9 }
10 else if ( typeof exports === 'object' ) {
11 // CommonJS
12 var jq = require('jquery');
13 var cjsRequires = function (root, $) {
14 if ( ! $.fn.dataTable ) {
15 require('datatables.net')(root, $);
16 }
17 };
18
19 if (typeof window !== 'undefined') {
20 module.exports = function (root, $) {
21 if ( ! root ) {
22 // CommonJS environments without a window global must pass a
23 // root. This will give an error otherwise
24 root = window;
25 }
26
27 if ( ! $ ) {
28 $ = jq( root );
29 }
30
31 cjsRequires( root, $ );
32 return factory( $, root, root.document );
33 };
34 }
35 else {
36 cjsRequires( window, jq );
37 module.exports = factory( jq, window, window.document );
38 }
39 }
40 else {
41 // Browser
42 factory( jQuery, window, document );
43 }
44 }(function( $, window, document, undefined ) {
45 'use strict';
46 var DataTable = $.fn.dataTable;
47
48
49 /**
50 * Data can often be a complicated mix of numbers and letters (file names
51 * are a common example) and sorting them in a natural manner is quite a
52 * difficult problem.
53 *
54 * Fortunately a deal of work has already been done in this area by other
55 * authors - the following plug-in uses the [naturalSort() function by Jim
56 * Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) to provide natural sorting in DataTables.
57 *
58 * @name Natural sorting
59 * @summary Sort data with a mix of numbers and letters _naturally_.
60 * @author [Jim Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support)
61 * @author [Michael Buehler] (https://github.com/AnimusMachina)
62 *
63 * @example
64 * $('#example').dataTable( {
65 * columnDefs: [
66 * { type: 'natural', targets: 0 }
67 * ]
68 * } );
69 *
70 * Html can be stripped from sorting by using 'natural-nohtml' such as
71 *
72 * $('#example').dataTable( {
73 * columnDefs: [
74 * { type: 'natural-nohtml', targets: 0 }
75 * ]
76 * } );
77 *
78 */
79 /*
80 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
81 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
82 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
83 * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
84 */
85 function naturalSort(a, b, html) {
86 var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?%?$|^0x[0-9a-f]+$|[0-9]+)/gi, sre = /(^[ ]*|[ ]*$)/g, dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, hre = /^0x[0-9a-f]+$/i, ore = /^0/, htmre = /(<([^>]+)>)/gi,
87 // convert all to strings and trim()
88 x = a.toString().replace(sre, '') || '', y = b.toString().replace(sre, '') || '';
89 // remove html from strings if desired
90 if (!html) {
91 x = x.replace(htmre, '');
92 y = y.replace(htmre, '');
93 }
94 // chunk/tokenize
95 var xN = x
96 .replace(re, '\0$1\0')
97 .replace(/\0$/, '')
98 .replace(/^\0/, '')
99 .split('\0'), yN = y
100 .replace(re, '\0$1\0')
101 .replace(/\0$/, '')
102 .replace(/^\0/, '')
103 .split('\0'),
104 // numeric, hex or date detection
105 xD = parseInt(x.match(hre), 10) ||
106 (xN.length !== 1 && x.match(dre) && Date.parse(x)), yD = parseInt(y.match(hre), 10) ||
107 (xD && y.match(dre) && Date.parse(y)) ||
108 null;
109 // first try and sort Hex codes or Dates
110 if (yD) {
111 if (xD < yD) {
112 return -1;
113 }
114 else if (xD > yD) {
115 return 1;
116 }
117 }
118 // natural sorting through split numeric strings and default strings
119 for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
120 // find floats not starting with '0', string or 0 if not defined (Clint Priest)
121 var oFxNcL = (!(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc])) || xN[cLoc] || 0;
122 var oFyNcL = (!(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc])) || yN[cLoc] || 0;
123 // handle numeric vs string comparison - number < string - (Kyle Adams)
124 if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {
125 return isNaN(oFxNcL) ? 1 : -1;
126 }
127 // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
128 else if (typeof oFxNcL !== typeof oFyNcL) {
129 oFxNcL += '';
130 oFyNcL += '';
131 }
132 if (oFxNcL < oFyNcL) {
133 return -1;
134 }
135 if (oFxNcL > oFyNcL) {
136 return 1;
137 }
138 }
139 return 0;
140 }
141 DataTable.ext.order['natural-asc'] = function (a, b) {
142 return naturalSort(a, b, true);
143 };
144 DataTable.ext.order['natural-desc'] = function (a, b) {
145 return naturalSort(a, b, true) * -1;
146 };
147 DataTable.ext.order['natural-nohtml-asc'] = function (a, b) {
148 return naturalSort(a, b, false);
149 };
150 DataTable.ext.order['natural-nohtml-asc'] = function (a, b) {
151 return naturalSort(a, b, false) * -1;
152 };
153 DataTable.ext.order['natural-ci-asc'] = function (a, b) {
154 a = a.toString().toLowerCase();
155 b = b.toString().toLowerCase();
156 return naturalSort(a, b, true);
157 };
158 DataTable.ext.order['natural-ci-asc'] = function (a, b) {
159 a = a.toString().toLowerCase();
160 b = b.toString().toLowerCase();
161 return naturalSort(a, b, true) * -1;
162 };
163
164
165 return DataTable;
166 }));