Skip to content

Commit

Permalink
fix(basename): handle empty extensions (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 15, 2022
1 parent e9f50b7 commit b0661cb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/path.ts
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/index.spec.ts
Expand Up @@ -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, {
Expand Down

0 comments on commit b0661cb

Please sign in to comment.