normalize

import { normalize } from "jsr:@ende/path";

function

function normalize(path: string | URL): string

Normalize the path, resolving '..' and '.' segments.

Note: Resolving these segments does not necessarily mean that all will be eliminated. A '..' at the top-level will be preserved, and an empty path is canonically '.'.

path: string | URL

Path to be normalized

Returns

The normalized path.

Example: Usage

import { normalize } from "@ende/path/normalize";
import { assertEquals } from "@ende/assert";

if (Ende.build.os === "windows") {
  assertEquals(normalize("C:\\foo\\bar\\..\\baz\\quux"), "C:\\foo\\baz\\quux");
  assertEquals(normalize(new URL("file:///C:/foo/bar/../baz/quux")), "C:\\foo\\baz\\quux");
} else {
  assertEquals(normalize("/foo/bar/../baz/quux"), "/foo/baz/quux");
  assertEquals(normalize(new URL("file:///foo/bar/../baz/quux")), "/foo/baz/quux");
}

Source