os-slash
v1.0.3
Published
Cross-platform path utility for consistent slash normalization and manipulation
Maintainers
Readme
os-slash
Cross-platform, dependency-free JavaScript utility for consistent path normalization and manipulation.
Overview
os-slash is a lightweight utility library designed to handle file path transformations across multiple operating systems such as Windows, Linux, and macOS. It provides functions to:
- Convert Windows backslashes (
\) to POSIX-style forward slashes (/) and vice versa - Normalize paths by removing redundant slashes and trailing slashes
- Detect absolute paths on different platforms
- Safely join multiple path segments
- Calculate relative paths between two paths
- Manage trailing slashes (add or remove)
- Preserve special Windows extended-length paths (
\\?\) intact
The library is written in pure JavaScript, has zero dependencies, and is compatible with Node.js, browsers, React, Angular, and any JavaScript environment.
Installation
npm install os-slashUsage
Import individual functions as needed in your project:
import {
slash,
backslash,
normalize,
ensureTrailingSlash,
removeTrailingSlash,
isAbsolute,
join,
relative,
} from "os-slash";API Reference
slash(path: string): string
Converts Windows-style backslashes (\) to forward slashes (/).
Leaves Windows extended-length paths (starting with \\?\) unchanged.
Example:
console.log(slash("C:\\Users\\Alice\\Documents"));
// Output: C:/Users/Alice/Documents
console.log(slash("\\\\?\\C:\\Very\\Long\\Path"));
// Output: \\?\C:\Very\Long\Path (unchanged)backslash(path: string): string
Converts forward slashes (/) to Windows backslashes ().
backslash(path: string): string
Example:
console.log(backslash("C:/Users/Alice/Documents"));
// Output: C:\Users\Alice\Documentsnormalize(path: string): string
Normalizes paths by converting backslashes to forward slashes, removing redundant slashes, and trimming trailing slashes except root /. Preserves extended-length Windows paths.
Example:
console.log(normalize("C:\\Users\\\\Alice\\/Documents\\/"));
// Output: C:/Users/Alice/Documents
console.log(normalize("////usr//local///bin/"));
// Output: /usr/local/binensureTrailingSlash(path: string): string
Adds a trailing slash (/) if it is missing.
Example:
console.log(ensureTrailingSlash("C:/Users/Alice"));
// Output: C:/Users/Alice/
console.log(ensureTrailingSlash("/usr/local/bin/"));
// Output: /usr/local/bin/removeTrailingSlash(path: string): string
Removes trailing slash (/) unless the path is root (/).
Example:
console.log(removeTrailingSlash("C:/Users/Alice/"));
// Output: C:/Users/Alice
console.log(removeTrailingSlash("/"));
// Output: /isAbsolute(path: string): boolean
Detects if a path is absolute on Windows or POSIX.
POSIX: paths starting with / Windows: paths starting with drive letter + :\ or UNC paths starting with \
Example:
console.log(isAbsolute("/usr/local/bin")); // true
console.log(isAbsolute("C:\\Windows")); // true
console.log(isAbsolute("Documents/file")); // falsejoin(...paths: string[]): string
Joins multiple path segments into a single normalized path using forward slashes.
Example:
console.log(join("C:/Users", "Alice", "Documents"));
// Output: C:/Users/Alice/Documents
console.log(join("/usr", "local//", "/bin/"));
// Output: /usr/local/binrelative(from: string, to: string): string
Calculates relative path from from to to.
Example:
console.log(relative("/Users/Alice/Documents", "/Users/Alice/Music"));
// Output: ../Music
console.log(relative("C:/Projects/app/src", "C:/Projects/app/tests"));
// Output: ../tests