Function to get title based on pathname this code seems fine at once but the problem I see with this code maintainability I can imagine if one will have to add 10 or more routes the function will grow badly.
can we refactor this code for better maintainibility .
export const getMetaDescription = (pathname) =&gt; {
  if (pathname.includes(&quot;about&quot;)) return &quot;About Us | &quot; + BASE_SITE_NAME;
  else if (pathname.includes(&quot;fees&quot;)) return &quot;Our Fees | &quot; + BASE_SITE_NAME;
  else if (pathname.includes(&quot;legal&quot;)) return &quot;Legal Info | &quot; + BASE_SITE_NAME;
  else if (pathname.includes(&quot;register&quot;))
    return &quot;Registration | &quot; + BASE_SITE_NAME;
  else if (pathname.includes(&quot;login&quot;)) return &quot;Login | &quot; + BASE_SITE_NAME;
  return BASE_SITE_NAME;
};

Edit:
i had a similar function which i refactor by using a map (kind of)
const titles = {
  about: &quot;About Us&quot;,
  login: &quot;Login&quot;,
  wallet: &quot;Wallet&quot;,
  profile: &quot;Profile&quot;,
  beneficiary: &quot;Beneficiary&quot;,
};

export const getMetaTitle = pathname =&gt; {
  for (let key in titles) {
    if (pathname.includes(key)) {
      return titles[key] + &quot; | &quot; + BASE_SITE_NAME;
    }
  }
  return BASE_SITE_NAME;
};
I'd make an object whose key is the pathname and whose value is the unique string for that pathname, then iterate through the object to find the matching key:


const BASE_SITE_NAME = 'BASE_SITE_NAME';
const descriptions = {
    about: 'About Us',
    fees: 'Our Fees',
    legal: 'Legal Info',
    register: 'Registration',
    login: 'Login',
};
const getMetaDescription = (pathname) =&gt; {
    const foundDescriptionKey = Object.keys(descriptions).find(key =&gt; pathname.includes(key));
    return foundDescriptionKey
        ? descriptions[foundDescriptionKey] + ' | ' + BASE_SITE_NAME
        : BASE_SITE_NAME;
};

console.log(getMetaDescription('about'));
console.log(getMetaDescription('foobar'));



If pathname is directly from the URL in the browser, make sure to call toLowerCase() on it first (just in case, for example, someone types in example.com/ABOUT instead of clicking a link that redirects to /about).
This is faithful to your original code. But if each of the descriptions correspond to an entirely separate route, it'd be better to just use plain property lookup. For example, if the pathname is /about, extract the about from that and look up that key on the object, instead of iterating.


const BASE_SITE_NAME = 'BASE_SITE_NAME';
const descriptions = {
    about: 'About Us',
    fees: 'Our Fees',
    legal: 'Legal Info',
    register: 'Registration',
    login: 'Login',
};
const getMetaDescription = (pathname) =&gt; {
    const description = descriptions[pathname.slice(1)];
    return description
        ? description + ' | ' + BASE_SITE_NAME
        : BASE_SITE_NAME;
};

console.log(getMetaDescription('/about'));
console.log(getMetaDescription('/foobar'));



The .includes approach is needed only if you have multiple pathnames associated with a single description. Even if you have multiple pathnames associated with a single description, consider using startsWith instead - it'll be more precise and less likely to have collisions. For example, if you have example.com/profile/aboutme, the about would result in About Us being displayed, which would not be desirable.


const BASE_SITE_NAME = 'BASE_SITE_NAME';
const descriptions = {
    about: 'About Us',
    fees: 'Our Fees',
    legal: 'Legal Info',
    register: 'Registration',
    login: 'Login',
    profile: 'Profile',
};
const getMetaDescription = (pathname) =&gt; {
    const foundDescriptionKey = Object.keys(descriptions).find(key =&gt; pathname.slice(1).startsWith(key));
    return foundDescriptionKey
        ? descriptions[foundDescriptionKey] + ' | ' + BASE_SITE_NAME
        : BASE_SITE_NAME;
};

console.log(getMetaDescription('/about'));
console.log(getMetaDescription('/profile/aboutme'));
console.log(getMetaDescription('/foobar'));