2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-07 07:45:33 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2023-08-25 09:07:43 +00:00
|
|
|
import { isWindows } from "./_os.ts";
|
2023-09-28 10:54:53 +00:00
|
|
|
import { toFileUrl as posixToFileUrl } from "./posix/to_file_url.ts";
|
|
|
|
import { toFileUrl as windowsToFileUrl } from "./windows/to_file_url.ts";
|
2023-08-07 07:45:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a path string to a file URL.
|
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* @example Usage
|
2023-08-07 07:45:33 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { toFileUrl } from "@std/path/to-file-url";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-08-07 07:45:33 +00:00
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* if (Deno.build.os === "windows") {
|
|
|
|
* assertEquals(toFileUrl("\\home\\foo"), new URL("file:///home/foo"));
|
|
|
|
* assertEquals(toFileUrl("C:\\Users\\foo"), new URL("file:///C:/Users/foo"));
|
|
|
|
* assertEquals(toFileUrl("\\\\127.0.0.1\\home\\foo"), new URL("file://127.0.0.1/home/foo"));
|
|
|
|
* } else {
|
|
|
|
* assertEquals(toFileUrl("/home/foo"), new URL("file:///home/foo"));
|
|
|
|
* }
|
2023-08-07 07:45:33 +00:00
|
|
|
* ```
|
2024-06-02 02:46:36 +00:00
|
|
|
*
|
|
|
|
* @param path Path to convert to file URL.
|
|
|
|
* @returns The file URL equivalent to the path.
|
2023-08-07 07:45:33 +00:00
|
|
|
*/
|
|
|
|
export function toFileUrl(path: string): URL {
|
|
|
|
return isWindows ? windowsToFileUrl(path) : posixToFileUrl(path);
|
|
|
|
}
|