Abbreviate names with javascript

Asked

Viewed 812 times

-1

Hello I am wanting to write a function to reduce names by passing a limit, for example: limit 10 to the name: Sara Flávia Cristina to Sara F. C.

I wrote the code below, but I wonder if there’s a simpler way to do that.

Thank you.

let name = "Sabrina Boing Moreira";
let limit = 13;
verifyName(name, limit);

function verifyName(name, limit) {
  let offLimit = isOffLimit(name, limit);

  if(offLimit > 0) {
    let namesList = splitName(name);
    let reducedName = reduceName(namesList, limit, offLimit);
    print(reducedName);
  }
}

function isOffLimit(name, limit) {
  if(name.length >= limit) {
    return limit - name.length;
  } 
}

function splitName(name, limit) {
  return name.split(" ", limit);
}

function reduceName(namesList, limit, offLimit) {
  let name;
  let reduceOfLimit = limit;

  for (var i = 0; i < namesList.length; i++) {   
    name = countName(namesList[i]);
    if(reduceOfLimit != 0) {
      if(name.length + 1 == offLimit) {
        namesList[i].slice(0, offLimit);
        namesList[i] + ".";
        reduceOfLimit = 0;
      }
    } else {
      break
    }
  }
}

function countName(name) {
  return name.length;
}

function print(string) {
  console.log(string);
}
´´´ 

  • 2

    What does "limit 10" mean? And if this value were different, what would be the output, for example, to limit 15? And to limit 50? Or limit 5?

  • 1

    I also did not understand the limit, is if the name is greater than the limit there abbreviates the surnames, if it is less or equal the name does not suffer change?

  • If you pass the limit, abbreviate if you do not keep the original text. @Woss

  • 2

    And if you have "António de Sousa" or "Maria da Conceição d'Espada" as the abbreviated version?

  • would be Maria C. E. and Antonio S.

  • 2

    As you would abbreviate "Pedro de Alcântara Francisco António João Carlos Xavier de Paula Miguel Rafael Joaquim José Gonzaga Pascoal Cipriano Serafim"?

  • 1

    @Victorstafusa D. Pedro I always breaks any test... :-)

  • Thanks for all your help, I was able to adapt my code based on your help.

Show 3 more comments

2 answers

1


If I understand correctly, this must be it.

Follows the code:

console.log(abridgedControl('Sabrina Boing Moreira', 13)); //Sabrina B. M.

function abridgedControl(fullName, limit) {
    if (fullName.length > limit) {
        return toAbridged(fullName);
    }
    return fullName;
}


function toAbridged(fullName) { 
    const token = '.';
    const separator = ' ';
    const names = removePrepositions(fullName).split(separator);
    const firstName = names[0];
    let surnames = '';
    names
        .filter((name, index) => index)
        .map(name => surnames += `${separator}${name.charAt()}${token}`);
    return `${firstName}${surnames.toUpperCase()}`;
}

function removePrepositions(fullName) {
    return fullName.replace(/\ dos|\ das|\ dos|\ das|\ de|\ d\'/gi, '');
}

I took the liberty of removing the best-known prepositions.

  • Perfect, that was it, thank you very much, I’ll adjust my code.

1

This is more complex than it looks and Javascript is not very good at this because it is difficult to detect capital letters. Letters like Ì, Ó, Å are capital letters but a regex with A-Z don’t catch them. Actually the list of possibilities is giant. Take a look to this other question who has a similar problem.

Having said that, and considering only a few more common large letters, you could do so:

const testes = [
  "António de Sousa",
  "Maria da Conceição d 'Espada",
  "Johan Öström",
  "Joana Ìlhavo"
];

function abreviar(str) {
  const [nome, ...sobrenomes] = str.split(' ');

  const abreviaturas = sobrenomes.reduce((arr, str) => {
    const letraGrande = str.match(/[A-ZÖÄÅÀÁÂÃÌÍÒÓÉÊÚ]/);
    if (!letraGrande) return arr;
    return arr.concat(`${letraGrande[0]}.`);
  }, []);

  return [nome, ...abreviaturas].join(' ');
}

testes.forEach((teste, i) => {
  console.log(i, '>', abreviar(teste));
});

  • Thank you so much for the example, solved my problem in a much simpler way from the line of thought I was following.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.