Mike
Mike

Reputation: 801

Typescript : Check for words in a string

There are many options in javascript, but are there any options in typescript to do the find words in a given string:

Eg:

For the list: ['Mr', 'Mrs', 'FM.', 'Sir'] and a string called 'Sir FM. Sam Manekshaw'. I have the words 'Sir' and 'FM.' present, hence needs to be assigned to a string and the remaining parts of the string assigned to another string. i.e:

a = Sir FM.
b = Sam Manekshaw

NOTE: It should validate only full words and not a substring.

UPDATE: What I tried:

var tempPrefixList = ['Mr', 'Mrs', 'FM.', 'Sir'];
var firstName = "Sir FM. Sam Manekshaw";

var prefixSearchExp = new RegExp(tempPrefixList.join("|"),"gi");
if(firstName && prefixSearchExp.test(firstName)) {
    console.log("Caught");
    var requestFirstNameSplit = firstName?.split(" ");
    console.log("Prefix: " + requestFirstNameSplit[0]);
    console.log("First name: " + requestFirstNameSplit[1]);
}

But this considers only if the name has one Prefix. And also, has only one name in firstName. Eg: Sir Sam. But doesn't work for the example I mentioned earlier.

Upvotes: 2

Views: 2430

Answers (1)

Kevin Glier
Kevin Glier

Reputation: 1386

You can solve it like this:

class PersonDetails {
  formOfAddress: string;
  name: string;

  constructor(formOfAddress: string, name: string) {
    this.formOfAddress = formOfAddress;
    this.name = name;
  }

  get fullName(): string {
    return [this.formOfAddress, this.name].join(' ');
  }
}

function extractPersonDetailsFromFullName(fullName: string): PersonDetails {
  const formOfAddresses = ['Mr', 'Mrs', 'FM.', 'Sir'];

  let usedFormOfAddresses = [];

  const wordsInName = fullName.split(' ').filter((x) => x);
  for (const word of wordsInName ?? []) {
    if (formOfAddresses.filter((foa) => foa == word).length == 1) {
      usedFormOfAddresses.push(word);
    }
  }

  const formOfAddress = usedFormOfAddresses.join(' ');

  return new PersonDetails(
    formOfAddress,
    fullName.substr(formOfAddress.length).trim()
  );
}

const testNames = ['Sir FM. Sam Manekshaw', 'Mr Tom'];

for (const testName of testNames) {
  const personDetails = extractPersonDetailsFromFullName(testName);
  appDiv.innerHTML +=
    testName +
    ' => <br>&nbsp;&nbsp;' +
    JSON.stringify(personDetails) +
    '<br><br>';
}

Code: https://stackblitz.com/edit/typescript-iyk3ul?file=index.ts

If you don't want to use the implementation with the separate function and object, you can use the following code:

const fullName = 'Sir FM. Sam Manekshaw';

const formOfAddresses = ['Mr', 'Mrs', 'FM.', 'Sir'];

let formOfAddressParts = [];
let nameParts = [];

const wordsInName = fullName.split(' ').filter((x) => x);
for (const word of wordsInName ?? []) {
  if (formOfAddresses.filter((foa) => foa == word).length == 1) {
    formOfAddressParts.push(word);
  } else {
    nameParts.push(word);
  }
}

let formOfAddress = formOfAddressParts.join(' ');
let name = nameParts.join(' ');

console.log('formOfAddress: ', formOfAddress);
console.log('name: ', name);

Upvotes: 2

Related Questions