UNPKG

som-exp-sdk

Version:

Evaluate the User Expression

98 lines (97 loc) 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeExpression = void 0; const base_expression_1 = require("./base-expression"); const constants_1 = require("./constants"); class TimeExpression { constructor() { // Ex: timeExpr - hh:mm:ss/ hh:mm this.hour = (timeExpr) => { let output = this.const.INVALID_EXPRESSION; if (!!timeExpr) { try { const localTime = this.parseTimeExpr(timeExpr); if (!!localTime) { output = localTime.getHours().toString(); } } catch (ex) { console.log(`Error in parsing timeExpr: ${timeExpr}`); } } return output; }; this.minute = (timeExpr) => { let output = this.const.INVALID_EXPRESSION; if (!!timeExpr) { try { const localTime = this.parseTimeExpr(timeExpr); if (!!localTime) { output = localTime.getMinutes().toString(); } } catch (ex) { console.log(`Error in parsing timeExpr: ${timeExpr}`); } } return output; }; this.parseTimeExpr = (timeExpr) => { let timeVal = new Date(); const regex = this.const.TIME_REGEX; // clearing the time fields timeVal.setHours(0); timeVal.setMinutes(0); timeVal.setSeconds(0); if (timeExpr && !!regex.test(timeExpr)) { try { const exprVariables = regex.exec(timeExpr); let hour = -1; let minute = -1; let second = 0; if (typeof exprVariables != undefined) { const hourVal = this.baseExpression.removeSpaces(exprVariables === null || exprVariables === void 0 ? void 0 : exprVariables["1"]); const minuteVal = this.baseExpression.removeSpaces(exprVariables === null || exprVariables === void 0 ? void 0 : exprVariables["2"]); const secondVal = this.baseExpression.removeSpaces(exprVariables === null || exprVariables === void 0 ? void 0 : exprVariables["3"]); if (hourVal && typeof hourVal != undefined) { // set the hour captured from the input hour = parseInt(hourVal) < 24 ? parseInt(hourVal) : -1; } if (minuteVal && typeof minuteVal != undefined) { // set the minute captured from the input minute = parseInt(minuteVal) <= 59 ? parseInt(minuteVal) : -1; } if (secondVal && typeof secondVal != undefined) { // set the second captured from the input second = parseInt(secondVal) <= 59 ? parseInt(secondVal) : -1; } if (hour < 0 || minute < 0 || second < 0) { timeVal = null; } else { if (hour >= 0) { timeVal.setHours(hour); } if (minute >= 0) { timeVal.setMinutes(minute); } if (second >= 0) { timeVal.setSeconds(second); } } } } catch (ex) { console.log(`Error in parsing time input with format ${ex}`); } } else { console.log(`Invalid time expression: ${timeExpr}`); } return timeVal; }; this.const = new constants_1.Constants(); this.baseExpression = new base_expression_1.BaseExpression(); } } exports.TimeExpression = TimeExpression;