From 586c5362eac23e3508ac7ce09ae1f127d47cc965 Mon Sep 17 00:00:00 2001 From: Sebastian Webster <84299475+Sebastian-Webster@users.noreply.github.com> Date: Fri, 1 Dec 2023 04:43:00 +1300 Subject: [PATCH] feat: return path as name for root directories --- lib/index.js | 13 +++++++++++-- test/index.js | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index afb1dbb..6c4b1ae 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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)) +} diff --git a/test/index.js b/test/index.js index 787f413..896dac9 100644 --- a/test/index.js +++ b/test/index.js @@ -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')