export function normalizePath()

in src/common/symbol-id.ts [185:210]


export function normalizePath(sourcePath: string, rootDir?: string, outDir?: string): string {
  if (rootDir === undefined || outDir === undefined) {
    return sourcePath;
  }

  outDir = removeEndSlash(path.normalize(outDir));
  const outDirLength = outDir.split(path.sep).length;
  rootDir = removeEndSlash(path.normalize(rootDir));

  let paths = path.normalize(sourcePath).split(path.sep);
  const pathDir = paths.slice(0, outDirLength).join(path.sep);

  if (outDir === pathDir || outDir === '.') {
    // outDir === '.' is a special case where we do not want
    // to remove any paths from the list.
    if (outDir !== '.') {
      paths = paths.slice(outDirLength);
    }
    sourcePath = rootDir === '.' ? paths.join('/') : `${rootDir}/${paths.join('/')}`;
  }
  return unixize(sourcePath);

  function removeEndSlash(filePath: string) {
    return filePath.endsWith(path.sep) ? filePath.slice(0, filePath.length - 1) : filePath;
  }
}