-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
84 lines (82 loc) · 2.06 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
function getTime(num) {
var tempHour = String(Math.trunc(num / 60));
var hour = tempHour + "".length === 1 ? "0" + tempHour : tempHour;
var min = num % 60 === 0 ? "00" : num % 60;
return { num: num, time: hour + ":" + min };
}
function getTimeSlots(blockTimes, showTimeAsString, interval, includeStartBlockedTime,includeEndBlockedTime) {
var times = 1,
sums = 60;
includeStartBlockedTime = includeStartBlockedTime === true ? true : false;
includeEndBlockedTime = includeEndBlockedTime === true ? true : false;
switch (interval) {
case "tenth":
times = 6;
sums = 10;
break;
case "quarter":
times = 4;
sums = 15;
break;
case "half":
times = 2;
sums = 30;
break;
case "one":
times = 1;
sums = 60;
break;
case "two":
times = 1 / 2;
sums = 120;
break;
case "three":
times = 1 / 3;
sums = 180;
break;
case "four":
times = 1 / 4;
sums = 240;
break;
default:
times = 1;
sums = 60;
break;
}
var start = 0;
var dateTimes = Array(Math.round(24 * times))
.fill(0)
.map(function(_) {
start = start + sums;
return start;
});
blockTimes = Array.isArray(blockTimes) === true && blockTimes.length > 0 ? blockTimes : [];
if (blockTimes.length > 0) {
dateTimes = blockTimes.reduce(function(acc, x) {
return acc
.filter(function(y) {
return includeStartBlockedTime == true ? y <= x[0] : y < x[0];
})
.concat(
acc.filter(function(y) {
return includeEndBlockedTime == true ? y >= x[1] : y > x[1];
})
);
}, dateTimes);
}
if (showTimeAsString === true) {
return dateTimes
.map(function(x) {
return getTime(x);
})
.reduce(function(accc, element) {
accc["" + element.num] = element.time;
return accc;
}, {});
}
return dateTimes;
}
module.exports = {
getTimeSlots: getTimeSlots
};
// console.log(getTimeSlots([[340, 550], [920, 1240]], true, "tenth"));