deno.land / std@0.224.0 / path / posix / dirname.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { assertArg } from "../_common/dirname.ts";import { stripTrailingSeparators } from "../_common/strip_trailing_separators.ts";import { isPosixPathSeparator } from "./_util.ts";
/** * Return the directory path of a `path`. * * @example * ```ts * import { dirname } from "https://deno.land/std@$STD_VERSION/path/dirname.ts"; * * console.log(dirname("/home/user/Documents/")); // "/home/user" * console.log(dirname("/home/user/Documents/image.png")); // "/home/user/Documents" * ``` * * @param path - path to extract the directory from. */export function dirname(path: string): string { assertArg(path);
let end = -1; let matchedNonSeparator = false;
for (let i = path.length - 1; i >= 1; --i) { if (isPosixPathSeparator(path.charCodeAt(i))) { if (matchedNonSeparator) { end = i; break; } } else { matchedNonSeparator = true; } }
// No matches. Fallback based on provided path: // // - leading slashes paths // "/foo" => "/" // "///foo" => "/" // - no slash path // "foo" => "." if (end === -1) { return isPosixPathSeparator(path.charCodeAt(0)) ? "/" : "."; }
return stripTrailingSeparators( path.slice(0, end), isPosixPathSeparator, );}
Version Info