Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix basename comparison on windows #4669

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,12 @@ win32.basename = function(path, ext) {
if (ext !== undefined && typeof ext !== 'string')
throw new TypeError('"ext" argument must be a string');

var f = win32SplitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
let f = win32SplitPath(path)[2];

if (ext && f.toLowerCase().endsWith(ext.toLowerCase())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would if (ext && f.substr(-1 * ext.length).toLowerCase() === ext.toLowerCase()) { have been enough to address the original comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would, but you were concerned with readability in a previous comment, that would work, but would kill readability

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability, it could be: if (ext && f.toLowerCase().endsWith(ext.toLowerCase())) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is much more elegant and witty.

I'll push a fix soon, thanks @cjihrig.

f = f.substr(0, f.length - ext.length);
}

return f;
};

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ assert.equal(path.win32.delimiter, ';');
// posix
assert.equal(path.posix.delimiter, ':');

// ensure ext comparison is case-insensitive on windows
const upBaseName = path.win32.basename('same.txt', '.TXT');
const loBaseName = path.win32.basename('SAME.TXT', '.txt');

assert.strictEqual(upBaseName.length,
loBaseName.length,
'both should return "same"');

if (common.isWindows)
assert.deepEqual(path, path.win32, 'should be win32 path module');
Expand Down