std/path
2022-11-27 13:59:10 -05:00
..
_constants.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
_interface.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
_util.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
basename_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
common_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
common.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
dirname_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
extname_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
from_file_url_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
glob_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
glob.ts docs: reorganize docs (#2658) 2022-11-25 12:40:23 +01:00
isabsolute_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
join_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
mod.ts docs: reorganize docs (#2658) 2022-11-25 12:40:23 +01:00
parse_format_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
posix.ts docs(path): clarify the meaning of resolve for normalize (#2946) 2022-11-27 13:59:10 -05:00
README.md docs(path/README): fix syntax highlight (#1883) 2022-02-06 18:17:09 +09:00
relative_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
resolve_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
separator.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
to_file_url_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
win32.ts refactor: use _util/asserts.ts for non-test code (#2857) 2022-11-10 16:28:20 +09:00
zero_length_strings_test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00

path

path module is made to provide helpers to manipulate the path.

Usage

import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

Codes in the following example uses POSIX path but it automatically use Windows path on Windows. Use methods under posix or win32 object instead to handle non platform specific path like:

import { posix, win32 } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p1 = posix.fromFileUrl("file:///home/foo");
const p2 = win32.fromFileUrl("file:///home/foo");
console.log(p1); // "/home/foo"
console.log(p2); // "\\home\\foo"

basename

Return the last portion of a path. Trailing directory separators are ignored.

import { basename } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = basename("./deno/std/path/mod.ts");
console.log(p); // "mod.ts"

dirname

Return the directory name of a path.

import { dirname } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = dirname("./deno/std/path/mod.ts");
console.log(p); // "./deno/std/path"

extname

Return the extension of the path.

import { extname } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = extname("./deno/std/path/mod.ts");
console.log(p); // ".ts"

format

Generate a path from FormatInputPathObject object.

import { format } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = format({
  root: "/",
  dir: "/home/user/dir",
  ext: ".html",
  name: "index",
});
console.log(p); // "/home/user/dir/index.html"

fromFileUrl

Converts a file URL to a path string.

import { fromFileUrl } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = fromFileUrl("file:///home/foo");
console.log(p); // "/home/foo"

isAbsolute

Verifies whether provided path is absolute

import { isAbsolute } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
console.log(isAbsolute("/home/foo")); // true
console.log(isAbsolute("foo")); // false

join

Join all given a sequence of paths,then normalizes the resulting path.

import { join } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = join("foo", "bar");
console.log(p); // "foo/bar"

normalize

Normalize the path, resolving '..' and '.' segments.

import { normalize } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = normalize("/home/foo/bar/../hoge/./piyo");
console.log(p); // "/home/foo/hoge/piyo"

parse

Return a ParsedPath object of the path.

import { parse } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = parse("/home/user/dir/index.html");
console.log(p);
/*
 * {
 *   root: "/",
 *   dir: "/home/user/dir",
 *   base: "index.html",
 *   ext: ".html",
 *   name: "index",
 * }
 */

relative

Return the relative path from from to to based on current working directory.

import { relative } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = relative("/var/lib", "/var/apache");
console.log(p); // "../apache"

resolve

Resolves pathSegments into an absolute path.

import { resolve } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = resolve("/var/lib", "../", "file/");
console.log(p); // "/var/file"

toFileUrl

Converts a path string to a file URL.

import { toFileUrl } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = toFileUrl("/home/foo");
console.log(p);
/*
 * URL {
 *   href: "file:///home/foo",
 *   origin: "null",
 *   protocol: "file:",
 *   username: "",
 *   password: "",
 *   host: "",
 *   hostname: "",
 *   port: "",
 *   pathname: "/home/foo",
 *   hash: "",
 *   search: ""
 * }
 */

toNamespacedPath

Resolves path to a namespace path

import { toNamespacedPath } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = toNamespacedPath("/home/foo");
console.log(p); // "/home/foo"

common

Determines the common path from a set of paths, using an optional separator, which defaults to the OS default separator.

import { common } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
const p = common([
  "./deno/std/path/mod.ts",
  "./deno/std/fs/mod.ts",
]);
console.log(p); // "./deno/std/"

globToRegExp

Generate a regex based on glob pattern and options This was meant to be using the fs.walk function but can be used anywhere else.

import { globToRegExp } from "https://deno.land/std@$STD_VERSION/path/glob.ts";

globToRegExp("foo/**/*.json", {
  extended: true,
  globstar: true,
  caseInsensitive: false,
}); // returns the regex to find all .json files in the folder foo.