Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return path as name for root directories #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: removing ternary operators is a good way to get on my good side ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know ;)

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')