forked from pocketrocket/floatify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatify.js
More file actions
156 lines (126 loc) · 4.13 KB
/
floatify.js
File metadata and controls
156 lines (126 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
var floatify = function floatify(str) {
var toFloatFormat = function toFloatFormat(str, ts, ds) {
var string = str;
var thousandsSeperator = ts || '';
var decimalSeperator = ds || '';
thousandsSeperator = thousandsSeperator === '.' ? '\\.' : thousandsSeperator;
decimalSeperator = decimalSeperator === '.' ? '\\.' : decimalSeperator;
string = thousandsSeperator !== '' ? string.replace(new RegExp(thousandsSeperator, 'g'), '') : string;
string = decimalSeperator !== '' ? string.replace(new RegExp(decimalSeperator, 'g'), '.') : string;
return parseFloat(string);
};
var parseParts = function parseParts(str, ele, count) {
var string = str;
var element = ele;
var parts = string.split(element);
for (var i = 1; i < parts.length; i++) {
var left = parseInt(parts[i - 1], 10);
if (parts[i].length === 0) {
return Number.NaN;
} else if (parts[i].length === 3) {
if (parts[i - 1].length > 3 && parts.length - 1 !== i) {
return Number.NaN;
}
if (
(left === 0 || isNaN(left) || parts[i - 1].length > 3)
&& parts.length - 1 === i
) {
return toFloatFormat(string, '', element);
}
} else if (i < parts.length - 1) {
// violation in midPart -> NaN
return Number.NaN;
} else {
// violation in end -> Could be decimalSeparator
if (count === 1) {
return toFloatFormat(string, '', element);
}
return Number.NaN;
}
}
return toFloatFormat(string, element, '');
};
var parse = function parse(str) {
var string = str;
var spacePos;
var spaceSplit;
var spaceCount;
var dotPos;
var commaPos;
var lDotPos;
var lCommaPos;
var dotCount;
var commaCount;
string = string.trim();
// 1st dot position
dotPos = string.indexOf('.');
// 1st comma position
commaPos = string.indexOf(',');
// 1st space position
spacePos = string.indexOf(' ');
if (dotPos + commaPos + spacePos === -3) {
// life is good, no separators
return toFloatFormat(string);
}
// space count
spaceSplit = string.split(' ');
spaceCount = spaceSplit.length - 1;
if (spaceCount > 0) {
var first = spaceSplit.shift();
var last = spaceSplit.pop();
if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) {
return Number.NaN;
}
if (dotPos > -1 && commaPos > -1) {
// fomat mixed with space, comma and dot is invalid
return Number.NaN;
}
spaceSplit.unshift(first);
spaceSplit.push(last);
string = spaceSplit.join('');
}
// dot count
dotCount = string.split('.').length - 1;
// comma count
commaCount = string.split(',').length - 1;
if (dotPos !== -1 && commaPos !== -1) {
// format is using dot and comma
// last dot position
lDotPos = string.lastIndexOf('.');
// last comma position
lCommaPos = string.lastIndexOf(',');
// order of 1st dot -> comma must be same as last dot -> comma
// 123.123.123,123 -> ok 123.123,123.123 -> not ok
if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) {
return Number.NaN;
}
// check positions to guess the thousands separator
if (dotPos > commaPos && dotCount === 1) {
// best guess: . is thousands separator and , is decimal point
return toFloatFormat(string, ',', '.');
}
if (commaPos > dotPos && commaCount === 1) {
// best guess: , is thousands separator and . is decimal point
return toFloatFormat(string, '.', ',');
}
return Number.NaN;
}
if (dotPos !== -1) {
// only dot(s) in format
return parseParts(string, '.', dotCount);
}
if (commaPos !== -1) {
// only comma(s) in format
return parseParts(string, ',', commaCount);
}
return toFloatFormat(string);
};
return parse(str);
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = floatify;
}
exports.floatify = floatify;
}