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 { format as posixFormat } from "./posix/format.ts";
|
|
|
|
import { format as windowsFormat } from "./windows/format.ts";
|
2024-07-05 03:26:18 +00:00
|
|
|
import type { ParsedPath } from "./types.ts";
|
2023-08-07 07:45:33 +00:00
|
|
|
|
|
|
|
/**
|
2024-07-05 03:26:18 +00:00
|
|
|
* Generate a path from a {@linkcode ParsedPath} object. It does the
|
2024-06-02 02:46:36 +00:00
|
|
|
* opposite of {@linkcode https://jsr.io/@std/path/doc/~/parse | parse()}.
|
2024-03-16 05:32:20 +00:00
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { format } from "@std/path/format";
|
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";
|
2024-06-02 02:46:36 +00:00
|
|
|
*
|
|
|
|
* if (Deno.build.os === "windows") {
|
|
|
|
* assertEquals(format({ dir: "C:\\path\\to", base: "script.ts" }), "C:\\path\\to\\script.ts");
|
|
|
|
* } else {
|
|
|
|
* assertEquals(format({ dir: "/path/to/dir", base: "script.ts" }), "/path/to/dir/script.ts");
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param pathObject Object with path components.
|
|
|
|
* @returns The formatted path.
|
2023-08-07 07:45:33 +00:00
|
|
|
*/
|
2024-07-05 03:26:18 +00:00
|
|
|
export function format(pathObject: Partial<ParsedPath>): string {
|
2023-08-07 07:45:33 +00:00
|
|
|
return isWindows ? windowsFormat(pathObject) : posixFormat(pathObject);
|
|
|
|
}
|