Browse Source

proportions: sort firmware based on version

Nils Schneider 8 years ago
parent
commit
aeae866998
2 changed files with 63 additions and 3 deletions
  1. 3 3
      lib/proportions.js
  2. 60 0
      lib/vercomp.js

+ 3 - 3
lib/proportions.js

@@ -1,5 +1,5 @@
-define(["chroma-js", "virtual-dom", "numeral-intl"],
-  function (Chroma, V, numeral) {
+define(["chroma-js", "virtual-dom", "numeral-intl", "vercomp" ],
+  function (Chroma, V, numeral, vercomp) {
 
   return function (config) {
     var self = this
@@ -128,7 +128,7 @@ define(["chroma-js", "virtual-dom", "numeral-intl"],
       })
 
       fillTable(statusTable, statusDict.sort(function (a, b) { return b[1] - a[1] }))
-      fillTable(fwTable, fwDict.sort(function (a, b) { return b[1] - a[1] }))
+      fillTable(fwTable, fwDict.sort(function (a, b) { return vercomp(b[0], a[0]) }))
       fillTable(hwTable, hwDict.sort(function (a, b) { return b[1] - a[1] }))
       fillTable(geoTable, geoDict.sort(function (a, b) { return b[1] - a[1] }))
       fillTable(autoTable, autoDict.sort(function (a, b) { return b[1] - a[1] }))

+ 60 - 0
lib/vercomp.js

@@ -0,0 +1,60 @@
+define([], function () {
+  function order(c) {
+    if (/^\d$/.test(c))
+      return 0
+    else if (/^[a-z]$/i.test(c))
+      return c.charCodeAt(0)
+    else if (c === "~")
+      return -1
+    else if (c)
+      return c.charCodeAt(0) + 256
+    else
+      return 0
+  }
+
+  // Based on dpkg code
+  function vercomp(a, b) {
+    var apos = 0, bpos = 0
+    while (apos < a.length || bpos < b.length) {
+      var firstDiff = 0
+
+      while ((apos < a.length && !/^\d$/.test(a[apos])) || (bpos < b.length && !/^\d$/.test(b[bpos]))) {
+        var ac = order(a[apos])
+        var bc = order(b[bpos])
+
+        if (ac !== bc)
+          return ac - bc
+
+        apos++
+        bpos++
+      }
+
+      while (a[apos] === "0")
+        apos++
+
+      while (b[bpos] === "0")
+        bpos++
+
+      while (/^\d$/.test(a[apos]) && /^\d$/.test(b[bpos])) {
+        if (firstDiff === 0)
+          firstDiff = a.charCodeAt(apos) - b.charCodeAt(bpos)
+
+        apos++
+        bpos++
+      }
+
+      if (/^\d$/.test(a[apos]))
+        return 1
+
+      if (/^\d$/.test(b[bpos]))
+        return -1
+
+      if (firstDiff !== 0)
+        return firstDiff
+    }
+
+    return 0
+  }
+
+  return vercomp
+})