CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-path

Node.js path module providing utilities for working with file and directory paths in a cross-platform manner

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

path-parsing.mddocs/

Path Parsing

Path parsing utilities for extracting components from file paths and reconstructing paths from components.

Capabilities

Dirname

Returns the directory name of a path, similar to the Unix dirname command. Trailing directory separators are ignored.

/**
 * Returns the directory portion of a path
 * @param {string} path - Path to extract directory from
 * @returns {string} Directory portion of path
 */
function dirname(path: string): string;

Usage Examples:

const path = require("path");

// Extract directory from file path
path.dirname("/users/john/document.txt");
// Result: "/users/john"

path.dirname("/users/john/");
// Result: "/users"

// Handle relative paths
path.dirname("docs/readme.md");
// Result: "docs"

// No directory returns "."
path.dirname("file.txt");
// Result: "."

// Root directory
path.dirname("/");
// Result: "/"

Platform Differences:

// Windows paths
path.win32.dirname("C:\\Users\\John\\file.txt");
// Result: "C:\\Users\\John"

path.win32.dirname("C:\\");
// Result: "C:\\"

// UNC paths
path.win32.dirname("\\\\server\\share\\file.txt");
// Result: "\\\\server\\share"

Basename

Returns the last portion of a path, similar to the Unix basename command. If ext is provided and matches the end of the basename, it is removed.

/**
 * Returns the last portion of a path
 * @param {string} path - Path to extract basename from
 * @param {string} [ext] - Optional extension to remove
 * @returns {string} Basename of path
 */
function basename(path: string, ext?: string): string;

Usage Examples:

const path = require("path");

// Extract filename from path
path.basename("/users/john/document.txt");
// Result: "document.txt"

// Remove specific extension
path.basename("/users/john/document.txt", ".txt");
// Result: "document"

// Extension must match exactly
path.basename("/users/john/document.txt", ".md");
// Result: "document.txt" (extension doesn't match)

// Directory paths
path.basename("/users/john/");
// Result: "john"

// Handle relative paths
path.basename("docs/readme.md");
// Result: "readme.md"

// Root path
path.basename("/");
// Result: ""

Platform Differences:

// Windows paths
path.win32.basename("C:\\Users\\John\\file.txt");
// Result: "file.txt"

path.win32.basename("C:\\Users\\John\\", ".John");
// Result: "John" (if extension matches)

Extname

Returns the extension of the path, from the last occurrence of the . character to end of string in the last portion of the path. If there is no . in the last portion, or if there are no . characters other than the first character, then it returns an empty string.

/**
 * Returns the extension of a path
 * @param {string} path - Path to extract extension from
 * @returns {string} Extension including leading dot, or empty string
 */
function extname(path: string): string;

Usage Examples:

const path = require("path");

// Extract file extension
path.extname("document.txt");
// Result: ".txt"

path.extname("archive.tar.gz");
// Result: ".gz"

// No extension
path.extname("readme");
// Result: ""

// Hidden files starting with dot
path.extname(".gitignore");
// Result: ""

path.extname(".config.json");
// Result: ".json"

// Directory paths
path.extname("/users/john/");
// Result: ""

// Complex paths
path.extname("/users/john.doe/document.pdf");
// Result: ".pdf"

Parse

Parses a path string into an object with root, dir, base, ext, and name properties.

/**
 * Parses a path string into components
 * @param {string} pathString - Path to parse
 * @returns {ParsedPath} Object with path components
 * @throws {TypeError} If pathString is not a string
 */
function parse(pathString: string): ParsedPath;

interface ParsedPath {
  /** Root portion of path (e.g., '/', 'C:\\') */
  root: string;
  /** Directory portion of path */
  dir: string;
  /** File name including extension */
  base: string;
  /** File extension including leading dot */
  ext: string;
  /** File name without extension */
  name: string;
}

Usage Examples:

const path = require("path");

// Parse a complete file path
const parsed = path.parse("/users/john/document.txt");
console.log(parsed);
// Result: {
//   root: "/",
//   dir: "/users/john", 
//   base: "document.txt",
//   ext: ".txt",
//   name: "document"
// }

// Parse relative path
const relative = path.parse("docs/readme.md");
// Result: {
//   root: "",
//   dir: "docs",
//   base: "readme.md", 
//   ext: ".md",
//   name: "readme"
// }

// Parse directory path
const directory = path.parse("/users/john/");
// Result: {
//   root: "/",
//   dir: "/users",
//   base: "john",
//   ext: "",
//   name: "john"
// }

Platform Differences:

// Windows path parsing
const winParsed = path.win32.parse("C:\\Users\\John\\file.txt");
// Result: {
//   root: "C:\\",
//   dir: "C:\\Users\\John",
//   base: "file.txt",
//   ext: ".txt", 
//   name: "file"
// }

// UNC path parsing
const uncParsed = path.win32.parse("\\\\server\\share\\file.txt");
// Result: {
//   root: "\\\\server\\share\\",
//   dir: "\\\\server\\share",
//   base: "file.txt",
//   ext: ".txt",
//   name: "file"
// }

Format

Formats a path object into a path string. This is the opposite of parse(). When providing properties to the pathObject, precedence is given to properties in the following order: dir over root, base over name + ext.

/**
 * Formats a path object into a path string
 * @param {PathObject} pathObject - Object with path components
 * @returns {string} Formatted path string
 * @throws {TypeError} If pathObject is not an object or if root is not string/undefined
 */
function format(pathObject: PathObject): string;

interface PathObject {
  /** Root portion of path */
  root?: string;
  /** Directory portion of path */
  dir?: string;
  /** File name including extension */
  base?: string;
  /** File name without extension (used with ext if base not provided) */
  name?: string;
  /** File extension (used with name if base not provided) */
  ext?: string;
}

Usage Examples:

const path = require("path");

// Format with dir and base
path.format({
  dir: "/users/john",
  base: "document.txt"
});
// Result: "/users/john/document.txt"

// Format with root, name, and ext
path.format({
  root: "/",
  name: "document",
  ext: ".txt"
});
// Result: "/document.txt"

// Dir takes precedence over root
path.format({
  root: "/usr",
  dir: "/users/john",
  base: "file.txt"
});
// Result: "/users/john/file.txt"

// Base takes precedence over name + ext
path.format({
  dir: "/users",
  base: "document.txt",
  name: "readme",
  ext: ".md"
});
// Result: "/users/document.txt"

// Minimal object
path.format({
  base: "file.txt"
});
// Result: "file.txt"

Platform Differences:

// Windows formatting
path.win32.format({
  root: "C:\\",
  dir: "C:\\Users\\John", 
  base: "file.txt"
});
// Result: "C:\\Users\\John\\file.txt"

// POSIX formatting
path.posix.format({
  root: "/",
  dir: "/usr/local",
  base: "bin"
});
// Result: "/usr/local/bin"

Error Handling:

// Throws TypeError for invalid pathObject
try {
  path.format("not an object");
} catch (error) {
  console.log(error.message); // "Parameter 'pathObject' must be an object..."
}

// Throws TypeError for invalid root type
try {
  path.format({ root: 123 });
} catch (error) {
  console.log(error.message); // "'pathObject.root' must be a string or undefined..."
}

Install with Tessl CLI

npx tessl i tessl/npm-path

docs

index.md

path-manipulation.md

path-parsing.md

path-resolution.md

platform-operations.md

tile.json