All files / src/shared/lib/helpers path.ts

51.61% Statements 16/31
40% Branches 2/5
50% Functions 2/4
48.15% Lines 13/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4611x   11x 78x   11x                                                 11x 7x 7x 2x     5x 5x   5x 4x     1x    
import Path from "path";
 
export const pathToPosix = (filepath: string): string =>
  filepath.split(Path.sep).join(Path.posix.sep);
 
export const naturalSortPaths = (filepaths: string[]): string[] => {
  return [...filepaths].sort((a, b) => {
    const splitA = a.split(/[\\/]/);
    const splitB = b.split(/[\\/]/);
    const len = Math.min(splitA.length, splitB.length);
    for (let i = 0; i < len; i++) {
      const aPart = splitA[i].replace(/\.[^.]*/, "");
      const bPart = splitB[i].replace(/\.[^.]*/, "");
      Iif (aPart !== bPart) {
        return aPart.localeCompare(bPart, undefined, {
          numeric: true,
          sensitivity: "base",
        });
      }
    }
    Iif (splitA.length > splitB.length) {
      return 1;
    }
    Iif (splitA.length < splitB.length) {
      return -1;
    }
    return 0;
  });
};
 
export const ensureNonEmptyBasename = (filename: string): string => {
  const posixFilename = pathToPosix(filename);
  if (posixFilename.endsWith("/")) {
    return pathToPosix(Path.join(posixFilename, "_"));
  }
 
  const dir = Path.dirname(posixFilename);
  const base = Path.basename(posixFilename);
 
  if (base.startsWith(".")) {
    return pathToPosix(Path.join(dir, `_${base}`));
  }
 
  return posixFilename;
};