From c9a29123016a43e7b3ec8ea9b2edf1d7aeb027ed Mon Sep 17 00:00:00 2001 From: chinmay1819 Date: Fri, 2 Jun 2023 17:47:38 +0530 Subject: [PATCH 1/3] added dateEquality --- package-lock.json | 10 +- src/modules/tasks/providers/executor.ts | 293 ++++++++++++++++++++++++ 2 files changed, 300 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32a1a25..d8b516d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,13 @@ { - "name": "business-workflow-service", + "name": "io-flow", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "business-workflow-service", + "name": "io-flow", "version": "1.0.0", - "license": "UNLICENSED", + "license": "SEE LICENSE IN LICENSE.md", "dependencies": { "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", @@ -61,6 +61,10 @@ "ts-node": "^10.9.1", "tsconfig-paths": "^3.10.1", "typescript": "^4.3.5" + }, + "engines": { + "node": ">= v18.13.0", + "npm": ">= v6.13.6" } }, "node_modules/@ampproject/remapping": { diff --git a/src/modules/tasks/providers/executor.ts b/src/modules/tasks/providers/executor.ts index e6abf16..880adf0 100644 --- a/src/modules/tasks/providers/executor.ts +++ b/src/modules/tasks/providers/executor.ts @@ -125,6 +125,285 @@ export class Executor implements OnModuleInit { return path; } + checkEquality(lhsFinal: any, rhsFinal: any): boolean { + if (lhsFinal == rhsFinal) { + return true; + } + + return false; + } + + + countObliqueIsTwo(str: string): boolean { + let oblique = '/'; + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] == oblique) { + count++; + } + } + if (count == 2) { + return true; + } + + return false; + } + + countHyphenIsTwo(str: string): boolean { + let hyphen = '-'; + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] == hyphen) { + count++; + } + } + if (count == 2) { + return true; + } + + return false; + } + + convertDateObjectToString(dateObject: Date): string { + let date: any = dateObject.getDate(); + let month: any = dateObject.getMonth() + 1; + let year: any = dateObject.getFullYear(); + + date = date.toString(); + if (date.length == 1) { + date = '0' + date; + } + month = month.toString(); + if (month.length == 1) { + month = '0' + month; + } + + year = year.toString(); + + let result = date + '/' + month + '/' + year; + + return result; + } + + + + isIsoDateString(str: string): boolean { + // The regular expression to match the ISO 8601 date string pattern + const isoDateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?(Z|[+-]\d{2}:\d{2})$/; + // Test the string against the regular expression + return isoDateRegex.test(str); + } + + formatDate(str: any): string { + str = new Date(str).toString(); + let day: any = str.getDate(); + let month: any = str.getMonth() + 1; + let year: any = str.getFullYear(); + if (day < 10) { + day = day.toString(); + day = '0' + day; + } + if (month < 10) { + month = month.toString(); + month = '0' + month; + } + + let formattedDate = `${day}/${month}/${year}`; + + return formattedDate; + } + + + + checkDates(lhs: any, rhs: any): boolean { + let lhsFinal: string | undefined; + let rhsFinal: string | undefined; + let typeLhs = typeof lhs; + let typeRhs = typeof rhs; + + if (typeLhs === "object") { + lhsFinal = this.convertDateObjectToString(lhs); + } + if (typeLhs === "string") { + if (this.isIsoDateString(lhs)) { + lhsFinal = this.convertDateObjectToString(new Date(lhs)); + } else { + if (lhs.length <= 10) { + if (this.countObliqueIsTwo(lhs)) { + lhsFinal = lhs; + } else if (this.countHyphenIsTwo(lhs)) { + lhsFinal = lhs; + } + } else if (lhs.length > 10) { + lhsFinal = this.formatDate(lhs); + } + } + } + if (typeLhs === "number") { + lhsFinal = this.convertDateObjectToString(new Date(lhs)); + } + + if (typeRhs === "object") { + rhsFinal = this.convertDateObjectToString(rhs); + } + if (typeRhs === "string") { + if (this.isIsoDateString(rhs)) { + rhsFinal = this.convertDateObjectToString(new Date(rhs)); + } else { + if (rhs.length <= 10) { + if (this.countObliqueIsTwo(rhs)) { + rhsFinal = rhs; + } else if (this.countHyphenIsTwo(rhs)) { + rhsFinal = rhs; + } + } else if (rhs.length > 10) { + rhsFinal = this.formatDate(rhs); + console.log("rhsFinal :: ", rhsFinal); + } + } + } + + if (typeRhs === "number") { + rhsFinal = this.convertDateObjectToString(new Date(rhs)); + } + console.log("lhsFinal---::", lhsFinal); + console.log("rhsFinal---::", rhsFinal); + + return this.checkEquality(rhsFinal, lhsFinal); + } + + + lessThanCheck(lhs: any, rhs: any): boolean { + const lhsDate = new Date(lhs); + const rhsDate = new Date(rhs); + + return lhsDate.getTime() < rhsDate.getTime(); + } + + greaterThanCheck(lhs: any, rhs: any): boolean { + const lhsDate = new Date(lhs); + const rhsDate = new Date(rhs); + + return lhsDate.getTime() > rhsDate.getTime(); + } + + lessThanDate(lhs: any, rhs: any): boolean { + let lhsFinal: string | undefined; + let rhsFinal: string | undefined; + let typeLhs = typeof lhs; + let typeRhs = typeof rhs; + + if (typeLhs === "object") { + lhsFinal = this.convertDateObjectToString(lhs); + } + if (typeLhs === "string") { + if (this.isIsoDateString(lhs)) { + lhsFinal = this.convertDateObjectToString(new Date(lhs)); + } else { + if (lhs.length <= 10) { + if (this.countObliqueIsTwo(lhs)) { + lhsFinal = lhs; + } else if (this.countHyphenIsTwo(lhs)) { + lhsFinal = lhs; + } + } else if (lhs.length > 10) { + lhsFinal = this.formatDate(lhs); + } + } + } + if (typeLhs === "number") { + lhsFinal = this.convertDateObjectToString(new Date(lhs)); + } + + if (typeRhs === "object") { + rhsFinal = this.convertDateObjectToString(rhs); + } + if (typeRhs === "string") { + if (this.isIsoDateString(rhs)) { + rhsFinal = this.convertDateObjectToString(new Date(rhs)); + } else { + if (rhs.length <= 10) { + if (this.countObliqueIsTwo(rhs)) { + rhsFinal = rhs; + } else if (this.countHyphenIsTwo(rhs)) { + rhsFinal = rhs; + } + } else if (rhs.length > 10) { + rhsFinal = this.formatDate(rhs); + console.log("rhsFinal :: ", rhsFinal); + } + } + } + + if (typeRhs === "number") { + rhsFinal = this.convertDateObjectToString(new Date(rhs)); + } + console.log("lhsFinal---::", lhsFinal); + console.log("rhsFinal---::", rhsFinal); + + return this.lessThanCheck(lhsFinal, rhsFinal); + + } + + greaterThanDate(lhs: any, rhs: any): boolean { + let lhsFinal: string | undefined; + let rhsFinal: string | undefined; + let typeLhs = typeof lhs; + let typeRhs = typeof rhs; + + if (typeLhs === "object") { + lhsFinal = this.convertDateObjectToString(lhs); + } + if (typeLhs === "string") { + if (this.isIsoDateString(lhs)) { + lhsFinal = this.convertDateObjectToString(new Date(lhs)); + } else { + if (lhs.length <= 10) { + if (this.countObliqueIsTwo(lhs)) { + lhsFinal = lhs; + } else if (this.countHyphenIsTwo(lhs)) { + lhsFinal = lhs; + } + } else if (lhs.length > 10) { + lhsFinal = this.formatDate(lhs); + } + } + } + if (typeLhs === "number") { + lhsFinal = this.convertDateObjectToString(new Date(lhs)); + } + + if (typeRhs === "object") { + rhsFinal = this.convertDateObjectToString(rhs); + } + if (typeRhs === "string") { + if (this.isIsoDateString(rhs)) { + rhsFinal = this.convertDateObjectToString(new Date(rhs)); + } else { + if (rhs.length <= 10) { + if (this.countObliqueIsTwo(rhs)) { + rhsFinal = rhs; + } else if (this.countHyphenIsTwo(rhs)) { + rhsFinal = rhs; + } + } else if (rhs.length > 10) { + rhsFinal = this.formatDate(rhs); + console.log("rhsFinal :: ", rhsFinal); + } + } + } + + if (typeRhs === "number") { + rhsFinal = this.convertDateObjectToString(new Date(rhs)); + } + console.log("lhsFinal---::", lhsFinal); + console.log("rhsFinal---::", rhsFinal); + + return this.greaterThanCheck(lhsFinal, rhsFinal); + + } + + // evaluate the conditions and return the same array /** * @@ -151,6 +430,10 @@ export class Executor implements OnModuleInit { expression._lhs = valA; expression._rhs = valB; + let dateAreEqual = this.checkDates(expression._lhs, expression._rhs); + let lessThanDate = this.lessThanDate(expression._lhs, expression._rhs); + let greaterThanDate=this.greaterThanDate(expression._lhs,expression._rhs); + // both the values can not be undefined if (valA === undefined && valB === undefined) { throw new Error(`Condition [${condition.name}] expression [${JSON.stringify(expression)}] have [lhs] and [rhs] values as [undefined]!`); @@ -160,6 +443,16 @@ export class Executor implements OnModuleInit { throw new Error(`Condition [${condition.name}] expression [${JSON.stringify(expression)}] has no operator property set!`); } + if (dateAreEqual === false) { + if(lessThanDate==true){ + console.log('lhsrhs') + } + + } + // //Check if value is Not a number, if so JSON.Stringify it to make a 'simple' object comparison // //dangerous in code, but ok for JSON workflows in processus // if (isNaN(valA)) { valA = JSON.stringify(valA); } From f20ec72e293959955427b20dc8f02e536e49f880 Mon Sep 17 00:00:00 2001 From: Chinmay Date: Tue, 1 Aug 2023 10:08:30 +0530 Subject: [PATCH 2/3] refactor removed for loops and replaced with regex expressions and created a common method countOcurrences which will be invoked by countObliqueIsTwo & countHypenIsTwo method --- src/modules/tasks/providers/executor.ts | 63 +++++++------------------ 1 file changed, 18 insertions(+), 45 deletions(-) diff --git a/src/modules/tasks/providers/executor.ts b/src/modules/tasks/providers/executor.ts index 880adf0..76eb22d 100644 --- a/src/modules/tasks/providers/executor.ts +++ b/src/modules/tasks/providers/executor.ts @@ -126,64 +126,37 @@ export class Executor implements OnModuleInit { } checkEquality(lhsFinal: any, rhsFinal: any): boolean { - if (lhsFinal == rhsFinal) { - return true; - } - - return false; + return (lhsFinal==rhsFinal) } countObliqueIsTwo(str: string): boolean { - let oblique = '/'; - let count = 0; - for (let i = 0; i < str.length; i++) { - if (str[i] == oblique) { - count++; - } - } - if (count == 2) { - return true; - } - - return false; + const oblique = '/'; + const count = this.countOccurrences(str, oblique); + return count === 2; } countHyphenIsTwo(str: string): boolean { - let hyphen = '-'; - let count = 0; - for (let i = 0; i < str.length; i++) { - if (str[i] == hyphen) { - count++; - } - } - if (count == 2) { - return true; - } - - return false; + const hyphen = '-'; + const count = this.countOccurrences(str, hyphen); + return count === 2; } - convertDateObjectToString(dateObject: Date): string { - let date: any = dateObject.getDate(); - let month: any = dateObject.getMonth() + 1; - let year: any = dateObject.getFullYear(); - date = date.toString(); - if (date.length == 1) { - date = '0' + date; - } - month = month.toString(); - if (month.length == 1) { - month = '0' + month; - } + countOccurrences(str: string, char: string): number { + const regex = new RegExp(char, 'g'); + const count = (str.match(regex) || []).length; + return count; + } - year = year.toString(); + convertDateObjectToString(dateObject: Date): string { + const date = String(dateObject.getDate()).padStart(2, '0'); + const month = String(dateObject.getMonth() + 1).padStart(2, '0'); + const year = String(dateObject.getFullYear()); - let result = date + '/' + month + '/' + year; + return `${date}/${month}/${year}`; + } - return result; - } From 80c0025e00a0303e81834f13c16df38ce5875723 Mon Sep 17 00:00:00 2001 From: Chinmay Date: Tue, 1 Aug 2023 14:17:01 +0530 Subject: [PATCH 3/3] refactor removed unneccessary comments and console.log statements --- src/modules/tasks/providers/executor.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/modules/tasks/providers/executor.ts b/src/modules/tasks/providers/executor.ts index 76eb22d..55bd1d3 100644 --- a/src/modules/tasks/providers/executor.ts +++ b/src/modules/tasks/providers/executor.ts @@ -158,12 +158,8 @@ export class Executor implements OnModuleInit { } - - isIsoDateString(str: string): boolean { - // The regular expression to match the ISO 8601 date string pattern const isoDateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?(Z|[+-]\d{2}:\d{2})$/; - // Test the string against the regular expression return isoDateRegex.test(str); } @@ -239,8 +235,6 @@ export class Executor implements OnModuleInit { if (typeRhs === "number") { rhsFinal = this.convertDateObjectToString(new Date(rhs)); } - console.log("lhsFinal---::", lhsFinal); - console.log("rhsFinal---::", rhsFinal); return this.checkEquality(rhsFinal, lhsFinal); } @@ -311,8 +305,6 @@ export class Executor implements OnModuleInit { if (typeRhs === "number") { rhsFinal = this.convertDateObjectToString(new Date(rhs)); } - console.log("lhsFinal---::", lhsFinal); - console.log("rhsFinal---::", rhsFinal); return this.lessThanCheck(lhsFinal, rhsFinal); @@ -369,8 +361,6 @@ export class Executor implements OnModuleInit { if (typeRhs === "number") { rhsFinal = this.convertDateObjectToString(new Date(rhs)); } - console.log("lhsFinal---::", lhsFinal); - console.log("rhsFinal---::", rhsFinal); return this.greaterThanCheck(lhsFinal, rhsFinal);