Skip to content

Commit 94d069b

Browse files
committed
Add fix for isISO8601 for year < 1000 and add tests
1 parent a1e8476 commit 94d069b

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/lib/isISO8601.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import assertString from './util/assertString';
22

33
/* eslint-disable max-len */
44
// from http://goo.gl/0ejHHW
5-
const iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
5+
const iso8601 =
6+
/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
67
// same as above, except with a strict 'T' separator between date and time
7-
const iso8601StrictSeparator = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
8+
const iso8601StrictSeparator =
9+
/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
810
/* eslint-enable max-len */
911
const isValidDate = (str) => {
1012
// str must have passed the ISO8601 check
@@ -16,29 +18,33 @@ const isValidDate = (str) => {
1618
const oYear = Number(ordinalMatch[1]);
1719
const oDay = Number(ordinalMatch[2]);
1820
// if is leap year
19-
if ((oYear % 4 === 0 && oYear % 100 !== 0) || oYear % 400 === 0) return oDay <= 366;
21+
if ((oYear % 4 === 0 && oYear % 100 !== 0) || oYear % 400 === 0) { return oDay <= 366; }
2022
return oDay <= 365;
2123
}
2224
const match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
2325
const year = match[1];
2426
const month = match[2];
2527
const day = match[3];
26-
const monthString = month ? `0${month}`.slice(-2) : month;
27-
const dayString = day ? `0${day}`.slice(-2) : day;
28+
const monthString = month ? `${month}`.padStart(2, '0') : month;
29+
const dayString = day ? `${day}`.padStart(2, '0') : day;
30+
const yearString = year < 100 ? `${year}`.padStart(4, '0') : year;
2831

2932
// create a date object and compare
30-
const d = new Date(`${year}-${monthString || '01'}-${dayString || '01'}`);
33+
const d = new Date(`${yearString}-${monthString || '01'}-${dayString || '01'}`);
3134
if (month && day) {
32-
return d.getUTCFullYear() === year
33-
&& (d.getUTCMonth() + 1) === month
34-
&& d.getUTCDate() === day;
35+
return (
36+
d.getUTCFullYear() === year &&
37+
d.getUTCMonth() + 1 === month &&
38+
d.getUTCDate() === day
39+
);
3540
}
3641
return true;
3742
};
38-
3943
export default function isISO8601(str, options = {}) {
4044
assertString(str);
41-
const check = options.strictSeparator ? iso8601StrictSeparator.test(str) : iso8601.test(str);
45+
const check = options.strictSeparator
46+
? iso8601StrictSeparator.test(str)
47+
: iso8601.test(str);
4248
if (check && options.strict) return isValidDate(str);
4349
return check;
4450
}

test/validators.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11924,6 +11924,9 @@ describe('Validators', () => {
1192411924
'2009-10-10',
1192511925
'2020-366',
1192611926
'2000-366',
11927+
'0080-05-19',
11928+
'0008-W51-1',
11929+
'0009-05-19T14:39Z',
1192711930
];
1192811931

1192911932
const invalidISO8601 = [
@@ -11953,6 +11956,9 @@ describe('Validators', () => {
1195311956
'2010-13-1',
1195411957
'nonsense2021-01-01T00:00:00Z',
1195511958
'2021-01-01T00:00:00Znonsense',
11959+
'0010-02-18T16:23.33.600',
11960+
'0200-02-18T16,25:23:48,444',
11961+
'nonsense0021-01-01T00:00:00Z',
1195611962
];
1195711963

1195811964
it('should validate ISO 8601 dates', () => {
@@ -11987,6 +11993,8 @@ describe('Validators', () => {
1198711993
'2009-222',
1198811994
'2020-366',
1198911995
'2400-366',
11996+
'0050-02-15',
11997+
'0010-05-19T14:39Z',
1199011998
],
1199111999
invalid: [
1199212000
'2010-02-30',
@@ -12038,6 +12046,10 @@ describe('Validators', () => {
1203812046
'2009-10-10',
1203912047
'2020-366',
1204012048
'2000-366',
12049+
'0010-02-18T16:23.4',
12050+
'0010-02-18T16:23,25',
12051+
'0200-02-18T16:23.33+0600',
12052+
'0200-02-18T16.23334444',
1204112053
],
1204212054
invalid: [
1204312055
'200905',
@@ -12072,6 +12084,9 @@ describe('Validators', () => {
1207212084
'2009-05-19 14:39:22-06:00',
1207312085
'2009-05-19 14:39:22+0600',
1207412086
'2009-05-19 14:39:22-01',
12087+
'0009-05-19 14.5.44',
12088+
'0010-02-18T16:23.33.600',
12089+
'0010-02-18T16,25:23:48,444',
1207512090
],
1207612091
});
1207712092
});
@@ -12088,6 +12103,8 @@ describe('Validators', () => {
1208812103
'2009-222',
1208912104
'2020-366',
1209012105
'2400-366',
12106+
'0020-366',
12107+
'0400-366',
1209112108
],
1209212109
invalid: [
1209312110
'2010-02-30',
@@ -12100,6 +12117,8 @@ describe('Validators', () => {
1210012117
'2009-05-19 14:39:22-06:00',
1210112118
'2009-05-19 14:39:22+0600',
1210212119
'2009-05-19 14:39:22-01',
12120+
'0009-05-19 14:39:22+0600',
12121+
'0009-05-19 14:39:22-01',
1210312122
],
1210412123
});
1210512124
});

0 commit comments

Comments
 (0)