path-slashes

Adding or removing of trailing and/or leading slashes in strings, test for their existence or join strings with slashes without duplicating them.

Usage no npm install needed!

<script type="module">
  import pathSlashes from 'https://cdn.skypack.dev/path-slashes';
</script>

README

path-slashes

npm Package Version Package Dependencies Coveralls github GitHub Workflow Status (branch) GitHub Repo License MIT

Adding or removing of trailing and/or leading slashes in strings, test for their existence or join strings with slashes without duplicating them. Native Typescript support included.


Install

# using npm
npm install --save path-slashes

# using yarn
yarn add path-slashes

Or import it from a CDN:

<script type="module" src="https://cdn.jsdelivr.net/npm/path-slashes/dist/path-slashes.modern.js"></script>
<script type="module" src="https://unpkg.com/browse/path-slashes/dist/path-slashes.modern.js"></script>

Quick Start

// Check if given path has a leading slash
hasLeadingSlash('/foo'); // => true
hasLeadingSlash('foo'); // => false

// Check if given path has a trailing slash
hasTrailingSlash('foo/'); // => true
hasTrailingSlash('foo'); // => false

// Ensure given path has a leading slash
withLeadingSlash('foo'); // => '/foo'

// Ensure given path has a trailing slash
withTrailingSlash('foo'); // => 'foo/'

// Ensure given path has no leading slash
withoutLeadingSlash('/foo/'); // => 'foo/'

// Ensure given path has no trailing slash
withoutTrailingSlash('/foo/'); // => '/foo'

// Ensure given path has both leading and trailing slashes
withSlashes('foo'); // => '/foo/'

// Ensure given path has neither leading nor trailing slashes
withoutSlashes('/foo/'); // => 'foo'

// Join path parts and add slashes where necessary
slashJoin('foo', 'bar/', '/baz'); // => 'foo/bar/baz'

License and Author

MIT © Simon Lepel