GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch decomplexify (a2b2c6)
by Oliver
02:20
created

src/floatify.js   A

Complexity

Total Complexity 39
Complexity/F 6.5

Size

Lines of Code 173
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 92
c 4
b 0
f 0
nc 3
dl 0
loc 173
ccs 78
cts 78
cp 1
crap 3
rs 9.28
wmc 39
mnd 3
bc 31
fnc 6
bpm 5.1666
cpm 6.5
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B floatify.js ➔ floatify 0 165 1
1
'use strict';
2
3 1
var floatify = function floatify(str) {
4 279
  var toFloatFormat = function toFloatFormat(str, ts, ds) {
5 229
    var string = str;
6 229
    var decimalSeparator = ds || '';
7
8 229
    string = string.split(ts || '').join('');
9 229
    if (decimalSeparator !== '') {
10 195
      string = string.split(decimalSeparator).join('.');
11
    }
12
13 229
    return parseFloat(string);
14
  };
15
16 279
  var parseParts = function parseParts(str, ele, count) {
17 165
    var string = str;
18 165
    var element = ele;
19 165
    var parts = string.split(element);
20
21
    function parseMidPart() {
22 25
      if (current.length !== 3) {
23 16
        return Number.NaN;
24
      }
25
26 9
      if (left.length > 3) {
27 2
        return Number.NaN;
28
      }
29
30
      // no decision, continue
31 7
      return null;
32
    }
33
34
    function parseEndPart() {
35 147
      if ((leftVal === 0 || isNaN(leftVal) || left.length > 3)) {
36 94
        return toFloatFormat(string, '', element);
37
      }
38
39 53
      if (current.length === 3) {
40 11
        return toFloatFormat(string, element, '');
41
      }
42
43 42
      if (count === 1) {
44 38
        return toFloatFormat(string, '', element);
45
      }
46
47 4
      return Number.NaN;
48
    }
49
50 165
    for (var i = 1; i < parts.length; i++) {
51 172
      var current = parts[i];
52 172
      var left = parts[i - 1];
53 172
      var leftVal = parseInt(left, 10);
54 172
      var isLast = parts.length - 1 === i;
55
      var parseResult;
56
57 172
      if (!isLast) {
58 25
        parseResult = parseMidPart();
59
      } else {
60 147
        parseResult = parseEndPart();
61
      }
62
63 172
      if (parseResult !== null) {
64 165
        break;
65
      }
66
    }
67 165
    return parseResult;
0 ignored issues
show
Bug introduced by
The variable parseResult seems to not be initialized for all possible execution paths.
Loading history...
68
  };
69
70 279
  var parse = function parse(str) {
71 279
    var string = str;
72
    var spacePos;
73
    var spaceSplit;
74
    var spaceCount;
75
    var dotPos;
76
    var commaPos;
77
    var lDotPos;
78
    var lCommaPos;
79
    var dotCount;
80
    var commaCount;
81
82 279
    string = string.trim();
83
84
    // 1st dot position
85 279
    dotPos = string.indexOf('.');
86
    // 1st comma position
87 279
    commaPos = string.indexOf(',');
88
    // 1st space position
89 279
    spacePos = string.indexOf(' ');
90
91 279
    if (dotPos + commaPos + spacePos === -3) {
92
      // life is good, no separators
93 18
      return toFloatFormat(string);
94
    }
95
96 261
    spaceSplit = string.split(' ');
97 261
    spaceCount = spaceSplit.length - 1;
98 261
    dotCount = string.split('.').length - 1;
99 261
    commaCount = string.split(',').length - 1;
100
101
    // only combination of 2 separators allowed
102 261
    if (dotCount > 0 && commaCount > 0 && spaceCount > 0) {
103 1
      return Number.NaN;
104
    }
105
106
    // if there is any separator (space, comma, dot) found more than once,
107
    // all other must not be found more than once
108 260
    if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) {
109 4
      return Number.NaN;
110
    }
111
112 256
    if (commaCount > 1 && spaceCount > 1) {
113 1
      return Number.NaN;
114
    }
115
116 255
    if (spaceCount > 0) {
117 72
      if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) {
118 17
        return Number.NaN;
119
      }
120 55
      string = spaceSplit.join('');
121
    }
122
123 238
    if (dotPos !== -1 && commaPos !== -1) {
124
      // format is using dot and comma
125
126
      // last dot position
127 68
      lDotPos = string.lastIndexOf('.');
128
      // last comma position
129 68
      lCommaPos = string.lastIndexOf(',');
130
131
      // order of 1st dot -> comma must be same as last dot -> comma
132
      // 123.123.123,123 -> ok 123.123,123.123 -> not ok
133 68
      if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) {
134 3
        return Number.NaN;
135
      }
136
137
      // check positions to guess the thousands separator
138 65
      if (dotPos > commaPos) {
139 57
        if (dotCount > 1) {
140 1
          return Number.NaN;
141
        }
142
        // best guess: . is thousands separator and , is decimal point
143 56
        return toFloatFormat(string, ',', '.');
144
      }
145
146 8
      if (commaCount > 1) {
147 1
        return Number.NaN;
148
      }
149
      // best guess: , is thousands separator and . is decimal point
150 7
      return toFloatFormat(string, '.', ',');
151
    }
152
153 170
    if (dotPos !== -1) {
154
      // only dot(s) in format
155 85
      return parseParts(string, '.', dotCount);
156
    }
157
158 85
    if (commaPos !== -1) {
159
      // only comma(s) in format
160 80
      return parseParts(string, ',', commaCount);
161
    }
162
163 5
    return toFloatFormat(string);
164
  };
165
166 279
  return parse(str);
167
};
168
169 2
if (typeof exports !== 'undefined') {
170 4
  if (typeof module !== 'undefined' && module.exports) {
171 1
    exports = module.exports = floatify;
172
  }
173
}
174