@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function parseFilepathInfo

    • parses the provided file path and breaks it down into useful bit described by the interface FilepathInfo. note that a file path must never end in a trailing slash ("/"), and conversely, a folder path must always in a trailing slash ("/"), otherwise it will be parsed as a file.

      Parameters

      • file_path: string

      Returns FilepathInfo

      import { assertEquals } from "jsr:@std/assert"

      // aliasing our functions for brevity
      const eq = assertEquals, fn = parseFilepathInfo

      eq(fn("/home\\user/docs"), {
      path: "/home/user/docs",
      dirpath: "/home/user/",
      dirname: "user",
      filename: "docs",
      basename: "docs",
      extname: "",
      })

      eq(fn("home\\user/docs/"), {
      path: "home/user/docs/",
      dirpath: "home/user/docs/",
      dirname: "docs",
      filename: "",
      basename: "",
      extname: "",
      })

      eq(fn("/home/xyz/.././././user/.bashrc."), {
      path: "/home/user/.bashrc.",
      dirpath: "/home/user/",
      dirname: "user",
      filename: ".bashrc.",
      basename: ".bashrc.",
      extname: "",
      })

      eq(fn("C:\\home\\user/.file.tar.gz"), {
      path: "C:/home/user/.file.tar.gz",
      dirpath: "C:/home/user/",
      dirname: "user",
      filename: ".file.tar.gz",
      basename: ".file.tar",
      extname: ".gz", // only the last bit of the extension makes it to here
      })

      eq(fn("/home/user///file.txt"), {
      path: "/home/user///file.txt",
      dirpath: "/home/user///",
      dirname: "", // this is because the there is no name attached between the last two slashes of the `dirpath = "/home/user///"`
      filename: "file.txt",
      basename: "file",
      extname: ".txt",
      })

      eq(fn("file://C:/home\\hello world.txt"), {
      path: "file://C:/home/hello world.txt", // file-urls are not converted, nor is any kind of url
      dirpath: "file://C:/home/",
      dirname: "home",
      filename: "hello world.txt",
      basename: "hello world",
      extname: ".txt",
      })