Ver código fonte

further enhancements on the search functionality.

Enzo 1 ano atrás
pai
commit
eb00de0e66
2 arquivos alterados com 29 adições e 17 exclusões
  1. 14 16
      services/query.service.ts
  2. 15 1
      test/test1.ts

+ 14 - 16
services/query.service.ts

@@ -33,18 +33,10 @@ export class queryService {
         let searchObj = Object.assign({}, ...conditions)
         this.dataFromStorage.subscribe({
             next: element => {
-                if (Object.keys(conditions).length == 1 && searchObj.hasOwnProperty('regex')) {
-                    if (this.filterViaRegex(element, searchObj.regex)) {
-                        this.filteredResult.next(element)
-                    } else {
-                        // console.log(`${element.header.messageName} does not match search criteria`)
-                    } 
+                if (this.filterByKeyValue(element, ...conditions)) {
+                    this.filteredResult.next(element)
                 } else {
-                    if (this.filterByKeyValue(element, ...conditions)) {
-                        this.filteredResult.next(element)
-                    } else {
-                        // console.log(`${element.header.messageName} does not match search criteria`)
-                    }
+                    // console.log(`${element.header.messageName} does not match search criteria`)
                 }
             }
         })
@@ -73,23 +65,29 @@ export class queryService {
         // Merge all conditions into searchObj
         let searchObj = Object.assign({}, ...conditions)
         let dateCheck = true
+        let regexCheck = true
         if (searchObj.hasOwnProperty("dateRange")) {
             dateCheck = this.filterByDateRange(data, searchObj.dateRange)
             // Must delete, otherwise the function below will attempt to match date range with the date property and it will inevitably returns false
             delete searchObj.dateRange
-        } 
+        }
+        if (searchObj.hasOwnProperty("regex")) {
+            dateCheck = this.filterViaRegex(data, searchObj.regex)
+            // Must delete, otherwise the function below will attempt to match date range with the date property and it will inevitably returns false
+            delete searchObj.regex
+        }
         if (typeof data !== 'object' || typeof searchObj !== 'object') {
             return false;
         }
 
-        if(dateCheck == true){
+        if (dateCheck == true && regexCheck == true) {
             let matchKeys = Object.keys(searchObj);
             let isMatchingObject = (object) => {
                 return matchKeys.every((key) => {
                     let lodashPath = key.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '');
                     let objectValue = _.get(object, lodashPath);
                     let searchValue = searchObj[key];
-    
+
                     if (Array.isArray(searchValue) && key === 'msgTag') {
                         // Check if any of the search values are included in the object value
                         return searchValue.some((value) => {
@@ -102,14 +100,14 @@ export class queryService {
                     }
                 });
             };
-    
+
             let isObjectMatching = (object) => {
                 if (typeof object !== 'object') {
                     return false;
                 }
                 return isMatchingObject(object) || Object.values(object).some(isObjectMatching);
             };
-    
+
             return isObjectMatching(data);
         } else {
             return false

+ 15 - 1
test/test1.ts

@@ -53,6 +53,20 @@ let conditions5: Conditions[] = [
     { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
     { "msgLogDateTime": "2023-01-14T21:50:19.917Z" }
 ]
-query.query(storageAddress, ...conditions4).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
+
+// Ultimate search. With all conditions piling at once: Should at least returns 1 data
+let conditions6: Conditions[] = [
+    {
+        "dateRange": {
+            "startDate": "2023-04-09T21:00:00.000Z",
+            'endDate': "2023-04-10T00:00:00.000Z"
+        }
+    },
+    { "regex": "maxime voluptatibus ad quasi eveniet" },
+    { 'msgTag': ['basic'] },
+    { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+    { "msgLogDateTime": "2023-01-14T21:50:19.917Z" }
+]
+query.query(storageAddress, ...conditions6).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })