pathifist
pathifist
is an extremely simple, rather naïve URL path manipulation library. It is meant to be used in browser and non-browser environments to conveniently deal with URL(-ish) paths.
Installation
Using NPM:
npm install -S pathifist
Using Yarn:
yarn add pathifist
API
resolve(path, ...)
resolve
is pathifist
's most elaborate function: it mimics a browser's path resolution and deals with both absolute (e.g. /foo
) and relative (e.g. ../
) path segments. It accepts an arbitrary number of path segments as arguments.
resolve('foo', '/bar');
resolve('./foo', 'bar///', '../baz');
join(path, ...)
join
glues an arbitrary number of arguments together with slashes, replacing multiple consecutive slashes in the process.
join('foo', '/bar');
join('./foo', 'bar///', '../baz');
dedupeSlashes(path)
dedupeSlashes
removes obsolete consecutive slashes from the path it is being passed.
dedupe('//foo//bar//');
trim{Leading,Trailing}Slashes(path)
trim{Leading,Trailing}Slashes
removes single or consecutive leading and/or trailing slashes from the path
it is being passed. It leaves internal slashes untouched.
trimSlashes('/foo/bar/');
trimLeadingSlashes('/foo/bar/');
trimTrailingSlashes('/foo/bar/');
ensure{Leading,Trailing}Slashes(path)
ensure{Leading,Trailing}Slashes
makes sure there are single or consecutive leading and/or trailing slashes in the path
it is being passed. It leaves internal slashes untouched.
ensureSlashes('foo/bar');
ensureLeadingSlashes('foo/bar');
ensureTrailingSlashes('foo/bar');