trailing-slash-it
v0.3.0
Published
Add or remove trailing slashes.
Downloads
79
Maintainers
Readme
trailing-slash-it
appends a forward slash to a string. It will remove trailing
forward slashes if they exist already, before adding a trailing forward
slash. This prevents double slashing a string or path.
Install
$ npm install --save trailing-slash-it
Usage
var trailingSlashIt = require('trailing-slash-it');
trailingSlashIt('foo'); // 'foo/'
trailingSlashIt('foo/'); // 'foo/'
trailingSlashIt('foo//'); // 'foo/'
trailingSlashIt('foo///'); // 'foo/'
trailingSlashIt('foo\\'); // 'foo/'
trailingSlashIt('foo\\\\'); // 'foo/'
trailingSlashIt('foo\\/\\/'); // 'foo/'
If you need to make sure that something does NOT have a trailing slash, you can do that as well:
import {
trailingSlashIt,
unTrailingSlashIt,
} from 'trailing-slash-it';
trailingSlashIt('foo'); // 'foo/'
unTrailingSlashIt('foo'); // 'foo'
unTrailingSlashIt('foo/'); // 'foo'
unTrailingSlashIt('foo//'); // 'foo'
unTrailingSlashIt('foo///'); // 'foo'
unTrailingSlashIt('foo\\'); // 'foo'
unTrailingSlashIt('foo\\\\'); // 'foo'
unTrailingSlashIt('foo\\/\\/'); // 'foo'
Based on WordPress' PHP function trailingslashit().
Changelog
0.3.0 - Strip backslashes as well as forward slashes.
License
GNU GPLv3