@sap_oss/wdio-qmate-service
Version:
[](https://api.reuse.software/info/github.com/SAP/wdio-qmate-service)[](http
91 lines • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeHelper = void 0;
const date_constants_1 = require("../modules/common/constants/date.constants");
class TimeHelper {
static updateDateWithTime(date, time) {
if (this._isValidTimeAnchor(time)) {
return this._updateDateWithTimeAnchor(date, time);
}
if (this._isValidTimeValue(time)) {
return this._updateDateWithTimeValue(date, time);
}
throw new Error("Please provide a valid time string as second argument.");
}
// =================================== PRIVATE ===================================
static _isValidTimeAnchor(time) {
return Object.values(date_constants_1.CalculateTimeAnchors).includes(time);
}
static _isValidTimeValue(time) {
const { hours, minutes, seconds } = this._extractTimeComponents(time);
return this._isValidHours(hours, this._extractAmPm(time))
&& (minutes ? this._isValidMinutes(minutes) : true)
&& (seconds ? this._isValidSeconds(seconds) : true);
}
static _updateDateWithTimeAnchor(date, time) {
switch (time) {
case date_constants_1.CalculateTimeAnchors.CURRENT_TIME:
this._updateDateWithCurrentTime(date);
break;
case date_constants_1.CalculateTimeAnchors.START_OF_DAY:
this._updateDateWithStartOfDay(date);
break;
case date_constants_1.CalculateTimeAnchors.END_OF_DAY:
this._updateDateWithEndOfDay(date);
break;
default:
throw new Error(`Unsupported time anchor: ${time}`);
}
return date;
}
static _updateDateWithTimeValue(date, time) {
const { hours, minutes, seconds } = this._extractTimeComponents(time);
date.setHours(hours
? this._adjustTo24HourFormat(Number(hours), this._extractAmPm(time))
: 0);
date.setMinutes(Number(minutes) || 0);
date.setSeconds(Number(seconds) || 0);
return date;
}
static _extractTimeComponents(time) {
const [hours, minutes, seconds] = time.replace(/AM|PM/i, "").trim().split(":");
return { hours, minutes, seconds };
}
static _extractAmPm(time) {
const match = time.toUpperCase().match(/AM|PM/i);
return match ? match[0] : "";
}
static _isValidHours(hours, amPm) {
const hoursRegex = /^(2[0-3]|[01]?[0-9])$/; // 00-23
return (hoursRegex.test(hours) && (amPm ? Number(hours) <= 12 : true));
}
static _isValidMinutes(minutes) {
const minutesRegex = /^([0-5]?[0-9])$/; // 00-59
return minutesRegex.test(minutes);
}
static _isValidSeconds(seconds) {
const secondsRegex = /^([0-5]?[0-9])$/; // 00-59
return secondsRegex.test(seconds);
}
static _updateDateWithCurrentTime(date) {
const now = new Date();
date.setHours(now.getHours(), now.getMinutes(), now.getSeconds(), 0);
}
static _updateDateWithStartOfDay(date) {
date.setHours(0, 0, 0, 0);
}
static _updateDateWithEndOfDay(date) {
date.setHours(23, 59, 59, 999);
}
static _adjustTo24HourFormat(hours, amPm) {
if (amPm === "PM" && hours < 12) {
return hours + 12;
}
if (amPm === "AM" && hours === 12) {
return 0;
}
return hours;
}
}
exports.TimeHelper = TimeHelper;
//# sourceMappingURL=timeHelper.js.map