Skip to content

Commit ae355f3

Browse files
committed
Refactor date
1 parent 28cf2b9 commit ae355f3

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/rules/date.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
'use strict'
12

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]+$/
47

58
/**
69
* Wrap a rule with validation and formatting logic.
@@ -9,32 +12,30 @@ let chrono = require('chrono-node')
912
* @return {Function} wrapped
1013
*/
1114

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
1618

17-
// remove whitespace for easier parsing
18-
value = value.trim()
19+
// remove whitespace for easier parsing
20+
value = value.trim()
1921

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()
2224

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+
}
2931

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()
3335

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()
3839
}
3940

4041
/**
@@ -67,21 +68,24 @@ module.exports = [
6768
wrap(($) => $('[id*="date"]').text()),
6869
wrap(($) => $('[class*="post-meta"]').text()),
6970
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)
7273
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)
7577
return date.toISOString()
7678
}),
7779
wrap(($) => {
78-
let text = $('[class*="byline"]').text()
80+
const text = $('[class*="byline"]').text()
7981
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)
8285
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)
8589
return date.toISOString()
8690
})
8791
]

0 commit comments

Comments
 (0)