mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05: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
2
cli/js/lib.deno.shared_globals.d.ts
vendored
2
cli/js/lib.deno.shared_globals.d.ts
vendored
|
@ -1092,7 +1092,7 @@ interface URL {
|
|||
|
||||
declare const URL: {
|
||||
prototype: URL;
|
||||
new (url: string, base?: string | URL): URL;
|
||||
new (url: string | URL, base?: string | URL): URL;
|
||||
createObjectURL(object: any): string;
|
||||
revokeObjectURL(url: string): void;
|
||||
};
|
||||
|
|
|
@ -356,7 +356,7 @@ export class URLImpl implements URL {
|
|||
return this.#searchParams;
|
||||
}
|
||||
|
||||
constructor(url: string, base?: string | URL) {
|
||||
constructor(url: string | URL, base?: string | URL) {
|
||||
let baseParts: URLParts | undefined;
|
||||
if (base) {
|
||||
baseParts = typeof base === "string" ? parse(base) : parts.get(base);
|
||||
|
@ -365,7 +365,7 @@ export class URLImpl implements URL {
|
|||
}
|
||||
}
|
||||
|
||||
const urlParts = parse(url);
|
||||
const urlParts = typeof url === "string" ? parse(url) : parts.get(url);
|
||||
if (!urlParts) {
|
||||
throw new TypeError("Invalid URL.");
|
||||
}
|
||||
|
|
37
std/path/from_file_url_test.ts
Normal file
37
std/path/from_file_url_test.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import * as path from "./mod.ts";
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
|
||||
Deno.test("[path] fromFileUrl", function () {
|
||||
assertEquals(
|
||||
path.posix.fromFileUrl(new URL("file:///home/foo")),
|
||||
"/home/foo"
|
||||
);
|
||||
assertEquals(path.posix.fromFileUrl("file:///home/foo"), "/home/foo");
|
||||
assertEquals(path.posix.fromFileUrl("https://example.com/foo"), "/foo");
|
||||
assertEquals(path.posix.fromFileUrl("file:///"), "/");
|
||||
});
|
||||
|
||||
Deno.test("[path] fromFileUrl (win32)", function () {
|
||||
assertEquals(
|
||||
path.win32.fromFileUrl(new URL("file:///home/foo")),
|
||||
"\\home\\foo"
|
||||
);
|
||||
assertEquals(path.win32.fromFileUrl("file:///home/foo"), "\\home\\foo");
|
||||
assertEquals(path.win32.fromFileUrl("https://example.com/foo"), "\\foo");
|
||||
assertEquals(path.win32.fromFileUrl("file:///"), "\\");
|
||||
// FIXME(nayeemrmn): Support UNC paths. Needs support in the underlying URL
|
||||
// built-in like Chrome has.
|
||||
// assertEquals(path.win32.fromFileUrl("file:////"), "\\");
|
||||
// assertEquals(path.win32.fromFileUrl("file:////server"), "\\");
|
||||
// assertEquals(path.win32.fromFileUrl("file:////server/file"), "\\file");
|
||||
assertEquals(path.win32.fromFileUrl("file:///c"), "\\c");
|
||||
assertEquals(path.win32.fromFileUrl("file:///c:"), "\\c:");
|
||||
assertEquals(path.win32.fromFileUrl("file:///c:/"), "c:\\");
|
||||
assertEquals(path.win32.fromFileUrl("file:///C:/"), "C:\\");
|
||||
assertEquals(path.win32.fromFileUrl("file:///C:/Users/"), "C:\\Users\\");
|
||||
assertEquals(
|
||||
path.win32.fromFileUrl("file:///C:cwd/another"),
|
||||
"\\C:cwd\\another"
|
||||
);
|
||||
});
|
|
@ -11,19 +11,20 @@ const path = isWindows ? _win32 : _posix;
|
|||
export const win32 = _win32;
|
||||
export const posix = _posix;
|
||||
export const {
|
||||
resolve,
|
||||
normalize,
|
||||
isAbsolute,
|
||||
join,
|
||||
relative,
|
||||
toNamespacedPath,
|
||||
dirname,
|
||||
basename,
|
||||
delimiter,
|
||||
dirname,
|
||||
extname,
|
||||
format,
|
||||
fromFileUrl,
|
||||
isAbsolute,
|
||||
join,
|
||||
normalize,
|
||||
parse,
|
||||
relative,
|
||||
resolve,
|
||||
sep,
|
||||
delimiter,
|
||||
toNamespacedPath,
|
||||
} = path;
|
||||
|
||||
export { common } from "./common.ts";
|
||||
|
|
|
@ -421,3 +421,14 @@ export function parse(path: string): ParsedPath {
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Converts a file URL to a path string.
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
|
|
|
@ -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
Reference in a new issue