Skip to content

Commit

Permalink
feat(lib): isAbsolute
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 9, 2022
1 parent e710004 commit df42e5c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/lib/__tests__/is-absolute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @file Unit Tests - isAbsolute
* @module pathe/lib/tests/unit/isAbsolute
* @see https://github.com/nodejs/node/blob/main/test/parallel/test-path-isabsolute.js
*/

import { posix, win32 } from 'node:path'
import testSubject from '../is-absolute'

describe('unit:lib/isAbsolute', () => {
it('should return false if path is not absolute', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [[''], ['./baz'], ['bar/']]

// Act + Expect
cases.forEach(([path]) => {
expect(testSubject(path)).to.equal(posix.isAbsolute(path))
})
})

it('should return true if path is absolute', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
['/home/foo'],
['/home/foo/..'],
[posix.sep]
]

// Act + Expect
cases.forEach(([path]) => {
expect(testSubject(path)).to.equal(posix.isAbsolute(path))
})
})

describe('windows', () => {
it('should return false if path is not absolute', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[''],
['C:cwd/another'],
['C:cwd\\another'],
['c'],
['c:'],
['directory/directory'],
['directory\\directory']
]

// Act + Expect
cases.forEach(([path]) => {
expect(testSubject(path)).to.equal(win32.isAbsolute(path))
})
})

it('should return true if path is absolute', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
['//server'],
['//server/file'],
['\\\\server'],
['\\\\server\\file'],
['C:/Users/'],
['C:\\Users\\'],
['c:/'],
['c://'],
['c:\\'],
[posix.sep.repeat(2)],
[posix.sep],
[win32.sep.repeat(2)]
]

// Act + Expect
cases.forEach(([path]) => {
expect(testSubject(path)).to.equal(win32.isAbsolute(path))
})
})
})
})
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export { default as basename } from './basename'
export { default as delimiter } from './delimiter'
export { default as dirname } from './dirname'
export { default as extname } from './extname'
export { default as isAbsolute } from './is-absolute'
export { default as sep } from './sep'
30 changes: 30 additions & 0 deletions src/lib/is-absolute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @file isAbsolute
* @module pathe/lib/isAbsolute
*/

import ensurePosix from '#src/internal/ensure-posix'
import isDrivePath from '#src/internal/is-drive-path'
import isSep from '#src/internal/is-sep'
import validateString from '#src/internal/validate-string'

/**
* Determines if a path is an absolute path.
*
* @param {string} path - Path to evaluate
* @return {boolean} `true` if `path` is absolute
* @throws {TypeError} If `path` is not a string
*/
const isAbsolute = (path: string): boolean => {
validateString(path, 'path')

// exit early if path is empty string
if (path.length === 0) return false

// ensure path meets posix standards
path = ensurePosix(path)

return isSep(path.charAt(0)) || (isDrivePath(path) && isSep(path.charAt(2)))
}

export default isAbsolute

0 comments on commit df42e5c

Please sign in to comment.