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 || Number.NaN; |
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
|
|
|
function parseMixedSeparators() { |
83
|
|
|
// format is using dot and comma |
84
|
|
|
|
85
|
|
|
// last dot position |
86
|
68 |
|
lDotPos = string.lastIndexOf('.'); |
87
|
|
|
// last comma position |
88
|
68 |
|
lCommaPos = string.lastIndexOf(','); |
89
|
|
|
|
90
|
|
|
// order of 1st dot -> comma must be same as last dot -> comma |
91
|
|
|
// 123.123.123,123 -> ok 123.123,123.123 -> not ok |
92
|
68 |
|
if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) { |
93
|
3 |
|
return Number.NaN; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// check positions to guess the thousands separator |
97
|
65 |
|
if (dotPos > commaPos) { |
98
|
57 |
|
if (dotCount > 1) { |
99
|
1 |
|
return Number.NaN; |
100
|
|
|
} |
101
|
|
|
// best guess: . is thousands separator and , is decimal point |
102
|
56 |
|
return toFloatFormat(string, ',', '.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
8 |
|
if (commaCount > 1) { |
106
|
1 |
|
return Number.NaN; |
107
|
|
|
} |
108
|
|
|
// best guess: , is thousands separator and . is decimal point |
109
|
7 |
|
return toFloatFormat(string, '.', ','); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
function parseSingleSeparators() { |
113
|
170 |
|
if (dotPos !== -1) { |
114
|
|
|
// only dot(s) in format |
115
|
85 |
|
return parseParts(string, '.', dotCount); |
116
|
|
|
} |
117
|
|
|
|
118
|
85 |
|
if (commaPos !== -1) { |
119
|
|
|
// only comma(s) in format |
120
|
80 |
|
return parseParts(string, ',', commaCount); |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
return toFloatFormat(string); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
function precheckFormat() { |
127
|
|
|
// only combination of 2 separators allowed |
128
|
261 |
|
if (dotCount > 0 && commaCount > 0 && spaceCount > 0) { |
129
|
1 |
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// if there is any separator (space, comma, dot) found more than once, |
133
|
|
|
// all other must not be found more than once |
134
|
260 |
|
if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) { |
135
|
4 |
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
256 |
|
return !(commaCount > 1 && spaceCount > 1); |
139
|
|
|
} |
140
|
|
|
|
141
|
279 |
|
string = string.trim(); |
142
|
|
|
|
143
|
|
|
// 1st dot position |
144
|
279 |
|
dotPos = string.indexOf('.'); |
145
|
|
|
// 1st comma position |
146
|
279 |
|
commaPos = string.indexOf(','); |
147
|
|
|
// 1st space position |
148
|
279 |
|
spacePos = string.indexOf(' '); |
149
|
|
|
|
150
|
279 |
|
if (dotPos + commaPos + spacePos === -3) { |
151
|
|
|
// life is good, no separators |
152
|
18 |
|
return toFloatFormat(string); |
153
|
|
|
} |
154
|
|
|
|
155
|
261 |
|
spaceSplit = string.split(' '); |
156
|
261 |
|
spaceCount = spaceSplit.length - 1; |
157
|
261 |
|
dotCount = string.split('.').length - 1; |
158
|
261 |
|
commaCount = string.split(',').length - 1; |
159
|
|
|
|
160
|
261 |
|
if (!precheckFormat()) { |
161
|
6 |
|
return Number.NaN; |
162
|
|
|
} |
163
|
|
|
|
164
|
255 |
|
if (spaceCount > 0) { |
165
|
72 |
|
if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) { |
166
|
17 |
|
return Number.NaN; |
167
|
|
|
} |
168
|
55 |
|
string = spaceSplit.join(''); |
169
|
|
|
} |
170
|
|
|
|
171
|
238 |
|
if (dotPos !== -1 && commaPos !== -1) { |
172
|
68 |
|
return parseMixedSeparators(); |
173
|
|
|
} |
174
|
|
|
|
175
|
170 |
|
return parseSingleSeparators(); |
176
|
|
|
}; |
177
|
|
|
|
178
|
279 |
|
return parse(str); |
179
|
|
|
}; |
180
|
|
|
|
181
|
2 |
|
if (typeof exports !== 'undefined') { |
182
|
4 |
|
if (typeof module !== 'undefined' && module.exports) { |
183
|
1 |
|
exports = module.exports = floatify; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|