slash-modern
v0.1.0
Published
Normalize path separators to forward slashes across Windows, POSIX, URLs.
Maintainers
Readme
slash-modern
Normalize path separators to forward slashes. Works with Windows paths, POSIX paths, UNC paths, and URLs. Tiny, zero-runtime-deps, publish-ready.
Install
npm install slash-modernUsage
import slash from "slash-modern";
slash("C\\\\Users\\\\Name\\\\file.txt"); //=> "C:/Users/Name/file.txt"
slash("\\\\server\\\\share\\\\folder\\\\file.ext"); //=> "//server/share/folder/file.ext"
slash("https://example.com\\foo\\bar"); //=> "https://example.com/foo/bar"
slash("C\\\\foo/bar\\\\baz"); //=> "C:/foo/bar/baz"CommonJS
const slash = require("slash-modern");
slash("C\\\\Users\\\\Name\\\\file.txt");API
slash(input: string, options?: { preserveExtendedWindowsPaths?: boolean }): stringinput: Any path-like string.preserveExtendedWindowsPaths(defaulttrue): Leaves\\?\and\\.\prefixed Windows paths unchanged.
Behavior
- Replaces
\with/everywhere except extended-length (\\?\) and device (\\.\) Windows paths when preserved. - Detects protocols like
http://,https://,file://, and normalizes the rest of the string. - Handles Windows drive-letter paths (e.g.,
C:) and mixed separators.
Caveats
- Input should be a real path string, not a string containing escape sequences (e.g.,
"\\n"). Converting those will yield/n. - Does not resolve or validate paths; it only changes separators.
- For Windows extended-length or device paths, set
preserveExtendedWindowsPaths: falseif you explicitly want normalization.
Tests
Run the test suite:
npm testIncludes cases for Windows paths, UNC paths, URLs, mixed separators, and special Windows prefixes.
License
MIT
