Skip to content

Commit

Permalink
feat: return path as name for root directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Nov 30, 2023
1 parent 2442a86 commit 586c536
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ const { basename, dirname } = require('path')
const getName = (parent, base) =>
parent.charAt(0) === '@' ? `${parent}/${base}` : base

module.exports = dir => dir ? getName(basename(dirname(dir)), basename(dir))
: false
module.exports = (dir) => {
if (!dir) {
return false
}

if (dir.length < 4 && (dir === '/' || dir.slice(1, 3) === ':\\')) {
return dir
}

return getName(basename(dirname(dir)), basename(dir))
}
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ const t = require('tap')
t.equal(nff('/a/b/c/@foo/bar'), '@foo/bar')
t.equal(nff('/a/b/c/foo/bar'), 'bar')
t.equal(nff(null), false)
t.equal(nff('/'), '/', 'root directories return their path as name')
t.equal(nff('C:\\'), 'C:\\', 'root directories return their path as name')

0 comments on commit 586c536

Please sign in to comment.