Skip to content

Commit

Permalink
feat: 🎸 add basenem() utility
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent a4268c6 commit 8b27695
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/node-to-fsa/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {basename} from '../util';

describe('basename()', () => {
test('handles empty string', () => {
expect(basename('')).toBe('');
});

test('return the same string if there is no nesting', () => {
expect(basename('scary.exe')).toBe('scary.exe');
});

test('returns last step in path', () => {
expect(basename('/gg/wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('gg/wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('/wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('/hf/gl.txt')).toBe('gl.txt');
expect(basename('hf/gl.txt')).toBe('gl.txt');
expect(basename('/gl.txt')).toBe('gl.txt');
expect(basename('gl.txt')).toBe('gl.txt');
});

test('handles double slashes', () => {
expect(basename('/gg/wp/hf//gl.txt')).toBe('gl.txt');
expect(basename('//gl.txt')).toBe('gl.txt');
});
});
4 changes: 4 additions & 0 deletions src/node-to-fsa/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const basename = (path: string) => {
const lastSlashIndex = path.lastIndexOf('/');
return lastSlashIndex === -1 ? path : path.slice(lastSlashIndex + 1);
};

0 comments on commit 8b27695

Please sign in to comment.