diff --git a/src/_internal.ts b/src/_internal.ts index 1f24bdd..c7e5cb6 100644 --- a/src/_internal.ts +++ b/src/_internal.ts @@ -1,7 +1,11 @@ +const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//; + // Util to normalize windows paths to posix export function normalizeWindowsPath(input = "") { - if (!input || !input.includes("\\")) { + if (!input) { return input; } - return input.replace(/\\/g, "/"); + return input + .replace(/\\/g, "/") + .replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase()); } diff --git a/test/index.spec.ts b/test/index.spec.ts index 17a530e..22ef36f 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -24,7 +24,7 @@ runTest("normalizeWindowsPath", normalizeWindowsPath, { "/foo/bar": "/foo/bar", // Windows - "c:\\foo\\bar": "c:/foo/bar", + "c:\\foo\\bar": "C:/foo/bar", "\\foo\\bar": "/foo/bar", ".\\foo\\bar": "./foo/bar", }); @@ -179,11 +179,11 @@ runTest("normalize", normalize, { "C:\\temp\\..": "C:/", "C:\\temp\\\\foo\\bar\\..\\": "C:/temp/foo/", "C:////temp\\\\/\\/\\/foo/bar": "C:/temp/foo/bar", - "c:/windows/nodejs/path": "c:/windows/nodejs/path", - "c:/windows/../nodejs/path": "c:/nodejs/path", + "c:/windows/nodejs/path": "C:/windows/nodejs/path", + "c:/windows/../nodejs/path": "C:/nodejs/path", - "c:\\windows\\nodejs\\path": "c:/windows/nodejs/path", - "c:\\windows\\..\\nodejs\\path": "c:/nodejs/path", + "c:\\windows\\nodejs\\path": "C:/windows/nodejs/path", + "c:\\windows\\..\\nodejs\\path": "C:/nodejs/path", "/windows\\unix/mixed": "/windows/unix/mixed", "\\windows//unix/mixed": "/windows/unix/mixed", @@ -245,6 +245,7 @@ runTest("relative", relative, [ // Windows ["C:\\orandea\\test\\aaa", "C:\\orandea\\impl\\bbb", "../../impl/bbb"], + ["C:\\orandea\\test\\aaa", "c:\\orandea\\impl\\bbb", "../../impl/bbb"], ["C:\\", "C:\\foo\\bar", "foo/bar"], ["C:\\foo", "C:\\", ".."], [