summaryrefslogtreecommitdiff
path: root/std/path/posix.ts
diff options
context:
space:
mode:
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;
+}