mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 20:59:10 +00:00
feat(std/path): Add fromFileUrl() (#4993)
Fix: URL constructor accepts a URL object which is not a base
This commit is contained in:
parent
3e6ea62841
commit
b51c863550
6 changed files with 74 additions and 11 deletions
|
@ -898,3 +898,17 @@ export function parse(path: string): ParsedPath {
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Converts a file URL to a path string.
|
||||
*
|
||||
* fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo"
|
||||
* fromFileUrl("file:///home/foo"); // "\\home\\foo"
|
||||
*
|
||||
* Note that non-file URLs are treated as file URLs and irrelevant components
|
||||
* are ignored.
|
||||
*/
|
||||
export function fromFileUrl(url: string | URL): string {
|
||||
return new URL(url).pathname
|
||||
.replace(/^\/(?=[A-Za-z]:\/)/, "")
|
||||
.replace(/\//g, "\\");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue