-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
24 lines (23 loc) · 838 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
const places = 'second minute hour day month year'.split(' ')
const maxes = [59, 59, 23, 30]
module.exports = function(string, opts) {
const match = string.match(/(\d+)? (second|minute|hour|day|month|year)/)
const rand = opts && opts.random
if (match) {
let pattern = Array(6).fill('*')
const count = match[1] || 1
const interval = match[2]
const intervalPlace = places.indexOf(interval)
if (count !== 1) {
pattern[intervalPlace] = `${rand ? Math.round(Math.random() * (maxes[intervalPlace]/count))+`-${maxes[intervalPlace]}` : '*'}/${count}`
// pattern[intervalPlace] = `*/${count}`
}
for (let i=intervalPlace-1; i>=0; i--) {
const fillVal = rand ? Math.round(Math.random() * maxes[i]) : 0
pattern[i] = fillVal
}
return pattern.join(' ')
} else {
return null
}
}