summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-04-29 21:20:55 +0100
committerGitHub <noreply@github.com>2020-04-29 16:20:55 -0400
commitb51c863550cb377f6e720bccf4f1485ed8d97222 (patch)
treeb17087c22cecc21d1020c8580be712280aafa9ab /std
parent3e6ea6284178df0be4982d9775f47b47b14c6139 (diff)
feat(std/path): Add fromFileUrl() (#4993)
Fix: URL constructor accepts a URL object which is not a base
Diffstat (limited to 'std')
-rw-r--r--std/path/from_file_url_test.ts37
-rw-r--r--std/path/mod.ts17
-rw-r--r--std/path/posix.ts11
-rw-r--r--std/path/win32.ts14
4 files changed, 71 insertions, 8 deletions
diff --git a/std/path/from_file_url_test.ts b/std/path/from_file_url_test.ts
new file mode 100644
index 000000000..79389b91b
--- /dev/null
+++ b/std/path/from_file_url_test.ts
@@ -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"
+ );
+});
diff --git a/std/path/mod.ts b/std/path/mod.ts
index 402adcf00..104e0b616 100644
--- a/std/path/mod.ts
+++ b/std/path/mod.ts
@@ -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";
diff --git a/std/path/posix.ts b/std/path/posix.ts
index ba4cf7499..156aba796 100644
--- a/std/path/posix.ts
+++ b/std/path/posix.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;
+}
diff --git a/std/path/win32.ts b/std/path/win32.ts
index 9bba66e2b..2c262b1b6 100644
--- a/std/path/win32.ts
+++ b/std/path/win32.ts
@@ -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, "\\");
+}