import { URL } from 'url'; type PageData = Pick; /** * Given a list of paths, provides a minimal list of paths that would serve * as prefixes for all of the provided paths. * */ export const getMinimumCommonPath = (paths: string[]): string => { if (paths.length === 0) { return '/'; } return paths.reduce((prev, path) => { const segments = path.split('/'); const prevSegments = prev.split('/'); const commonSegments = new Array(); prevSegments.forEach((segment, index) => { if (segments[index] == segment && commonSegments.length === index) { commonSegments.push(segment); } }, []); const commonSegmentsAsString = commonSegments.join('/'); return commonSegmentsAsString.length === 0 ? '/' : commonSegmentsAsString; }, paths[0]); }; /** * Takes a list of urls and provides a minimal set of hosts and pathnames that * could be used to determine the minimal path to capture all of the paths, * grouped by hostname. */ export const getMinimumPageURLs = (urls: PageData[]): PageData[] => { const hosts = Array.from(new Set(urls.map(({ host }) => host))); return hosts.map((host) => { const allPathnames = Array.from( new Set( urls.filter((url) => host === url.host).map(({ pathname }) => pathname) ) ); const pathname = getMinimumCommonPath(allPathnames); return { host, pathname }; }); };