Removing Unwanted Words by JavaScript

Scenario: The formatFundName function cleans up a string by removing specific words like “The” at the beginning and “Fund” at the end. This is useful for standardizing names while keeping the core meaning intact. It uses startsWith() and endsWith() to check if the string contains these words and then removes them using replace().

/**
* @description format string by eliminating special words from the string
* @author JJ0021
* @param {string} parameter string
* @returns {string} formatted string
*/
function formatFundName(parameter) {
 try {
  parameter = (parameter.startsWith('The ') || parameter.startsWith('the ')) ? parameter.replace('The ', '').replace('the ', '') : parameter;
  parameter = (parameter.endsWith(' Fund') || parameter.endsWith(' fund')) ? parameter.replace(' Fund', '').replace(' fund', '') : parameter
   return parameter;
   } catch (er) {
  log.debug("error@formatFundName", er.message);
 }
}

Output:

console.log(formatFundName(“The Green Fund”)); // Output: “Green”

console.log(formatFundName(“Blue Fund”));   // Output: “Blue”

This function ensures consistency in fund names and avoids redundant words 

Leave a comment

Your email address will not be published. Required fields are marked *