1
+ 'use strict'
1
2
2
- let isIso = require ( 'is-isodate' )
3
- let chrono = require ( 'chrono-node' )
3
+ const isIso = require ( 'is-isodate' )
4
+ const chrono = require ( 'chrono-node' )
5
+
6
+ const REGEX_NUMBER = / ^ [ 0 - 9 ] + $ /
4
7
5
8
/**
6
9
* Wrap a rule with validation and formatting logic.
@@ -9,32 +12,30 @@ let chrono = require('chrono-node')
9
12
* @return {Function } wrapped
10
13
*/
11
14
12
- function wrap ( rule ) {
13
- return ( $ ) => {
14
- let value = rule ( $ )
15
- if ( ! value ) return
15
+ const wrap = ( rule ) => ( $ ) => {
16
+ let value = rule ( $ )
17
+ if ( ! value ) return
16
18
17
- // remove whitespace for easier parsing
18
- value = value . trim ( )
19
+ // remove whitespace for easier parsing
20
+ value = value . trim ( )
19
21
20
- // convert isodates to restringify, because sometimes they are truncated
21
- if ( isIso ( value ) ) return new Date ( value ) . toISOString ( )
22
+ // convert isodates to restringify, because sometimes they are truncated
23
+ if ( isIso ( value ) ) return new Date ( value ) . toISOString ( )
22
24
23
- // parse number strings as milliseconds
24
- if ( / ^ [ 0 - 9 ] + $ / . test ( value ) ) {
25
- let int = parseInt ( value , 10 )
26
- let date = new Date ( int )
27
- return date . toISOString ( )
28
- }
25
+ // parse number strings as milliseconds
26
+ if ( REGEX_NUMBER . test ( value ) ) {
27
+ const int = parseInt ( value , 10 )
28
+ const date = new Date ( int )
29
+ return date . toISOString ( )
30
+ }
29
31
30
- // try to parse with the built-in date parser
31
- let native = new Date ( value )
32
- if ( ! isNaN ( native . getTime ( ) ) ) return native . toISOString ( )
32
+ // try to parse with the built-in date parser
33
+ const native = new Date ( value )
34
+ if ( ! isNaN ( native . getTime ( ) ) ) return native . toISOString ( )
33
35
34
- // try to parse a complex date string
35
- let parsed = chrono . parseDate ( value )
36
- if ( parsed ) return parsed . toISOString ( )
37
- }
36
+ // try to parse a complex date string
37
+ const parsed = chrono . parseDate ( value )
38
+ if ( parsed ) return parsed . toISOString ( )
38
39
}
39
40
40
41
/**
@@ -67,21 +68,24 @@ module.exports = [
67
68
wrap ( ( $ ) => $ ( '[id*="date"]' ) . text ( ) ) ,
68
69
wrap ( ( $ ) => $ ( '[class*="post-meta"]' ) . text ( ) ) ,
69
70
wrap ( ( $ , url ) => {
70
- let regexp = / ( \d { 4 } [ \- \ /] \d { 2 } [ \- \ /] \d { 2 } ) /
71
- let match = regexp . exec ( url )
71
+ const regexp = / ( \d { 4 } [ - / ] \d { 2 } [ - / ] \d { 2 } ) /
72
+ const match = regexp . exec ( url )
72
73
if ( ! match ) return
73
- let string = match [ 1 ]
74
- let date = new Date ( string )
74
+
75
+ const string = match [ 1 ]
76
+ const date = new Date ( string )
75
77
return date . toISOString ( )
76
78
} ) ,
77
79
wrap ( ( $ ) => {
78
- let text = $ ( '[class*="byline"]' ) . text ( )
80
+ const text = $ ( '[class*="byline"]' ) . text ( )
79
81
if ( ! text ) return
80
- let regexp = / ( \w + \d { 2 } , ? \d { 4 } ) /
81
- let match = regexp . exec ( text )
82
+
83
+ const regexp = / ( \w + \d { 2 } , ? \d { 4 } ) /
84
+ const match = regexp . exec ( text )
82
85
if ( ! match ) return
83
- let string = match [ 1 ]
84
- let date = new Date ( string )
86
+
87
+ const string = match [ 1 ]
88
+ const date = new Date ( string )
85
89
return date . toISOString ( )
86
90
} )
87
91
]
0 commit comments