From 069e209d1746ee2355d1e8d4635d11ab3e3c2a9b Mon Sep 17 00:00:00 2001 From: leejooy96 Date: Thu, 4 Apr 2024 17:29:22 +0900 Subject: [PATCH] fix: incorrect path retuning on windows and change method name from `resolvePath` --- CHANGELOG.md | 5 +++++ README.md | 6 +++--- lib/index.ts | 16 ++++++++++------ test/common.spec.ts | 23 +++++++++++++---------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69e7430..d010424 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## 1.11.1 (2024--) + +- `toValidPath`: `resolvePath` method renamed to `toValidPath` +- `toValidPath`: Fix incorrect path retuning on Windows + ## 1.11.0 (2024-03-11) - BREAKING CHANGE: `normalize` now requires the `normalizationForm` value to be entered directly instead of the `os` argument value. If specified as `undefined`, it defaults to `NFC` (e.g. `NFC`, `NFD`...) diff --git a/README.md b/README.md index 5571261..3dfb973 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ console.log(await fsman.humanizeSize(1000000)); // '976.56 KB' console.log(await fsman.humanizeSize(100000000, 3)); // '95.367 MB' ``` -### `resolvePath ()` +### `toValidPath ()` Remove invalid or unnecessary characters in the path. @@ -82,8 +82,8 @@ Remove invalid or unnecessary characters in the path. - `isWindows `: Whether the target operating system to be checked is Windows ```javascript -console.log(fsman.resolvePath('C:\\Windows\\System32\\'), true); // 'C:\Windows\System32' -console.log(fsman.resolvePath('home/user/.bashrc')); // '/home/user/.bashrc' +console.log(fsman.toValidPath('C:\\Windows\\System32\\'), true); // 'C:\Windows\System32' +console.log(fsman.toValidPath('home/user/.bashrc')); // '/home/user/.bashrc' ``` ### `getPathLevel ()` diff --git a/lib/index.ts b/lib/index.ts index d54e094..14921aa 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -60,18 +60,22 @@ export default class FsMan { }`; } - static resolvePath(filePath: string, isWindows?: boolean): string { + static toValidPath(filePath: string, isWindows?: boolean): string { if (isWindows) { let windowsPath = filePath; - if (windowsPath.length > 2 && !/[a-zA-Z]:\\/.test(windowsPath)) { + if (windowsPath.length > 2 && !/^[a-zA-Z]:/.test(windowsPath)) { windowsPath = `\\${windowsPath}`; } - if (windowsPath.length > 2 && /\\$/.test(windowsPath)) { + if ((windowsPath.match(/\\/g) || []).length > 1 && /\\$/.test(windowsPath)) { windowsPath = windowsPath.replace(/\\$/, ''); } + if (/^[a-zA-Z]:$/.test(windowsPath)) { + windowsPath = `${windowsPath}\\`; + } + return windowsPath.replace(/\\{2,}/g, '\\'); } @@ -92,7 +96,7 @@ export default class FsMan { static joinPath(isWindows: boolean, ...paths: string[]): string { if (isWindows) { - return FsMan.resolvePath(isWindows ? win32.join(...paths) : join(...paths), true); + return FsMan.toValidPath(isWindows ? win32.join(...paths) : join(...paths), true); } let fullPath = ''; @@ -101,7 +105,7 @@ export default class FsMan { fullPath = `${fullPath}/${paths[i]}`; } - return FsMan.resolvePath(fullPath, false); + return FsMan.toValidPath(fullPath, false); } static getPathLevel(filePath: string): number { @@ -395,7 +399,7 @@ export { FsMan }; export const { isHidden, humanizeSize, - resolvePath, + toValidPath, joinPath, getPathLevel, toPosixPath, diff --git a/test/common.spec.ts b/test/common.spec.ts index 6bab57b..d7e70f6 100644 --- a/test/common.spec.ts +++ b/test/common.spec.ts @@ -2,7 +2,7 @@ import assert from 'assert'; import { isHidden, humanizeSize, - resolvePath, + toValidPath, joinPath, getPathLevel, toPosixPath, @@ -46,15 +46,18 @@ describe('fsman', () => { done(); }); - it('resolvePath', (done) => { - assert.strictEqual(resolvePath('home'), '/home'); - assert.strictEqual(resolvePath('/home//test/'), '/home/test'); - assert.strictEqual(resolvePath('home/test/.conf'), '/home/test/.conf'); - assert.strictEqual(resolvePath('/'), '/'); - assert.strictEqual(resolvePath('C:\\\\Users\\test\\', true), 'C:\\Users\\test'); - assert.strictEqual(resolvePath('C:\\Users\\test\\.config', true), 'C:\\Users\\test\\.config'); - assert.strictEqual(resolvePath('\\Users\\test\\.config', true), '\\Users\\test\\.config'); - assert.strictEqual(resolvePath('C:', true), 'C:'); + it('toValidPath', (done) => { + assert.strictEqual(toValidPath('home'), '/home'); + assert.strictEqual(toValidPath('/home//test/'), '/home/test'); + assert.strictEqual(toValidPath('home/test/.conf'), '/home/test/.conf'); + assert.strictEqual(toValidPath('/'), '/'); + assert.strictEqual(toValidPath('C:\\\\Users\\test\\', true), 'C:\\Users\\test'); + assert.strictEqual(toValidPath('C:\\Users\\test\\.config', true), 'C:\\Users\\test\\.config'); + assert.strictEqual(toValidPath('\\Users\\test\\.config', true), '\\Users\\test\\.config'); + assert.strictEqual(toValidPath('Users\\test\\.config', true), '\\Users\\test\\.config'); + assert.strictEqual(toValidPath('C:', true), 'C:\\'); + assert.strictEqual(toValidPath('C:\\\\', true), 'C:\\'); + assert.strictEqual(toValidPath('C:\\Users\\', true), 'C:\\Users'); done(); });