Skip to content

Commit

Permalink
fix(basename): return extension (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored and billiegoose committed Jul 10, 2019
1 parent c0a21e2 commit 0abdc18
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions src/__tests__/basename.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { basename } from '../basename';

describe('posix basename', () => {
it.each`
path | expected
${'/a/b/foo.json'} | ${'foo.json'}
${'/a/b'} | ${'b'}
${'/a'} | ${'a'}
${''} | ${''}
${'/'} | ${''}
${'////'} | ${''}
${'//a'} | ${'a'}
${'foo'} | ${'foo'}
`('computes dirname of $path', ({ path, expected }) => {
expect(basename(path)).toBe(expected);
});
});

describe('win32 basename', () => {
it.each`
path | expected
${'c:\\'} | ${''}
${'c:\\foo'} | ${'foo'}
${'c:\\foo\\'} | ${'foo'}
${'c:\\foo\\bar'} | ${'bar'}
${'c:\\foo\\bar\\'} | ${'bar'}
${'c:\\foo\\bar\\baz'} | ${'baz'}
${'c:\\foo\\bar\\baz.json'} | ${'baz.json'}
${'\\'} | ${''}
${'\\foo'} | ${'foo'}
${'\\foo\\'} | ${'foo'}
${'\\foo\\bar'} | ${'bar'}
${'\\foo\\bar\\'} | ${'bar'}
${'\\foo\\bar\\baz'} | ${'baz'}
${'\\foo\\bar\\baz.bar'} | ${'baz.bar'}
${'c:'} | ${'c:'}
${'c:foo'} | ${'c:foo'}
${'c:foo\\'} | ${'c:foo'}
${'c:foo\\bar'} | ${'bar'}
${'c:foo\\bar.test'} | ${'bar.test'}
${'c:foo\\bar\\'} | ${'bar'}
${'c:foo\\bar\\baz'} | ${'baz'}
${'file:stream'} | ${'file:stream'}
${'dir\\file:stream'} | ${'file:stream'}
${'\\\\unc\\share'} | ${'share'}
${'\\\\unc\\share\\foo'} | ${'foo'}
${'\\\\unc\\share\\foo\\'} | ${'foo'}
${'\\\\unc\\share\\foo\\bar'} | ${'bar'}
${'\\\\unc\\share\\foo\\bar\\'} | ${'bar'}
${'\\\\unc\\share\\foo\\bar\\baz'} | ${'baz'}
${'\\\\unc\\share\\foo\\bar\\baz.x'} | ${'baz.x'}
${'foo'} | ${'foo'}
`('computes dirname of $path', ({ path, expected }) => {
expect(basename(path)).toBe(expected);
});
});
4 changes: 2 additions & 2 deletions src/basename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const basename = (path: string) => {
const parsed = normalizeParsed(parse(path));
const base = parsed.path.pop();
if (!base) return '';
const { name } = parseBase(base);
return name;
const { name, ext } = parseBase(base);
return `${name}${ext}`;
};

0 comments on commit 0abdc18

Please sign in to comment.