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?