Skip to content
Merged
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
17 changes: 14 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function encodePath(path: string): string {
export function basename(path: string): string {
return path
.replace(/\\/g, '/')
.replace(/\/+$/g, '')
.replace(/.*\//, '')
}

Expand All @@ -38,9 +39,19 @@ export function basename(path: string): string {
* @param path - The path to get the directory of
*/
export function dirname(path: string): string {
return path
.replace(/\\/g, '/')
.replace(/\/[^/]*$/, '')
path = path.replaceAll(/\\/g, '/')
const sections = path.split('/')
if (sections.length <= 1) {
// there was no slash in the path
return '.'
}

sections.pop()
if (sections.length === 1 && sections[0] === '') {
return '/'
}

return sections.join('/')
}

/**
Expand Down
9 changes: 2 additions & 7 deletions test/basename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,10 @@ describe('basename', function() {
it('Returns dot if file name is dot', function() {
expect(basename('/subdir/.')).toEqual('.')
})
// TODO: fix the source to make it work like PHP's basename
it('Returns the dir itself if no file name given', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(basename('subdir/')).toEqual('subdir');
expect(basename('subdir/')).toEqual('')
expect(basename('subdir/')).toEqual('subdir')
})
it('Returns the dir itself if no file name given with root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(basename('/subdir/')).toEqual('subdir');
expect(basename('/subdir/')).toEqual('')
expect(basename('/subdir/')).toEqual('subdir')
})
})
26 changes: 9 additions & 17 deletions test/dirname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,26 @@ import { describe, expect, it } from 'vitest'
import { dirname } from '../lib/index.ts'

describe('dirname', function() {
it('Returns the nothing if no file name given', function() {
expect(dirname('')).toEqual('')
it('Returns current folder if no file name given', function() {
expect(dirname('')).toEqual('.')
})
it('Returns the root if dir is root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(dirname('/')).toEqual('/');
expect(dirname('/')).toEqual('')
expect(dirname('/')).toEqual('/')
})
it('Returns the root if dir is double root', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(dirname('//')).toEqual('/');
expect(dirname('//')).toEqual('/') // oh no...
expect(dirname('//')).toEqual('/')
})
it('Returns dot if dir is dot', function() {
expect(dirname('.')).toEqual('.')
})
it('Returns dot if no root given', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(dirname('some dir')).toEqual('.');
expect(dirname('some dir')).toEqual('some dir') // oh no...
expect(dirname('some dir')).toEqual('.')
})
it('Returns the dir name if file name and root path given', function() {
// TODO: fix the source to make it work like PHP's dirname
// expect(dirname('/some name.txt')).toEqual('/');
expect(dirname('/some name.txt')).toEqual('')
expect(dirname('/some name.txt')).toEqual('/')
})
it('Returns the dir name if double root path given', function() {
expect(dirname('//some name.txt')).toEqual('/') // how lucky...
expect(dirname('//some name.txt')).toEqual('/')
})
it('Returns the dir name if subdir given without root', function() {
expect(dirname('subdir/some name.txt')).toEqual('subdir')
Expand All @@ -43,9 +35,9 @@ describe('dirname', function() {
expect(dirname('/subdir/some name.txt')).toEqual('/subdir')
})
it('Returns the dir name if subdir given with double root', function() {
// TODO: fix the source to make it work like PHP's dirname
// TODO: currently it behaves like Node's dirname, but for PHP it would be:
// expect(dirname('//subdir/some name.txt')).toEqual('/subdir');
expect(dirname('//subdir/some name.txt')).toEqual('//subdir') // oh...
expect(dirname('//subdir/some name.txt')).toEqual('//subdir')
})
it('Returns the dir name if subdir has dot', function() {
expect(dirname('/subdir.dat/some name.txt')).toEqual('/subdir.dat')
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"erasableSyntaxOnly": true,
"rewriteRelativeImportExtensions": true,
"lib": [
"es6",
"dom"
"ESNext",
"DOM"
],
}
}