Skip to content

Commit 1d05b89

Browse files
committed
lib: fix basename comparison on windows
1 parent 4383acd commit 1d05b89

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/path.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,12 @@ win32.basename = function(path, ext) {
340340
if (ext !== undefined && typeof ext !== 'string')
341341
throw new TypeError('"ext" argument must be a string');
342342

343-
var f = win32SplitPath(path)[2];
344-
// TODO: make this comparison case-insensitive on windows?
345-
if (ext && f.substr(-1 * ext.length) === ext) {
343+
let f = win32SplitPath(path)[2];
344+
345+
if (ext && f.toLowerCase().endsWith(ext.toLowerCase())) {
346346
f = f.substr(0, f.length - ext.length);
347347
}
348+
348349
return f;
349350
};
350351

test/parallel/test-path.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ assert.equal(path.win32.delimiter, ';');
406406
// posix
407407
assert.equal(path.posix.delimiter, ':');
408408

409+
// ensure ext comparison is case-insensitive on windows
410+
const upBaseName = path.win32.basename('same.txt', '.TXT');
411+
const loBaseName = path.win32.basename('SAME.TXT', '.txt');
412+
413+
assert.strictEqual(upBaseName.length,
414+
loBaseName.length,
415+
'both should return "same"');
409416

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

0 commit comments

Comments
 (0)