user3142695
user3142695

Reputation: 17332

Convert string with multiple full names into names with first character surname

I need to convert a string with multiple names and an optional 'et al' at the end of the string to a string with name and first character of each surnames.

To make it easier to understand:

Name Firstname, name firstname secondname et al
Name Firstname, name firstname secondname, et al
Name Firstname,name firstname secondname,et al
Name Firstname, name firstname secondname, et al.
Name Firstname,     name     firstname     secondname,   et   al.
Name F., name firstn. sec., et al.
Name F, name f s, et al.

...should all result in:

Name F, Name FS et al.

So this is what I'm using so far, but the first example fails as the last comma is missing.

I the first step has to be extracting the optional 'et al' string. After this, the split makes sense...

And maybe someone has an idea to make the code a bit easier:

const names = string.split(',')
const etal = names[names.length - 1].indexOf('et al') !== -1 && names.pop().trim()

const condensed = names.map(function (name) {
  return name.trim().split(' ').reduce(function (r, part, index) {
    return index ? r + part[0].toUpperCase() : part + ' '
  }, '')
})

etal && condensed.push('et al.')
return condensed.join(', ').trim()

Upvotes: 0

Views: 102

Answers (2)

Matthew Lagerwey
Matthew Lagerwey

Reputation: 120

I think the approach your using is lengthy and (difficult to read, because you are chaining function calls) I would use a Regular Expression to rid the elements you desire. The Regular Expression would then look something like this you could first get rid of extra spaces and "FirstName, " and " name "

string.replace(/[ ]{2,}/g , " ");
string.replace(/FirstName, | name /g, "");

That leaves you with Name firstname, secondname, et al

then you would write another Regular Expression like

let set = /\B .+ .+\B(?=, et al\b)/g;

then

string.replace( set, "");

For a more thorough discussion of Regular Expressions visit this link

You could also combine all the steps but that makes Regular Expressions ridiculously difficult to read and decipher what is going on. Regular Expressions are built for doing these kinds of replacements with fewer lines of code. Instead of chaining function calls you have 3 or 4 calls to string replace if you do it in steps makes the code a lot easier to read. This doesn't account for all the inconsistencies but hopefully you get the general idea.

Upvotes: 0

Mamun
Mamun

Reputation: 68933

There might be better solution but I came up with the following:

var names = [
  "Name Firstname,name firstname secondname,et al",
  "Name Firstname, name firstname secondname, et al.",
  "Name Firstname,     name     firstname     secondname,   et   al.",
  "Name F., name firstn. sec., et al.",
  "Name F, name f s, et al."
];
var nameRes = names.map(function(name){
  nameTemp = name.split(',');
  let res = "";
  nameTemp.forEach(function(n, i){
    if(i==0){
      res = n.split(' ')[0] + " " + n.split(' ')[1].charAt(0);
    }
    if(i==1){
      n = n.split(' ').filter(j=>j!=="");
      var n1 = n[0].charAt(0).toUpperCase() + n[0].slice(1);
      var n2 = (n[1].charAt(0) + n[2].charAt(0)).toUpperCase();
      n = n1+' '+n2
      res += ', ' + n;
    }
    if(i==2){
      res += ' ' + n.split(' ').filter(j=>j!=="").join(' ');
    }
  });
  
  return res;
});
console.log(nameRes);

Upvotes: 1

Related Questions