-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
e710004
commit df42e5c
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |