I need to retrieve names from a database, these names can be searched using accented and unaccented vowels, in both ways the response has to return all matched names. The approach I used to solve this problem is a function that replaces the vowels from the searched name with a string that contains both vowels in order to look for both options with a regex method.
How can I optimize this function?
const replaceVowels = fullName => {
const v = [
{ vocal: "a", replace: "[aá]" },
{ vocal: "e", replace: "[eé]" },
{ vocal: "i", replace: "[ií]" },
{ vocal: "o", replace: "[oó]" },
{ vocal: "u", replace: "[uú]" },
{ vocal: "á", replace: "[aá]" },
{ vocal: "é", replace: "[eé]" },
{ vocal: "í", replace: "[ií]" },
{ vocal: "ó", replace: "[oó]" },
{ vocal: "ú", replace: "[uú]" }
];
for (let i = 0; i < v.length; i++) {
fullName = fullName.replace(new RegExp(v[i].vocal, "gi"), v[i].replace);
}
return { $regex: fullName, $options: "i" };
};
replaceVowels("mayúsculas");
// returns "m[aá]y[uú]sc[uú]l[aá]s"