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

    Function ensureFileUrlIsLocalPath

    • a fault tolerant variant of fileUrlToLocalPath that assures you that any file-url path will get converted into a filesystem local-path. otherwise, when a non-file-url is provided, its string representation (href) will be returned if it was a URL, else the original string will be returned back.

      Parameters

      • path: string | URL

      Returns string

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

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

      eq( fn("C:/Users/me/projects/"), "C:/Users/me/projects/")
      eq( fn("C:\\Users\\me/projects/"), "C:/Users/me/projects/")
      eq( fn("/C:/Users\\me/projects/"), "/C:/Users/me/projects/") // note the erroneous leading slash
      eq( fn("/sys\\etc/bin\\deno.so"), "/sys/etc/bin/deno.so")
      eq( fn("file:///C:/Users/me/projects/"), "C:/Users/me/projects/")
      eq( fn("file://////C:\\Users\\me/projects/"), "C:/Users/me/projects/")
      eq( fn("file:///sys\\etc/bin\\deno.so"), "/sys/etc/bin/deno.so")
      eq( fn("file://localhost/C:/Users/me/projects/"), "C:/Users/me/projects/")
      eq(fn(new URL("file://localhost/sys/etc/bin/deno.so")), "/sys/etc/bin/deno.so")
      eq( fn("http://localhost:8000/hello/world/"), "http://localhost:8000/hello/world/")
      eq( fn("npm:react-jsx"), "npm:react-jsx")
      eq( fn("jsr:@std/assert"), "jsr:@std/assert")
      eq( fn("./src/mod.ts"), "./src/mod.ts")
      eq( fn(""), "")