Skip to content

Commit

Permalink
fix(extname): handle edge cases wirth leading dot
Browse files Browse the repository at this point in the history
resolves #21
  • Loading branch information
pi0 committed Jun 29, 2022
1 parent d6fa1f2 commit dfe46d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ export const toNamespacedPath: typeof path.toNamespacedPath = function (p) {
}

// extname
const _EXTNAME_RE = /(?<!^)\.[^/.]+$/
export const extname: typeof path.extname = function (p) {
const segments = normalizeWindowsPath(p).split('/').pop().split('.')
return segments.length > 1 ? `.${segments.pop()}` : ''
const match = _EXTNAME_RE.exec(normalizeWindowsPath(p))
return (match && match[0]) || ''
}

// relative
Expand Down
8 changes: 8 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ runTest('extname', extname, {
'/temp/myfile.html': '.html',
'./myfile.html': '.html',

'.foo': '',
'..foo': '.foo',
'foo.123': '.123',
'..': '',
'.': '',
'./': '',
// '...': '.', // TODO: Edge case behavior of Node?

// Windows
'C:\\temp\\myfile.html': '.html',
'\\temp\\myfile.html': '.html',
Expand Down

0 comments on commit dfe46d7

Please sign in to comment.