Browse Source

include regex search

Enzo 1 năm trước cách đây
mục cha
commit
55eade2299
2 tập tin đã thay đổi với 30 bổ sung11 xóa
  1. 25 9
      services/query.service.ts
  2. 5 2
      test/test1.ts

+ 25 - 9
services/query.service.ts

@@ -25,18 +25,27 @@ export class queryService {
                 clearInterval(intervalId);
                 this.dataFromStorage.complete();
             }
-        }, 1000)
+        }, 250)
     }
 
     // Search and Filter: Pure Observables. To be moved out to become a separate service again.
     private filterFromObs(...conditions: Conditions[]) {
+        let enquiry = conditions[0]
+        let key = Object.keys(enquiry)[0]
         this.dataFromStorage.subscribe({
             next: element => {
-                // Logic to check if data meets the conditions, if so, put it into result.next{}
-                if (this.hasKeyAndValue(element, ...conditions)) {
-                    this.filteredResult.next(element)
+                if (key == "regex") {
+                    if (this.filterViaRegex(element, enquiry)) {
+                        this.filteredResult.next(element)
+                    } else {
+                        // console.log(`${element.header.messageName} does not match search criteria`)
+                    }
                 } else {
-                    // console.log(`${element.header.messageName} does not match search criteria`)
+                    if (this.hasKeyAndValue(element, ...conditions)) {
+                        this.filteredResult.next(element)
+                    } else {
+                        // console.log(`${element.header.messageName} does not match search criteria`)
+                    }
                 }
             }
         })
@@ -62,7 +71,7 @@ export class queryService {
 
     // Logic 2: Success: More superior version than Logic 1 since it can perform flat searches like {messageID : 1234} without specifying its nested properties
     private hasKeyAndValue(data, ...conditions): boolean {
-        // Merge all condtions into searchObj
+        // Merge all conditions into searchObj
         let searchObj = Object.assign({}, ...conditions)
         if (typeof data !== 'object' || typeof searchObj !== 'object') {
             return false;
@@ -88,8 +97,6 @@ export class queryService {
             });
         };
 
-
-
         let isObjectMatching = (object) => {
             if (typeof object !== 'object') {
                 return false;
@@ -114,6 +121,14 @@ export class queryService {
         the conditions specified in searchObj. 
         PS: this function is not my code. */
     }
+
+    private filterViaRegex(element: any, inquiry: any): boolean {
+        // create a new regular expression to use regex.test
+        const regex = new RegExp(inquiry.regex);
+        const hasMatchingSubstring = regex.test(JSON.stringify(element));
+        return hasMatchingSubstring;
+    }
+    
 }
 
 
@@ -127,7 +142,8 @@ export interface Conditions {
     msgDateTime?: Date | string,
     msgTag?: string[],
     msgPayload?: string,
-    messageID?: string
+    messageID?: string,
+    regex?: string
 }
 
 export interface Storage {

+ 5 - 2
test/test1.ts

@@ -17,11 +17,14 @@ let conditions2: Conditions[] = [
     { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
     { "msgLogDateTime": "2023-01-14T21:50:19.917Z" },
 ]
-query.query(storageAddress, ...conditions).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
+
+let conditions3: Conditions[] = [
+    { "regex": "cum incidunt maxime voluptatibus" }
+]
+query.query(storageAddress, ...conditions3).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
 
 // query.query(storageAddress, ...conditions2).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
 
 // the key is to do it in one line. Client just pass 2 arguments, one is the location of the data, which could be file, sql or mongodb, and also
 // pass in the conditions of their search enquiries. We will aslo have to cater to different file storage location to determine how to prep the
 // data to be filtered
-