Deviopath › Node.js

Base Name of a Path

Easy · Paths

Node's path.basename(p, ext) returns the last part of a path. Write a function basename(p, ext) that matches it for POSIX paths (with /):

- "/home/user/notes.txt" → "notes.txt"

- trailing slashes are ignored: "docs/" → "docs"

- "/" and "" → ""

- if ext is given and the name ends with it, remove it: basename("notes.txt", ".txt") → "notes"

The ext check is a plain, case-sensitive "ends with", so basename("file.js", "js") → "file." and basename("file.JS", ".js") → "file.JS". ext is never the whole name.

Next: Parse a Version