@@ -2,9 +2,11 @@ import assertString from './util/assertString';
2
2
3
3
/* eslint-disable max-len */
4
4
// from http://goo.gl/0ejHHW
5
- const iso8601 = / ^ ( [ \+ - ] ? \d { 4 } (? ! \d { 2 } \b ) ) ( ( - ? ) ( ( 0 [ 1 - 9 ] | 1 [ 0 - 2 ] ) ( \3( [ 1 2 ] \d | 0 [ 1 - 9 ] | 3 [ 0 1 ] ) ) ? | W ( [ 0 - 4 ] \d | 5 [ 0 - 3 ] ) ( - ? [ 1 - 7 ] ) ? | ( 0 0 [ 1 - 9 ] | 0 [ 1 - 9 ] \d | [ 1 2 ] \d { 2 } | 3 ( [ 0 - 5 ] \d | 6 [ 1 - 6 ] ) ) ) ( [ T \s ] ( ( ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) ( ( : ? ) [ 0 - 5 ] \d ) ? | 2 4 : ? 0 0 ) ( [ \. , ] \d + (? ! : ) ) ? ) ? ( \17[ 0 - 5 ] \d ( [ \. , ] \d + ) ? ) ? ( [ z Z ] | ( [ \+ - ] ) ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) : ? ( [ 0 - 5 ] \d ) ? ) ? ) ? ) ? $ / ;
5
+ const iso8601 =
6
+ / ^ ( [ \+ - ] ? \d { 4 } (? ! \d { 2 } \b ) ) ( ( - ? ) ( ( 0 [ 1 - 9 ] | 1 [ 0 - 2 ] ) ( \3( [ 1 2 ] \d | 0 [ 1 - 9 ] | 3 [ 0 1 ] ) ) ? | W ( [ 0 - 4 ] \d | 5 [ 0 - 3 ] ) ( - ? [ 1 - 7 ] ) ? | ( 0 0 [ 1 - 9 ] | 0 [ 1 - 9 ] \d | [ 1 2 ] \d { 2 } | 3 ( [ 0 - 5 ] \d | 6 [ 1 - 6 ] ) ) ) ( [ T \s ] ( ( ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) ( ( : ? ) [ 0 - 5 ] \d ) ? | 2 4 : ? 0 0 ) ( [ \. , ] \d + (? ! : ) ) ? ) ? ( \17[ 0 - 5 ] \d ( [ \. , ] \d + ) ? ) ? ( [ z Z ] | ( [ \+ - ] ) ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) : ? ( [ 0 - 5 ] \d ) ? ) ? ) ? ) ? $ / ;
6
7
// 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( [ 1 2 ] \d | 0 [ 1 - 9 ] | 3 [ 0 1 ] ) ) ? | W ( [ 0 - 4 ] \d | 5 [ 0 - 3 ] ) ( - ? [ 1 - 7 ] ) ? | ( 0 0 [ 1 - 9 ] | 0 [ 1 - 9 ] \d | [ 1 2 ] \d { 2 } | 3 ( [ 0 - 5 ] \d | 6 [ 1 - 6 ] ) ) ) ( [ T ] ( ( ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) ( ( : ? ) [ 0 - 5 ] \d ) ? | 2 4 : ? 0 0 ) ( [ \. , ] \d + (? ! : ) ) ? ) ? ( \17[ 0 - 5 ] \d ( [ \. , ] \d + ) ? ) ? ( [ z Z ] | ( [ \+ - ] ) ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) : ? ( [ 0 - 5 ] \d ) ? ) ? ) ? ) ? $ / ;
8
+ const iso8601StrictSeparator =
9
+ / ^ ( [ \+ - ] ? \d { 4 } (? ! \d { 2 } \b ) ) ( ( - ? ) ( ( 0 [ 1 - 9 ] | 1 [ 0 - 2 ] ) ( \3( [ 1 2 ] \d | 0 [ 1 - 9 ] | 3 [ 0 1 ] ) ) ? | W ( [ 0 - 4 ] \d | 5 [ 0 - 3 ] ) ( - ? [ 1 - 7 ] ) ? | ( 0 0 [ 1 - 9 ] | 0 [ 1 - 9 ] \d | [ 1 2 ] \d { 2 } | 3 ( [ 0 - 5 ] \d | 6 [ 1 - 6 ] ) ) ) ( [ T ] ( ( ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) ( ( : ? ) [ 0 - 5 ] \d ) ? | 2 4 : ? 0 0 ) ( [ \. , ] \d + (? ! : ) ) ? ) ? ( \17[ 0 - 5 ] \d ( [ \. , ] \d + ) ? ) ? ( [ z Z ] | ( [ \+ - ] ) ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) : ? ( [ 0 - 5 ] \d ) ? ) ? ) ? ) ? $ / ;
8
10
/* eslint-enable max-len */
9
11
const isValidDate = ( str ) => {
10
12
// str must have passed the ISO8601 check
@@ -16,29 +18,33 @@ const isValidDate = (str) => {
16
18
const oYear = Number ( ordinalMatch [ 1 ] ) ;
17
19
const oDay = Number ( ordinalMatch [ 2 ] ) ;
18
20
// 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 ; }
20
22
return oDay <= 365 ;
21
23
}
22
24
const match = str . match ( / ( \d { 4 } ) - ? ( \d { 0 , 2 } ) - ? ( \d * ) / ) . map ( Number ) ;
23
25
const year = match [ 1 ] ;
24
26
const month = match [ 2 ] ;
25
27
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 ;
28
31
29
32
// 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' } ` ) ;
31
34
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
+ ) ;
35
40
}
36
41
return true ;
37
42
} ;
38
-
39
43
export default function isISO8601 ( str , options = { } ) {
40
44
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 ) ;
42
48
if ( check && options . strict ) return isValidDate ( str ) ;
43
49
return check ;
44
50
}
0 commit comments