This repository was archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
feat: path utils #60
Closed
Closed
feat: path utils #60
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| import { isAbsolute, normalize } from '../path'; | ||
|
|
||
| describe('Path utils', () => { | ||
| describe('isAbsolute fn', () => { | ||
| test.each([ | ||
| 'c:\\foo\\bar.json', | ||
| 'c:\\', | ||
| 'c:/', | ||
| 'c:/foo/bar.json', | ||
| '/home/test', | ||
| '/', | ||
| '/var/lib/test/', | ||
| '/var/bin.d', | ||
| ])('should treat %s path as absolute', filepath => { | ||
| expect(isAbsolute(filepath)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| test.each(['\\foo\\bar.json', 'foo/bar', 'test'])('should treat %s path as non-absolute', filepath => { | ||
| expect(isAbsolute(filepath)).toBe(false); | ||
| }); | ||
|
|
||
| describe('normalize fn', () => { | ||
| test('should replace windows-like slashes with POSIX-compatible ones', () => { | ||
| expect(normalize('c:\\foo\\bar')).toEqual('c:/foo/bar'); | ||
| }); | ||
|
|
||
| test('should ignore POSIX slashes', () => { | ||
| expect(normalize('/d/foo')).toEqual('/d/foo'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,12 @@ | ||
| import * as path from 'path'; | ||
| import * as URI from 'urijs'; | ||
| import { URI as VSURI } from 'vscode-uri'; | ||
|
|
||
| export const isAbsolute = (filepath: string) => path.isAbsolute(filepath) || new URI(filepath).is('absolute'); | ||
|
|
||
| export const join = (...parts: string[]) => parts.join('/'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| export const normalize = (filepath: string) => path.normalize(filepath).replace(/\\/g, '/'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when non file e.g. |
||
|
|
||
| export function toFSPath(uri: string) { | ||
| return path.normalize(VSURI.file(uri).fsPath); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a relative helper? https://nodejs.org/api/path.html#path_path_relative_from_to |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the tests, could we perhaps run tests twice, once with path mocked/set to
path.posixand once withpath.win32? https://nodejs.org/api/path.html#path_windows_vs_posixThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no. Just don't use
path