https://github.com/untool/pathifist
A JavaScript utility library simplifying the handling of URL paths in both server and browser environments.
https://github.com/untool/pathifist
library universal url utility
Last synced: 8 months ago
JSON representation
A JavaScript utility library simplifying the handling of URL paths in both server and browser environments.
- Host: GitHub
- URL: https://github.com/untool/pathifist
- Owner: untool
- License: mit
- Created: 2018-10-15T22:20:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-09-28T16:48:44.000Z (9 months ago)
- Last Synced: 2025-09-28T18:27:47.319Z (9 months ago)
- Topics: library, universal, url, utility
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/pathifist
- Size: 850 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pathifist
[](https://travis-ci.org/untool/pathifist) [](https://www.npmjs.com/package/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](https://www.npmjs.com/get-npm):
```text
npm install -S pathifist
```
Using [Yarn](https://yarnpkg.com/en/):
```text
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.
```javascript
resolve('foo', '/bar'); // => /bar
resolve('./foo', 'bar///', '../baz'); // => ./foo/baz
```
#### `join(path, ...)`
`join` glues an arbitrary number of arguments together with slashes, replacing multiple consecutive slashes in the process.
```javascript
join('foo', '/bar'); // => foo/bar
join('./foo', 'bar///', '../baz'); // => ./foo/bar/../baz
```
#### `dedupeSlashes(path)`
`dedupeSlashes` removes obsolete consecutive slashes from the path it is being passed.
```javascript
dedupe('//foo//bar//'); // => /foo/bar/
```
#### `trimSlashes(path)` / `trim{Leading,Trailing}Slash(path)`
`trimSlashes` / `trim{Leading,Trailing}Slash` removes single or consecutive leading and/or trailing slashes from the `path` it is being passed. It leaves internal slashes untouched.
```javascript
trimSlashes('/foo/bar/'); // => foo/bar
trimLeadingSlash('/foo/bar/'); // => foo/bar/
trimTrailingSlash('/foo/bar/'); // => /foo/bar
```
#### `ensureSlashes(path)` / `ensure{Leading,Trailing}Slash(path)`
`ensureSlashes` / `ensure{Leading,Trailing}Slash` makes sure there are single or consecutive leading and/or trailing slash in the `path` it is being passed. It leaves internal slash untouched.
```javascript
ensureSlashes('foo/bar'); // => /foo/bar/
ensureLeadingSlash('foo/bar'); // => /foo/bar
ensureTrailingSlash('foo/bar'); // => foo/bar/
```