Skip to content

Commit

Permalink
fix(isISO8601): strict mode now works with other JS engines (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
natesilva authored and chriso committed Nov 13, 2018
1 parent f67111d commit bf5b2dc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
10 changes: 6 additions & 4 deletions lib/isISO8601.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ var isValidDate = function isValidDate(str) {
var match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
var year = match[1];
var month = match[2];
var day = match[3]; // create a date object and compare
var day = match[3];
var monthString = month ? "0".concat(month).slice(-2) : month;
var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare

var d = new Date("".concat(year, "-").concat(month || 1, "-").concat(day || 1));
if (isNaN(d.getFullYear())) return false;
var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
if (isNaN(d.getUTCFullYear())) return false;

if (month && day) {
return d.getFullYear() === year && d.getMonth() + 1 === month && d.getDate() === day;
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
}

return true;
Expand Down
13 changes: 8 additions & 5 deletions src/lib/isISO8601.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ const isValidDate = (str) => {
const year = match[1];
const month = match[2];
const day = match[3];
const monthString = month ? `0${month}`.slice(-2) : month;
const dayString = day ? `0${day}`.slice(-2) : day;

// create a date object and compare
const d = new Date(`${year}-${month || 1}-${day || 1}`);
if (isNaN(d.getFullYear())) return false;
const d = new Date(`${year}-${monthString || '01'}-${dayString || '01'}`);
if (isNaN(d.getUTCFullYear())) return false;
if (month && day) {
return d.getFullYear() === year
&& (d.getMonth() + 1) === month
&& d.getDate() === day;
return d.getUTCFullYear() === year
&& (d.getUTCMonth() + 1) === month
&& d.getUTCDate() === day;
}
return true;
};
Expand Down
1 change: 1 addition & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5636,6 +5636,7 @@ describe('Validators', () => {
'2010-02-18T16,2283',
'2009-05-19 143922.500',
'2009-05-19 1439,55',
'2009-10-10',
];

const invalidISO8601 = [
Expand Down
10 changes: 6 additions & 4 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1575,13 +1575,15 @@ var isValidDate = function isValidDate(str) {
var match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
var year = match[1];
var month = match[2];
var day = match[3]; // create a date object and compare
var day = match[3];
var monthString = month ? "0".concat(month).slice(-2) : month;
var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare

var d = new Date("".concat(year, "-").concat(month || 1, "-").concat(day || 1));
if (isNaN(d.getFullYear())) return false;
var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
if (isNaN(d.getUTCFullYear())) return false;

if (month && day) {
return d.getFullYear() === year && d.getMonth() + 1 === month && d.getDate() === day;
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit bf5b2dc

Please sign in to comment.