-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (24 loc) · 884 Bytes
/
index.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
'use strict'
module.exports = options => {
let params = Object.assign(
{ round: Math.floor, minutes: 5, epoch: true, date: Date.now() },
options || {}
)
const roundType = typeof params.round
if (roundType !== 'function') {
throw new Error(
'Specified option "round" must be a function; found ' + roundType
)
}
if (!Number.isInteger(params.minutes) || params.minutes < 0) {
throw new Error('Specified option "minutes" must be a valid integer')
}
if (params.date instanceof Date) {
params.date = params.date.getTime()
} else if (!Number.isInteger(params.date) || params.date < 0) {
throw new Error('Specified option "date" must be a valid epoch or Date')
}
const ms = 1000 * 60 * params.minutes
const date = new Date(params.round(params.date / ms) * ms)
return !!params.epoch ? Math.round(date.getTime() / 1000) : date
}