-
Notifications
You must be signed in to change notification settings - Fork 24
/
EveryTime.ts
89 lines (73 loc) · 2.47 KB
/
EveryTime.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Helpers from "./helpers";
type TimeInterval = number | "even" | "uneven" | number[];
type EveryTimeConfig = {
between?: boolean;
};
/**
* Every Time Class
*/
class EveryTime {
public interval: TimeInterval = 1;
public config: EveryTimeConfig = {};
/**
*
* @param {number[]|string|number} every
* @param {{}} config
*/
constructor(every: TimeInterval, config: EveryTimeConfig = {}) {
if (every === "even") every = 2;
this.interval = every;
this.config = Object.assign(this.config, config);
return this;
}
/**
* Every nth Minute
*/
minutes(): string {
if (this.config["between"] && Array.isArray(this.interval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(0, this.interval.join("-"), Helpers.minute());
}
if (typeof this.interval === "number" && this.interval > 1) {
return Helpers.spliceIntoPosition(0, "*/" + this.interval);
} else if (this.interval === "uneven") {
return Helpers.spliceIntoPosition(0, "1-59/2");
}
return Helpers.minute();
}
/**
* Every nth Hour
*/
hours(): string {
const hour = Helpers.hour();
if (this.config["between"] && Array.isArray(this.interval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(1, this.interval.join("-"), hour);
}
if (typeof this.interval === "number" && this.interval > 1) {
return Helpers.spliceIntoPosition(1, "*/" + this.interval, hour);
} else if (this.interval === "uneven") {
return Helpers.spliceIntoPosition(1, "1-23/2", hour);
}
return hour;
}
/**
* Every nth Days after
* @param hoursOfDay
* @param $minutesOfDay
*/
days(hoursOfDay = 0, $minutesOfDay = 0): string {
const day = Helpers.day(hoursOfDay, $minutesOfDay);
if (this.config["between"] && Array.isArray(this.interval)) {
this.config["between"] = false;
return Helpers.spliceIntoPosition(2, this.interval.join("-"), day);
}
if (typeof this.interval === "number" && this.interval > 1) {
return Helpers.spliceIntoPosition(2, "*/" + this.interval, day);
} else if (this.interval === "uneven") {
return Helpers.spliceIntoPosition(2, "1-31/2", day);
}
return day;
}
}
export default EveryTime;