-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (143 loc) · 3.78 KB
/
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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// variables
let ruleObject = {}
// dictionary for the output
const translate = {
"DAILY": "day",
"WEEKLY": "week",
"MONTHLY": "month",
"MO": "Monday",
"TU": "Tuesday",
"WE": "Wednesday",
"TH": "Thursday",
"FR": "Friday",
"SA": "Saturday",
"SU": "Sunday",
}
var daysArray = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
// check if multiple days and format either way
function translateDays(day){
let result = "";
if ( day === undefined ) {
return;
}
if ( day.includes(",") ) {
result = day.split(",");
result = result.map(d => translate[d]);
result = result.join(", ")
return result;
}
return translate[day];
}
// convert to ordinal number
function ordinalSuffix(num) {
var s = ["th", "st", "nd", "rd"];
var v = num % 100;
return num + (s[(v-20) % 10] || s[v] || s[0]);
}
// the convert function
const convertToWords = (rule, timeformat) => {
// check if function is getting a string
if ( typeof rule != "string") {
console.log('Expecting a string')
return;
}
// remove the RRULE: from the string
rule = rule.replace("RRULE:", "");
// convert the rrule string to array
ruleArray = rule.split(";");
// convert the rrule string to object
ruleArray.forEach(function(r){
keyAndValue = r.split("=");
const [key, value] = keyAndValue;
ruleObject[key] = value;
})
// keys
let freq = ruleObject["FREQ"];
let byday = ruleObject["BYDAY"];
let bymonthday = ruleObject["BYMONTHDAY"];
let byhour = ruleObject["BYHOUR"];
let byminute = ruleObject["BYMINUTE"];
let count = ruleObject["COUNT"];
let interval = ruleObject["INTERVAL"];
// initiate output
let output = "";
// format
// daily
if ( freq === "DAILY" ) {
if ( byminute ) {
output = `Every day at ${byhour}:${byminute}`;
} else {
output = `Every day at ${byhour}:00`;
}
if ( timeformat == "12HM") {
let ampm = "";
if ( byhour >= 12 && byhour < 24 ) {
ampm = "PM";
} else {
ampm = "AM";
}
if ( byminute ) {
output = `Every day at ${byhour > 12 ? byhour - 12 : byhour}:${byminute} ${ampm}`;
} else {
output = `Every day at ${byhour > 12 ? byhour - 12 : byhour} ${ampm}`;
}
}
if ( timeformat == "12H") {
let ampm = "";
if ( byhour >= 12 && byhour < 24 ) {
ampm = "PM";
} else {
ampm = "AM";
}
output = `Every day at ${byhour > 12 ? byhour - 12 : byhour}${ampm}`;
}
if ( count ) {
if ( count == "1" ) {
output += ` for 1 time`;
} else {
output += ` for ${count} times`;
}
}
return output;
}
// weekly
if ( freq === "WEEKLY" ) {
if ( interval && parseInt(interval) > 1 ) {
output = `Every ${interval} ${translate[freq]}s on ${translateDays(byday)}`;
} else {
output = `Every ${translate[freq]} on ${translateDays(byday)}`;
}
if ( bymonthday ) {
output += ` the ${ordinalSuffix(bymonthday)}`;
}
if ( count ) {
if ( count == "1" ) {
output += ` for 1 time`;
} else {
output += ` for ${count} times`;
}
}
if ( !daysArray.includes(byday) ) {
output = "";
console.log(`Error: ${byday} is invalid day, use MO, TU, WE, TH, FR, SA or SU`);
}
return output;
}
// monthly
if ( freq === "MONTHLY" ) {
if ( interval && parseInt(interval) > 1 ) {
output = `Every ${interval} ${translate[freq]}s on the ${ordinalSuffix(bymonthday)}`;
} else {
output = `Every ${translate[freq]} on the ${ordinalSuffix(bymonthday)}`;
}
if ( count ) {
if ( count == "1" ) {
output += ` for 1 time`;
} else {
output += ` for ${count} times`;
}
}
return output;
}
}
module.exports = convertToWords;