summaryrefslogtreecommitdiff
path: root/std/path/posix.ts
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-10-19 13:36:53 +0100
committerGitHub <noreply@github.com>2020-10-19 14:36:53 +0200
commit19b918d112c786b1db17fe2d83be79f1114ba240 (patch)
treeb8ae7e1f5aa4b70570245b61c504c5c46e2e78e8 /std/path/posix.ts
parent342b151b5d9d6c97926588dd118b59032f3b0e40 (diff)
feat(std/path): Add toFileUrl() (#7971)
Diffstat (limited to 'std/path/posix.ts')
-rw-r--r--std/path/posix.ts13
1 files changed, 13 insertions, 0 deletions
diff --git a/std/path/posix.ts b/std/path/posix.ts
index 68ffb06c9..d2845f3da 100644
--- a/std/path/posix.ts
+++ b/std/path/posix.ts
@@ -440,3 +440,16 @@ export function fromFileUrl(url: string | URL): string {
url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
);
}
+
+/** Converts a path string to a file URL.
+ *
+ * toFileUrl("/home/foo"); // new URL("file:///home/foo")
+ */
+export function toFileUrl(path: string): URL {
+ if (!isAbsolute(path)) {
+ throw new TypeError("Must be an absolute path.");
+ }
+ const url = new URL("file:///");
+ url.pathname = path.replace(/%/g, "%25").replace(/\\/g, "%5C");
+ return url;
+}