Skip to content

Commit

Permalink
fix: incorrect path retuning on windows and change method name from `…
Browse files Browse the repository at this point in the history
…resolvePath`
  • Loading branch information
jooy2 committed Apr 4, 2024
1 parent 353c94f commit 069e209
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`...)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ console.log(await fsman.humanizeSize(1000000)); // '976.56 KB'
console.log(await fsman.humanizeSize(100000000, 3)); // '95.367 MB'
```

### `resolvePath (<String>)`
### `toValidPath (<String>)`

Remove invalid or unnecessary characters in the path.

- `filePath <String>`: File or directory path
- `isWindows <Boolean>`: 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 (<Number>)`
Expand Down
16 changes: 10 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\');
}

Expand All @@ -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 = '';
Expand All @@ -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 {
Expand Down Expand Up @@ -395,7 +399,7 @@ export { FsMan };
export const {
isHidden,
humanizeSize,
resolvePath,
toValidPath,
joinPath,
getPathLevel,
toPosixPath,
Expand Down
23 changes: 13 additions & 10 deletions test/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import {
isHidden,
humanizeSize,
resolvePath,
toValidPath,
joinPath,
getPathLevel,
toPosixPath,
Expand Down Expand Up @@ -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();
});

Expand Down

0 comments on commit 069e209

Please sign in to comment.