-
Notifications
You must be signed in to change notification settings - Fork 0
/
time2jarvis.js
53 lines (47 loc) · 1.47 KB
/
time2jarvis.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
// AUTHOR: rfesler@gmail.com
// INPUT: msg.payload ("HH:MM")
// OUTPUT: msg.payload (array)
// ie: "13:45" -> "13,45"
// ie: "01:02" -> "1,oh,2"
// ie: "00:00" -> "midnight"
// Extract hours and minutes from the payload ("HH:MM" format)
let time = msg.payload.split(":");
let hours = parseInt(time[0], 10);
let minutes = parseInt(time[1], 10);
let result;
// Define output based on specific rules
if (hours === 0 && minutes === 0) {
result = "midnight";
} else if (hours === 12 && minutes === 0) {
result = "noon";
} else {
result = [];
// Handle hours
if (hours === 0) {
result.push(0);
} else if (hours >= 20) {
result.push(20, hours % 10); // Break 23 into 20, 3
} else {
result.push(hours);
}
// Handle minutes with specific rules
if (minutes === 0) {
result.push(100); // Special case for ":00"
} else if (minutes < 10) {
result.push("oh", minutes); // Use "oh" for single-digit minutes
} else if (minutes % 10 === 0 || (minutes >= 11 && minutes <= 19)) {
result.push(minutes); // Handle exact minutes or teen numbers
} else {
let tens = Math.floor(minutes / 10) * 10;
let units = minutes % 10;
// Replace 0 in the tens place with "oh"
if (tens === 0) {
result.push("oh", units);
} else {
result.push(tens, units);
}
}
}
// Assign the result to msg.payload to output it
msg.payload = result;
return msg;