-
-
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
de150ee
commit 98c393a
Showing
13 changed files
with
237 additions
and
26 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,13 @@ | ||
/** | ||
* @file Unit Tests - ParsedPath | ||
* @module pathe/interfaces/tests/unit-d/ParsedPath | ||
*/ | ||
|
||
import type TestSubject from '../parsed-path' | ||
import type PathObject from '../path-object' | ||
|
||
describe('unit-d:interfaces/ParsedPath', () => { | ||
it('should extend Required<PathObject>', () => { | ||
expectTypeOf<TestSubject>().toMatchTypeOf<Required<PathObject>>() | ||
}) | ||
}) |
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,23 @@ | ||
/** | ||
* @file Interfaces - ParsedPath | ||
* @module pathe/interfaces/ParsedPath | ||
*/ | ||
|
||
import type PathObject from './path-object' | ||
|
||
/** | ||
* Object representing significant elements of a path. | ||
* | ||
* This object can be generated by [`parse`][1] or consumed by [`format`][2]. | ||
* | ||
* [1]: {@link ../lib/parse.ts} | ||
* [2]: {@link ../lib/format.ts} | ||
* [3]: {@link ./path-object.ts} | ||
* | ||
* @see [`PathObject`][3] | ||
* | ||
* @extends {Required<PathObject>} | ||
*/ | ||
interface ParsedPath extends Required<PathObject> {} | ||
|
||
export type { ParsedPath as default } |
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
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
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,88 @@ | ||
/** | ||
* @file Unit Tests - parse | ||
* @module pathe/lib/tests/unit/parse | ||
* @see https://github.com/nodejs/node/blob/main/test/parallel/test-path-parse-format.js | ||
* @see https://github.com/nodejs/node/issues/18655 | ||
*/ | ||
|
||
import type { ParsedPath } from '#src/interfaces' | ||
import sep from '#src/lib/sep' | ||
import { posix, win32 } from 'node:path' | ||
import testSubject from '../parse' | ||
|
||
describe('unit:lib/parse', () => { | ||
it('should return parsed path object', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [ | ||
[''], | ||
['/.'], | ||
['/.foo'], | ||
['/.foo.bar'], | ||
['/foo'], | ||
['/foo.'], | ||
['/foo.bar'], | ||
['/foo///'], | ||
['/foo///bar.baz'], | ||
['/foo/bar.baz'], | ||
['/home/user/a dir//another&file.'], | ||
['/home/user/a dir/another file.zip'], | ||
['/home/user/a$$$dir//another file.zip'], | ||
['/home/user/dir/file.txt'], | ||
['/home/user/dir/file.txt'], | ||
['user/dir/another file.zip'], | ||
[posix.sep], | ||
[posix.sep.repeat(2)], | ||
[posix.sep.repeat(3)] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([path]) => { | ||
expect(testSubject(path)).to.deep.equal(posix.parse(path)) | ||
}) | ||
}) | ||
|
||
describe('windows', () => { | ||
/** | ||
* Converts Windows-style path separators (`\`) to POSIX (`/`). | ||
* | ||
* @param {ParsedPath} parsed - Parsed path object to normalize | ||
* @return {string} `parsed` with values normalized | ||
*/ | ||
const ensurePosix = (parsed: ParsedPath): ParsedPath => { | ||
for (const [key, value] of Object.entries<string>(parsed)) { | ||
if (!value) continue | ||
parsed[key] = value.replace(/\\/g, sep) | ||
} | ||
|
||
return parsed | ||
} | ||
|
||
it('should return parsed path object', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [ | ||
[''], | ||
['.\\file'], | ||
['C:'], | ||
['C:.'], | ||
['C:..'], | ||
['C:\\'], | ||
['C:\\abc'], | ||
['C:\\another_path\\DIR\\1\\2\\33\\\\index'], | ||
['C:\\path\\dir\\index.html'], | ||
['C:abc'], | ||
['\\\\?\\UNC\\server\\share'], | ||
['\\\\server two\\shared folder\\file path.zip'], | ||
['\\\\server\\share\\file_path'], | ||
['\\\\user\\admin$\\system32'], | ||
['\\foo\\C:'], | ||
['another_path\\DIR with spaces\\1\\2\\33\\index'], | ||
[win32.sep] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([path]) => { | ||
expect(testSubject(path)).to.deep.equal(ensurePosix(win32.parse(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
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
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,66 @@ | ||
/** | ||
* @file parse | ||
* @module pathe/lib/parse | ||
*/ | ||
|
||
import type { ParsedPath } from '#src/interfaces' | ||
import { DRIVE_PATH_REGEX, UNC_PATH_REGEX } from '#src/internal/constants' | ||
import ensurePosix from '#src/internal/ensure-posix' | ||
import isDrivePath from '#src/internal/is-drive-path' | ||
import isUncPath from '#src/internal/is-unc-path' | ||
import validateString from '#src/internal/validate-string' | ||
import removeExt from '#src/utils/remove-ext' | ||
import basename from './basename' | ||
import dirname from './dirname' | ||
import extname from './extname' | ||
import isAbsolute from './is-absolute' | ||
import sep from './sep' | ||
|
||
/** | ||
* Returns an object representing the given `path`. | ||
* | ||
* Trailing directory [separators][1] are ignored. | ||
* | ||
* **Note**: Unlike in Node.js, `pathe.parse(path).dir === pathe.dirname(path)` | ||
* when `path` is a non-empty string. See [`nodejs/node#18655`][3] for details. | ||
* | ||
* [1]: {@link ./sep.ts} | ||
* [2]: {@link ./dirname.ts} | ||
* [3]: https://github.com/nodejs/node/issues/18655 | ||
* | ||
* @param {string} path - Path to evaluate | ||
* @return {ParsedPath} Object representing significant elements of `path` | ||
* @throws {TypeError} If `path` is not a string | ||
*/ | ||
const parse = (path: string): ParsedPath => { | ||
validateString(path, 'path') | ||
|
||
// ensure path meets posix standards | ||
path = ensurePosix(path) | ||
|
||
/** | ||
* Parsed path object. | ||
* | ||
* @const {ParsedPath} ret | ||
*/ | ||
const ret: ParsedPath = { base: '', dir: '', ext: '', name: '', root: '' } | ||
|
||
// exit early if path is empty string | ||
if (path.length === 0) return ret | ||
|
||
ret.base = basename(path) | ||
ret.dir = dirname(path) | ||
ret.ext = extname(path) | ||
ret.name = removeExt(ret.base, ret.ext) | ||
ret.root = isUncPath(path) | ||
? UNC_PATH_REGEX.exec(path)![1]! | ||
: isDrivePath(path) | ||
? DRIVE_PATH_REGEX.exec(path)![1]! | ||
: isAbsolute(path) | ||
? sep | ||
: '' | ||
|
||
return ret | ||
} | ||
|
||
export default parse |