This release is 21 versions behind 1.1.4 — the latest version of @std/path. Jump to latest
Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
Utilities for working with file system paths
This package works with Cloudflare Workers, Deno, Browsers


JSR Score
94%
Published
2 years ago (0.221.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright the Browserify authors. MIT License. // Ported from https://github.com/browserify/path-browserify/ import type { FormatInputPathObject, ParsedPath } from "./mod.ts"; import { assertEquals } from "jsr:@std/assert@^0.221.0"; import * as posix from "./posix/mod.ts"; import * as windows from "./windows/mod.ts"; type FormatTestCase = [FormatInputPathObject, string]; type ParseTestCase = [string, ParsedPath]; const winPaths: Array<[string, string]> = [ // [path, root] ["C:\\path\\dir\\index.html", "C:\\"], ["C:\\another_path\\DIR\\1\\2\\33\\\\index", "C:\\"], ["another_path\\DIR with spaces\\1\\2\\33\\index", ""], ["\\", "\\"], ["\\foo\\C:", "\\"], ["file", ""], ["file:stream", ""], [".\\file", ""], ["C:", "C:"], ["C:.", "C:"], ["C:..", "C:"], ["C:abc", "C:"], ["C:\\", "C:\\"], ["C:\\abc", "C:\\"], ["", ""], // unc ["\\\\server\\share\\file_path", "\\\\server\\share\\"], [ "\\\\server two\\shared folder\\file path.zip", "\\\\server two\\shared folder\\", ], ["\\\\teela\\admin$\\system32", "\\\\teela\\admin$\\"], ["\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\"], ]; const winSpecialCaseParseTests: ParseTestCase[] = [ ["/foo/bar", { root: "/", dir: "/foo", base: "bar", ext: "", name: "bar" }], ]; const winSpecialCaseFormatTests: FormatTestCase[] = [ [{ dir: "some\\dir" }, "some\\dir\\"], [{ base: "index.html" }, "index.html"], [{ root: "C:\\" }, "C:\\"], [{ name: "index", ext: ".html" }, "index.html"], [{ dir: "some\\dir", name: "index", ext: ".html" }, "some\\dir\\index.html"], [{ root: "C:\\", name: "index", ext: ".html" }, "C:\\index.html"], [{}, ""], ]; const unixPaths: Array<[string, string, string?]> = [ // [path, root, formatted] ["/home/user/dir/file.txt", "/"], ["/home/user/a dir/another File.zip", "/"], ["/home/user/a dir//another&File.", "/", "/home/user/a dir/another&File."], [ "/home/user/a$$$dir//another File.zip", "/", "/home/user/a$$$dir/another File.zip", ], ["user/dir/another File.zip", ""], ["file", ""], [".\\file", ""], ["./file", ""], ["C:\\foo", ""], ["/", "/"], ["", ""], [".", ""], ["..", ""], ["/foo", "/"], ["/foo.", "/"], ["/foo.bar", "/"], ["/.", "/"], ["/.foo", "/"], ["/.foo.bar", "/"], ["/foo/bar.baz", "/"], ]; const unixSpecialCaseFormatTests: FormatTestCase[] = [ [{ dir: "some/dir" }, "some/dir/"], [{ base: "index.html" }, "index.html"], [{ root: "/" }, "/"], [{ name: "index", ext: ".html" }, "index.html"], [{ dir: "some/dir", name: "index", ext: ".html" }, "some/dir/index.html"], [{ root: "/", name: "index", ext: ".html" }, "/index.html"], [{}, ""], ]; function checkParseFormat( path: typeof windows | typeof posix, testCases: Array<[string, string, string?]>, ) { testCases.forEach(([element, root, formatted]) => { const output = path.parse(element); assertEquals(typeof output.root, "string"); assertEquals(typeof output.dir, "string"); assertEquals(typeof output.base, "string"); assertEquals(typeof output.ext, "string"); assertEquals(typeof output.name, "string"); assertEquals(output.root, root); assertEquals(output.dir, output.dir ? path.dirname(element) : ""); assertEquals(output.base, path.basename(element)); assertEquals(output.ext, path.extname(element)); // We normalize incorrect paths during parsing, so some "incorrect" // input cannot be asserted for equality onto itself. if (formatted) { assertEquals(path.format(output), formatted); } else { assertEquals(path.format(output), element); } }); } function checkSpecialCaseParseFormat( path: typeof windows | typeof posix, testCases: ParseTestCase[], ) { testCases.forEach(([element, expect]) => { assertEquals(path.parse(element), expect); }); } function checkFormat( path: typeof windows | typeof posix, testCases: FormatTestCase[], ) { testCases.forEach((testCase) => { assertEquals(path.format(testCase[0]), testCase[1]); }); } Deno.test("windows.parse()", function () { checkParseFormat(windows, winPaths); checkSpecialCaseParseFormat(windows, winSpecialCaseParseTests); }); Deno.test("posix.parse()", function () { checkParseFormat(posix, unixPaths); }); Deno.test("windows.format()", function () { checkFormat(windows, winSpecialCaseFormatTests); }); Deno.test("posix.format()", function () { checkFormat(posix, unixSpecialCaseFormatTests); }); // Test removal of trailing path separators const windowsTrailingTests: ParseTestCase[] = [ [".\\", { root: "", dir: "", base: ".", ext: "", name: "." }], ["\\\\", { root: "\\", dir: "\\", base: "\\", ext: "", name: "" }], ["\\\\", { root: "\\", dir: "\\", base: "\\", ext: "", name: "" }], [ "c:\\foo\\\\\\", { root: "c:\\", dir: "c:\\", base: "foo", ext: "", name: "foo" }, ], [ "D:\\foo\\\\\\bar.baz", { root: "D:\\", dir: "D:\\foo\\\\", base: "bar.baz", ext: ".baz", name: "bar", }, ], ]; const posixTrailingTests: ParseTestCase[] = [ ["./", { root: "", dir: "", base: ".", ext: "", name: "." }], ["//", { root: "/", dir: "/", base: "/", ext: "", name: "" }], ["///", { root: "/", dir: "/", base: "/", ext: "", name: "" }], ["/foo///", { root: "/", dir: "/", base: "foo", ext: "", name: "foo" }], [ "/foo///bar.baz", { root: "/", dir: "/foo", base: "bar.baz", ext: ".baz", name: "bar" }, ], ]; Deno.test("windows.parseTrailing()", function () { windowsTrailingTests.forEach(function (p) { const actual = windows.parse(p[0]); const expected = p[1]; assertEquals(actual, expected); }); }); Deno.test("parseTrailing()", function () { posixTrailingTests.forEach(function (p) { const actual = posix.parse(p[0]); const expected = p[1]; assertEquals(actual, expected); }); });