Skip to content

Commit

Permalink
perf: cache normalized paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 committed Aug 14, 2020
1 parent 0c6b218 commit 92f68e3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/lib/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import * as nodePath from 'path';

const PATH_REGEXP = new RegExp('\\' + nodePath.win32.sep, 'g');
const ensureUnixPathCache = new Map<string, string>();

export const ensureUnixPath = (path?: string): string | null => {
if (!path) {
return null;
}

const cachePath = ensureUnixPathCache.get(path);
if (cachePath) {
return cachePath;
}

// we use a regex instead of the character literal due to a bug in some versions of node.js
// the path separator needs to be preceded by an escape character
const regex = new RegExp('\\' + nodePath.win32.sep, 'g');
return path.replace(regex, nodePath.posix.sep);
const normalizedPath = path.replace(PATH_REGEXP, nodePath.posix.sep);
ensureUnixPathCache.set(path, normalizedPath);
return normalizedPath;
};

0 comments on commit 92f68e3

Please sign in to comment.