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

version.js ➔ define   B

Complexity

Conditions 1
Paths 4608

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 4608
dl 0
loc 92
rs 8.491
nop 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
C version.js ➔ ... ➔ Version.compareStrings 0 23 15
A version.js ➔ ... ➔ Version.charCode 0 6 3
B version.js ➔ ... ➔ Version.compare 0 9 5
B version.js ➔ ... ➔ Version 0 8 5
B version.js ➔ ... ➔ Version.compareChunk 0 20 7
A version.js ➔ ... ➔ compare 0 5 1
A version.js ➔ ... ➔ Version.findIndex 0 8 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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