Rahul Kundu
Rahul Kundu

Reputation: 125

Modify a string's first letters based on certain types

How do I make the first letter of a string uppercase based on certain conditions like below -

I have done this function below, but with this approach, the last three conditions are not satisfying

beautifyString(key: string): string {
    let separateWord: string[];

    if (key.includes('_')) {
        separateWord = key.split('_');
    } else if (key.includes('-')) {
        separateWord = key.split('-');
    } else {
        separateWord = key.split(/(?=[A-Z])/g);
    }

    for (let i = 0; i < separateWord.length; i++) {
        separateWord[i] =
            separateWord[i].charAt(0).toUpperCase() +
            separateWord[i].substring(1);
    }

    return separateWord.join(' ');
}

Upvotes: 0

Views: 65

Answers (1)

Behemoth
Behemoth

Reputation: 9310

As mentioned in the comments im not 100% positive what your criterion is but the function below beautifies a string and fulfills all your conditions by splitting it up either

  • at - and title-casing it,
  • at _ and title-casing it,
  • or by splitting up the camelCase and title-casing it.

You can to additional string manipulation later on (e. g. Capitalizing the first three letters as given in your result examples).

const beautifyString = (word) => {
  let result = "";
  const splitters = ["-", "_"];

  if (word.includes(splitters[0])) result = word.split(splitters[0]);
  else if (word.includes(splitters[1])) result = word.split(splitters[1]);
  else result = word.replace(/([a-z])([A-Z])/g, "$1 $2").split(" ");

  return [...result.map((e, i) => e[0].toUpperCase() + e.slice(1))].join(" ");
};

console.log(beautifyString("booking_engine"));
console.log(beautifyString("booking-engine"));
console.log(beautifyString("bookingEngine"));
console.log(beautifyString("crsProvider"));
console.log(beautifyString("crs_Provider"));
console.log(beautifyString("crs-Provider"));

TypeScript Version

const beautifyString: (word: string) => string = (word: string) => {
  let result: string[] = [];
  const splitters = ["-", "_"] as const;

  if (word.includes(splitters[0])) result = word.split(splitters[0]);
  else if (word.includes(splitters[1])) result = word.split(splitters[1]);
  else result = word.replace(/([a-z])([A-Z])/g, "$1 $2").split(" ");

  return [
    ...result.map((e: string, i: number) => e[0].toUpperCase() + e.slice(1)),
  ].join(" ");
};

console.log(beautifyString("booking_engine")); // Booking Engine
console.log(beautifyString("booking-engine")); // Booking Engine
console.log(beautifyString("bookingEngine"));  // Booking Engine
console.log(beautifyString("crsProvider"));    // Crs Provider
console.log(beautifyString("crs_Provider"));   // Crs Provider
console.log(beautifyString("crs-Provider"));   // Crs Provider

Edit

If you want to capitalize the first three letters I suggest just defining a function which does the job dynamically. And then just nest the functions calls.

const beautifyString = (word) => {
  let result = "";
  const splitters = ["-", "_"];

  if (word.includes(splitters[0])) result = word.split(splitters[0]);
  else if (word.includes(splitters[1])) result = word.split(splitters[1]);
  else result = word.replace(/([a-z])([A-Z])/g, "$1 $2").split(" ");

  return [...result.map((e, i) => e[0].toUpperCase() + e.slice(1))].join(" ");
};

const capitalizeFirstLetters = (word, amount) =>
  word.slice(0, amount).toUpperCase() + word.substr(amount, word.length);

console.log(capitalizeFirstLetters(beautifyString("crs_Provider"), 3));

TypeScript Version

const capitalizeFirstLetters: (word: string, amount: number) => string = (
  word: string,
  amount: number
) => word.slice(0, amount).toUpperCase() + word.substr(amount, word.length);

console.log(capitalizeFirstLetters(beautifyString("crs_Provider"), 3));  // CRS Provider

Upvotes: 1

Related Questions