2

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.

4
  • 3
    Options in JavaScript can also be used in TypeScript. Commented Feb 8, 2022 at 7:08
  • 1
    typescript is a superset of javascript. Everything which works in javascript works in typescript. Commented Feb 8, 2022 at 7:09
  • 2
    You may have forgotten to ask a question... Stack overflow is not a coding service where you drop in your requirements and get out some code. What have you tried yourself? Commented Feb 8, 2022 at 7:14
  • Please find the UPDATE in question. Commented Feb 8, 2022 at 9:14

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the answer Kevin. Your code is throwing Object is possibly undefined for wordsInName in for loop. Sorry that I am new to typescript and need help on this.
I change the loop definition now to for (const word of (wordsInName ?? [])). It is a shortcut, you can also surround the loop if an if statement where the condition is, that wordsInName is not undefined.
@Mike, was this the solution or do you still have compiling errors?
No Kevin, this didn't work. For my input with Mr Tom as fullName, the formOfAddress was populated as Mr Tom and name is populated as null.
@Mike, check stackblitz.com/edit/typescript-iyk3ul?file=index.ts and the code above, then you have your working example. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.