Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(path): improve API docs #4900

Merged
merged 20 commits into from
Jun 2, 2024
Merged
1 change: 1 addition & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ENTRY_POINTS = [
"../internal/mod.ts",
"../jsonc/mod.ts",
"../media_types/mod.ts",
"../path/mod.ts",
"../semver/mod.ts",
"../streams/mod.ts",
"../text/mod.ts",
Expand Down
3 changes: 3 additions & 0 deletions path/_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ export interface ParsedPath {
name: string;
}

/**
* A parsed path object to be consumed by `path.format()`.
*/
export type FormatInputPathObject = Partial<ParsedPath>;
4 changes: 4 additions & 0 deletions path/_os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// This module is browser compatible.

// Keep this up-to-date with Deno.build.os
/**
* Operating system type, equivalent to the type of
* {@linkcode https://deno.land/api?s=Deno.build | Deno.build.os}.
*/
export type OSType =
| "darwin"
| "linux"
Expand Down
23 changes: 15 additions & 8 deletions path/basename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ import { basename as posixBasename } from "./posix/basename.ts";
import { basename as windowsBasename } from "./windows/basename.ts";

/**
* Return the last portion of a `path`.
* Trailing directory separators are ignored, and optional suffix is removed.
* Return the last portion of a path.
*
* @example
* The trailing directory separators are ignored, and optional suffix is
* removed.
*
* @example Usage
* ```ts
* import { basename } from "@std/path/basename";
* import { assertEquals } from "@std/assert/assert-equals";
*
* basename("/home/user/Documents/"); // "Documents"
* basename("C:\\user\\Documents\\image.png"); // "image.png"
* basename("/home/user/Documents/image.png", ".png"); // "image"
* if (Deno.build.os === "windows") {
* assertEquals(basename("C:\\user\\Documents\\image.png"), "image.png");
* } else {
* assertEquals(basename("/home/user/Documents/image.png"), "image.png");
* }
* ```
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
*
* @param path - path to extract the name from.
* @param [suffix] - suffix to remove from extracted name.
* @param path Path to extract the name from.
* @param suffix Suffix to remove from extracted name.
*
* @returns The basename of the path.
*/
export function basename(path: string, suffix = ""): string {
return isWindows
Expand Down
20 changes: 13 additions & 7 deletions path/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
import { _common } from "./_common/common.ts";
import { SEPARATOR } from "./constants.ts";

/** Determines the common path from a set of paths, using an optional separator,
/**
* Determines the common path from a set of paths, using an optional separator,
* which defaults to the OS default separator.
*
* @param paths Paths to search for common path.
* @param sep Path separator to use.
* @returns The common path.
*
* @example Usage
* ```ts
* import { common } from "@std/path";
* const p = common([
* "./deno/std/path/mod.ts",
* "./deno/std/fs/mod.ts",
* ]);
* console.log(p); // "./deno/std/"
* import { common } from "@std/path";
*
* common([
* "./deno/std/path/mod.ts",
* "./deno/std/fs/mod.ts"
* ]); // "./deno/std/"
* ```
*/
export function common(
Expand Down
11 changes: 11 additions & 0 deletions path/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
// This module is browser compatible.
import { isWindows } from "./_os.ts";

/**
* The character used to separate entries in the PATH environment variable.
* On Windows, this is `;`. On all other platforms, this is `:`.
*/

Check warning on line 8 in path/constants.ts

View check run for this annotation

Codecov / codecov/patch

path/constants.ts#L8

Added line #L8 was not covered by tests
export const DELIMITER = isWindows ? ";" as const : ":" as const;
/**
* The character used to separate components of a file path.
* On Windows, this is `\`. On all other platforms, this is `/`.
*/

Check warning on line 13 in path/constants.ts

View check run for this annotation

Codecov / codecov/patch

path/constants.ts#L13

Added line #L13 was not covered by tests
export const SEPARATOR = isWindows ? "\\" as const : "/" as const;
/**
* A regular expression that matches one or more path separators.
*/

Check warning on line 17 in path/constants.ts

View check run for this annotation

Codecov / codecov/patch

path/constants.ts#L17

Added line #L17 was not covered by tests
export const SEPARATOR_PATTERN = isWindows ? /[\\/]+/ : /\/+/;
16 changes: 14 additions & 2 deletions path/dirname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ import { dirname as posixDirname } from "./posix/dirname.ts";
import { dirname as windowsDirname } from "./windows/dirname.ts";

/**
* Return the directory path of a `path`.
* @param path - path to extract the directory from.
* Return the directory path of a pauth.
*
* @param path Path to extract the directory from.
*
* @returns The directory path.
*
* @example Usage
* ```ts
* import { dirname } from "@std/path/dirname";
*
* dirname("/home/user/Documents/image.png"); // "/home/user/Documents"
* dirname("C:\\user\\Documents\\image.png"); // "C:\\user\\Documents"
* dirname("image.png"); // "."
* ```
*/
export function dirname(path: string): string {
return isWindows ? windowsDirname(path) : posixDirname(path);
Expand Down
16 changes: 13 additions & 3 deletions path/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import { isWindows } from "./_os.ts";
import { extname as posixExtname } from "./posix/extname.ts";
import { extname as windowsExtname } from "./windows/extname.ts";
/**
* Return the extension of the `path` with leading period.
* @param path with extension
* @returns extension (ex. for `file.ts` returns `.ts`)
* Return the extension of the path with leading period (".").
*
* @param path Path with extension.
*
* @returns The file extension. E.g. returns `.ts` for `file.ts`.
*
* @example Usage
* ```ts
* import { extname } from "@std/path/extname";
*
* extname("/home/user/Documents/image.png"); // ".png"
* extname("C:\\user\\Documents\\image.png"); // ".png"
* ```
*/
export function extname(path: string): string {
return isWindows ? windowsExtname(path) : posixExtname(path);
Expand Down
15 changes: 12 additions & 3 deletions path/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ import { format as windowsFormat } from "./windows/format.ts";
import type { FormatInputPathObject } from "./_interface.ts";

/**
* Generate a path from `FormatInputPathObject` object. It does the opposite
* of `parse`.
* Generate a path from a {@linkcode FormatInputPathObject} object. It does the
* opposite of {@linkcode https://jsr.io/@std/path/doc/~/parse | parse()}.
*
* @param pathObject with path
* @param pathObject Object with path components.
* @returns The formatted path.
*
* @example Usage
* ```ts
* import { format } from "@std/path/format";
*
* format({ dir: "/path/to/dir", base: "script.ts" }); // "/path/to/dir/script.ts"
* format({ root: "/", name: "script", ext: ".ts" }); // "/script.ts"
* ```
*/
export function format(pathObject: FormatInputPathObject): string {
return isWindows ? windowsFormat(pathObject) : posixFormat(pathObject);
Expand Down
5 changes: 4 additions & 1 deletion path/from_file_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fromFileUrl as windowsFromFileUrl } from "./windows/from_file_url.ts";
/**
* Converts a file URL to a path string.
*
* @example Usage
* ```ts
* import { fromFileUrl } from "@std/path/from-file-url";
*
Expand All @@ -19,7 +20,9 @@ import { fromFileUrl as windowsFromFileUrl } from "./windows/from_file_url.ts";
* fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo"
* fromFileUrl("file://localhost/home/foo"); // "\\\\localhost\\home\\foo"
* ```
* @param url of a file URL
* @param url The file URL to convert to a path.
*
* @returns The path string.
*/
export function fromFileUrl(url: string | URL): string {
return isWindows ? windowsFromFileUrl(url) : posixFromFileUrl(url);
Expand Down
25 changes: 22 additions & 3 deletions path/glob_to_regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import {
globToRegExp as windowsGlobToRegExp,
} from "./windows/glob_to_regexp.ts";

export type { GlobOptions };
export type { GlobOptions, OSType };

/** Options for {@linkcode globToRegExp}. */
export type GlobToRegExpOptions = GlobOptions & {
/**
* The operating system to interpret paths as.
*
* If unset, it defaults to the current operating system.
*/
os?: OSType;
};

/** Convert a glob string to a regular expression.
/**
* Converts a glob string to a regular expression.
*
* Tries to match bash glob expansion as closely as possible.
*
Expand Down Expand Up @@ -69,7 +76,19 @@ export type GlobToRegExpOptions = GlobOptions & {
* look-ahead followed by a wildcard. This means that `!(foo).js` will wrongly
* fail to match `foobar.js`, even though `foobar` is not `foo`. Effectively,
* `!(foo|bar)` is treated like `!(@(foo|bar)*)`. This will work correctly if
* the group occurs not nested at the end of the segment. */
* the group occurs not nested at the end of the segment.
*
* @param glob Glob string to convert.
* @param options Conversion options.
* @returns The regular expression equivalent to the glob.
*
* @example Usage
* ```ts
* import { globToRegExp } from "@std/path/glob-to-regexp";
*
* globToRegExp("*.js"); // /^[^/]*\.js\/*$/;
* ```
*/
export function globToRegExp(
glob: string,
options: GlobToRegExpOptions = {},
Expand Down
20 changes: 18 additions & 2 deletions path/is_absolute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ import { isAbsolute as posixIsAbsolute } from "./posix/is_absolute.ts";
import { isAbsolute as windowsIsAbsolute } from "./windows/is_absolute.ts";

/**
* Verifies whether provided path is absolute
* @param path to be verified as absolute
* Verifies whether provided path is absolute.
*
* @param path Path to be verified as absolute.
*
* @returns `true` if path is absolute, `false` otherwise
*
* @example Usage
* ```ts
* import { isAbsolute } from "@std/path/is-absolute";
*
* // posix
* isAbsolute("/home/foo"); // true
* isAbsolute("home/foo"); // false
*
* // win32
* isAbsolute("C:\\home\\foo"); // true
* isAbsolute("home\\foo"); // false
* ```
*/
export function isAbsolute(path: string): boolean {
return isWindows ? windowsIsAbsolute(path) : posixIsAbsolute(path);
Expand Down
17 changes: 16 additions & 1 deletion path/is_glob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/** Test whether the given string is a glob */
/**
* Test whether the given string is a glob.
*
* @param str String to test.
*
* @returns `true` if the given string is a glob, otherwise `false`
*
* @example Usage
* ```ts
* import { isGlob } from "@std/path/is-glob";
* import { assert } from "@std/assert/assert";
*
* assert(!isGlob("foo/bar/../baz"));
* assert(isGlob("foo/*ar/../baz"));
* ```
*/
export function isGlob(str: string): boolean {
const chars: Record<string, string> = { "{": "}", "(": ")", "[": "]" };
const regex =
Expand Down
18 changes: 16 additions & 2 deletions path/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ import { join as posixJoin } from "./posix/join.ts";
import { join as windowsJoin } from "./windows/join.ts";

/**
* Join all given a sequence of `paths`,then normalizes the resulting path.
* @param paths to be joined and normalized
* Join all given a sequence of paths, then normalizes the resulting path.
*
* @param paths Paths to be joined and normalized.
*
* @returns The joined and normalized path.
*
* @example Usage
* ```ts
* import { join } from "@std/path/join";
*
* // posix
* join("/foo", "bar", "baz/quux", "garply", ".."); // "/foo/bar/baz/quux"
*
* // win32
* join("C:\\foo", "bar", "baz\\quux", "garply", ".."); // "C:\\foo\\bar\\baz\\quux"
* ```
*/
export function join(...paths: string[]): string {
return isWindows ? windowsJoin(...paths) : posixJoin(...paths);
Expand Down
20 changes: 19 additions & 1 deletion path/join_globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ import { joinGlobs as windowsJoinGlobs } from "./windows/join_globs.ts";

export type { GlobOptions };

/** Like join(), but doesn't collapse "**\/.." when `globstar` is true. */
/**
* Joins a sequence of globs, then normalizes the resulting glob.
*
* Behaves like {@linkcode https://jsr.io/@std/path/doc/~/join | join()}, but
* doesn't collapse `**\/..` when `globstar` is true.
*
* @param globs Globs to be joined and normalized.
* @param options Glob options.
*
* @returns The joined and normalized glob string.
*
* @example Usage
* ```ts
* import { joinGlobs } from "@std/path/join-globs";
*
* joinGlobs(["foo", "bar", "..", "baz"]); // "foo/baz"
* joinGlobs(["foo", "**\/..", "bar", "..", "baz"], { globstar: true }); // "foo/**\/../baz"
* ```
*/
export function joinGlobs(
globs: string[],
options: GlobOptions = {},
Expand Down
4 changes: 4 additions & 0 deletions path/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ import * as _windows from "./windows/mod.ts";
import * as _posix from "./posix/mod.ts";

/**
* Module for working with Windows file paths.
*
* @deprecated This will be removed in 1.0.0. Import from
* {@link https://jsr.io/@std/path/doc/windows/~ | @std/path/windows} instead.
*/
export const win32: typeof _windows = _windows;

/**
* Module for working with POSIX file paths.
*
* @deprecated This will be removed in 1.0.0. Import from
* {@link https://jsr.io/@std/path/doc/posix/~ | @std/path/posix} instead.
*/
Expand Down
24 changes: 20 additions & 4 deletions path/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ import { isWindows } from "./_os.ts";
import { normalize as posixNormalize } from "./posix/normalize.ts";
import { normalize as windowsNormalize } from "./windows/normalize.ts";
/**
* Normalize the `path`, resolving `'..'` and `'.'` segments.
* Note that resolving these segments does not necessarily mean that all will be eliminated.
* A `'..'` at the top-level will be preserved, and an empty path is canonically `'.'`.
* @param path to be normalized
* Normalize the pat`, resolving `'..'` and `'.'` segments.
*
* Note: Resolving these segments does not necessarily mean that all will be
* eliminated. A `'..'` at the top-level will be preserved, and an empty path is
* canonically `'.'`.
*
* @param path Path to be normalized
*
* @returns The normalized path.
*
* @example Usage
* ```ts
* import { normalize } from "@std/path/normalize";
*
* // posix
* normalize("/foo/bar/./baz/.././quux"); // "/foo/bar/quux"
*
* // win32
* normalize("C:\\foo\\bar\\.\\baz\\..\\.\\quux"); // "C:\\foo\\bar\\quux"
* ```
*/
export function normalize(path: string): string {
return isWindows ? windowsNormalize(path) : posixNormalize(path);
Expand Down
Loading