Is there a way to optimize the following method or provide any suggestions on what can be improved? I am trying to create a function that converts author names from uppercase to only the first letter capitalized, while excluding certain words ('de', 'el', 'la', "'", "-") which are common in Spanish.
methods: {
nameAuthor (v) {
const author = v.toLowerCase().split(' ')
const exceptions = ["'", '-', 'y', 'de', 'la']
const length = exceptions.length
for (let i = 0; i < author.length; i++) {
for (let j = 0; j < length; j++) {
if (!exceptions.includes(author[i])) {
author[i] = author[i].charAt(0).toUpperCase() + author[i].substring(1)
}
}
if (author[i].includes(exceptions[0])) {
const specialCharacter = author[i].split("'")
author[i] = specialCharacter[0] + "'" + specialCharacter[1].charAt(0).toUpperCase() + specialCharacter[1].substring(1)
}
if (author[i].includes(exceptions[1])) {
const specialCharacter = author[i].split('-')
author[i] = specialCharacter[0] + '-' + specialCharacter[1].charAt(0).toUpperCase() + specialCharacter[1].substring(1)
}
}
return author.join(' ')
}
}
Appreciate your help!