Skip to content

Commit

Permalink
Merge pull request #63 from nodelib/FG-277_fs.walk_fix
Browse files Browse the repository at this point in the history
FG-277: correctly handle the leading slash
  • Loading branch information
mrmlnc committed Dec 27, 2020
2 parents cb5f7e8 + 9b3db1c commit a6ad025
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/fs/fs.walk/src/readers/common.spec.ts
Expand Up @@ -97,5 +97,18 @@ describe('Readers → Common', () => {

assert.strictEqual(actual, expected);
});

it('should return correct string when the first segment ens with the separator symbol', () => {
// Unix
assert.strictEqual(common.joinPathSegments('/', 'a', '/'), '/a');
assert.strictEqual(common.joinPathSegments('//', 'a', '/'), '//a');
assert.strictEqual(common.joinPathSegments('/a/', 'b', '/'), '/a/b');

// Windows
assert.strictEqual(common.joinPathSegments('C:/', 'Users', '/'), 'C:/Users');
assert.strictEqual(common.joinPathSegments('C:\\', 'Users', '\\'), 'C:\\Users');
assert.strictEqual(common.joinPathSegments('//?/C:/', 'Users', '/'), '//?/C:/Users');
assert.strictEqual(common.joinPathSegments('\\\\?\\C:\\', 'Users', '\\'), '\\\\?\\C:\\Users');
});
});
});
7 changes: 7 additions & 0 deletions packages/fs/fs.walk/src/readers/common.ts
Expand Up @@ -22,5 +22,12 @@ export function joinPathSegments(a: string, b: string, separator: string): strin
return b;
}

/**
* The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
*/
if (a.endsWith(separator)) {
return a + b;
}

return a + separator + b;
}

0 comments on commit a6ad025

Please sign in to comment.