/
expiration.js
42 lines (35 loc) · 826 Bytes
/
expiration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
const isValidMonth = require('is-valid-month')
const parseIntStrict = require('parse-int')
const parseYear = require('parse-year')
module.exports = {
isPast: isPast,
month: {
parse: parseMonth,
isValid: isValidMonth
},
year: {
parse: parseYear,
format: formatExpYear,
isValid: isExpYearValid,
isPast: isExpYearPast
}
}
function isPast (month, year) {
return Date.now() >= new Date(year, month)
}
function parseMonth (month) {
return parseIntStrict(month)
}
function formatExpYear (year, strip) {
year = year.toString()
return strip ? year.substr(2, 4) : year
}
function isExpYearValid (year) {
if (typeof year !== 'number') return false
year = parseIntStrict(year)
return year > 0
}
function isExpYearPast (year) {
return new Date().getFullYear() > year
}