Completed
Pull Request — develop (#236)
by
unknown
55s
created

lib/utils/version.js   B

Complexity

Total Complexity 43
Complexity/F 4.78

Size

Lines of Code 92
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 43
c 1
b 0
f 0
nc 73728
mnd 3
bc 24
fnc 9
dl 0
loc 92
rs 8.3157
bpm 2.6666
cpm 4.7777
noi 2

How to fix   Complexity   

Complexity

Complex classes like lib/utils/version.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
define(function () {
2
  'use strict';
3
4
  /*
5
    reimplate after node-deb-version-compare under MIT
6
    (https://github.com/sdumetz/node-deb-version-compare)
7
  */
8
9
  function Version(v) {
10
    var version = /^[a-zA-Z]?([0-9]*(?=:))?:(.*)/.exec(v);
11
    this.epoch = (version) ? version[1] : 0;
12
    version = (version && version[2]) ? version[2] : v;
13
    version = version.split('-');
14
    this.debian = (version.length > 1) ? version.pop() : '';
15
    this.upstream = version.join('-');
16
  }
17
  Version.prototype.compare = function (b) {
18
    if ((this.epoch > 0 || b.epoch > 0) && Math.sign(this.epoch - b.epoch) !== 0) {
19
      return Math.sign(this.epoch - b.epoch);
20
    }
21
    if (this.compareStrings(this.upstream, b.upstream) !== 0) {
22
      return this.compareStrings(this.upstream, b.upstream);
23
    }
24
    return this.compareStrings(this.debian, b.debian);
25
  };
26
  Version.prototype.charCode = function (c) { // the lower the charcode the lower the version.
27
  // if (c === '~') {return 0;} // tilde sort before anything
28
  // else
29
    if (/[a-zA-Z]/.test(c)) {return c.charCodeAt(0) - 'A'.charCodeAt(0) + 1;} else if (/[.:+-:]/.test(c)) {return c.charCodeAt(0) + 'z'.charCodeAt(0) + 1;} // charcodes are 46..58
30
    return 0;
31
  };
32
33
  // find index of "val" in "ar".
34
  Version.prototype.findIndex = function (ar, fn) {
35
    for (var i = 0; i < ar.length; i++) {
36
      if (fn(ar[i], i)) {
37
        return i;
38
      }
39
    }
40
    return -1;
41
  };
42
43
  Version.prototype.compareChunk = function (a, b) {
44
    var ca = a.split('');
45
    var cb = b.split('');
46
    var diff = this.findIndex(ca, function (c, index) {
47
      if (cb[index] && c === cb[index]) return false;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
48
      return true;
49
    });
50
    if (diff === -1) {
51
      if (cb.length > ca.length) {
52
        if (cb[ca.length] === '~') {
53
          return 1;
54
        }
55
        return -1;
56
      }
57
      return 0; // no diff found and same length
58
    } else if (!cb[diff]) {
59
      return (ca[diff] === '~') ? -1 : 1;
60
    }
61
    return (this.charCode(ca[diff]) > this.charCode(cb[diff])) ? 1 : -1;
62
  };
63
64
  Version.prototype.compareStrings = function (a, b) {
65
    if (a === b) return 0;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
66
    var parseA = /([^0-9]+|[0-9]+)/g;
67
    var parseB = /([^0-9]+|[0-9]+)/g;
68
    var ra = parseA.exec(a);
69
    var rb = parseB.exec(b);
70
    while (ra !== null && rb !== null ) {
71
      if ((isNaN(ra[1]) || isNaN(rb[1])) && ra[1] !== rb[1] ) { // a or b is not a number and they're not equal. Note : "" IS a number so both null is impossible
72
        return this.compareChunk(ra[1], rb[1]);
73
      } // both are numbers
74
      if (ra[1] !== rb[1]) {
75
        return (parseInt(ra[1], 10) > parseInt(rb[1], 10)) ? 1 : -1;
76
      }
77
      ra = parseA.exec(a);
78
      rb = parseB.exec(b);
79
    }
80
    if (!ra && rb) { // rb doesn't get exec-ed when ra == null
81
      return (parseB.exec(b)[1].split('')[0] === '~') ? 1 : -1;
82
    } else if (ra && !rb) {
83
      return (ra[1].split('')[0] === '~') ? -1 : 1;
84
    }
85
    return 0;
86
  };
87
  return function compare(a, b) {
88
    var va = new Version(a[0]);
89
    var vb = new Version(b[0]);
90
    return vb.compare(va);
91
  };
92
});
93