DataTypesUtils.js
4.3 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
/*
** 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.
/**
* 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.