DataTypesUtils.js
6.48 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
** This file is part of JSDataChecker.
**
** JSDataChecker is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** JSDataChecker is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with JSDataChecker. If not, see <http://www.gnu.org/licenses/>.
**
** Copyright (C) 2016 JSDataChecker - Donato Pirozzi (donatopirozzi@gmail.com)
** Distributed under the GNU GPL v3. For full terms see the file LICENSE.
** License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
**/
function DataTypesUtils() {}
DataTypesUtils.FilterTime = function (value) {
var expTime = /^[0-9]{2}:[0-9]{2}(:[0-9]{2})?(\+[0-9]{2}:[0-9]{2})?$/;
if (expTime.test(value) == false) return null;
var splitted = value.split(/[:|\+]/);
var expNumber = /^[0-9]{2}$/;
var HH = expNumber.test(splitted[0]) ? parseInt(splitted[0]) : 0;
var MM = expNumber.test(splitted[1]) ? parseInt(splitted[1]) : 0;
var SS = splitted.length >=3 && expNumber.test(splitted[2]) ? parseInt(splitted[2]) : 0;
var dt = new Date();
dt.setHours(HH);
dt.setMinutes(MM);
dt.setSeconds(SS);
return dt;
}//EndFunction.
DataTypesUtils.FilterDateTime = function (value) {
var _dtSplitted = value.split(/[T|\s]/);
if (_dtSplitted.length == 2) {
var dtTime = DataTypesUtils.FilterTime(_dtSplitted[1]);
if (dtTime == null) return null;
var dtDateTime = DataTypesUtils.FilterDate(_dtSplitted[0], dtTime);
return dtDateTime;
} else {
var dtDate = DataTypesUtils.FilterDate(value);
if (dtDate != null) return dtDate;
var dtTime = DataTypesUtils.FilterTime(value);
return dtTime;
}
}//EndFunction.
DataTypesUtils.FilterDate = function (value, dtDate) {
if (dtDate == null) dtDate = new Date();
//year-month.
if (/^[0-9][0-9][0-9][0-9]\-[0-9][0-9]$/.test(value)) {
var year = parseInt(value.substring(0, 4));
var month = parseInt(value.substring(5));
dtDate.setYear(year);
dtDate.setMonth(month);
return dtDate;
}
var expDate = /^[0-9]{4}(\-|\/)[0-9]{2}((\-|\/)[0-9]{2})?$/;
if (expDate.test(value)) {
var splitted = value.split(/[\-|\/]/);
var year = parseInt(splitted[0]);
var month = parseInt(splitted[1]);
var day = splitted.length == 3 ? parseInt(splitted[2]) : 0;
dtDate.setYear(year);
dtDate.setMonth(month);
dtDate.setDate(day);
return dtDate;
}
expDate = /^[0-9]{2}(\-|\/)[0-9]{2}(\-|\/)[0-9]{4}$/;
if (expDate.test(value)) {
var splitted = value.split(/[\-|\/]/);
var year = parseInt(splitted[2]);
var month = parseInt(splitted[1]);
var day = parseInt(splitted[0]);
dtDate.setYear(year);
dtDate.setMonth(month);
dtDate.setDate(day);
return dtDate;
}
return null;
};//EndFunction.
/**
* Converts the value in a number, NaN if it is not a number.
* @param value
* @returns {*}
*/
DataTypesUtils.FilterFloat = function (value) {
if(/^(\-|\+)?((0|([1-9][0-9]*))(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
};//EndFunction.
DataTypesUtils.FilterNumber = function (value) {
//Check immediatly if it is a classical number.
var valnum = DataTypesUtils.FilterFloat(value);
if (isNaN(valnum) == false) return valnum;
//Checks if the value is a string.
if (typeof value !== "string")
return NaN;
var parts = value.split(/(,|\.)/g);
//Find the smallest symbol.
var idxDot = { idx: value.indexOf('.'), sym: '.' };
var idxComma = { idx: value.lastIndexOf(','), sym: ',' };
var idxFirst = {};
if (idxDot.idx == -1) idxFirst = idxComma;
else if (idxComma.idx == -1) idxFirst = idxDot;
else if (idxDot.idx < idxComma.idx) idxFirst = idxDot;
else idxFirst = idxComma;
//Find the greatest symbol.
var idxLastDot = { idx: value.lastIndexOf('.'), sym: '.' };
var idxLastComma = { idx: value.lastIndexOf(','), sym: ',' };
var idxLast = {};
if (idxLastDot.idx == -1) idxLast = idxLastComma;
else if (idxLastComma.idx == -1) idxLast = idxLastDot;
else if (idxLastDot.idx > idxLastComma.idx) idxLast = idxLastDot;
else idxLast = idxLastComma;
//Splits over the dot and comma and check that are all numbers.
var splitted = value.split(/(\.|,|\-|\+)/g);
if (splitted.length == 0) return NaN;
var numOfDots = 0;
var numOfComma = 0;
var i=0;
if (splitted[0] == '-' || splitted[0] == '+') i=1;
for (var str; i<splitted.length, str=splitted[i]; i++) {
if (str == '.') numOfDots++;
else if (str == ',') numOfComma++;
else if (/^(0|([1-9][0-9]*))$/g.test(str) == false)
return NaN;
}//EndFor.
var lastValue = splitted[splitted.length-1];
if (lastValue == '.' || lastValue == ',' || lastValue.length == 0) return NaN;
//No dot/comma char found
if (idxFirst.idx == -1 && idxLast.idx == -1)
return DataTypesUtils.FilterFloat(value);
//Only one dot symbol found
if (idxFirst.idx == idxLast.idx && idxFirst.sym == '.')
return Number(value);
//Only one comma symbol found
if (idxFirst.idx == idxLast.idx && idxFirst.sym == ',') {
var nval = value.replace(',', '.');
return Number(nval);
}
return NaN;
};//EndFunction.
/**
* Solution from here:
* http://stackoverflow.com/questions/10454518/javascript-how-to-retrieve-the-number-of-decimals-of-a-string-number
* @param num
* @returns {number}
*/
DataTypesUtils.DecimalPlaces = function (num) {
var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) { return 0; }
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0)
// Adjust for scientific notation.
- (match[2] ? +match[2] : 0));
}//EndFunction.
DataTypesUtils.IsLatLng = function (num) {
if (DataTypesUtils.FilterFloat(num) == NaN) return false;
if (DataTypesUtils.DecimalPlaces(num) > 4) return true;
return false;
}//EndFunction.