[tor-commits] [atlas/master] Adds IPv6 support to datatables IP address sort

irl at torproject.org irl at torproject.org
Fri Jan 5 16:43:51 UTC 2018


commit 365d0d733eae2b2677c75fde560c1db447971194
Author: Iain R. Learmonth <irl at fsfe.org>
Date:   Fri Jan 5 14:31:29 2018 +0000

    Adds IPv6 support to datatables IP address sort
---
 js/libs/datatables/dataTables.Sorting.js | 138 ++++++++++++++++++++-----------
 1 file changed, 89 insertions(+), 49 deletions(-)

diff --git a/js/libs/datatables/dataTables.Sorting.js b/js/libs/datatables/dataTables.Sorting.js
index ba77fdd..eeea7a0 100644
--- a/js/libs/datatables/dataTables.Sorting.js
+++ b/js/libs/datatables/dataTables.Sorting.js
@@ -56,67 +56,107 @@ var loadSortingExtensions = function(){
             return ((x < y) ?  1 : ((x > y) ? -1 : 0));
         }
     } );
-
     /**
-     * Sorts a column containing IP addresses in typical dot notation. This can 
-     * be most useful when using DataTables for a networking application, and 
-     * reporting information containing IP address. Also has a matching type 
-     * detection plug-in for automatic type detection.
-     *  @name IP addresses 
+     * Sorts a column containing IP addresses (IPv4 and IPv6) in typical dot
+     * notation / colon. This can be most useful when using DataTables for a
+     * networking application, and reporting information containing IP address.
+     *
+     *  @name IP addresses
+     *  @summary Sort IP addresses numerically
+     *  @author Dominique Fournier
      *  @author Brad Wasson
+     *
+     *  @example
+     *    $('#example').dataTable( {
+     *       columnDefs: [
+     *         { type: 'ip-address', targets: 0 }
+     *       ]
+     *    } );
      */
 
     jQuery.extend( jQuery.fn.dataTableExt.oSort, {
-        "ip-address-asc": function ( a, b ) {
-            var m = a.split("."), x = "";
-            var n = b.split("."), y = "";
-            for(var i = 0; i < m.length; i++) {
-                var item = m[i];
-                if(item.length == 1) {
-                    x += "00" + item;
-                } else if(item.length == 2) {
-                    x += "0" + item;
-                } else {
-                    x += item;
+        "ip-address-pre": function ( a ) {
+            var i, item;
+            var m = a.split("."),
+                n = a.split(":"),
+                x = "",
+                xa = "";
+
+            if (m.length == 4) {
+                // IPV4
+                for(i = 0; i < m.length; i++) {
+                    item = m[i];
+
+                    if(item.length == 1) {
+                        x += "00" + item;
+                    }
+                    else if(item.length == 2) {
+                        x += "0" + item;
+                    }
+                    else {
+                        x += item;
+                    }
                 }
             }
-            for(var i = 0; i < n.length; i++) {
-                var item = n[i];
-                if(item.length == 1) {
-                    y += "00" + item;
-                } else if(item.length == 2) {
-                    y += "0" + item;
-                } else {
-                    y += item;
+            else if (n.length > 0) {
+                // IPV6
+                var count = 0;
+                for(i = 0; i < n.length; i++) {
+                    item = n[i];
+
+                    if (i > 0) {
+                        xa += ":";
+                    }
+
+                    if(item.length === 0) {
+                        count += 0;
+                    }
+                    else if(item.length == 1) {
+                        xa += "000" + item;
+                        count += 4;
+                    }
+                    else if(item.length == 2) {
+                        xa += "00" + item;
+                        count += 4;
+                    }
+                    else if(item.length == 3) {
+                        xa += "0" + item;
+                        count += 4;
+                    }
+                    else {
+                        xa += item;
+                        count += 4;
+                    }
+                }
+
+                // Padding the ::
+                n = xa.split(":");
+                var paddDone = 0;
+
+                for (i = 0; i < n.length; i++) {
+                    item = n[i];
+
+                    if (item.length === 0 && paddDone === 0) {
+                        for (var padding = 0 ; padding < (32-count) ; padding++) {
+                            x += "0";
+                            paddDone = 1;
+                        }
+                    }
+                    else {
+                        x += item;
+                    }
                 }
             }
-            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
+
+            return x;
+        },
+
+        "ip-address-asc": function ( a, b ) {
+            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
         },
 
         "ip-address-desc": function ( a, b ) {
-            var m = a.split("."), x = "";
-            var n = b.split("."), y = "";
-            for(var i = 0; i < m.length; i++) {
-                var item = m[i];
-                if(item.length == 1) {
-                    x += "00" + item;
-                } else if (item.length == 2) {
-                    x += "0" + item;
-                } else {
-                    x += item;
-                }
-            }
-            for(var i = 0; i < n.length; i++) {
-                var item = n[i];
-                if(item.length == 1) {
-                    y += "00" + item;
-                } else if (item.length == 2) {
-                    y += "0" + item;
-                } else {
-                    y += item;
-                }
-            }
-            return ((x < y) ? 1 : ((x > y) ? -1 : 0));
+            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
         }
     } );
 }
\ No newline at end of file





More information about the tor-commits mailing list