Skip to content

Instantly share code, notes, and snippets.

@edvaldoszy
Created January 30, 2019 13:25
Show Gist options
  • Select an option

  • Save edvaldoszy/9073661da00484632259135f10705aa5 to your computer and use it in GitHub Desktop.

Select an option

Save edvaldoszy/9073661da00484632259135f10705aa5 to your computer and use it in GitHub Desktop.
Função para abreviar nomes grandes
function abrevia(str) {
const nome = str.replace(/\s+/gi, ' ')
.trim();
return nome.split(' ')
.map((parte, index, nomes) => (index == (nomes.length - 1)) ? parte : `${parte[0]}.`)
.join(' ');
}
@yanagassi
Copy link
Copy Markdown

yanagassi commented Apr 29, 2019

Olá, o ideal seria:

function abrevia(str) {
 const nome = str.replace(/\s+/gi, ' ').trim();
    var array_nome = nome.split(' ');
    if(array_nome.length > 2){
        return array_nome.map((parte, index, nomes) => (index != 1) ? parte : `${parte[0]}.`).join(' ');
    }else{
        return str;
    }
}

Sendo assim ele vai poder abreviar somente o primeiro nome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment