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";
|
2024-07-05 03:26:18 +00:00
|
|
|
import type { ParsedPath } from "./types.ts";
|
2023-09-28 10:54:53 +00:00
|
|
|
import { parse as posixParse } from "./posix/parse.ts";
|
|
|
|
import { parse as windowsParse } from "./windows/parse.ts";
|
2023-08-07 07:45:33 +00:00
|
|
|
|
2024-07-05 03:26:18 +00:00
|
|
|
export type { ParsedPath } from "./types.ts";
|
2024-04-24 21:02:35 +00:00
|
|
|
|
2023-08-07 07:45:33 +00:00
|
|
|
/**
|
2024-06-02 02:46:36 +00:00
|
|
|
* Return an object containing the parsed components of the path.
|
2024-03-16 05:32:20 +00:00
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* Use {@linkcode https://jsr.io/@std/path/doc/~/format | format()} to reverse
|
|
|
|
* the result.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2024-03-16 05:32:20 +00:00
|
|
|
* ```ts
|
2024-06-02 02:46:36 +00:00
|
|
|
* import { parse } from "@std/path/parse";
|
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-03-16 05:32:20 +00:00
|
|
|
*
|
2024-06-02 02:46:36 +00:00
|
|
|
* if (Deno.build.os === "windows") {
|
|
|
|
* const parsedPathObj = parse("C:\\path\\to\\script.ts");
|
|
|
|
* assertEquals(parsedPathObj.root, "C:\\");
|
|
|
|
* assertEquals(parsedPathObj.dir, "C:\\path\\to");
|
|
|
|
* assertEquals(parsedPathObj.base, "script.ts");
|
|
|
|
* assertEquals(parsedPathObj.ext, ".ts");
|
|
|
|
* assertEquals(parsedPathObj.name, "script");
|
|
|
|
* } else {
|
|
|
|
* const parsedPathObj = parse("/path/to/dir/script.ts");
|
|
|
|
* parsedPathObj.root; // "/"
|
|
|
|
* parsedPathObj.dir; // "/path/to/dir"
|
|
|
|
* parsedPathObj.base; // "script.ts"
|
|
|
|
* parsedPathObj.ext; // ".ts"
|
|
|
|
* parsedPathObj.name; // "script"
|
|
|
|
* }
|
2024-03-16 05:32:20 +00:00
|
|
|
* ```
|
2024-06-02 02:46:36 +00:00
|
|
|
*
|
|
|
|
* @param path Path to process
|
|
|
|
* @returns An object with the parsed path components.
|
2023-08-07 07:45:33 +00:00
|
|
|
*/
|
|
|
|
export function parse(path: string): ParsedPath {
|
|
|
|
return isWindows ? windowsParse(path) : posixParse(path);
|
|
|
|
}
|