As mentioned previously, I want to clarify that I may not have the exact criteria you're looking for, but the following function enhances a string and meets all specified conditions by splitting it using any of the methods below:
- Separating at
-
and converting to title case,
- Dividing at
_
and converting to title case,
- Or breaking down camelCase notation and converting to title case.
You can perform additional string manipulations later on (e.g. capitalizing the first three letters as shown in your result samples).
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 wish to capitalize the first three letters, a good approach is to define a dynamic function specifically for this task. Then, simply nest the function calls accordingly.
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