From b181dafe1d3b72c33c3b4d90685b1250d6aa0e3e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 11 Dec 2023 11:19:16 +0000 Subject: [PATCH 1/2] fix: uppercase drive letters within `relative` --- src/path.ts | 5 +++-- test/index.spec.ts | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/path.ts b/src/path.ts index be254bd..d4855fa 100644 --- a/src/path.ts +++ b/src/path.ts @@ -13,6 +13,7 @@ import { normalizeWindowsPath } from "./_internal"; const _UNC_REGEX = /^[/\\]{2}/; const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/; const _DRIVE_LETTER_RE = /^[A-Za-z]:$/; +const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//; // Force POSIX contants export const sep = "/"; @@ -211,8 +212,8 @@ export const extname: typeof path.extname = function (p) { // relative export const relative: typeof path.relative = function (from, to) { - const _from = resolve(from).split("/"); - const _to = resolve(to).split("/"); + const _from = resolve(from).replace(_DRIVE_LETTER_START_RE, r => r.toUpperCase()).split("/"); + const _to = resolve(to).replace(_DRIVE_LETTER_START_RE, r => r.toUpperCase()).split("/"); const _fromCopy = [..._from]; for (const segment of _fromCopy) { if (_to[0] !== segment) { diff --git a/test/index.spec.ts b/test/index.spec.ts index 1fd0f46..db7f29a 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -243,6 +243,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"], [ () => process.cwd().replace(/\\/g, "/"), "./dist/client/b-scroll.d.ts", From 5a518f29fa96119bc5ae8fcef399bb9436045f59 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 2 Jan 2024 12:46:17 +0000 Subject: [PATCH 2/2] refactor: always capitalize drive letters --- src/_internal.ts | 8 ++++++-- src/path.ts | 12 +++--------- test/index.spec.ts | 10 +++++----- 3 files changed, 14 insertions(+), 16 deletions(-) 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/src/path.ts b/src/path.ts index 71bea54..21ecb3a 100644 --- a/src/path.ts +++ b/src/path.ts @@ -13,7 +13,6 @@ import { normalizeWindowsPath } from "./_internal"; const _UNC_REGEX = /^[/\\]{2}/; const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/; const _DRIVE_LETTER_RE = /^[A-Za-z]:$/; -const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//; const _ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/; // Force POSIX contants @@ -213,14 +212,9 @@ export const extname: typeof path.extname = function (p) { // relative export const relative: typeof path.relative = function (from, to) { - const _from = resolve(from) - .replace(_ROOT_FOLDER_RE, "$1") - .replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase()) - .split("/"); - const _to = resolve(to) - .replace(_ROOT_FOLDER_RE, "$1") - .replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase()) - .split("/"); + const _from = resolve(from).replace(_ROOT_FOLDER_RE, "$1").split("/"); + const _to = resolve(to).replace(_ROOT_FOLDER_RE, "$1").split("/"); + const _fromCopy = [..._from]; for (const segment of _fromCopy) { if (_to[0] !== segment) { diff --git a/test/index.spec.ts b/test/index.spec.ts index a40a63c..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",