diff --git a/src/path.ts b/src/path.ts index 48f5e1e..27e1327 100644 --- a/src/path.ts +++ b/src/path.ts @@ -218,7 +218,7 @@ export const format: typeof path.format = function (p) { // basename export const basename: typeof path.basename = function (p, ext) { const lastSegment = normalizeWindowsPath(p).split('/').pop() - return lastSegment.endsWith(ext) ? lastSegment.slice(0, -ext.length) : lastSegment + return ext && lastSegment.endsWith(ext) ? lastSegment.slice(0, -ext.length) : lastSegment } // parse diff --git a/test/index.spec.ts b/test/index.spec.ts index 5a5b3cc..64370af 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -62,12 +62,14 @@ runTest('basename', basename, [ ['/temp/myfile.html', 'myfile.html'], ['./myfile.html', 'myfile.html'], ['./myfile.html', '.html', 'myfile'], + ['./undefined', undefined, 'undefined'], // Windows ['C:\\temp\\myfile.html', 'myfile.html'], ['\\temp\\myfile.html', 'myfile.html'], ['.\\myfile.html', 'myfile.html'], - ['.\\myfile.html', '.html', 'myfile'] + ['.\\myfile.html', '.html', 'myfile'], + ['.\\undefined', undefined, 'undefined'] ]) runTest('dirname', dirname, {