-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (89 loc) · 3.1 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
//Function need daysAndTimeSlots and options object. Then optionally function that will run within time range or out of time range or both situations.
module.exports.intervalChecker = (
daysAndTimeSlots,
options,
functionWithinRange,
functionOutOfRange
) => {
var weekdays = [
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday"
];
// FUNCTION to check if current time is in range for scheduled timeslots.
function isInRange(value, range) {
return value >= range[0] && value <= range[1];
}
// FUNCTION to convert 12 hour format to 24 format. Need to be done to work with isInRange function.
function convertTimeFormat(format, str) {
var time = str;
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if (AMPM.toLowerCase() === "pm" && hours < 12) hours = hours + 12;
if (AMPM.toLowerCase() === "am" && hours == 12) hours = hours - 12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if (hours < 10) sHours = "0" + sHours;
if (minutes < 10) sMinutes = "0" + sMinutes;
return sHours + ":" + sMinutes;
}
//Just like the variables says
var d = new Date();
var currentDay = weekdays[d.getDay()];
var currentDayIndex = d.getDay();
// Checking if user has this current day as false or true.
var currentDayState = daysAndTimeSlots[currentDayIndex][currentDay];
// Getting the users timeslots for this day.
var pickedTimeSlots = daysAndTimeSlots[currentDayIndex].time;
// Getting the user current timezone.
var userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Converting the user time to the developers time .
var convertedTime = d.toLocaleTimeString(navigator.language, {
timeZone: options.myTimeZone,
hour: "2-digit",
minute: "2-digit"
});
// console.log(
// "This is the time from where I as a user am right now " +
// d.toLocaleTimeString(navigator.language, {
// hour: "2-digit",
// minute: "2-digit"
// })
// );
// console.log(
// "This is the time from which the developer has set scheduled times " +
// convertedTime
// );
if (currentDayState) {
if (
options.timeFormat === 24 &&
isInRange(convertedTime, pickedTimeSlots)
) {
functionWithinRange && functionWithinRange();
} else if (options.timeFormat === 12) {
var myNativeTimeSlots = daysAndTimeSlots[currentDayIndex].time;
console.log(daysAndTimeSlots[currentDayIndex].time);
var convertedTimeSlots = [];
myNativeTimeSlots.forEach(timeSlot => {
convertedTimeSlots.push(convertTimeFormat("24", timeSlot));
});
if (
myNativeTimeSlots.length === convertedTimeSlots.length &&
isInRange(convertedTime, convertedTimeSlots)
) {
functionWithinRange && functionWithinRange();
} else {
functionOutOfRange && functionOutOfRange();
}
} else {
functionOutOfRange && functionOutOfRange();
}
} else {
functionOutOfRange && functionOutOfRange();
}
};