0

I have 2 strings that contain functions, as a string. I need to combine both of these strings but sometimes the functions are duplicate. These functions will always start with public, do not contain nested functions & will always have a unique function name.

Small example:

let str1 = "
   public getPetById(petId: number, header: {}) {
      return this.http.get(this.baseUrl + pet/${petId}`, { headers: header });
   }

   public updatePetWithForm(petId: number, body: {name?: string, status?: string}, header: {}) {
      return this.http.post(this.baseUrl + `/pet/${petId}`, body, { headers: header });
   }
";

let str2 = "
   public getPetById(petId: number, header: {}) {
      return this.http.get(this.baseUrl + `/pet/${petId}`, { headers: header });
   }

   public deletePet(petId: number, header: {api_key: string}) {
      return this.http.delete(this.baseUrl + `/pet/${petId}`, { headers: header });
   }
";

This is how I'm currently detecting duplicates:

function hasDuplicates(array: any[]) { //https://stackoverflow.com/a/7376645/21297478
   return (new Set(array)).size !== array.length;
}
hasDuplicates(combinedString.match(/(?<=public ).+(?=[$\(])/gm));

Desired result:

"public getPetById(petId: number, header: {}) {
   return this.http.get(this.baseUrl + pet/${petId}`, { headers: header });
}
public updatePetWithForm(petId: number, body: {name?: string, status?: string}, header: {}) {
   return this.http.post(this.baseUrl + `/pet/${petId}`, body, { headers: header });
}
public deletePet(petId: number, header: {api_key: string}) {
   return this.http.delete(this.baseUrl + `/pet/${petId}`, { headers: header });
}"

But I haven't found a way to remove said duplicates.

How would I go about combining these strings and exclude one entry of the function 'getPetById' in the previous example?

  • Are you trying to detect duplicate function *names*, or only identical duplicate functions? How consistent is your source data? i.e. are all the functions labeled `public`, are there ever any functions nested inside other functions, are there always two empty lines between functions as shown here, etc. If the input is very regular, this could be doable with regex, but if you want to be able to throw arbitrary javascript structures at it you're going to drown in edge cases. (I hope this is an exercise and not a data structure someone decided to use on purpose?) – Daniel Beck Feb 27 '23 at 14:07
  • Using regex to parse code is never a good idea, so many edge cases for your code to fail. For doing any kind of `code` processing it's usually a good idea to look into parsing AST's. There are lots of free Lib's for doing this. – Keith Feb 27 '23 at 14:07
  • I am trying to filter out the whole duplicate function, function names should be unique @DanielBeck and will always start with `public`, no nested functions either – Matthias VdC Feb 27 '23 at 14:10
  • So... the names *should* be unique, or they *are* unique? If the structure of your input is very predictable, you can guarantee there are no nested functions, and that two functions with the same name are automatically considered duplicates, this might be possible; I'd start by splitting the string into an array on the word `public`. If you have to actually test the function contents for similarity, regex is not viable and this becomes a Hard Problem; any whitespace difference would break things. (Note that your multiline strings are not valid input, so that'd be the first thing to fix) – Daniel Beck Feb 27 '23 at 14:21

0 Answers0