https://github.com/simbo/path-slashes
A javascript library to add or remove trailing and/or leading slashes in path strings, test for their existence or join path strings with slashes without duplicating them.
https://github.com/simbo/path-slashes
javascript join leading path slash slashes string trailing typescript
Last synced: about 1 year ago
JSON representation
A javascript library to add or remove trailing and/or leading slashes in path strings, test for their existence or join path strings with slashes without duplicating them.
- Host: GitHub
- URL: https://github.com/simbo/path-slashes
- Owner: simbo
- License: mit
- Created: 2021-01-16T17:04:11.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-05-07T23:17:18.000Z (almost 3 years ago)
- Last Synced: 2025-03-05T16:08:33.140Z (about 1 year ago)
- Topics: javascript, join, leading, path, slash, slashes, string, trailing, typescript
- Language: TypeScript
- Homepage:
- Size: 481 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Path Slashes
[](https://www.npmjs.com/package/path-slashes)
[](https://www.npmjs.com/package/path-slashes?activeTab=dependencies)
[](https://coveralls.io/github/simbo/path-slashes)
[](https://github.com/simbo/path-slashes/actions/workflows/ci.yml)
[](https://github.com/simbo/path-slashes)
[](http://simbo.mit-license.org/)
A javascript library to add or remove trailing and/or leading slashes in path
strings, test for their existence or join path strings with slashes without
duplicating them.
---
## Install
This package is published to the npm registry as
[`path-slahes`](https://www.npmjs.com/package/path-slashes).
You can install it:
```sh
# using npm:
npm install --save path-slashes
# using yarn:
yarn add path-slashes
```
ℹ️ **HINT**: This library is a pure ESM package. (You may want to
[read this](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).)
## Usage Examples
```js
import { hasLeadingSlash } from 'path-slashes';
// Check if given path has a leading slash
hasLeadingSlash('/foo'); // -> true
hasLeadingSlash('foo'); // -> false
```
```js
import { hasTrailingSlash } from 'path-slashes';
// Check if given path has a trailing slash
hasTrailingSlash('foo/'); // -> true
hasTrailingSlash('foo'); // -> false
```
```js
import { withLeadingSlash } from 'path-slashes';
// Ensure given path has a leading slash
withLeadingSlash('foo'); // -> '/foo'
```
```js
import { withTrailingSlash } from 'path-slashes';
// Ensure given path has a trailing slash
withTrailingSlash('foo'); // -> 'foo/'
```
```js
import { withoutLeadingSlash } from 'path-slashes';
// Ensure given path has no leading slash
withoutLeadingSlash('/foo/'); // -> 'foo/'
```
```js
import { withoutTrailingSlash } from 'path-slashes';
// Ensure given path has no trailing slash
withoutTrailingSlash('/foo/'); // -> '/foo'
```
```js
import { withSlashes } from 'path-slashes';
// Ensure given path has both leading and trailing slashes
withSlashes('foo'); // -> '/foo/'
```
```js
import { withoutSlashes } from 'path-slashes';
// Ensure given path has neither leading nor trailing slashes
withoutSlashes('/foo/'); // -> 'foo'
```
```js
import { slashJoin } from 'path-slashes';
// Join path parts and add slashes where necessary
slashJoin('foo', 'bar/', '/baz'); // -> 'foo/bar/baz'
```
## API
```ts
function hasLeadingSlash(path: string): boolean;
```
```ts
function hasTrailingSlash(path: string): boolean;
```
```ts
function withLeadingSlash(path: string): string;
```
```ts
function withTrailingSlash(path: string): string;
```
```ts
function withSlashes(path: string): string;
```
```ts
function withoutLeadingSlash(path: string): string;
```
```ts
function withoutTrailingSlash(path: string): string;
```
```ts
function withoutSlashes(path: string): string;
```
```ts
function slashJoin(...strings: (string | string[])[]): string;
```
## License and Author
[MIT © Simon Lepel](http://simbo.mit-license.org/)