forked from validatorjs/validator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisDate.js
100 lines (90 loc) · 2.89 KB
/
isDate.js
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isDate;
var _assertString = require('./util/assertString');
var _assertString2 = _interopRequireDefault(_assertString);
var _isISO = require('./isISO8601');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getTimezoneOffset(str) {
var iso8601Parts = str.match(_isISO.iso8601);
var timezone = void 0,
sign = void 0,
hours = void 0,
minutes = void 0;
if (!iso8601Parts) {
str = str.toLowerCase();
timezone = str.match(/(?:\s|gmt\s*)(-|\+)(\d{1,4})(\s|$)/);
if (!timezone) {
return str.indexOf('gmt') !== -1 ? 0 : null;
}
sign = timezone[1];
var offset = timezone[2];
if (offset.length === 3) {
offset = '0' + offset;
}
if (offset.length <= 2) {
hours = 0;
minutes = parseInt(offset, 10);
} else {
hours = parseInt(offset.slice(0, 2), 10);
minutes = parseInt(offset.slice(2, 4), 10);
}
} else {
timezone = iso8601Parts[21];
if (!timezone) {
// if no hour/minute was provided, the date is GMT
return !iso8601Parts[12] ? 0 : null;
}
if (timezone === 'z' || timezone === 'Z') {
return 0;
}
sign = iso8601Parts[22];
if (timezone.indexOf(':') !== -1) {
hours = parseInt(iso8601Parts[23], 10);
minutes = parseInt(iso8601Parts[24], 10);
} else {
hours = 0;
minutes = parseInt(iso8601Parts[23], 10);
}
}
return (hours * 60 + minutes) * (sign === '-' ? 1 : -1);
}
function isDate(str) {
(0, _assertString2.default)(str);
var normalizedDate = new Date(Date.parse(str));
if (isNaN(normalizedDate)) {
return false;
}
// normalizedDate is in the user's timezone. Apply the input
// timezone offset to the date so that the year and day match
// the input
var timezoneOffset = getTimezoneOffset(str);
if (timezoneOffset !== null) {
var timezoneDifference = normalizedDate.getTimezoneOffset() - timezoneOffset;
normalizedDate = new Date(normalizedDate.getTime() + 60000 * timezoneDifference);
}
var day = String(normalizedDate.getDate());
var dayOrYear = void 0,
dayOrYearMatches = void 0,
year = void 0;
// check for valid double digits that could be late days
// check for all matches since a string like '12/23' is a valid date
// ignore everything with nearby colons
dayOrYearMatches = str.match(/(^|[^:\d])[23]\d([^T:\d]|$)/g);
if (!dayOrYearMatches) {
return true;
}
dayOrYear = dayOrYearMatches.map(function (digitString) {
return digitString.match(/\d+/g)[0];
}).join('/');
year = String(normalizedDate.getFullYear()).slice(-2);
if (dayOrYear === day || dayOrYear === year) {
return true;
} else if (dayOrYear === '' + day / year || dayOrYear === '' + year / day) {
return true;
}
return false;
}
module.exports = exports['default'];