src/app/lib/utilities/makeRelativeUrlPath/index.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import Url from 'url-parse';

const allowList = [
  'www.bbc.com',
  'bbc.com',
  'www.bbc.co.uk',
  'bbc.co.uk',
  'www.bbcrussian.com',
  'bbcrussian.com',
];

const makeRelativeUrlPath = urlPath => {
  if (!urlPath) return null;

  const url = new Url(urlPath);
  const isBBCDomain = allowList.some(domain => url.hostname === domain);

  if (isBBCDomain) {
    return `${url.pathname}${url.query}${url.hash}`;
  }

  return urlPath;
};

export default makeRelativeUrlPath;