UNPKG

patha

Version:

File paths library. All you need to work with paths. Tiny drop-in replacement for 'path'. Works in Node, Browser, and Deno.

23 lines (21 loc) 606 B
import { normalize, sep } from "path" import escapeRegexp from "escape-string-regexp" /** * Normalizes the path and removes the trailing slashes. * * @example * * ```js * import { normalize, normalizeTrim } from "patha" * * normalizeTrim("/foo/bar//baz/asdf/hello/../") // gives "/foo/bar/baz/asdf" * * normalize("/foo/bar//baz/asdf/hello/../") // gives "/foo/bar/baz/asdf/" * ``` * * @param path The given file path * @returns The normalized and trimmed file path */ export function normalizeTrim(path: string) { return normalize(path).replace(new RegExp(`${escapeRegexp(sep)}$`), "") }